From 5fe891c10d2f9d9efd24dbea2703ad56e16bd9e3 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Fri, 4 Oct 2024 12:36:18 -0400 Subject: (w)str: Guard against error-prone str_c() usage Also, reorder al_str_insert() arguments. Signed-off-by: Andrew Opalach --- include/al/str.h | 7 ++++--- include/al/wstr.h | 3 ++- 2 files changed, 6 insertions(+), 4 deletions(-) (limited to 'include') diff --git a/include/al/str.h b/include/al/str.h index d5c7266..b06bf57 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -14,7 +14,8 @@ typedef struct { #define al_str_at(s, i) (s)->data[i] #define al_str_atr(s, i) (s)->data[(s)->len + i] -#define al_str_c(s) (&((str){ (u32)(sizeof(s) - 1), 0, ((char *)(s)) })) +#define al_string_if_literal(s) ((char *)(s "")) // use al_str_cr for runtime. +#define al_str_c(s) (&((str){ (u32)((sizeof(s) / sizeof(char)) - 1), 0, al_string_if_literal(s) })) #define al_str_cr(s) (&((str){ (u32)al_strlen(s), 0, ((char *)(s)) })) // runtime #define al_str_w(s, i, n) (&((str){ ((u32)(n)), 0, &(s)[i] })) #define al_str_substr(s, start, end) al_str_w((s)->data, start, ((end) - (start))) @@ -98,7 +99,7 @@ static inline void al_str_cat(str *s, str *c) s->len += c->len; } -static inline void al_str_insert(str *s, str *c, u32 i) +static inline void al_str_insert(str *s, u32 i, str *c) { s->alloc = al_str_reserve(s, s->len + c->len); char *data = s->data + i; @@ -109,7 +110,7 @@ static inline void al_str_insert(str *s, str *c, u32 i) static inline void al_str_prepend(str *s, str *c) { - al_str_insert(s, c, 0); + al_str_insert(s, 0, c); } static inline s32 al_str_cmp(str *s, str *c, u32 start, u32 len) diff --git a/include/al/wstr.h b/include/al/wstr.h index 7c7e441..ed2c051 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -17,7 +17,8 @@ typedef struct { #define al_wstr_at(w, i) (w)->data[i] #define al_wstr_atr(w, i) (w)->data[(w)->len + i] -#define al_wstr_c(w) (&((wstr){ (u32)((sizeof(w) / sizeof(wchar_t)) - 1), 0, ((wchar_t *)(w)) })) +#define al_wide_string_if_literal(w) ((wchar_t *)(L"" w)) // use al_wstr_cr for runtime. +#define al_wstr_c(w) (&((wstr){ (u32)((sizeof(w) / sizeof(wchar_t)) - 1), 0, al_wide_string_if_literal(w) })) #define al_wstr_cr(w) (&((wstr){ (u32)wcslen(w), 0, ((wchar_t *)(w)) })) // runtime #define al_wstr_w(w, i, n) (&((wstr){ ((u32)(n)), 0, &al_wstr_at(w, i) })) #define al_wstr_substr(w, start, end) al_wstr_w(w, start, ((end) - (start))) -- cgit v1.2.3-101-g0448