BAN: Formatter now uses perfect forwarding on its arguments
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include <BAN/Move.h>
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stddef.h>
|
||||
|
||||
@@ -12,16 +14,19 @@ namespace BAN::Formatter
|
||||
static void print(F putc, const char* format);
|
||||
|
||||
template<typename F, typename Arg, typename... Args>
|
||||
static void print(F putc, const char* format, const Arg& arg, const Args&... args);
|
||||
static void print(F putc, const char* format, Arg&& arg, Args&&... args);
|
||||
|
||||
template<typename F, typename... Args>
|
||||
static void println(F putc, const char* format = "", const Args&... args);
|
||||
static void println(F putc, const char* format, Args&&... args);
|
||||
|
||||
template<typename F, typename T>
|
||||
static size_t print_argument(F putc, const char* format, const T& arg);
|
||||
static void print_argument(F putc, T value, const ValueFormat& format);
|
||||
|
||||
template<typename F, typename T>
|
||||
static void print_argument_impl(F putc, T value, const ValueFormat& format);
|
||||
namespace detail
|
||||
{
|
||||
template<typename F, typename T>
|
||||
static size_t parse_format_and_print_argument(F putc, const char* format, T&& arg);
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
@@ -49,7 +54,7 @@ namespace BAN::Formatter
|
||||
}
|
||||
|
||||
template<typename F, typename Arg, typename... Args>
|
||||
void print(F putc, const char* format, const Arg& arg, const Args&... args)
|
||||
void print(F putc, const char* format, Arg&& arg, Args&&... args)
|
||||
{
|
||||
while (*format && *format != '{')
|
||||
{
|
||||
@@ -59,166 +64,171 @@ namespace BAN::Formatter
|
||||
|
||||
if (*format == '{')
|
||||
{
|
||||
size_t arg_len = print_argument(putc, format, arg);
|
||||
size_t arg_len = detail::parse_format_and_print_argument(putc, format, forward<Arg>(arg));
|
||||
if (arg_len == size_t(-1))
|
||||
return print(putc, format);
|
||||
print(putc, format + arg_len, args...);
|
||||
print(putc, format + arg_len, forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename F, typename... Args>
|
||||
void println(F putc, const char* format, const Args&... args)
|
||||
void println(F putc, const char* format, Args&&... args)
|
||||
{
|
||||
print(putc, format, args...);
|
||||
putc('\n');
|
||||
}
|
||||
|
||||
template<typename F, typename Arg>
|
||||
size_t print_argument(F putc, const char* format, const Arg& argument)
|
||||
namespace detail
|
||||
{
|
||||
ValueFormat value_format;
|
||||
|
||||
if (format[0] != '{')
|
||||
return size_t(-1);
|
||||
|
||||
size_t i = 1;
|
||||
do
|
||||
template<typename F, typename Arg>
|
||||
size_t parse_format_and_print_argument(F putc, const char* format, Arg&& argument)
|
||||
{
|
||||
if (!format[i] || format[i] == '}')
|
||||
break;
|
||||
ValueFormat value_format;
|
||||
|
||||
if ('0' <= format[i] && format[i] <= '9')
|
||||
if (format[0] != '{')
|
||||
return size_t(-1);
|
||||
|
||||
size_t i = 1;
|
||||
do
|
||||
{
|
||||
int fill = 0;
|
||||
while ('0' <= format[i] && format[i] <= '9')
|
||||
if (!format[i] || format[i] == '}')
|
||||
break;
|
||||
|
||||
if ('0' <= format[i] && format[i] <= '9')
|
||||
{
|
||||
fill = (fill * 10) + (format[i] - '0');
|
||||
i++;
|
||||
int fill = 0;
|
||||
while ('0' <= format[i] && format[i] <= '9')
|
||||
{
|
||||
fill = (fill * 10) + (format[i] - '0');
|
||||
i++;
|
||||
}
|
||||
value_format.fill = fill;
|
||||
}
|
||||
value_format.fill = fill;
|
||||
}
|
||||
|
||||
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')
|
||||
switch (format[i])
|
||||
{
|
||||
percision = (percision * 10) + (format[i] - '0');
|
||||
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;
|
||||
}
|
||||
value_format.percision = percision;
|
||||
}
|
||||
|
||||
} while(false);
|
||||
if (!format[i] || format[i] == '}')
|
||||
break;
|
||||
|
||||
if (format[i] != '}')
|
||||
return size_t(-1);
|
||||
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;
|
||||
}
|
||||
|
||||
print_argument_impl(putc, argument, value_format);
|
||||
} while(false);
|
||||
|
||||
return i + 1;
|
||||
}
|
||||
if (format[i] != '}')
|
||||
return size_t(-1);
|
||||
|
||||
static char value_to_base_char(uint8_t value, int base, bool upper)
|
||||
{
|
||||
if (base <= 10)
|
||||
return value + '0';
|
||||
if (base <= 36)
|
||||
print_argument(putc, forward<Arg>(argument), value_format);
|
||||
|
||||
return i + 1;
|
||||
}
|
||||
|
||||
static char value_to_base_char(uint8_t value, int base, bool upper)
|
||||
{
|
||||
if (value < 10)
|
||||
if (base <= 10)
|
||||
return value + '0';
|
||||
return value + (upper ? 'A' : 'a') - 10;
|
||||
if (base <= 36)
|
||||
{
|
||||
if (value < 10)
|
||||
return value + '0';
|
||||
return value + (upper ? 'A' : 'a') - 10;
|
||||
}
|
||||
return '?';
|
||||
}
|
||||
return '?';
|
||||
}
|
||||
|
||||
template<typename F, typename T>
|
||||
void print_integer(F putc, T value, const ValueFormat& format)
|
||||
{
|
||||
if (value == 0)
|
||||
template<typename F, typename T>
|
||||
void print_integer(F putc, T value, const ValueFormat& format)
|
||||
{
|
||||
for (int i = 0; i < format.fill || i < 1; i++)
|
||||
putc('0');
|
||||
return;
|
||||
if (value == 0)
|
||||
{
|
||||
for (int i = 0; i < format.fill || i < 1; i++)
|
||||
putc('0');
|
||||
return;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
while (ptr >= buffer + sizeof(buffer) - format.fill)
|
||||
*(--ptr) = '0';
|
||||
|
||||
if (sign)
|
||||
*(--ptr) = '-';
|
||||
|
||||
print(putc, ptr);
|
||||
}
|
||||
|
||||
bool sign = false;
|
||||
|
||||
// Fits signed 64-bit binary number and null
|
||||
char buffer[66];
|
||||
char* ptr = buffer + sizeof(buffer);
|
||||
*(--ptr) = '\0';
|
||||
|
||||
if (value < 0)
|
||||
template<typename F, typename T>
|
||||
void print_floating(F putc, T value, const ValueFormat& format)
|
||||
{
|
||||
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);
|
||||
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, int_part, format);
|
||||
|
||||
if (format.percision > 0)
|
||||
putc('.');
|
||||
|
||||
for (int i = 0; i < format.percision; i++)
|
||||
{
|
||||
frac_part *= format.base;
|
||||
if (i == format.percision - 1)
|
||||
frac_part += 0.5;
|
||||
|
||||
putc(value_to_base_char((uint8_t)frac_part % format.base, format.base, format.upper));
|
||||
}
|
||||
}
|
||||
|
||||
while (value)
|
||||
template<typename F>
|
||||
void print_pointer(F putc, void* ptr, const ValueFormat& format)
|
||||
{
|
||||
*(--ptr) = value_to_base_char(value % format.base, format.base, format.upper);
|
||||
value /= format.base;
|
||||
uintptr_t value = (uintptr_t)ptr;
|
||||
print(putc, "0x");
|
||||
for (int i = sizeof(void*) * 8 - 4; i >= 0; i -= 4)
|
||||
putc(value_to_base_char((value >> i) & 0xF, 16, format.upper));
|
||||
}
|
||||
|
||||
while (ptr >= buffer + sizeof(buffer) - format.fill)
|
||||
*(--ptr) = '0';
|
||||
|
||||
if (sign)
|
||||
*(--ptr) = '-';
|
||||
|
||||
print(putc, ptr);
|
||||
}
|
||||
|
||||
template<typename F, typename T>
|
||||
void print_floating(F putc, 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, int_part, format);
|
||||
|
||||
if (format.percision > 0)
|
||||
putc('.');
|
||||
|
||||
for (int i = 0; i < format.percision; i++)
|
||||
{
|
||||
frac_part *= format.base;
|
||||
if (i == format.percision - 1)
|
||||
frac_part += 0.5;
|
||||
|
||||
putc(value_to_base_char((uint8_t)frac_part % format.base, format.base, format.upper));
|
||||
}
|
||||
}
|
||||
|
||||
template<typename F>
|
||||
void print_pointer(F putc, void* ptr, const ValueFormat& format)
|
||||
{
|
||||
uintptr_t value = (uintptr_t)ptr;
|
||||
print(putc, "0x");
|
||||
for (int i = sizeof(void*) * 8 - 4; i >= 0; i -= 4)
|
||||
putc(value_to_base_char((value >> i) & 0xF, 16, format.upper));
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -227,27 +237,16 @@ namespace BAN::Formatter
|
||||
|
||||
*/
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, short value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, int value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, long long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F, integral T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
|
||||
template<typename F, floating_point T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_floating(putc, value, format); }
|
||||
template<typename F, pointer T> void print_argument(F putc, T value, const ValueFormat& format) { detail::print_pointer(putc, (void*)value, format); }
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, unsigned short value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned int value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned long long value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, float value, const ValueFormat& format) { print_floating(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, double value, const ValueFormat& format) { print_floating(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, long double value, const ValueFormat& format) { print_floating(putc, value, format); }
|
||||
template<typename F> void print_argument(F putc, char value, const ValueFormat&) { putc(value); }
|
||||
template<typename F> void print_argument(F putc, bool value, const ValueFormat&) { print(putc, value ? "true" : "false"); }
|
||||
template<typename F> void print_argument(F putc, const char* value, const ValueFormat&) { print(putc, value);}
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, char value, const ValueFormat&) { putc(value); }
|
||||
template<typename F> void print_argument_impl(F putc, signed char value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, unsigned char value, const ValueFormat& format) { print_integer(putc, value, format); }
|
||||
|
||||
template<typename F> void print_argument_impl(F putc, bool value, const ValueFormat& format) { print(putc, value ? "true" : "false"); }
|
||||
|
||||
template<typename F, typename T> void print_argument_impl(F putc, T* value, const ValueFormat& format) { print_pointer(putc, (void*)value, format); }
|
||||
template<typename F> void print_argument_impl(F putc, const char* value, const ValueFormat&) { print(putc, value);}
|
||||
//template<typename F> void print_argument(F putc, signed char value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
|
||||
//template<typename F> void print_argument(F putc, unsigned char value, const ValueFormat& format) { detail::print_integer(putc, value, format); }
|
||||
//template<typename F, typename T> void print_argument(F putc, T* value, const ValueFormat& format) { detail::print_pointer(putc, (void*)value, format); }
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user