From 93f664a69a3c688d196bdc228d572b45a65e7991 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Wed, 11 Jun 2025 10:00:46 -0400 Subject: (w)str: Fix str macros Signed-off-by: Andrew Opalach --- include/al/lib.h | 7 +++++++ include/al/str.h | 3 +-- include/al/wstr.h | 18 ++++++++---------- 3 files changed, 16 insertions(+), 12 deletions(-) (limited to 'include/al') diff --git a/include/al/lib.h b/include/al/lib.h index 154474e..e661572 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -39,6 +39,7 @@ #ifdef AL_USE_STDLIB #include #include +#include #include #include #include @@ -116,6 +117,12 @@ static inline char *al_strndup(const char *s, size_t n) return ret; } +static inline size_t al_wcsnlen(const wchar_t *w, size_t n) +{ + const wchar_t *p = (const wchar_t *)wmemchr(w, L'\0', n); + return p ? (size_t)(p - w) : n; +} + static inline s32 al_printf(const char *fmt, ...) { #ifdef AL_FORCE_DISABLE_OUTPUT diff --git a/include/al/str.h b/include/al/str.h index 10f30ea..2055301 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -2,7 +2,6 @@ #define _AL_STR_H #include "lib.h" -#include "macros.h" typedef struct { u32 length; @@ -17,7 +16,7 @@ typedef struct { #define al_str_at(s, index) (s)->data[index] #define al_str_atr(s, index) (s)->data[(s)->length - (index)] -#define al_chars_if_literal(s) ((char *)(s "")) // use al_str_cs/cr for non-static arrays. +#define al_chars_if_literal(s) ((char *)("" s "")) // use al_str_cs/cr for non-static arrays. #define al_str_c(c) ((str){ (u32)(sizeof(c) - 1), 0, al_chars_if_literal(c) }) #define al_str_cs(c) ((str){ (u32)al_strnlen(c, sizeof(c)), 0, ((char *)(c)) }) // (s)ized #define al_str_cr(c) ((str){ (u32)al_strnlen(c, MAX_ALLOC_32), 0, ((char *)(c)) }) // (r)untime diff --git a/include/al/wstr.h b/include/al/wstr.h index 27239d9..47dac45 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -4,8 +4,6 @@ #include "lib.h" #include "str.h" -#include - typedef struct { u32 length; u32 alloc; @@ -19,21 +17,16 @@ typedef struct { #define al_wstr_at(w, index) (w)->data[index] #define al_wstr_atr(w, index) (w)->data[(w)->length - (index)] -#define al_wide_chars_if_literal(wc) ((wchar_t *)(L"" wc)) // use al_wstr_cs/r for non-static arrays. +#define al_wide_chars_if_literal(wc) ((wchar_t *)(L"" wc L"")) // use al_wstr_cs/r for non-static arrays. #define al_wstr_c(wc) ((wstr){ (u32)((sizeof(wc) / sizeof(wchar_t)) - 1), 0, al_wide_chars_if_literal(wc) }) -#define al_wstr_cs(wc) ((wstr){ (u32)wcsnlen(wc, sizeof(wc)), 0, ((wchar_t *)(wc)) }) // (s)ized -#define al_wstr_cr(wc) ((wstr){ (u32)wcsnlen(wc, MAX_ALLOC_32), 0, ((wchar_t *)(wc)) }) // (r)untime +#define al_wstr_cs(wc) ((wstr){ (u32)al_wcsnlen(wc, sizeof(wc) / sizeof(wchar_t)), 0, ((wchar_t *)(wc)) }) // (s)ized +#define al_wstr_cr(wc) ((wstr){ (u32)al_wcsnlen(wc, MAX_ALLOC_32 / sizeof(wchar_t)), 0, ((wchar_t *)(wc)) }) // (r)untime #define al_wstr_w(wc, index, n) ((wstr){ ((u32)(n)), 0, (wc) + (index) }) #define al_wstr_substr(w, start, end) al_wstr_w((w)->data, start, ((end) - (start))) #define al_wstr_x(w) (w)->data // e(x)pand -static inline bool al_wstr_is_empty(wstr *w) -{ - return w->length == 0; -} - static inline s32 al_wchar_width(wchar_t wc) { #ifdef AL_HAVE_WIDE_STRING_WIDTH @@ -44,6 +37,11 @@ static inline s32 al_wchar_width(wchar_t wc) #endif } +static inline bool al_wstr_is_empty(wstr *w) +{ + return w->length == 0; +} + static inline void al_wstr_free(wstr *w) { if (w->alloc > 0) al_free(w->data); -- cgit v1.2.3-101-g0448