diff options
author | 3gg <3gg@shellblade.net> | 2023-02-04 14:36:42 -0800 |
---|---|---|
committer | 3gg <3gg@shellblade.net> | 2023-02-04 14:36:42 -0800 |
commit | 20169ed6d5551d2fee59d9e4bc8caa73a8b8da7c (patch) | |
tree | 7ca8b1729d566330b812d5d18c5e846b14ef0a11 /cstring | |
parent | 5ac66dc387f93f10fba1f142e61637a48046fc73 (diff) |
Add itoa.
Diffstat (limited to 'cstring')
-rw-r--r-- | cstring/include/cstring.h | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/cstring/include/cstring.h b/cstring/include/cstring.h index 9425d51..75a5388 100644 --- a/cstring/include/cstring.h +++ b/cstring/include/cstring.h | |||
@@ -4,6 +4,7 @@ | |||
4 | #include <assert.h> | 4 | #include <assert.h> |
5 | #include <bsd/string.h> | 5 | #include <bsd/string.h> |
6 | #include <stdbool.h> | 6 | #include <stdbool.h> |
7 | #include <stdio.h> | ||
7 | 8 | ||
8 | /// A fixed-size string. | 9 | /// A fixed-size string. |
9 | /// The string is null-terminated so that it can be used with the usual C APIs. | 10 | /// The string is null-terminated so that it can be used with the usual C APIs. |
@@ -90,6 +91,14 @@ | |||
90 | \ | 91 | \ |
91 | static inline bool STRING##_eq_cstr(STRING a, const char* b) { \ | 92 | static inline bool STRING##_eq_cstr(STRING a, const char* b) { \ |
92 | return (a.length == strlen(b)) && strncmp(a.str, b, a.length) == 0; \ | 93 | return (a.length == strlen(b)) && strncmp(a.str, b, a.length) == 0; \ |
94 | } \ | ||
95 | \ | ||
96 | static inline STRING STRING##_itoa(int n) { \ | ||
97 | STRING str = (STRING){0}; \ | ||
98 | const int written = snprintf(str.str, SIZE, "%d", n); \ | ||
99 | assert(written >= 0); \ | ||
100 | str.length = (size_t)written; \ | ||
101 | return str; \ | ||
93 | } | 102 | } |
94 | 103 | ||
95 | DEF_STRING(sstring, 32) // Small. | 104 | DEF_STRING(sstring, 32) // Small. |