From abe21ac46759ffef041e52310a17552f36ee9297 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Thu, 9 Apr 2026 09:49:33 -0400 Subject: lib: Add strscmp() for static char arrays Signed-off-by: Andrew Opalach --- include/al/lib.h | 3 +++ include/al/str.h | 3 +-- include/al/wstr.h | 5 +++-- 3 files changed, 7 insertions(+), 4 deletions(-) (limited to 'include/al') diff --git a/include/al/lib.h b/include/al/lib.h index 3195e7b..8e16700 100644 --- a/include/al/lib.h +++ b/include/al/lib.h @@ -26,6 +26,8 @@ AL_STATIC_ASSERT(t1_size_eq_t2, sizeof(t1), ==, sizeof(t2)); \ AL_STATIC_ASSERT(t1_signedness_eq_t2, AL_IS_SIGNED(t1), ==, AL_IS_SIGNED(t2)) +#define AL_CHARS_IF_LITERAL(s) ((char *)("" s "")) // This argument must be a static char array. + #define AL_USE_STDLIB //#define AL_PARANOID_REALLOC //#define AL_MEMORY_TRACKING @@ -95,6 +97,7 @@ static inline void *al_realloc(void *ptr, size_t n) { return ptr ? realloc(ptr, #define al_bzero bzero #define al_strlen strlen #define al_strncmp strncmp +#define al_strscmp(s1, s2) strncmp(s1, AL_CHARS_IF_LITERAL(s2), sizeof(s2)) // strndup is _POSIX_C_SOURCE >= 200809L. //#define al_strndup strndup #define al_snprintf snprintf diff --git a/include/al/str.h b/include/al/str.h index cb7c98f..20a1c26 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -16,8 +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_str_c(c) ((str){ (u32)(sizeof(c) - 1), 0, al_chars_if_literal(c) }) +#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 8af16be..e5610a2 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -12,6 +12,8 @@ typedef struct { wchar_t *__sized_by(alloc) data; } wstr; +#define AL_WIDE_CHARS_IF_LITERAL(wc) ((wchar_t *)(L"" wc L"")) // Use al_wstr_cs/r for non-static char arrays. + #define AL_WSTR_NO_POS ((u32)-1) #define al_wstr_null() ((wstr){ 0, 0, NULL }) @@ -19,8 +21,7 @@ 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 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_c(wc) ((wstr){ (u32)((sizeof(wc) / sizeof(wchar_t)) - 1), 0, AL_WIDE_CHARS_IF_LITERAL(wc) }) #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 -- cgit v1.2.3-101-g0448