From e4703b59fe99e57186b8993f46e85295b317d771 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 24 Dec 2024 16:46:22 -0500 Subject: wstr: Handle errors, separate width functions Signed-off-by: Andrew Opalach --- include/al/wstr.h | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'include') diff --git a/include/al/wstr.h b/include/al/wstr.h index b6d7afd..765445b 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -30,11 +30,11 @@ AL_UNUSED_FUNCTION_PUSH static inline s32 al_wchar_width(wchar_t wc) { -#ifdef AL_HAVE_WIDE_STRING +#ifdef AL_HAVE_WIDE_STRING_WIDTH return wcwidth(wc); #else (void)wc; - return 0; + al_assert_and_return(0); #endif } @@ -71,18 +71,22 @@ static inline void al_wstr_clone(wstr *dest, wstr *src) dest->len = src->len; } -static inline void al_wstr_from_cstr(wstr *w, char *c) +static inline bool al_wstr_from_cstr(wstr *w, char *c) { #ifdef AL_HAVE_WIDE_STRING mbstate_t mbstate = { 0 }; size_t len = mbsrtowcs(NULL, (const char **)&c, 0, &mbstate); + if (len == (size_t)-1) + return false; al_wstr_sized(w, (u32)(len + 1)); w->len = (u32)mbsrtowcs(w->data, (const char **)&c, len, &mbstate); al_assert(w->len == (u32)len); w->data[w->len] = L'\0'; + return true; #else (void)c; (void)w; + al_assert_and_return(false); #endif } @@ -93,30 +97,35 @@ static inline void al_wstr_from_str(wstr *w, str *s) al_free(c_str); } -static inline void al_wstr_to_str(wstr *w, str *s) +static inline bool al_wstr_to_str(wstr *w, str *s) { + // This is broken on windows. locale issue? #ifdef AL_HAVE_WIDE_STRING wchar_t *data = w->data; mbstate_t mbstate = { 0 }; size_t len = wcsrtombs(NULL, (const wchar_t **)&w->data, 0, &mbstate); + if (len == (size_t)-1) + return false; al_str_sized(s, (u32)len); // Excludes the null terminator. s->len = (u32)wcsrtombs(s->data, (const wchar_t **)&w->data, len, &mbstate); al_assert(s->len == (u32)len); w->data = data; + return true; #else (void)s; (void)w; + al_assert_and_return(false); #endif } static inline s32 al_wstr_width(wstr *w) { -#ifdef AL_HAVE_WIDE_STRING +#ifdef AL_HAVE_WIDE_STRING_WIDTH return wcswidth(w->data, w->len); #else (void)w; - return 0; + al_assert_and_return(0); #endif } @@ -129,7 +138,7 @@ static inline s32 al_wstr_cmp(wstr *w, wstr *c, u32 start, u32 len) (void)c; (void)start; (void)len; - return 0; + al_assert_and_return(0); #endif } -- cgit v1.2.3-101-g0448