Update sprintf to allow 64 bit types

This commit is contained in:
Drew Galbraith 2023-06-08 00:22:24 -07:00
parent 56789400d7
commit a2fd14a9a8
1 changed files with 28 additions and 2 deletions

View File

@ -4,7 +4,7 @@
namespace { namespace {
uint32_t num_chars(uint32_t num, uint8_t base) { uint32_t num_chars(uint64_t num, uint8_t base) {
uint32_t width = 0; uint32_t width = 0;
while (num > 0) { while (num > 0) {
num /= base; num /= base;
@ -13,7 +13,7 @@ uint32_t num_chars(uint32_t num, uint8_t base) {
return width; return width;
} }
int sprint_base(char *str, uint32_t num, uint32_t base) { int sprint_base(char *str, uint64_t num, uint32_t base) {
uint32_t width = num_chars(num, base); uint32_t width = num_chars(num, base);
if (width == 0) { if (width == 0) {
*str = '0'; *str = '0';
@ -58,6 +58,32 @@ int vsprintf(char *str, const char *format, va_list arg) {
*(str++) = *(format++); *(str++) = *(format++);
chars++; chars++;
break; break;
case 'l': {
switch (*(++format)) {
case 'x': {
int width = sprint_base(str, va_arg(arg, uint64_t), 16);
if (width == -1) {
return -1;
}
chars += width;
str += width;
format++;
break;
}
case 'u': {
int width = sprint_base(str, va_arg(arg, uint64_t), 10);
if (width == -1) {
return -1;
}
chars += width;
str += width;
format++;
break;
}
}
break;
}
case 'x': { case 'x': {
int width = sprint_base(str, va_arg(arg, uint32_t), 16); int width = sprint_base(str, va_arg(arg, uint32_t), 16);
if (width == -1) { if (width == -1) {