From a2fd14a9a895a99b3268675ca73ed5bca2666249 Mon Sep 17 00:00:00 2001 From: Drew Galbraith Date: Thu, 8 Jun 2023 00:22:24 -0700 Subject: [PATCH] Update sprintf to allow 64 bit types --- lib/libc/src/stdio.cpp | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/lib/libc/src/stdio.cpp b/lib/libc/src/stdio.cpp index e523299..b6b2176 100644 --- a/lib/libc/src/stdio.cpp +++ b/lib/libc/src/stdio.cpp @@ -4,7 +4,7 @@ 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; while (num > 0) { num /= base; @@ -13,7 +13,7 @@ uint32_t num_chars(uint32_t num, uint8_t base) { 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); if (width == 0) { *str = '0'; @@ -58,6 +58,32 @@ int vsprintf(char *str, const char *format, va_list arg) { *(str++) = *(format++); chars++; 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': { int width = sprint_base(str, va_arg(arg, uint32_t), 16); if (width == -1) {