<commit_before>#include "io2.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
//#include <string.h>


#define FORMAT_BUFFER_SIZE 128


int is_specifier(const char ch)
{
	return (ch >= '0' && ch <= '9')
		|| ch == 'h' || ch == 'l'
		|| ch == 'j' || ch == 'z' 
		|| ch == 't' || ch == 'L'
		|| ch == '.';
}

int is_integer(const char ch)
{
	return ch == 'd' || ch == 'i';
}

int is_unsigned(const char ch)
{
	return ch == 'u' || ch == 'o'
		|| ch == 'x' || ch == 'X';
}

int is_floating(const char ch)
{
	return ch == 'f' || ch == 'F'
		|| ch == 'e' || ch == 'E'
		|| ch == 'g' || ch == 'G'
		|| ch == 'a' || ch == 'A';
}

int is_characters(const char ch)
{
	return ch == 'c' || ch == 'C'
		|| ch == 's' || ch == 'S'
		|| ch == '[';
}

int is_pointer(const char ch)
{
	return ch == 'p';
}

int is_count(const char ch)
{
	return ch == 'n';
}


int scan_arg(universal_io *const io, const size_t position, const char *const format, size_t size, void *arg)
{
	char buffer[FORMAT_BUFFER_SIZE];
		
	for (size_t i = 0; i < size; i++)
	{
		buffer[i] = format[i];
	}

	buffer[size] = '%';
	buffer[size + 1] = 'n';
	buffer[size + 2] = '\0';

	int number = 0;
	int ret = sscanf(&io->in_buffer[io->in_position], buffer, arg, &number);
	io->in_position += number;

	if (is_count(format[size - 1]))
	{
		*(int *)arg = io->in_position - position;
	}

	return ret;
}

int in_func_buffer(universal_io *const io, const char *const format, va_list args)
{
	const size_t position = io->in_position;
	int ret = 0;

	size_t last = 0;
	size_t i = 0;
	while (format[i] != '\0')
	{
		if (format[i] == '%')
		{
			while (is_specifier(format[i + 1]) || is_integer(format[i + 1]) || is_unsigned(format[i + 1])
				|| is_floating(format[i + 1]) || is_characters(format[i + 1]) || is_pointer(format[i + 1])
				|| is_count(format[i + 1]))
			{
				i++;
				if (format[i] == '[')
				{
					while (format[i + 1] != '\0' && format[i] != '\\' && format[i + 1] != ']')
					{
						i++;
					}
				}
			}

			if (format[i] != '%' && !is_specifier(format[i]))
			{
				ret += scan_arg(io, position, &format[last], i + 1, va_arg(args, void *));
				last = i + 1;
			}<commit_msg>Оптимизирована работа in_func_buffer<commit_after>#include "io2.h"
#include <stdarg.h>
#include <stdint.h>
#include <stdlib.h>
//#include <string.h>


#define MAX_FORMAT_SIZE 128


int is_specifier(const char ch)
{
	return (ch >= '0' && ch <= '9')
		|| ch == 'h' || ch == 'l'
		|| ch == 'j' || ch == 'z' 
		|| ch == 't' || ch == 'L'
		|| ch == '*';
}

int is_integer(const char ch)
{
	return ch == 'd' || ch == 'i';
}

int is_unsigned(const char ch)
{
	return ch == 'u' || ch == 'o'
		|| ch == 'x' || ch == 'X';
}

int is_floating(const char ch)
{
	return ch == 'f' || ch == 'F'
		|| ch == 'e' || ch == 'E'
		|| ch == 'g' || ch == 'G'
		|| ch == 'a' || ch == 'A';
}

int is_characters(const char ch)
{
	return ch == 'c' || ch == 'C'
		|| ch == 's' || ch == 'S'
		|| ch == '[';
}

int is_pointer(const char ch)
{
	return ch == 'p';
}

int is_count(const char ch)
{
	return ch == 'n';
}


int scan_arg(universal_io *const io, const char *const format, size_t size, void *arg)
{
	char buffer[MAX_FORMAT_SIZE];
		
	for (size_t i = 0; i < size; i++)
	{
		buffer[i] = format[i];
	}

	sprintf(&buffer[size], "%%zn");

	size_t number = 0;
	int ret = sscanf(&io->in_buffer[io->in_position], buffer, arg, &number);
	io->in_position += number;

	return ret;
}

int in_func_buffer(universal_io *const io, const char *const format, va_list args)
{
	const size_t position = io->in_position;
	int ret = 0;

	size_t last = 0;
	size_t i = 0;
	while (format[i] != '\0')
	{
		if (format[i] == '%')
		{
			int was_count = 0;

			while (is_specifier(format[i + 1]) || is_integer(format[i + 1]) || is_unsigned(format[i + 1])
				|| is_floating(format[i + 1]) || is_characters(format[i + 1]) || is_pointer(format[i + 1])
				|| is_count(format[i + 1]))
			{
				i++;
				was_count = was_count || format[i] == 'n';
				
				if (format[i] == '[')
				{
					while (format[i + 1] != '\0' && format[i] != '\\' && format[i + 1] != ']')
					{
						i++;
					}
				}
			}

			if (format[i] != '%')
			{
				void *arg = va_arg(args, void *);
				ret += scan_arg(io, &format[last], i + 1, arg);

				if (was_count)
				{
					*(int *)arg = io->in_position - position;
				}

				last = i + 1;
			}
{'fraction_non_alphanumeric': 0.22859787135585377, 'fraction_numerical': 0.012262841277186487, 'mean_word_length': 2.9515539305301646, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5239272', 'n_tokens_mistral': 2059, 'n_tokens_neox': 1821, 'n_words': 455}