From a35dc6bc6815b5ae077b9ce38e76bfd509c6cabc Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Mon, 14 Sep 2026 08:48:15 -0400 Subject: wstr: Cleanup conversion functions Signed-off-by: Andrew Opalach --- include/al/wstr.h | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) (limited to 'include') diff --git a/include/al/wstr.h b/include/al/wstr.h index 70dccff..4c37453 100644 --- a/include/al/wstr.h +++ b/include/al/wstr.h @@ -81,14 +81,14 @@ static inline bool al_wstr_from_cstr(wstr *w, char *c) #ifdef AL_HAVE_WIDE_STRING mbstate_t mbstate = { 0 }; size_t length = mbsrtowcs(NULL, (const char **)&c, 0, &mbstate); - if (length == (size_t)-1) { - return false; + bool success = length != (size_t)-1; + if (success) { + al_wstr_sized(w, (u32)(length + 1)); + w->length = (u32)mbsrtowcs(w->data, (const char **)&c, length, &mbstate); + al_assert(w->length == (u32)length); + w->data[w->length] = L'\0'; } - al_wstr_sized(w, (u32)(length + 1)); - w->length = (u32)mbsrtowcs(w->data, (const char **)&c, length, &mbstate); - al_assert(w->length == (u32)length); - w->data[w->length] = L'\0'; - return true; + return success; #else (void)c; (void)w; @@ -103,10 +103,8 @@ static inline void al_wstr_from_str(wstr *w, str *s) al_free(c_str); } -// This is horrible. static inline bool al_str_from_wstr(str *s, wstr *w) { - // This is broken on Windows. Locale issue? #ifdef AL_HAVE_WIDE_STRING wstr copy; // We have to make a copy in case w->length is less than the @@ -115,15 +113,15 @@ static inline bool al_str_from_wstr(str *s, wstr *w) wchar_t *copy_data = copy.data; mbstate_t mbstate = { 0 }; size_t length = wcsrtombs(NULL, (const wchar_t **)©_data, 0, &mbstate); - if (length == (size_t)-1) { - return false; + bool success = length != (size_t)-1; + if (success) { + al_str_sized(s, (u32)length); + // Excludes the null terminator. + s->length = (u32)wcsrtombs(s->data, (const wchar_t **)©_data, length, &mbstate); + al_assert(s->length == (u32)length); } - al_str_sized(s, (u32)length); - // Excludes the null terminator. - s->length = (u32)wcsrtombs(s->data, (const wchar_t **)©_data, length, &mbstate); - al_assert(s->length == (u32)length); al_wstr_free(©); - return true; + return success; #else (void)s; (void)w; -- cgit v1.2.3-101-g0448