-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkm_string.cpp
453 lines (394 loc) · 10.8 KB
/
km_string.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include "km_string.h"
#include <ctype.h>
#include <stb_sprintf.h>
#define UTF8PROC_STATIC
#ifdef KM_UTF8
#include <utf8proc.h>
#endif
#include "km_math.h"
uint32 StringLength(const char* str)
{
uint32 length = 0;
while (*(str++) != '\0') {
length++;
}
return length;
}
const_string ToString(const char* cString)
{
return const_string {
.size = StringLength(cString),
.data = (char*)cString
};
}
string ToNonConstString(const_string constString)
{
return string {
.size = constString.size,
.data = (char*)constString.data
};
}
#ifdef KM_CPP_STD
string ToString(const std::string& str)
{
return {
.size = str.size(),
.data = (char*)str.c_str()
};
}
#endif
template <uint32 S>
void InitFromCString(FixedArray<char, S>* string, const char* cString)
{
uint32 stringLength = StringLength(cString);
if (stringLength > S) {
stringLength = S;
}
string->array.size = stringLength;
MemCopy(string->fixedArray, cString, stringLength * sizeof(char));
}
template <typename Allocator>
char* ToCString(const_string str, Allocator* allocator)
{
char* cString = (char*)allocator->Allocate(str.size + 1);
if (!cString) {
return nullptr;
}
MemCopy(cString, str.data, str.size * sizeof(char));
cString[str.size] = '\0';
return cString;
}
int StringCompare(const_string str1, const_string str2)
{
uint32 minSize = MinUInt32(str1.size, str2.size);
for (uint32 i = 0; i < minSize; i++) {
if (str1[i] < str2[i]) {
return -1;
}
else if (str1[i] > str2[i]) {
return 1;
}
}
if (str1.size < str2.size) {
return -1;
}
else if (str1.size > str2.size) {
return 1;
}
return 0;
}
bool StringEquals(const_string str1, const_string str2)
{
if (str1.size != str2.size) {
return false;
}
for (uint32 i = 0; i < str1.size; i++) {
if (str1[i] != str2[i]) {
return false;
}
}
return true;
}
void CatStrings(size_t sourceACount, const char* sourceA,
size_t sourceBCount, const char* sourceB,
size_t destCount, char* dest)
{
UNREFERENCED_PARAMETER(destCount);
DEBUG_ASSERT(sourceACount + sourceBCount <= destCount);
for (size_t i = 0; i < sourceACount; i++) {
*dest++ = *sourceA++;
}
for (size_t i = 0; i < sourceBCount; i++) {
*dest++ = *sourceB++;
}
*dest++ = '\0';
}
void StringCat(const char* str1, const char* str2, char* dest, uint32 destMaxLength)
{
CatStrings(StringLength(str1), str1, StringLength(str2), str2, destMaxLength, dest);
}
// TODO slow, naive implementation, but perfectly fine for small strings
uint32 SubstringSearch(const_string str, const_string substr)
{
for (uint32 i = 0; i < str.size; i++) {
bool match = true;
for (uint32 j = 0; j < substr.size; j++) {
uint32 ind = i + j;
if (ind >= str.size) {
match = false;
break;
}
if (str[i + j] != substr[j]) {
match = false;
break;
}
}
if (match) {
return i;
}
}
return str.size;
}
bool StringContains(const_string str, const_string substr)
{
return SubstringSearch(str, substr) != str.size;
}
inline bool IsNewline(char c)
{
return c == '\n' || c == '\r';
}
inline bool IsWhitespace(char c)
{
return c == ' ' || c == '\t'
|| c == '\n' || c == '\v' || c == '\f' || c == '\r';
}
inline bool IsAlphanumeric(char c)
{
return isalnum(c) != 0;
}
string TrimWhitespace(const_string str)
{
uint32 start = 0;
while (start < str.size && IsWhitespace(str[start])) {
start++;
}
uint32 end = str.size;
while (end > 0 && IsWhitespace(str[end - 1])) {
end--;
}
return string {
.size = end - start,
.data = (char*)str.data + start
};
}
bool StringToIntBase10(const_string str, int* intBase10)
{
if (str.size == 0) {
return false;
}
bool negative = false;
*intBase10 = 0;
for (uint32 i = 0; i < str.size; i++) {
char c = str[i];
if (i == 0 && c == '-') {
negative = true;
continue;
}
if (c < '0' || c > '9') {
return false;
}
*intBase10 = (*intBase10) * 10 + (int)(c - '0');
}
if (negative) {
*intBase10 = -(*intBase10);
}
return true;
}
bool StringToUInt32Base10(const_string str, uint32* intBase10)
{
if (str.size == 0) {
return false;
}
*intBase10 = 0;
for (uint32 i = 0; i < str.size; i++) {
char c = str[i];
*intBase10 = (*intBase10) * 10 + (uint32)(c - '0');
}
return true;
}
bool StringToFloat32(const_string str, float32* f)
{
uint32 dotIndex = 0;
while (dotIndex < str.size && str[dotIndex] != '.') {
dotIndex++;
}
int whole = 0;
float32 wholeNegative = false;
if (dotIndex > 0) {
const_string stringWhole = str.SliceTo(dotIndex);
if (!StringToIntBase10(stringWhole, &whole)) {
return false;
}
wholeNegative = str[0] == '-';
}
*f = (float32)whole;
int frac = 0;
if (dotIndex + 1 < str.size) {
const_string fracString = str.SliceFrom(dotIndex + 1);
if (!StringToIntBase10(fracString, &frac)) {
return false;
}
frac = wholeNegative ? -frac : frac;
float32 fractional = (float32)frac;
for (uint32 i = 0; i < fracString.size; i++) {
fractional /= 10.0f;
}
*f += fractional;
}
return true;
}
template <typename Allocator>
void StringSplit(const_string str, char c, DynamicArray<string, Allocator>* outSplit)
{
outSplit->Clear();
string s = ToNonConstString(str);
while (true) {
uint32 next = s.FindFirst(c);
if (next == s.size) {
outSplit->Append(s);
break;
}
uint32 newSize = s.size - next - 1;
s.size = next;
outSplit->Append(s);
s.data += next + 1;
s.size = newSize;
}
}
string NextSplitElement(string* str, char separator)
{
string next = *str;
for (uint32 i = 0; i < str->size; i++) {
if ((*str)[i] == separator) {
next.size = i;
str->size--;
break;
}
}
str->size -= next.size;
str->data += next.size + 1;
return next;
}
template <typename Allocator>
string StringConcatenate(const_string str1, const_string str2, Allocator* allocator)
{
string result;
result.size = str1.size + str2.size;
result.data = allocator->New<char>(result.size);
if (result.data == nullptr) {
return string::empty;
}
MemCopy(result.data, str1.data, str1.size);
MemCopy(result.data + str1.size, str2.data, str2.size);
return result;
}
bool SizedPrintf(string* str, const char* format, ...)
{
va_list args;
va_start(args, format);
const int length = stbsp_vsnprintf(str->data, str->size, format, args);
va_end(args);
if (length < 0 || length >= (int)str->size) {
return false;
}
str->size = length;
return true;
}
template <typename Allocator>
string AllocPrintf(Allocator* allocator, const char* format, ...)
{
int bufferSize = 256;
while (true) {
char* buffer = (char*)allocator->Allocate(bufferSize * sizeof(char));
va_list args;
va_start(args, format);
int length = stbsp_vsnprintf(buffer, bufferSize, format, args);
va_end(args);
if (length < 0) {
return { .size = 0, .data = nullptr };
}
else if (length < bufferSize) {
return { .size = (uint32)length, .data = buffer };
}
else {
bufferSize *= 2;
}
}
}
template <typename Allocator>
char* AllocPrintfDynamicArrayCallback(const char* buffer, void* user, int length)
{
DynamicArray<char, Allocator>* result = *((DynamicArray<char, Allocator>*)user);
result->Append(string { .size = length, .data = buffer });
return buffer;
}
template <typename Allocator>
DynamicArray<char, Allocator> AllocPrintf(const char* format, ...)
{
DynamicArray<char, Allocator> result;
char buffer[STB_SPRINTF_MIN];
va_list args;
va_start(args, format);
int length = stbsp_vsprintfcb(AllocPrintfDynamicArrayCallback<Allocator>,
&result, buffer, format, args);
va_end(args);
if (length < 0) {
result.Clear();
}
else if (length != result.size) {
result.Clear();
}
return result;
}
template <typename T>
bool StringToElementArray(const_string str, char sep, bool trimElements,
bool (*conversionFunction)(const_string, T*),
int maxElements, T* array, int* numElements)
{
int elementInd = 0;
string s = ToNonConstString(str);
while (true) {
string next = NextSplitElement(&s, sep);
string trimmed;
if (trimElements) {
trimmed = TrimWhitespace(next);
}
else {
trimmed = next;
}
if (!conversionFunction(trimmed, array + elementInd)) {
LOG_ERROR("String to array failed for %.*s in element %d conversion\n",
(int)str.size, str.data, elementInd);
return false;
}
if (s.size == 0) {
break;
}
elementInd++;
if (elementInd >= maxElements) {
LOG_ERROR("String to array failed in %.*s (too many elements, max %d)\n",
(int)str.size, str.data, maxElements);
return false;
}
}
*numElements = elementInd + 1;
return true;
}
#ifdef KM_UTF8
template <typename Allocator>
bool Utf8ToUppercase(const_string utf8String, DynamicArray<char, Allocator>* outString)
{
FixedArray<char, 4> utf8Buffer;
uint64 i = 0;
while (i < utf8String.size) {
int32 codePoint;
utf8proc_ssize_t codePointBytes = utf8proc_iterate((uint8*)&utf8String[i],
utf8String.size - i, &codePoint);
if (codePointBytes < 0) {
LOG_ERROR("Invalid UTF-8 bytes\n");
return false;
}
i += codePointBytes;
int32 codePointUpper = utf8proc_toupper(codePoint);
utf8proc_ssize_t codePointUpperBytes = utf8proc_encode_char(codePointUpper,
(uint8*)utf8Buffer.data);
if (codePointUpperBytes == 0) {
LOG_ERROR("Failed to write UTF-8 codePointUpper\n");
return false;
}
utf8Buffer.size = codePointUpperBytes;
outString->Append(utf8Buffer.ToArray());
}
return true;
}
#endif