LibC: Fix printf with alternate format and zero values

This commit is contained in:
Bananymous 2024-09-26 15:25:16 +03:00
parent cea6dedccc
commit e4982a1a5c
1 changed files with 14 additions and 1 deletions

View File

@ -50,6 +50,16 @@ static void integer_to_string(char* buffer, T value, int base, bool upper, forma
options.zero_padded = false; options.zero_padded = false;
} }
if (value == 0 && options.alternate_form)
{
if (digits == 0 && base == 8)
digits = 1;
for (int i = 0; i < digits; i++)
buffer[i] = '0';
buffer[digits] = '\0';
return;
}
auto digit_char = [](int digit, bool upper) auto digit_char = [](int digit, bool upper)
{ {
if (digit < 10) if (digit < 10)
@ -83,6 +93,7 @@ static void integer_to_string(char* buffer, T value, int base, bool upper, forma
{ {
prefix_length = 1; prefix_length = 1;
prefix[0] = '0'; prefix[0] = '0';
digits--;
} }
else if (options.alternate_form && base == 16) else if (options.alternate_form && base == 16)
{ {
@ -361,7 +372,9 @@ extern "C" int printf_impl(const char* format, va_list arguments, int (*putc_fun
format--; format--;
format++; format++;
char conversion[128]; // FIXME: this should be thread-local to keep
// satisfy multithreaded requirement
static char conversion[4096];
const char* string = nullptr; const char* string = nullptr;
int length = -1; int length = -1;