-
Notifications
You must be signed in to change notification settings - Fork 0
/
newstring.cpp
390 lines (312 loc) · 8.81 KB
/
newstring.cpp
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// https://github.com/onelivesleft/jai-string/blob/main/Strings_Alloc/Strings_Alloc.jai
#include "newstring.h"
#include <cctype>
// @Important: Must have defined 'allocator' and 'result' beforehand!
#define set_result_allocator() \
if (allocator) result = allocator; \
else if (global_context.allocator) result = global_context.allocator; \
else result = {NULL, __default_allocator};
// @Note: Call get_allocator first before calling this
String alloc_string(i64 nbytes_excluding_zero,
Allocator allocator)
{
auto count = nbytes_excluding_zero;
assert(count >= 0);
String s;
s.count = count;
s.data = static_cast<u8*>(allocator.proc(Allocator_Mode::ALLOCATE, count, 0, NULL, allocator.data));
return s;
}
Allocator get_allocator(Allocator parameter_allocator,
_type_Type type)
{
if (parameter_allocator)
return parameter_allocator;
else if (global_context.allocator)
return global_context.allocator;
else
return {NULL, __default_allocator};
}
bool equal(String a, String b)
{
if (a.count != b.count) return false;
return compare(a, b) == 0;
}
bool equal(char a, char b)
{
return a == b;
}
bool equal_nocase(String a, String b)
{
// Mostly the same as equal(...)
if (a.count != b.count) return false;
return compare_nocase(a, b) == 0;
}
// -1 if a < b
// 0 if a == b
// 1 if a > b
i32 compare(String a, String b)
{
if (a.count > b.count) return -1 * compare(b, a);
i64 it = 0;
while ((it < a.count) && (a[it] == b[it])) ++it;
if (it == a.count)
{
// Example: compare("fun", "fun")
if (it == b.count) return 0;
else
{
// Example: compare("dog", "dog2")
// Treat the shorter one as of it has trailing 0's
return 0 - b[it];
}
}
// Example: compare("fat", "far")
return a[it] - b[it];
}
i32 compare_nocase(String a, String b)
{
if (a.count > b.count) return -1 * compare(b, a);
i64 it = 0;
// -----------------------v This is the only difference between compare(...) and compare_nocase(...)
while ((it < a.count) && (tolower(a[it]) == tolower(b[it]))) ++it;
if (it == a.count)
{
if (it == b.count) return 0;
else return 0 - b[it];
}
return a[it] - b[it];
}
bool contains(String str, String substring)
{
if (substring.count > str.count) return false;
i64 it = 0;
while (it <= (str.count - substring.count))
{
for (i64 sub_it = 0;
(sub_it < substring.count) && (str[it + sub_it] == substring[sub_it]);
++sub_it)
{
if (sub_it == (substring.count - 1)) return true;
}
it += 1;
}
return false;
}
// @Note: Ignore the empty byte case
bool contains(String str, u8 byte)
{
if (str.count == 0) return false;
for (i64 it = 0; it < str.count; ++it)
if (str[it] == byte) return true;
return false;
}
bool begins_with(String str, String prefix)
{
if (str.count <= 0) return false;
i64 it = 0;
while ((it < prefix.count) &&
(it < str.count) &&
(str[it] == prefix[it]))
{
it += 1;
}
return it == prefix.count;
}
bool ends_with(String str, String suffix)
{
if (str.count <= 0) return false;
i64 it = str.count - 1;
i64 index = suffix.count - 1;
while ((index >= 0) &&
(it >= 0) &&
(str[it] == suffix[index]))
{
it -= 1;
index -= 1;
}
return index == -1;
}
// @Important: need to free the returned c_string
u8 *to_c_string(String s, Allocator allocator)
{
auto copied_s = copy_string(s, allocator, true);
return copied_s.data;
}
u8 *temp_c_string(String s)
{
auto temp_copied_s = copy_string(s, {global_context.temporary_storage, __temporary_allocator}, true);
return temp_copied_s.data;
}
String copy_string(String string, Allocator allocator, bool null_terminate)
{
if (!string) return empty(null_terminate);
String result;
auto a = get_allocator(allocator);
if (null_terminate && (string[string.count - 1] != '\0'))
{
result = alloc_string(string.count + 1, a);
memcpy(result.data, string.data, string.count);
result.data[result.count - 1] = '\0';
}
else
{
result = alloc_string(string.count, a);
memcpy(result.data, string.data, string.count);
}
return result;
}
#include <cstdarg>
String join(i64 nstrings, ...) // Temporary storage
{
// First pass to calculate the total length.
va_list args;
va_start(args, nstrings);
i64 len = 0;
for (i64 it_index = 0; it_index < nstrings; ++it_index)
{
auto s = va_arg(args, String);
len += s.count;
}
va_end(args);
// Second pass to memcpy to the result.
String result = talloc_string(len);
i64 cursor = 0;
va_list args2;
va_start(args2, nstrings);
for (i64 it_index = 0; it_index < nstrings; ++it_index)
{
auto s = va_arg(args2, String);
u8 *dest = result.data + cursor;
u8 *src = s.data;
memcpy(dest, src, s.count);
cursor += s.count;
}
va_end(args2);
return result;
}
String join(RArr<String> inputs,
Allocator allocator, bool null_terminate)
{
if (inputs.count == 0) return empty(null_terminate);
if (inputs.count == 1)
{
Allocator result = {};
set_result_allocator();
return copy_string(inputs[0], result, null_terminate);
}
auto last_string = inputs[inputs.count - 1];
bool terminating = null_terminate &&
(last_string.data[last_string.count] != '\0');
auto extra_byte = (terminating) ? 1 : 0;
auto count = 0;
for (auto it : inputs) count += it.count;
Allocator result = {};
set_result_allocator();
auto joined = alloc_string(count + extra_byte, result);
memcpy(joined.data, inputs[0].data, inputs[0].count);
auto position = joined.data + inputs[0].count;
for (i64 i = 1; i < inputs.count; ++i)
{
memcpy(position, inputs[i].data, inputs[i].count);
position += inputs[i].count;
}
if (terminating) joined[position] = '\0';
return joined;
}
String join(RArr<String> inputs, String separator,
Allocator allocator, bool null_terminate)
{
if (inputs.count == 0) return empty(null_terminate);
if (inputs.count == 1)
{
Allocator result = {};
set_result_allocator();
return copy_string(inputs[0], result, null_terminate);
}
auto last_string = inputs[inputs.count - 1];
bool terminating = null_terminate &&
(last_string.data[last_string.count] != '\0');
auto extra_byte = (terminating) ? 1 : 0;
auto count = separator.count * (inputs.count - 1);
for (auto it : inputs) count += it.count;
Allocator result = {};
set_result_allocator();
auto joined = alloc_string(count + extra_byte, result);
memcpy(joined.data, inputs[0].data, inputs[0].count);
auto position = joined.data + inputs[0].count;
for (i64 i = 1; i < inputs.count; ++i)
{
if (separator)
{
memcpy(position, separator.data, separator.count);
position += separator.count;
}
memcpy(position, inputs[i].data, inputs[i].count);
position += inputs[i].count;
}
if (terminating) joined.data[count] = '\0';
return joined;
}
i64 find_index_from_left(String str, u8 byte)
{
for (i64 it = 0; it < str.count; ++it)
if (str[it] == byte) return it;
return -1;
}
i64 find_index_from_left(String str, String substring)
{
assert(0);
}
i64 find_index_from_right(String str, u8 byte)
{
for (i64 it = str.count - 1; it >= 0; --it)
if (str[it] == byte) return it;
return -1;
}
i64 find_index_from_right(String str, String substring)
{
assert(0);
}
my_pair<String, bool> copy_substring(String s, i64 index, i64 bytes, Allocator allocator)
{
String result;
if (s.count < (index + bytes))
{
return {String(""), false};
}
auto a = get_allocator(allocator);
result = alloc_string(bytes, a);
if (!result.data) return {String(""), false};
memcpy(result.data, s.data + index, bytes);
return {result, true};
}
// // Split
// RArr<String> split(String text, String separator, bool reversed, i64 max_results,
// bool skip_empty, bool keep_separator, Allocator allocator)
// {
// assert(0);
// }
/*
#include "array.h"
SArr<String> split(String string, String separator)
{
}
//
// Modifying
String trim_left(String str)
{
}
String trim_right(String str)
{
}
String trim(String str)
{
}
void to_lower_in_place(String str)
{
}
void to_upper_in_place(String str)
{
}
*/