1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#ifndef _AL_WSTR_H
#define _AL_WSTR_H
#include "lib.h"
#include "str.h"
#include <wchar.h>
typedef struct {
u32 length;
u32 alloc;
wchar_t *__sized_by(alloc) data;
} wstr;
#define AL_WSTR_NPOS ((u32)-1)
#define al_wstr_null() ((wstr){ 0, 0, NULL })
#define al_wstr_at(w, i) (w)->data[i]
#define al_wstr_atr(w, i) (w)->data[(w)->length - (i)]
#define al_wide_chars_if_literal(wc) ((wchar_t *)(L"" wc)) // 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_w(wc, i, n) ((wstr){ ((u32)(n)), 0, (wc) + (i) })
#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
return wcwidth(wc);
#else
(void)wc;
al_assert_and_return(0);
#endif
}
static inline void al_wstr_free(wstr *w)
{
if (w->alloc > 0) al_free(w->data);
}
static inline u32 al_wstr_reserve(wstr *w, u32 size)
{
return al_growing_allocation((void **)&w->data, w->alloc, size, al_malloc, al_realloc);
}
static inline void al_wstr_sized(wstr *w, u32 size)
{
*w = al_wstr_null();
w->alloc = al_wstr_reserve(w, size * sizeof(wchar_t));
}
static inline void al_wstr_clone(wstr *dest, wstr *src)
{
u32 length = src->length;
al_wstr_sized(dest, length + 1);
al_memcpy(dest->data, src->data, length * sizeof(wchar_t));
dest->data[length] = L'\0';
dest->length = src->length;
}
static inline bool al_wstr_from_cstr(wstr *w, char *c)
{
#ifdef AL_HAVE_WIDE_STRING
mbstate_t mbstate = { 0 };
size_t length;
length = mbsrtowcs(NULL, (const char **)&c, 0, &mbstate);
if (length == (size_t)-1) {
return false;
}
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;
#else
(void)c;
(void)w;
al_assert_and_return(false);
#endif
}
static inline void al_wstr_from_str(wstr *w, str *s)
{
char *c_str = al_str_to_c_str(s);
al_wstr_from_cstr(w, c_str);
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 here incase w->length is less than the
// full string pointed to by w->data.
al_wstr_clone(©, 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;
}
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;
#else
(void)s;
(void)w;
al_assert_and_return(false);
#endif
}
static inline void al_wstr_cat(wstr *w, wstr *c)
{
w->alloc = al_wstr_reserve(w, ((w->length + c->length) + 1) * sizeof(wchar_t));
al_memcpy(w->data + w->length, c->data, c->length * sizeof(wchar_t));
w->length += c->length;
w->data[w->length] = L'\0';
}
static inline void al_wstr_insert(wstr *w, u32 i, wstr *c)
{
w->alloc = al_wstr_reserve(w, ((w->length + c->length) + 1) * sizeof(wchar_t));
wchar_t *data = w->data + i;
if (i < w->length) {
al_memmove(data + c->length, data, (w->length - i) * sizeof(wchar_t));
}
al_memcpy(data, c->data, c->length * sizeof(wchar_t));
w->length += c->length;
w->data[w->length] = L'\0';
}
static inline void al_wstr_remove_at(wstr *w, u32 i)
{
if (i >= w->length) {
return;
} else if (i != w->length - 1) {
wchar_t *data = w->data + i;
al_memmove(data, data + 1, (w->length - i - 1) * sizeof(wchar_t));
}
w->length--;
w->data[w->length] = L'\0';
}
static inline s32 al_wstr_width(wstr *w)
{
#ifdef AL_HAVE_WIDE_STRING_WIDTH
return wcswidth(w->data, w->length);
#else
(void)w;
al_assert_and_return(0);
#endif
}
static inline s32 al_wstr_cmp(wstr *w, wstr *c, u32 start, u32 length)
{
#ifdef AL_HAVE_WIDE_STRING
if (length > c->length) return 1;
else if (length + start > w->length) return -1;
return wcsncmp(&al_wstr_at(w, start), c->data, length);
#else
(void)w;
(void)c;
(void)start;
(void)len;
al_assert_and_return(0);
#endif
}
static inline bool al_wstr_eq(wstr *w, wstr *c)
{
return al_wstr_cmp(w, c, 0, MAX(w->length, c->length)) == 0;
}
static inline u32 al_wstr_find(wstr *w, wchar_t c)
{
for (u32 i = 0; i < w->length; i++) {
if (al_wstr_at(w, i) == c) {
return i;
}
}
return AL_WSTR_NPOS;
}
static inline u32 al_wstr_rfind(wstr *w, wchar_t c)
{
for (u32 i = w->length; --i > 0;) {
if (al_wstr_at(w, i) == c) {
return i;
}
}
return AL_WSTR_NPOS;
}
static inline bool al_wstr_tok(wstr *result, wchar_t delim)
{
u32 index = al_wstr_find(result, delim);
if (index == AL_WSTR_NPOS) {
return false;
}
*result = al_wstr_substr(result, index + 1, result->length);
return true;
}
static inline s64 al_wstr_to_long(wstr *w, u32 base, bool *error)
{
str s;
al_str_from_wstr(&s, w);
s64 num = al_str_to_long(&s, base, error);
al_str_free(&s);
return num;
}
#endif // _AL_WSTR_H
|