2023-05-10 15:41:11 +03:00
|
|
|
#include <BAN/Traits.h>
|
2023-05-10 22:36:03 +03:00
|
|
|
#include <BAN/Math.h>
|
2023-05-26 22:21:07 +03:00
|
|
|
#include <bits/printf.h>
|
2023-05-10 02:00:28 +03:00
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
2023-08-16 10:42:35 +03:00
|
|
|
#include <math.h>
|
2023-05-10 02:00:28 +03:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#define BAN_PRINTF_PUTC(c) \
|
|
|
|
do \
|
|
|
|
{ \
|
|
|
|
if (putc_fun((c), data) == EOF) \
|
|
|
|
return -1; \
|
|
|
|
written++; \
|
|
|
|
} while (false)
|
|
|
|
|
2023-09-28 11:42:57 +03:00
|
|
|
enum class length_t
|
|
|
|
{
|
|
|
|
none,
|
|
|
|
hh,
|
|
|
|
h,
|
|
|
|
l,
|
|
|
|
ll,
|
|
|
|
j,
|
|
|
|
z,
|
|
|
|
t,
|
|
|
|
L,
|
|
|
|
};
|
|
|
|
|
2023-05-10 02:00:28 +03:00
|
|
|
struct format_options_t
|
|
|
|
{
|
|
|
|
bool alternate_form { false };
|
|
|
|
bool zero_padded { false };
|
|
|
|
bool left_justified { false };
|
|
|
|
bool show_plus_sign_as_space { false };
|
|
|
|
bool show_plus_sign { false };
|
|
|
|
int width { -1 };
|
|
|
|
int percision { -1 };
|
2023-09-28 11:42:57 +03:00
|
|
|
length_t length { length_t::none };
|
2023-05-10 02:00:28 +03:00
|
|
|
};
|
|
|
|
|
2023-05-10 15:41:11 +03:00
|
|
|
template<BAN::integral T>
|
2023-05-15 22:02:33 +03:00
|
|
|
static void integer_to_string(char* buffer, T value, int base, bool upper, format_options_t options)
|
2023-05-10 02:00:28 +03:00
|
|
|
{
|
2023-05-15 22:02:33 +03:00
|
|
|
int digits = 1;
|
2023-05-10 02:00:28 +03:00
|
|
|
if (options.percision != -1)
|
|
|
|
{
|
2023-05-15 22:02:33 +03:00
|
|
|
digits = options.percision;
|
|
|
|
options.zero_padded = false;
|
2023-05-10 02:00:28 +03:00
|
|
|
}
|
2023-05-10 22:36:03 +03:00
|
|
|
|
2023-05-10 02:00:28 +03:00
|
|
|
auto digit_char = [](int digit, bool upper)
|
|
|
|
{
|
|
|
|
if (digit < 10)
|
|
|
|
return '0' + digit;
|
|
|
|
return (upper ? 'A' : 'a') + (digit - 10);
|
|
|
|
};
|
|
|
|
|
|
|
|
int offset = 0;
|
2023-05-15 22:02:33 +03:00
|
|
|
|
|
|
|
int prefix_length = 0;
|
|
|
|
char prefix[2] {};
|
|
|
|
if constexpr(BAN::is_signed_v<T>)
|
2023-05-10 02:00:28 +03:00
|
|
|
{
|
2023-05-15 22:02:33 +03:00
|
|
|
prefix_length = 1;
|
|
|
|
if (value < 0)
|
|
|
|
{
|
|
|
|
prefix[0] = '-';
|
|
|
|
buffer[offset++] = digit_char(-(value % base), upper);
|
|
|
|
value = -(value / base);
|
|
|
|
}
|
|
|
|
else if (options.show_plus_sign)
|
|
|
|
prefix[0] = '+';
|
|
|
|
else if (options.show_plus_sign_as_space)
|
|
|
|
prefix[0] = ' ';
|
|
|
|
else
|
|
|
|
prefix_length = 0;
|
2023-05-10 02:00:28 +03:00
|
|
|
}
|
2023-05-15 22:02:33 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if (options.alternate_form && base == 8)
|
|
|
|
{
|
|
|
|
prefix_length = 1;
|
|
|
|
prefix[0] = '0';
|
|
|
|
}
|
|
|
|
else if (options.alternate_form && base == 16)
|
|
|
|
{
|
|
|
|
prefix_length = 2;
|
|
|
|
prefix[0] = '0';
|
|
|
|
prefix[1] = 'x';
|
|
|
|
}
|
|
|
|
}
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-05-15 22:02:33 +03:00
|
|
|
while (value || offset < digits)
|
2023-05-10 02:00:28 +03:00
|
|
|
{
|
|
|
|
buffer[offset++] = digit_char(value % base, upper);
|
|
|
|
value /= base;
|
|
|
|
}
|
|
|
|
|
2023-05-15 22:02:33 +03:00
|
|
|
if (options.zero_padded)
|
2023-05-10 02:00:28 +03:00
|
|
|
{
|
2023-05-15 22:02:33 +03:00
|
|
|
int zeroes = options.width - prefix_length;
|
|
|
|
for (int i = offset; i < zeroes; i++)
|
|
|
|
buffer[offset++] = '0';
|
2023-05-10 02:00:28 +03:00
|
|
|
}
|
|
|
|
|
2023-05-15 22:02:33 +03:00
|
|
|
if (prefix[1]) buffer[offset++] = prefix[1];
|
|
|
|
if (prefix[0]) buffer[offset++] = prefix[0];
|
|
|
|
|
2023-05-10 02:00:28 +03:00
|
|
|
for (int i = 0; i < offset / 2; i++)
|
|
|
|
{
|
|
|
|
char temp = buffer[i];
|
|
|
|
buffer[i] = buffer[offset - i - 1];
|
|
|
|
buffer[offset - i - 1] = temp;
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer[offset++] = '\0';
|
|
|
|
}
|
|
|
|
|
2023-09-23 02:28:25 +03:00
|
|
|
#if __enable_sse
|
2023-05-10 15:41:11 +03:00
|
|
|
template<BAN::floating_point T>
|
2023-05-10 02:00:28 +03:00
|
|
|
static void floating_point_to_string(char* buffer, T value, bool upper, const format_options_t options)
|
|
|
|
{
|
|
|
|
int percision = 6;
|
|
|
|
if (options.percision != -1)
|
|
|
|
percision = options.percision;
|
|
|
|
|
2023-05-10 15:41:11 +03:00
|
|
|
int offset = 0;
|
|
|
|
|
2023-05-10 22:36:03 +03:00
|
|
|
// Add sign if needed
|
|
|
|
if (value < (T)0.0)
|
2023-05-10 15:41:11 +03:00
|
|
|
{
|
|
|
|
buffer[offset++] = '-';
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
else if (options.show_plus_sign)
|
|
|
|
buffer[offset++] = '+';
|
|
|
|
else if (options.show_plus_sign_as_space)
|
|
|
|
buffer[offset++] = ' ';
|
|
|
|
|
2024-02-16 15:34:24 +02:00
|
|
|
if (isnan(value))
|
|
|
|
{
|
|
|
|
strcpy(buffer + offset, upper ? "NAN" : "nan");
|
|
|
|
return;
|
|
|
|
}
|
2023-08-16 10:42:35 +03:00
|
|
|
if (isinf(value))
|
|
|
|
{
|
2023-12-28 19:14:42 +02:00
|
|
|
strcpy(buffer + offset, upper ? "INF" : "inf");
|
2023-08-16 10:42:35 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:36:03 +03:00
|
|
|
// Round last digit
|
|
|
|
value += (T)0.5 * BAN::Math::pow<T>(10.0, -percision);
|
2023-05-10 15:41:11 +03:00
|
|
|
|
2023-05-10 22:36:03 +03:00
|
|
|
// Add integer part of the decimal
|
|
|
|
if (value < (T)1.0)
|
2023-05-10 15:41:11 +03:00
|
|
|
{
|
2023-05-10 22:36:03 +03:00
|
|
|
buffer[offset++] = '0';
|
|
|
|
value *= (T)10.0;
|
2023-05-10 15:41:11 +03:00
|
|
|
}
|
2023-05-10 22:36:03 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
int exponent = (int)BAN::Math::log10<T>(value);
|
|
|
|
T magnitude = BAN::Math::pow<T>(10, exponent);
|
2023-05-10 15:41:11 +03:00
|
|
|
|
2023-05-10 22:36:03 +03:00
|
|
|
value /= magnitude;
|
|
|
|
|
|
|
|
for (int i = 0; i <= exponent; i++)
|
|
|
|
{
|
|
|
|
int digit = 0;
|
|
|
|
for (; digit < 10; digit++)
|
|
|
|
if (value < digit + 1)
|
|
|
|
break;
|
|
|
|
buffer[offset++] = '0' + digit;
|
|
|
|
value -= (T)digit;
|
|
|
|
value *= (T)10.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// We are done if the decimal part is not written
|
|
|
|
if (percision == 0 && !options.alternate_form)
|
2023-05-10 15:41:11 +03:00
|
|
|
{
|
|
|
|
buffer[offset++] = '\0';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
buffer[offset++] = '.';
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-05-10 22:36:03 +03:00
|
|
|
// Add the 'percision' digits after decimal point
|
2023-05-10 15:41:11 +03:00
|
|
|
for (int i = 0; i < percision; i++)
|
|
|
|
{
|
2023-05-10 22:36:03 +03:00
|
|
|
int digit = 0;
|
|
|
|
for (; digit < 10; digit++)
|
|
|
|
if (value < digit + 1)
|
|
|
|
break;
|
2023-05-10 15:41:11 +03:00
|
|
|
buffer[offset++] = '0' + digit;
|
2023-05-10 22:36:03 +03:00
|
|
|
value -= (T)digit;
|
|
|
|
value *= (T)10.0;
|
2023-05-10 15:41:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buffer[offset++] = '\0';
|
2023-05-10 02:00:28 +03:00
|
|
|
}
|
|
|
|
|
2023-05-10 22:36:03 +03:00
|
|
|
template<BAN::floating_point T>
|
|
|
|
static void floating_point_to_exponent_string(char* buffer, T value, bool upper, const format_options_t options)
|
|
|
|
{
|
|
|
|
int offset = 0;
|
|
|
|
|
|
|
|
// Add sign if needed
|
|
|
|
if (value < (T)0.0)
|
|
|
|
{
|
|
|
|
buffer[offset++] = '-';
|
|
|
|
value = -value;
|
|
|
|
}
|
|
|
|
else if (options.show_plus_sign)
|
|
|
|
buffer[offset++] = '+';
|
|
|
|
else if (options.show_plus_sign_as_space)
|
|
|
|
buffer[offset++] = ' ';
|
|
|
|
|
2024-02-16 15:34:24 +02:00
|
|
|
if (isnan(value))
|
|
|
|
{
|
|
|
|
strcpy(buffer + offset, upper ? "NAN" : "nan");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (isinf(value))
|
|
|
|
{
|
|
|
|
strcpy(buffer + offset, upper ? "INF" : "inf");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-05-10 22:36:03 +03:00
|
|
|
// Calculate which number to put as exponent
|
|
|
|
int exponent = 0;
|
|
|
|
if (value != (T)0.0)
|
|
|
|
{
|
2023-06-03 15:07:02 +03:00
|
|
|
exponent = (int)BAN::Math::log10<T>(value);
|
2023-05-10 22:36:03 +03:00
|
|
|
value /= BAN::Math::pow<T>(10.0, exponent);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add first numbers before 'e'
|
|
|
|
floating_point_to_string<T>(buffer + offset, value, upper, options);
|
|
|
|
offset = strlen(buffer);
|
|
|
|
|
|
|
|
// Add the exponent part
|
|
|
|
buffer[offset++] = (upper ? 'E' : 'e');
|
|
|
|
format_options_t exponent_options;
|
|
|
|
exponent_options.show_plus_sign = true;
|
|
|
|
exponent_options.zero_padded = true;
|
|
|
|
exponent_options.width = 3;
|
|
|
|
integer_to_string<int>(buffer + offset, exponent, 10, upper, exponent_options);
|
|
|
|
}
|
2023-09-23 02:28:25 +03:00
|
|
|
#endif
|
2023-05-10 22:36:03 +03:00
|
|
|
|
2023-05-10 02:00:28 +03:00
|
|
|
extern "C" int printf_impl(const char* format, va_list arguments, int (*putc_fun)(int, void*), void* data)
|
|
|
|
{
|
|
|
|
int written = 0;
|
|
|
|
while (*format)
|
|
|
|
{
|
|
|
|
if (*format == '%')
|
|
|
|
{
|
|
|
|
format_options_t options;
|
|
|
|
|
|
|
|
// PARSE FLAGS
|
|
|
|
bool flags_done = false;
|
|
|
|
while (!flags_done)
|
|
|
|
{
|
|
|
|
format++;
|
|
|
|
switch (*format)
|
|
|
|
{
|
|
|
|
case '#': options.alternate_form = true; break;
|
|
|
|
case '0': options.zero_padded = true; break;
|
|
|
|
case '-': options.left_justified = true; break;
|
|
|
|
case ' ': options.show_plus_sign_as_space = true; break;
|
|
|
|
case '+': options.show_plus_sign = true; break;
|
|
|
|
default:
|
|
|
|
flags_done = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (options.zero_padded && options.left_justified)
|
|
|
|
options.zero_padded = false;
|
|
|
|
|
|
|
|
// PARSE FIELD WIDTH
|
|
|
|
if (*format == '*')
|
|
|
|
{
|
|
|
|
options.width = va_arg(arguments, int);
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
else if (isdigit(*format) && *format != '0')
|
|
|
|
{
|
|
|
|
int width = 0;
|
|
|
|
while (isdigit(*format))
|
|
|
|
{
|
|
|
|
width *= 10;
|
|
|
|
width += *format - '0';
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
options.width = width;
|
|
|
|
}
|
|
|
|
|
|
|
|
// PARSE PERCISION
|
|
|
|
if (*format == '.')
|
|
|
|
{
|
|
|
|
format++;
|
|
|
|
int percision = 0;
|
|
|
|
if (isdigit(*format))
|
|
|
|
{
|
|
|
|
while (isdigit(*format))
|
|
|
|
{
|
|
|
|
percision *= 10;
|
|
|
|
percision += *format - '0';
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
}
|
2023-05-15 22:02:33 +03:00
|
|
|
else if (*format == '-')
|
|
|
|
{
|
|
|
|
percision = -1;
|
|
|
|
format++;
|
|
|
|
while (isdigit(*format))
|
|
|
|
format++;
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
else if (*format == '*')
|
|
|
|
{
|
|
|
|
percision = va_arg(arguments, int);
|
2023-06-03 15:07:02 +03:00
|
|
|
format++;
|
2023-05-10 02:00:28 +03:00
|
|
|
}
|
2023-05-10 15:41:11 +03:00
|
|
|
if (percision < 0)
|
|
|
|
percision = -1;
|
2023-05-10 02:00:28 +03:00
|
|
|
options.percision = percision;
|
|
|
|
}
|
2024-01-24 14:43:46 +02:00
|
|
|
|
2023-09-28 11:42:57 +03:00
|
|
|
// PARSE LENGTH
|
|
|
|
if (*format == 'h')
|
|
|
|
{
|
|
|
|
if (*(format + 1) == 'h')
|
|
|
|
{
|
|
|
|
format++;
|
|
|
|
options.length = length_t::hh;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
options.length = length_t::h;
|
|
|
|
}
|
|
|
|
else if (*format == 'l')
|
|
|
|
{
|
|
|
|
if (*(format + 1) == 'l')
|
|
|
|
{
|
|
|
|
format++;
|
|
|
|
options.length = length_t::ll;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
options.length = length_t::l;
|
|
|
|
}
|
|
|
|
else if (*format == 'j')
|
|
|
|
options.length = length_t::j;
|
|
|
|
else if (*format == 'z')
|
|
|
|
options.length = length_t::z;
|
|
|
|
else if (*format == 't')
|
|
|
|
options.length = length_t::t;
|
|
|
|
else if (*format == 'L')
|
|
|
|
options.length = length_t::L;
|
|
|
|
else
|
|
|
|
format--;
|
|
|
|
format++;
|
2023-05-10 02:00:28 +03:00
|
|
|
|
2023-08-18 16:49:27 +03:00
|
|
|
char conversion[128];
|
2023-05-10 02:00:28 +03:00
|
|
|
const char* string = nullptr;
|
2023-06-03 15:07:02 +03:00
|
|
|
|
|
|
|
int length = -1;
|
|
|
|
|
2023-09-28 11:42:57 +03:00
|
|
|
#define PARSE_INT_CASE(length, type) \
|
|
|
|
case length_t::length: integer_to_string<type>(conversion, va_arg(arguments, type), BASE_, UPPER_, options); break
|
|
|
|
|
|
|
|
#define PARSE_INT_CASE_CAST(length, cast, type) \
|
|
|
|
case length_t::length: integer_to_string<cast>(conversion, va_arg(arguments, type), BASE_, UPPER_, options); break
|
|
|
|
|
|
|
|
#define PARSE_INT_DEFAULT(type) \
|
|
|
|
default: integer_to_string<type>(conversion, va_arg(arguments, type), BASE_, UPPER_, options); break
|
|
|
|
|
2023-05-10 02:00:28 +03:00
|
|
|
switch (*format)
|
|
|
|
{
|
|
|
|
case 'd':
|
|
|
|
case 'i':
|
|
|
|
{
|
2023-09-28 11:42:57 +03:00
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
#define BASE_ 10
|
|
|
|
#define UPPER_ false
|
|
|
|
PARSE_INT_CASE_CAST(hh, signed char, int);
|
|
|
|
PARSE_INT_CASE_CAST(h, short, int);
|
|
|
|
PARSE_INT_CASE(l, long);
|
|
|
|
PARSE_INT_CASE(ll, long long);
|
|
|
|
PARSE_INT_CASE(j, intmax_t);
|
|
|
|
PARSE_INT_CASE(z, ssize_t);
|
|
|
|
PARSE_INT_CASE(t, ptrdiff_t);
|
|
|
|
PARSE_INT_DEFAULT(int);
|
|
|
|
#undef BASE_
|
|
|
|
#undef UPPER_
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'o':
|
|
|
|
{
|
2023-09-28 11:42:57 +03:00
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
#define BASE_ 8
|
|
|
|
#define UPPER_ false
|
|
|
|
PARSE_INT_CASE_CAST(hh, unsigned char, unsigned int);
|
|
|
|
PARSE_INT_CASE_CAST(h, unsigned short, unsigned int);
|
|
|
|
PARSE_INT_CASE(l, unsigned long);
|
|
|
|
PARSE_INT_CASE(ll, unsigned long long);
|
|
|
|
PARSE_INT_CASE(j, uintmax_t);
|
|
|
|
PARSE_INT_CASE(z, size_t);
|
|
|
|
PARSE_INT_CASE(t, uintptr_t);
|
|
|
|
PARSE_INT_DEFAULT(unsigned int);
|
|
|
|
#undef BASE_
|
|
|
|
#undef UPPER_
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'u':
|
|
|
|
{
|
2023-09-28 11:42:57 +03:00
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
#define BASE_ 10
|
|
|
|
#define UPPER_ false
|
|
|
|
PARSE_INT_CASE_CAST(hh, unsigned char, unsigned int);
|
|
|
|
PARSE_INT_CASE_CAST(h, unsigned short, unsigned int);
|
|
|
|
PARSE_INT_CASE(l, unsigned long);
|
|
|
|
PARSE_INT_CASE(ll, unsigned long long);
|
|
|
|
PARSE_INT_CASE(j, uintmax_t);
|
|
|
|
PARSE_INT_CASE(z, size_t);
|
|
|
|
PARSE_INT_CASE(t, uintptr_t);
|
|
|
|
PARSE_INT_DEFAULT(unsigned int);
|
|
|
|
#undef BASE_
|
|
|
|
#undef UPPER_
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'x':
|
2023-09-28 11:42:57 +03:00
|
|
|
{
|
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
#define BASE_ 16
|
|
|
|
#define UPPER_ false
|
|
|
|
PARSE_INT_CASE_CAST(hh, unsigned char, unsigned int);
|
|
|
|
PARSE_INT_CASE_CAST(h, unsigned short, unsigned int);
|
|
|
|
PARSE_INT_CASE(l, unsigned long);
|
|
|
|
PARSE_INT_CASE(ll, unsigned long long);
|
|
|
|
PARSE_INT_CASE(j, uintmax_t);
|
|
|
|
PARSE_INT_CASE(z, size_t);
|
|
|
|
PARSE_INT_CASE(t, uintptr_t);
|
|
|
|
PARSE_INT_DEFAULT(unsigned int);
|
|
|
|
#undef BASE_
|
|
|
|
#undef UPPER_
|
|
|
|
}
|
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
case 'X':
|
|
|
|
{
|
2023-09-28 11:42:57 +03:00
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
#define BASE_ 16
|
|
|
|
#define UPPER_ true
|
|
|
|
PARSE_INT_CASE_CAST(hh, unsigned char, unsigned int);
|
|
|
|
PARSE_INT_CASE_CAST(h, unsigned short, unsigned int);
|
|
|
|
PARSE_INT_CASE(l, unsigned long);
|
|
|
|
PARSE_INT_CASE(ll, unsigned long long);
|
|
|
|
PARSE_INT_CASE(j, uintmax_t);
|
|
|
|
PARSE_INT_CASE(z, size_t);
|
|
|
|
PARSE_INT_CASE(t, uintptr_t);
|
|
|
|
PARSE_INT_DEFAULT(unsigned int);
|
|
|
|
#undef BASE_
|
|
|
|
#undef UPPER_
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
2023-09-23 02:28:25 +03:00
|
|
|
#if __enable_sse
|
2023-05-10 02:00:28 +03:00
|
|
|
case 'e':
|
|
|
|
case 'E':
|
|
|
|
{
|
2023-09-28 11:42:57 +03:00
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
case length_t::L: floating_point_to_exponent_string<long double> (conversion, va_arg(arguments, long double), *format == 'E', options); break;
|
|
|
|
default: floating_point_to_exponent_string<double> (conversion, va_arg(arguments, double), *format == 'E', options); break;
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'f':
|
|
|
|
case 'F':
|
|
|
|
{
|
2023-09-28 11:42:57 +03:00
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
case length_t::L: floating_point_to_string<long double> (conversion, va_arg(arguments, long double), *format == 'F', options); break;
|
|
|
|
default: floating_point_to_string<double> (conversion, va_arg(arguments, double), *format == 'F', options); break;
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'g':
|
|
|
|
case 'G':
|
|
|
|
// TODO
|
|
|
|
break;
|
|
|
|
case 'a':
|
|
|
|
case 'A':
|
|
|
|
// TODO
|
|
|
|
break;
|
2023-09-28 11:42:57 +03:00
|
|
|
#endif
|
2023-05-10 02:00:28 +03:00
|
|
|
case 'c':
|
|
|
|
{
|
|
|
|
conversion[0] = va_arg(arguments, int);
|
|
|
|
conversion[1] = '\0';
|
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 's':
|
|
|
|
{
|
|
|
|
string = va_arg(arguments, const char*);
|
2023-06-03 15:07:02 +03:00
|
|
|
if (options.percision != -1)
|
|
|
|
{
|
|
|
|
length = 0;
|
|
|
|
while (string[length] && length < options.percision)
|
|
|
|
length++;
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'C':
|
|
|
|
// TODO (depricated)
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
// TODO (depricated)
|
|
|
|
break;
|
|
|
|
case 'p':
|
|
|
|
{
|
|
|
|
void* ptr = va_arg(arguments, void*);
|
|
|
|
options.alternate_form = true;
|
|
|
|
integer_to_string<uintptr_t>(conversion, (uintptr_t)ptr, 16, false, options);
|
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'n':
|
|
|
|
{
|
2023-09-28 11:42:57 +03:00
|
|
|
switch (options.length)
|
|
|
|
{
|
|
|
|
case length_t::hh: *va_arg(arguments, signed char*) = written; break;
|
|
|
|
case length_t::h: *va_arg(arguments, short*) = written; break;
|
|
|
|
case length_t::l: *va_arg(arguments, long*) = written; break;
|
|
|
|
case length_t::ll: *va_arg(arguments, long long*) = written; break;
|
|
|
|
case length_t::j: *va_arg(arguments, intmax_t*) = written; break;
|
|
|
|
case length_t::z: *va_arg(arguments, ssize_t*) = written; break;
|
|
|
|
case length_t::t: *va_arg(arguments, ptrdiff_t*) = written; break;
|
|
|
|
default: *va_arg(arguments, int*) = written; break;
|
|
|
|
}
|
2023-05-10 02:00:28 +03:00
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 'm':
|
|
|
|
{
|
|
|
|
// NOTE: this is a glibc extension
|
|
|
|
if (options.alternate_form)
|
2023-05-10 02:22:31 +03:00
|
|
|
string = strerrorname_np(errno);
|
2023-05-10 02:00:28 +03:00
|
|
|
else
|
|
|
|
string = strerror(errno);
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '%':
|
|
|
|
{
|
|
|
|
conversion[0] = '%';
|
|
|
|
conversion[1] = '\0';
|
|
|
|
string = conversion;
|
|
|
|
format++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (string)
|
|
|
|
{
|
2023-06-03 15:07:02 +03:00
|
|
|
if (length == -1)
|
|
|
|
length = strlen(string);
|
2023-05-10 02:00:28 +03:00
|
|
|
|
|
|
|
if (options.width == -1)
|
|
|
|
options.width = 0;
|
|
|
|
|
|
|
|
if (!options.left_justified)
|
2023-06-03 15:07:02 +03:00
|
|
|
for (int i = length; i < options.width; i++)
|
2023-05-10 02:00:28 +03:00
|
|
|
BAN_PRINTF_PUTC(' ');
|
|
|
|
|
2023-06-03 15:07:02 +03:00
|
|
|
for (int i = 0; i < length && string[i]; i++)
|
|
|
|
BAN_PRINTF_PUTC(string[i]);
|
2023-05-10 02:00:28 +03:00
|
|
|
|
|
|
|
if (options.left_justified)
|
2023-06-03 15:07:02 +03:00
|
|
|
for (int i = length; i < options.width; i++)
|
2023-05-10 02:00:28 +03:00
|
|
|
BAN_PRINTF_PUTC(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
BAN_PRINTF_PUTC(*format);
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
}
|
2023-09-09 22:52:03 +03:00
|
|
|
|
2023-05-10 02:00:28 +03:00
|
|
|
return written;
|
|
|
|
}
|