From b617e3ccb25a0ac3c22f8c02722c8108b2f78d6e Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 25 Aug 2026 19:29:52 -0400 Subject: str: Thread-local buffer for int to string Signed-off-by: Andrew Opalach --- src/str.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src/str.c') diff --git a/src/str.c b/src/str.c index 2ad46aa..25b267a 100644 --- a/src/str.c +++ b/src/str.c @@ -1,5 +1,25 @@ #include "../include/al/str.h" +static __thread char printbuf[24]; +static __thread str printstr = { + .length = 0, + .alloc = 0, + .data = NULL +}; + +#define DEFINE_PRINT_FUNC(type, fmt) \ + str *al_print_##type(type n) \ + { \ + s32 ret = al_snprintf(printbuf, sizeof(printbuf), fmt, n); \ + al_assert(ret > 0 && (size_t)ret < sizeof(printbuf)); \ + printstr.length = ret; \ + printstr.data = printbuf; \ + return &printstr; \ + } + +DEFINE_PRINT_FUNC(u32, "%u") +DEFINE_PRINT_FUNC(s32, "%d") + // https://sourceware.org/git/?p=newlib-cygwin.git;a=blob;f=newlib/libc/stdlib/strtol.c;h=09d4333ed05f497ec00b7192204644349441e58d;hb=HEAD#l130 s64 al_str_to_long(str *s, u32 base, bool *error) { -- cgit v1.2.3-101-g0448