<commit_before>#include <metodo/metodo.h>

#define is_digit(c) ((c) >= '0' && (c) <= '9')

int m_printn(OUT char *str, IN int maxlen, IN int len, IN unsigned int n,
		IN int base, IN int size, IN int flags, IN int precision)
{
	char tmp[36], sign = '\0';
	char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int i = 0;
	signed int signed_n = (signed int) n;

	/* Preprocess the flags. */

	if (flags & TF_SMALL)
		digits = "0123456789abcdefghijklmnopqrstuvwxyz";

	if (!(flags & TF_UNSIGNED) && signed_n < 0) {
		sign = '-';
		n = -signed_n;
	} else if (flags & TF_EXP_SIGN) {
		sign = '+';
	}

	if (sign)
		size--;

	if (flags & TF_ALTERNATE)
		if (base == 8) {
			if (len < maxlen)
				str[len++] = '0';
			else
				len++;
		} else if (base == 16) {
			if (len < maxlen)
				str[len++] = '0';
			else
				len++;
			if (len < maxlen)
				str[len++] = 'x';
			else
				len++;
		}

	/* Find the number in reverse. */
	if (n == 0)
		tmp[i++] = '0';
	else
		while (n != 0) {
			tmp[i++] = digits[n%base];
			n /= base;
		}

	/* Pad the number with zeros or spaces. */
	if (!(flags & TF_LEFT))
		while (size-- > i) {
			if (flags & TF_ZEROPAD)
				if (len < maxlen)
					str[len++] = '0';
				else
					len++;
			else
				if (len < maxlen)
					str[len++] = ' ';
				else
					len++;
		}

	if (sign)
		str[len++] = sign;

	/* Write any zeros to satisfy the precision. */ 
	while (i < precision--)
		if (len < maxlen)
			str[len++] = '0';
		else
			len++;

	/* Write the number. */
	while (i-- != 0) {
		size--;
		if (len < maxlen)
			str[len++] = tmp[i];
		else
			len++;
	}

	/* Left align the numbers. */
	if (flags & TF_LEFT)
		while (size-- > 0) {
			if (len < maxlen)
				str[len++] = ' ';
			else
				len++;
		}

	return len;
}

int printf(IN const char *fmt, ...)
{
#if PRINTF_USE_MM
	char *str;
#endif
	char str[1024];
	va_list args;
	int len, i = 0;

#if PRINTF_USE_MM
	va_start(args, fmt);
	len = vsnprintf(NULL, 0, fmt, args);
	va_end(args);

	str = malloc(len+1);

	va_start(args, fmt);
	len = vsnprintf(str, len+1, fmt, args);
	va_end(args);
#else
	va_start(args, fmt);
	len = vsnprintf(str, 1024, fmt, args);
	va_end(args);
#endif

	while (str[i])
		HalDisplayChar(str[i++]);

#if PRINTF_USE_MM<commit_msg>Bugs in vprintf.<commit_after>#include <metodo/metodo.h>

#define is_digit(c) ((c) >= '0' && (c) <= '9')

int m_printn(OUT char *str, IN int maxlen, IN int len, IN unsigned int n,
		IN int base, IN int size, IN int flags, IN int precision)
{
	char tmp[36], sign = '\0';
	char *digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
	int i = 0;
	signed int signed_n = (signed int) n;

	/* Preprocess the flags. */

	if (flags & TF_SMALL)
		digits = "0123456789abcdefghijklmnopqrstuvwxyz";

	if (!(flags & TF_UNSIGNED) && signed_n < 0) {
		sign = '-';
		n = -signed_n;
	} else if (flags & TF_EXP_SIGN) {
		sign = '+';
	}

	if (sign)
		size--;

	if (flags & TF_ALTERNATE)
		if (base == 8) {
			if (len < maxlen)
				str[len++] = '0';
			else
				len++;
		} else if (base == 16) {
			if (len < maxlen)
				str[len++] = '0';
			else
				len++;
			if (len < maxlen)
				str[len++] = 'x';
			else
				len++;
		}

	/* Find the number in reverse. */
	if (n == 0)
		tmp[i++] = '0';
	else
		while (n != 0) {
			tmp[i++] = digits[n%base];
			n /= base;
		}

	/* Pad the number with zeros or spaces. */
	if (!(flags & TF_LEFT))
		while (size-- > i) {
			if (flags & TF_ZEROPAD)
				if (len < maxlen)
					str[len++] = '0';
				else
					len++;
			else
				if (len < maxlen)
					str[len++] = ' ';
				else
					len++;
		}

	if (sign)
		str[len++] = sign;

	/* Write any zeros to satisfy the precision. */ 
	while (i < precision--)
		if (len < maxlen)
			str[len++] = '0';
		else
			len++;

	/* Write the number. */
	while (i-- != 0) {
		size--;
		if (len < maxlen)
			str[len++] = tmp[i];
		else
			len++;
	}

	/* Left align the numbers. */
	if (flags & TF_LEFT)
		while (size-- > 0) {
			if (len < maxlen)
				str[len++] = ' ';
			else
				len++;
		}

	return len;
}

int printf(IN const char *fmt, ...)
{
#if PRINTF_USE_MM
	char *str;
#else
	char str[1024];
#endif
	va_list args;
	int len, i = 0;

#if PRINTF_USE_MM
	va_start(args, fmt);
	len = vsnprintf(NULL, 0, fmt, args);
	va_end(args);

	str = malloc(len+1);

	va_start(args, fmt);
	len = vsnprintf(str, len+1, fmt, args);
	va_end(args);
#else
	va_start(args, fmt);
	len = vsnprintf(str, 1024, fmt, args);
	va_end(args);
#endif

	while (str[i])
		HalDisplayChar(str[i++]);

#if PRINTF_USE_MM
{'fraction_non_alphanumeric': 0.18880647336480108, 'fraction_numerical': 0.022926500337154418, 'mean_word_length': 2.6267318663406685, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 11, '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': '5358956', 'n_tokens_mistral': 2210, 'n_tokens_neox': 1881, 'n_words': 520}