From 4da4b06c8ae79370340a60ca83bd1f0206068ed3 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Fri, 2 Feb 2024 23:00:02 -0500 Subject: Add some wstr functions - al_wstr_c - al_wstr_cmp - al_wstr_rfind Signed-off-by: Andrew Opalach --- include/al/str.h | 2 +- include/al/wstr.h | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/al/str.h b/include/al/str.h index 9be1394..0ab7520 100644 --- a/include/al/str.h +++ b/include/al/str.h @@ -116,7 +116,7 @@ static inline s32 al_str_cmp(str *s, str *c, u32 start, u32 len) if (start + len > s->len || len > c->len) { return -1; } - return al_memcmp(s->data + start, c->data, len); + return al_memcmp(&al_str_at(s, start), c->data, len); } static inline bool al_str_eq(str *s, str *c) diff --git a/include/al/wstr.h b/include/al/wstr.h index 540ed55..2989ff9 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -18,6 +18,7 @@ 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)wcslen(w), 0, ((wchar_t *)(w)) })) #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))) @@ -86,6 +87,19 @@ static inline s32 al_wstr_width(wstr *w) return wcswidth(w->data, w->len); } +static inline s32 al_wstr_cmp(wstr *w, wstr *c, u32 start, u32 len) +{ + return wcsncmp(&al_wstr_at(w, start), c->data, len); +} + +static inline s32 al_wstr_rfind(wstr *w, wchar_t c) +{ + for (s32 i = w->len - 1; i >= 0; i--) { + if (al_wstr_at(w, i) == c) return i; + } + return -1; +} + AL_UNUSED_FUNCTION_POP #endif // _AL_WSTR_H -- cgit v1.2.3-101-g0448