Kernel: Formatter now supports fill to length

This commit is contained in:
Bananymous 2022-12-13 01:16:49 +02:00
parent 2938c85c1a
commit 1283e74ee6
1 changed files with 20 additions and 2 deletions

View File

@ -34,6 +34,7 @@ namespace Formatter
{ {
int base = 10; int base = 10;
int percision = 3; int percision = 3;
int fill = 0;
bool upper = false; bool upper = false;
}; };
@ -72,7 +73,6 @@ namespace Formatter
PUTC_LIKE('\n'); PUTC_LIKE('\n');
} }
template<void(*PUTC_LIKE)(char), typename Arg> template<void(*PUTC_LIKE)(char), typename Arg>
size_t print_argument(const char* format, Arg argument) size_t print_argument(const char* format, Arg argument)
{ {
@ -87,6 +87,17 @@ namespace Formatter
if (!format[i] || format[i] == '}') if (!format[i] || format[i] == '}')
break; break;
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;
}
switch (format[i]) switch (format[i])
{ {
case 'b': value_format.base = 2; value_format.upper = false; i++; break; case 'b': value_format.base = 2; value_format.upper = false; i++; break;
@ -142,7 +153,11 @@ namespace Formatter
void print_integer(T value, const ValueFormat& format) void print_integer(T value, const ValueFormat& format)
{ {
if (value == 0) if (value == 0)
return PUTC_LIKE('0'); {
for (int i = 0; i < format.fill || i < 1; i++)
PUTC_LIKE('0');
return;
}
bool sign = false; bool sign = false;
@ -165,6 +180,9 @@ namespace Formatter
value /= format.base; value /= format.base;
} }
while (ptr >= buffer + sizeof(buffer) - format.fill)
*(--ptr) = '0';
if (sign) if (sign)
*(--ptr) = '-'; *(--ptr) = '-';