summaryrefslogtreecommitdiff
path: root/include
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2025-06-11 10:00:46 -0400
committerAndrew Opalach <andrew@akon.city> 2025-06-11 10:00:46 -0400
commit93f664a69a3c688d196bdc228d572b45a65e7991 (patch)
tree3ea386156bf3e009cba22ca9112aafcda15bd955 /include
parentb403770e7eb6e0a5a8ca14a24f91dbf214ffe917 (diff)
downloadlibalabaster-93f664a69a3c688d196bdc228d572b45a65e7991.tar.gz
libalabaster-93f664a69a3c688d196bdc228d572b45a65e7991.tar.bz2
libalabaster-93f664a69a3c688d196bdc228d572b45a65e7991.zip
(w)str: Fix str macros
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'include')
-rw-r--r--include/al/lib.h7
-rw-r--r--include/al/str.h3
-rw-r--r--include/al/wstr.h18
3 files changed, 16 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);