diff options
| author | 2026-09-14 08:48:15 -0400 | |
|---|---|---|
| committer | 2026-09-14 08:48:15 -0400 | |
| commit | a35dc6bc6815b5ae077b9ce38e76bfd509c6cabc (patch) | |
| tree | 072bb11b74062ebdf5f1ffad1e889753c5bf0821 /include/al | |
| parent | 774d6e02030b0b33d4ce0829cf3fbddcd9d0869a (diff) | |
| download | libalabaster-a35dc6bc6815b5ae077b9ce38e76bfd509c6cabc.tar.gz libalabaster-a35dc6bc6815b5ae077b9ce38e76bfd509c6cabc.tar.bz2 libalabaster-a35dc6bc6815b5ae077b9ce38e76bfd509c6cabc.zip | |
wstr: Cleanup conversion functions
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'include/al')
| -rw-r--r-- | include/al/wstr.h | 30 |
1 files changed, 14 insertions, 16 deletions
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; |