summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/al/lib.h7
-rw-r--r--include/al/str.h3
-rw-r--r--include/al/wstr.h18
-rw-r--r--tests/str.c10
4 files changed, 26 insertions, 12 deletions
diff --git a/include/al/lib.h b/include/al/lib.h
index 154474e..e661572 100644
--- a/include/al/lib.h
+++ b/include/al/lib.h
@@ -39,6 +39,7 @@
#ifdef AL_USE_STDLIB
#include <stdlib.h>
#include <string.h>
+#include <wchar.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
@@ -116,6 +117,12 @@ static inline char *al_strndup(const char *s, size_t n)
return ret;
}
+static inline size_t al_wcsnlen(const wchar_t *w, size_t n)
+{
+ const wchar_t *p = (const wchar_t *)wmemchr(w, L'\0', n);
+ return p ? (size_t)(p - w) : n;
+}
+
static inline s32 al_printf(const char *fmt, ...)
{
#ifdef AL_FORCE_DISABLE_OUTPUT
diff --git a/include/al/str.h b/include/al/str.h
index 10f30ea..2055301 100644
--- a/include/al/str.h
+++ b/include/al/str.h
@@ -2,7 +2,6 @@
#define _AL_STR_H
#include "lib.h"
-#include "macros.h"
typedef struct {
u32 length;
@@ -17,7 +16,7 @@ typedef struct {
#define al_str_at(s, index) (s)->data[index]
#define al_str_atr(s, index) (s)->data[(s)->length - (index)]
-#define al_chars_if_literal(s) ((char *)(s "")) // use al_str_cs/cr for non-static arrays.
+#define al_chars_if_literal(s) ((char *)("" s "")) // use al_str_cs/cr for non-static arrays.
#define al_str_c(c) ((str){ (u32)(sizeof(c) - 1), 0, al_chars_if_literal(c) })
#define al_str_cs(c) ((str){ (u32)al_strnlen(c, sizeof(c)), 0, ((char *)(c)) }) // (s)ized
#define al_str_cr(c) ((str){ (u32)al_strnlen(c, MAX_ALLOC_32), 0, ((char *)(c)) }) // (r)untime
diff --git a/include/al/wstr.h b/include/al/wstr.h
index 27239d9..47dac45 100644
--- a/include/al/wstr.h
+++ b/include/al/wstr.h
@@ -4,8 +4,6 @@
#include "lib.h"
#include "str.h"
-#include <wchar.h>
-
typedef struct {
u32 length;
u32 alloc;
@@ -19,21 +17,16 @@ typedef struct {
#define al_wstr_at(w, index) (w)->data[index]
#define al_wstr_atr(w, index) (w)->data[(w)->length - (index)]
-#define al_wide_chars_if_literal(wc) ((wchar_t *)(L"" wc)) // use al_wstr_cs/r for non-static arrays.
+#define al_wide_chars_if_literal(wc) ((wchar_t *)(L"" wc L"")) // use al_wstr_cs/r for non-static arrays.
#define al_wstr_c(wc) ((wstr){ (u32)((sizeof(wc) / sizeof(wchar_t)) - 1), 0, al_wide_chars_if_literal(wc) })
-#define al_wstr_cs(wc) ((wstr){ (u32)wcsnlen(wc, sizeof(wc)), 0, ((wchar_t *)(wc)) }) // (s)ized
-#define al_wstr_cr(wc) ((wstr){ (u32)wcsnlen(wc, MAX_ALLOC_32), 0, ((wchar_t *)(wc)) }) // (r)untime
+#define al_wstr_cs(wc) ((wstr){ (u32)al_wcsnlen(wc, sizeof(wc) / sizeof(wchar_t)), 0, ((wchar_t *)(wc)) }) // (s)ized
+#define al_wstr_cr(wc) ((wstr){ (u32)al_wcsnlen(wc, MAX_ALLOC_32 / sizeof(wchar_t)), 0, ((wchar_t *)(wc)) }) // (r)untime
#define al_wstr_w(wc, index, n) ((wstr){ ((u32)(n)), 0, (wc) + (index) })
#define al_wstr_substr(w, start, end) al_wstr_w((w)->data, start, ((end) - (start)))
#define al_wstr_x(w) (w)->data // e(x)pand
-static inline bool al_wstr_is_empty(wstr *w)
-{
- return w->length == 0;
-}
-
static inline s32 al_wchar_width(wchar_t wc)
{
#ifdef AL_HAVE_WIDE_STRING_WIDTH
@@ -44,6 +37,11 @@ static inline s32 al_wchar_width(wchar_t wc)
#endif
}
+static inline bool al_wstr_is_empty(wstr *w)
+{
+ return w->length == 0;
+}
+
static inline void al_wstr_free(wstr *w)
{
if (w->alloc > 0) al_free(w->data);
diff --git a/tests/str.c b/tests/str.c
index de5f576..03284d6 100644
--- a/tests/str.c
+++ b/tests/str.c
@@ -346,6 +346,13 @@ static bool str_test_error_prone_str_c(void)
AL_TEST_TRUE(al_str_eq(&global_string, &al_str_c("yeeee")));
#endif
+ wstr w = al_wstr_c(L"Ayo");
+ wstr wr = al_wstr_cr(L"Ayo");
+ AL_TEST_EQ(w.length, al_wcsnlen(L"Ayo", sizeof(L"Ayo") / sizeof(wchar_t)), u32);
+ AL_TEST_EQ(wr.length, 3, u32);
+ AL_TEST_EQ(w.alloc, 0, u32);
+ AL_TEST_EQ(al_memcmp(w.data, L"Ayo", sizeof(L"Ayo")), 0, s32);
+
// This is expected to cause a compile error.
#if 0
char *d = al_malloc(26);
@@ -369,6 +376,9 @@ static bool str_test_error_prone_str_c(void)
AL_TEST_EQ(wss.length, (u32)wcslen(ws), u32);
al_free(wd);
+
+ str sss = al_str_c(true ? "true" : "never");
+ wstr www = al_wstr_c(true ? L"false" : L"always");
#endif
AL_TEST_END();