summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2024-12-24 16:46:22 -0500
committerAndrew Opalach <andrew@akon.city> 2024-12-24 16:46:22 -0500
commite4703b59fe99e57186b8993f46e85295b317d771 (patch)
treea73e26c84fdded4620408893e2edd5006187468d
parent42c161bbeb30c17c1ef39f84e13e54ff207f09da (diff)
downloadlibalabaster-e4703b59fe99e57186b8993f46e85295b317d771.tar.gz
libalabaster-e4703b59fe99e57186b8993f46e85295b317d771.tar.bz2
libalabaster-e4703b59fe99e57186b8993f46e85295b317d771.zip
wstr: Handle errors, separate width functions
Signed-off-by: Andrew Opalach <andrew@akon.city>
-rw-r--r--include/al/wstr.h23
-rw-r--r--meson.build7
2 files changed, 22 insertions, 8 deletions
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
}
diff --git a/meson.build b/meson.build
index d3d83e1..88247f8 100644
--- a/meson.build
+++ b/meson.build
@@ -12,7 +12,8 @@ have_builtin_expect = compiler.has_function('__builtin_expect')
have_typeof = compiler.has_function('__typeof__')
have_posix_memalign = compiler.has_function(
'posix_memalign', prefix: '#define _POSIX_C_SOURCE 200112L\n#include <stdlib.h>')
-have_wide_string = compiler.has_function('wcswidth')
+have_wide_string = compiler.has_function('wcsrtombs')
+have_wide_string_width = compiler.has_function('wcswidth')
alabaster_args = []
@@ -40,6 +41,10 @@ if have_wide_string
alabaster_args += ['-DAL_HAVE_WIDE_STRING']
endif
+if have_wide_string_width
+ alabaster_args += ['-DAL_HAVE_WIDE_STRING_WIDTH']
+endif
+
c89atomic = subproject('c89atomic').get_variable('c89atomic')
alabaster_src = [