2022-12-10 00:33:03 +02:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
|
2022-12-13 10:39:57 +02:00
|
|
|
namespace BAN::Formatter
|
2022-12-10 00:33:03 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
struct ValueFormat;
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char)>
|
|
|
|
static void print(const char* format);
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename Arg, typename... Args>
|
|
|
|
static void print(const char* format, Arg arg, Args... args);
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename... Args>
|
2022-12-13 00:50:21 +02:00
|
|
|
static void println(const char* format = "", Args... args);
|
2022-12-10 00:33:03 +02:00
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename T>
|
|
|
|
static size_t print_argument(const char* format, T arg);
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename T>
|
2022-12-13 12:30:48 +02:00
|
|
|
static void print_argument_impl(T value, const ValueFormat& format);
|
2022-12-10 00:33:03 +02:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
IMPLEMENTATION
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
struct ValueFormat
|
|
|
|
{
|
|
|
|
int base = 10;
|
|
|
|
int percision = 3;
|
2022-12-13 01:16:49 +02:00
|
|
|
int fill = 0;
|
2022-12-10 00:33:03 +02:00
|
|
|
bool upper = false;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char)>
|
|
|
|
void print(const char* format)
|
|
|
|
{
|
|
|
|
while (*format)
|
|
|
|
{
|
|
|
|
PUTC_LIKE(*format);
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename Arg, typename... Args>
|
|
|
|
void print(const char* format, Arg arg, Args... args)
|
|
|
|
{
|
|
|
|
while (*format && *format != '{')
|
|
|
|
{
|
|
|
|
PUTC_LIKE(*format);
|
|
|
|
format++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*format == '{')
|
|
|
|
{
|
|
|
|
size_t arg_len = print_argument<PUTC_LIKE>(format, arg);
|
|
|
|
if (arg_len == size_t(-1))
|
|
|
|
return print<PUTC_LIKE>(format);
|
|
|
|
print<PUTC_LIKE>(format + arg_len, args...);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename... Args>
|
|
|
|
void println(const char* format, Args... args)
|
|
|
|
{
|
|
|
|
print<PUTC_LIKE>(format, args...);
|
|
|
|
PUTC_LIKE('\n');
|
|
|
|
}
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename Arg>
|
|
|
|
size_t print_argument(const char* format, Arg argument)
|
|
|
|
{
|
|
|
|
ValueFormat value_format;
|
|
|
|
|
|
|
|
if (format[0] != '{')
|
|
|
|
return size_t(-1);
|
|
|
|
|
|
|
|
size_t i = 1;
|
|
|
|
do
|
|
|
|
{
|
|
|
|
if (!format[i] || format[i] == '}')
|
|
|
|
break;
|
|
|
|
|
2022-12-13 01:16:49 +02:00
|
|
|
if ('0' <= format[i] && format[i] <= '9')
|
|
|
|
{
|
|
|
|
int fill = 0;
|
|
|
|
while ('0' <= format[i] && format[i] <= '9')
|
|
|
|
{
|
|
|
|
fill = (fill * 10) + (format[i] - '0');
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
value_format.fill = fill;
|
|
|
|
}
|
|
|
|
|
2022-12-10 00:33:03 +02:00
|
|
|
switch (format[i])
|
|
|
|
{
|
|
|
|
case 'b': value_format.base = 2; value_format.upper = false; i++; break;
|
|
|
|
case 'B': value_format.base = 2; value_format.upper = true; i++; break;
|
|
|
|
case 'o': value_format.base = 8; value_format.upper = false; i++; break;
|
|
|
|
case 'O': value_format.base = 8; value_format.upper = true; i++; break;
|
|
|
|
case 'd': value_format.base = 10; value_format.upper = false; i++; break;
|
|
|
|
case 'D': value_format.base = 10; value_format.upper = true; i++; break;
|
|
|
|
case 'h': value_format.base = 16; value_format.upper = false; i++; break;
|
|
|
|
case 'H': value_format.base = 16; value_format.upper = true; i++; break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!format[i] || format[i] == '}')
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (format[i] == '.')
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
int percision = 0;
|
|
|
|
while ('0' <= format[i] && format[i] <= '9')
|
|
|
|
{
|
|
|
|
percision = (percision * 10) + (format[i] - '0');
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
value_format.percision = percision;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while(false);
|
|
|
|
|
|
|
|
if (format[i] != '}')
|
|
|
|
return size_t(-1);
|
|
|
|
|
2022-12-13 12:30:48 +02:00
|
|
|
print_argument_impl<PUTC_LIKE>(argument, value_format);
|
2022-12-10 00:33:03 +02:00
|
|
|
|
|
|
|
return i + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char value_to_base_char(uint8_t value, int base, bool upper)
|
|
|
|
{
|
|
|
|
if (base <= 10)
|
|
|
|
return value + '0';
|
|
|
|
if (base <= 36)
|
|
|
|
{
|
|
|
|
if (value < 10)
|
|
|
|
return value + '0';
|
|
|
|
return value + (upper ? 'A' : 'a') - 10;
|
|
|
|
}
|
|
|
|
return '?';
|
|
|
|
}
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename T>
|
|
|
|
void print_integer(T value, const ValueFormat& format)
|
|
|
|
{
|
|
|
|
if (value == 0)
|
2022-12-13 01:16:49 +02:00
|
|
|
{
|
|
|
|
for (int i = 0; i < format.fill || i < 1; i++)
|
|
|
|
PUTC_LIKE('0');
|
|
|
|
return;
|
|
|
|
}
|
2022-12-10 00:33:03 +02:00
|
|
|
|
|
|
|
bool sign = false;
|
|
|
|
|
|
|
|
// Fits signed 64-bit binary number and null
|
|
|
|
char buffer[66];
|
|
|
|
char* ptr = buffer + sizeof(buffer);
|
|
|
|
*(--ptr) = '\0';
|
|
|
|
|
|
|
|
if (value < 0)
|
|
|
|
{
|
|
|
|
sign = true;
|
|
|
|
T digit = (format.base - (value % format.base)) % format.base;
|
|
|
|
*(--ptr) = value_to_base_char(digit, format.base, format.upper);
|
|
|
|
value = -(value / format.base);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (value)
|
|
|
|
{
|
|
|
|
*(--ptr) = value_to_base_char(value % format.base, format.base, format.upper);
|
|
|
|
value /= format.base;
|
|
|
|
}
|
|
|
|
|
2022-12-13 01:16:49 +02:00
|
|
|
while (ptr >= buffer + sizeof(buffer) - format.fill)
|
|
|
|
*(--ptr) = '0';
|
|
|
|
|
2022-12-10 00:33:03 +02:00
|
|
|
if (sign)
|
|
|
|
*(--ptr) = '-';
|
|
|
|
|
|
|
|
print<PUTC_LIKE>(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char), typename T>
|
|
|
|
void print_floating(T value, const ValueFormat& format)
|
|
|
|
{
|
|
|
|
int64_t int_part = (int64_t)value;
|
|
|
|
T frac_part = value - (T)int_part;
|
|
|
|
if (frac_part < 0)
|
|
|
|
frac_part = -frac_part;
|
|
|
|
|
|
|
|
print_integer<PUTC_LIKE>(int_part, format);
|
|
|
|
|
|
|
|
if (format.percision > 0)
|
|
|
|
PUTC_LIKE('.');
|
|
|
|
|
|
|
|
for (int i = 0; i < format.percision; i++)
|
|
|
|
{
|
|
|
|
frac_part *= format.base;
|
|
|
|
if (i == format.percision - 1)
|
|
|
|
frac_part += 0.5;
|
|
|
|
|
|
|
|
PUTC_LIKE(value_to_base_char((uint8_t)frac_part % format.base, format.base, format.upper));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<void(*PUTC_LIKE)(char)>
|
|
|
|
void print_pointer(void* ptr, const ValueFormat& format)
|
|
|
|
{
|
|
|
|
uintptr_t value = (uintptr_t)ptr;
|
|
|
|
print<PUTC_LIKE>("0x");
|
|
|
|
for (int i = sizeof(void*) * 8 - 4; i >= 0; i -= 4)
|
|
|
|
PUTC_LIKE(value_to_base_char((value >> i) & 0xF, 16, format.upper));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
TEMPLATE SPECIALIZATIONS
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2022-12-13 12:30:48 +02:00
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(short value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(int value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(long long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
2022-12-10 00:33:03 +02:00
|
|
|
|
2022-12-13 12:30:48 +02:00
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(unsigned short value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(unsigned int value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(unsigned long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(unsigned long long value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
2022-12-10 00:33:03 +02:00
|
|
|
|
2022-12-13 12:30:48 +02:00
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(float value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(double value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(long double value, const ValueFormat& format) { print_floating<PUTC_LIKE>(value, format); }
|
2022-12-10 00:33:03 +02:00
|
|
|
|
2022-12-13 12:30:48 +02:00
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(char value, const ValueFormat&) { PUTC_LIKE(value); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(signed char value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(unsigned char value, const ValueFormat& format) { print_integer<PUTC_LIKE>(value, format); }
|
2022-12-10 00:33:03 +02:00
|
|
|
|
2022-12-13 12:30:48 +02:00
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(bool value, const ValueFormat& format) { print<PUTC_LIKE>(value ? "true" : "false"); }
|
2022-12-13 00:50:21 +02:00
|
|
|
|
2022-12-13 12:30:48 +02:00
|
|
|
template<void(*PUTC_LIKE)(char), typename T> void print_argument_impl(T* value, const ValueFormat& format) { print_pointer<PUTC_LIKE>((void*)value, format); }
|
|
|
|
template<void(*PUTC_LIKE)(char)> void print_argument_impl(const char* value, const ValueFormat&) { print<PUTC_LIKE>(value);}
|
2022-12-10 00:33:03 +02:00
|
|
|
|
|
|
|
}
|