summaryrefslogtreecommitdiff
path: root/src/string.c
blob: 9328911ab7718b178d33fdc9b1f5f162fedbde5f (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
#include "common.h"
#include "smrt_arena.h"
#include <string.h>

#include "string.h"

strng_t *strng_new(smrt_arena_t *arena, u64 size) {
    strng_t *s = smrt_arena_push(arena, sizeof(strng_t) + size, true);

    if (!s) {
        return NULL;
    }

    s->alloc_size = size;
    return s;
}

strng_t *strng_from(smrt_arena_t *arena, char *c) {
    u64 size = strlen(c);

    strng_t *s = strng_new(arena, size);

    if (!s) {
        return NULL;
    }

    memcpy((u8*)s+STRNG_BASE_POS, c, size);
    s->len = size;

    return s;
}

b32 strng_set(strng_t *string, char *c) {
    u64 size = strlen(c);

    if (string->alloc_size < size) {
        return false;
    }

    memcpy((u8*)string+STRNG_BASE_POS, c, size);
    string->len = size;

    return true;
}

void strng_clear(strng_t *string) {
    string->len = 0;
}