summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/al/str.h3
-rw-r--r--src/str.c20
2 files changed, 23 insertions, 0 deletions
diff --git a/include/al/str.h b/include/al/str.h
index e993cce..b73b8ee 100644
--- a/include/al/str.h
+++ b/include/al/str.h
@@ -186,6 +186,9 @@ static inline u32 al_str_hash(str *s)
return hash;
}
+str *al_print_u32(u32 n);
+str *al_print_s32(s32 n);
+
s64 al_str_to_long(str *s, u32 base, bool *error);
bool al_str_get_line(str *buffer, char sep, str *line);
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)
{