aboutsummaryrefslogtreecommitdiff
path: root/src/un_strings.c
blob: ff74c39389cfdc801df1b94707a68551d7523635 (plain)
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
u64 un_string_get_length(u8 *cstring) {
    u8* start = cstring;

    while (*start != '\0') {
        start++;
    }

    return start - cstring;
}

String un_string_from_cstring(u8* cstring) {
    String result;
    u64 length = un_string_get_length(cstring);
    assert(length < LLONG_MAX);
    result.size = length;
    result.data = cstring;
    return result;
}

u8* un_string_to_cstring(String string, Allocator alloc) {
    u8 *mem;

    assert(string.size > 0);

    mem = (u8*)un_memory_alloc(string.size + 1, alloc);
    un_memory_copy(mem, string.data, (u64)string.size);

    return mem;
}

String un_string_copy(String source, Allocator alloc) {
    u8 *mem;
    String result;

    mem = (u8*)un_memory_alloc(source.size, alloc);
    un_memory_copy(mem, source.data, (u64)source.size);

    result.size = source.size;
    result.data = mem;

    return result;
}

s32 un_string_compare(String left, String right) {
    s32 result;

    if (left.size == right.size && left.size == 0) return 0;
    if (left.size == 0) return -1;
    if (right.size == 0) return 1;

    result = un_memory_compare(left.data, right.data, left.size);

    if (result == 0) {
        if (left.size > right.size) return 1;
        if (left.size < right.size) return -1;
        return 0;
    } else {
        return result;
    }
}

String un_string_concat(String left, String right, Allocator alloc) {
    String result = { 0 };

    if (left.size == right.size && left.size == 0) {
        return result;
    }

    u8* data = (u8*)un_memory_alloc(left.size + right.size, alloc);

    if (left.size > 0) {
        assert(left.data != NULL);
        un_memory_copy(data, left.data, left.size);
    }

    if (right.size > 0) {
        assert(right.data != NULL);
        un_memory_copy((data + left.size), right.data, right.size);
    }

    result.size = left.size + right.size;
    result.data = data;

    return result;
}

String un_string_swap(String input, u8 from, u8 to, Allocator alloc) {
    u64 i;
    String output;

    output = un_string_copy(input, alloc);

    for (i = 0; i < input.size; i++) {
        if (output.data[i] != from)
            continue;

        output.data[i] = to;
    }

    return output;
}

List un_string_split(String input, String pattern, Allocator alloc) {
    List splits;
    String string;
    u64 i, start;
    u64 matches, size;
    u8 *buffer;

    UN_CLEAR(splits);

    if (input.size <= pattern.size) {
        return splits;
    }

    if (pattern.size == 0) {
        return splits;
    }

    for (i = 0, matches = 1; i < (input.size - (pattern.size - 1)); i++) {
        if (un_memory_compare(input.data + i, pattern.data, pattern.size) != 0) {
            continue;
        }

        matches++;
    }

    splits = un_list_create(matches, sizeof(String), alloc);

    start = 0;

    for (i = 0; i < (input.size - (pattern.size - 1)); i++) {
        if (un_memory_compare(input.data + i, pattern.data, pattern.size) != 0) {
            continue;
        }

        size = i - start; 

        if (size == 0) {
            start = i + pattern.size;
            continue;
        }

        buffer = (u8*)un_memory_alloc(size, alloc);
        un_memory_copy(buffer, input.data + start, size);
        string = CLITERAL(String) { .size = size, .data = buffer };
        un_list_append(&splits, &string);
        start = i + pattern.size;
    }

    if (start != input.size - pattern.size) {
        if (un_memory_compare(input.data + start, pattern.data, pattern.size) == 0) {
            return splits;
        }

        size = input.size - start; 
        if (size == 0) {
            return splits;
        }

        buffer = (u8*)un_memory_alloc(size, alloc);
        un_memory_copy(buffer, input.data + start, size);
        string = CLITERAL(String) { .size = size, .data = buffer };
        un_list_append(&splits, &string);
    }

    return splits;
}

String un_string_join(List string_list, String separator, Allocator alloc) {
    String cont, temp;
    u64 i;
    Allocator talloc;

    UN_CLEAR(cont);

    talloc = un_allocator_get_temporary();

    assert(string_list.element_size == sizeof(String));


    for (i = 0; i < string_list.count; i++) {
        temp = *(String *)un_list_get(&string_list, i);

        cont = un_string_concat(cont, temp, talloc);

        if (i != (string_list.count - 1)) {
            cont = un_string_concat(cont, separator, talloc);
        }
    }

    return un_string_copy(cont, alloc);
}

String un_string_substring(String input, u64 start, u64 max_size) {
    String slice;

    assert(max_size > 0);

    slice.size = UN_MIN(max_size, (input.size - (u64)max_size));
    slice.data = input.data + start;


    if (start > (input.size - max_size)) {
        slice.size = UN_MIN(max_size, (input.size - (u64)max_size));
    } else {
        slice.size = max_size;
    }

    slice.data = input.data + start;
    return slice;
}

s64 un_string_index_of(String input, u8 value, u64 skip_count) {
    u64 i;
    
    for (i = 0; i < input.size; i++) {
        if (input.data[i] != value) {
            continue;
        }

        if (skip_count) {
            skip_count--;
        } else {
            return i;
        }
    }

    return -1;
}
s64 un_string_index_of_last(String input, u8 value) {
    s64 i, index;

    index = -1;

    for (i = 0; i < (s64)input.size; i++) {
        if (input.data[i] == value) index = i;
    }

    return index;
}

static String format_u64(u64 value) {
    String output;
    u64 l, r, size;
    u8 t;
    u8 buffer[32];

    size = 0;

    if (value == 0) {
        return UN_STR("0");
    }
 
    while (value) {
        buffer[size++] = '0' + (value % 10);
        value /= 10;
    }

    assert(size <= 20);

    for (l = 0, r = size - 1; l < r; l++, r--) {
        t         = buffer[l];
        buffer[l] = buffer[r];
        buffer[r] = t;
    }

    output.size = size;
    output.data = buffer;
    return un_string_copy(output, un_allocator_get_temporary());
}

static String format_s64(s64 value) {
    String output;
    u64 l, r, size;
    b32 negative;
    u8 t;
    u8 buffer[32];

    size = 0;

    if (value == 0) {
        return UN_STR("0");
    }
 
    negative = false;

    if (value < 0) {
        value    = -value;
        negative = true;
    }

    while (value) {
        buffer[size++] = '0' + (value % 10);
        value /= 10;
    }

    if (negative) {
        buffer[size++] = '-';
    }

    assert(size <= 20);

    for (l = 0, r = size - 1; l < r; l++, r--) {
        t         = buffer[l];
        buffer[l] = buffer[r];
        buffer[r] = t;
    }

    output.size = size;
    output.data = buffer;
    return un_string_copy(output, un_allocator_get_temporary());
}

String un_string_format(Allocator alloc, String buffer, ...) {
    va_list args;
    String result;
    va_start(args, buffer);
    result = un_string_vformat(alloc, buffer, args);
    va_end(args);
    return result;
}

String un_string_vformat(Allocator alloc, String buffer, va_list args) {
    List output;
    String s;
    u64 i, j;
    u32 b;

    Allocator talloc = un_allocator_get_temporary();
    // @todo @bug: this would fail on realloc, because we need to realloc
    output = un_list_create(UN_KB(1), sizeof(u8), talloc);

    for (i = 0; i < buffer.size; i++) {
        if (buffer.data[i] != '%') {
            un_list_append(&output, buffer.data + i);
            continue;
        }

        switch (buffer.data[++i]) {
            case '%':
            {
                un_list_append(&output, buffer.data + i);
            } break;

            case 'c':
            {
                b = va_arg(args, u32);
                un_list_append(&output, &b);
            } break;

            case 'u':
            {
                s = format_u64(va_arg(args, u64));
                for (j = 0; j < s.size; j++) {
                    un_list_append(&output, s.data + j);
                }
            } break;

            case 'd': 
            {
                s = format_s64(va_arg(args, u64));
                for (j = 0; j < s.size; j++) {
                    un_list_append(&output, s.data + j);
                }
            } break;

            case 's':
            {
                s = va_arg(args, String);
                for (j = 0; j < s.size; j++) {
                    un_list_append(&output, s.data + j);
                }
            } break;
            default: break;
        }
    }

    s.size = (s64)output.count;
    s.data = output.data;

    return un_string_copy(s, alloc);
}