summaryrefslogtreecommitdiff
path: root/typeutils.h
blob: fd285b23a4002695cff866c4d74f1e88797d8c7f (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
#pragma once

#include <ctype.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>


#define DECLARE_VEC(T, Name)                                        \
    typedef struct {                                                \
        T* items;                                                   \
        size_t count;                                               \
        size_t cap;                                                 \
        size_t incr_step;                                           \
    } Name;                                                         \
    static inline Name make_##Name(size_t step) {                   \
        return (Name) {                                             \
            .items = malloc(step* sizeof(T)),                       \
            .count = 0,                                             \
            .cap = step,                                            \
            .incr_step = step,                                      \
        };                                                          \
    }                                                               \
    static inline void push_to_##Name(Name* vec, T item) {          \
        if (vec->count == vec->cap) {                               \
            vec->cap += vec->incr_step;                             \
            vec->items = realloc(                                   \
                vec->items,                                         \
                vec->cap * sizeof(T)                                \
            );                                                      \
        }                                                           \
        vec->items[vec->count++] = item;                            \
    }                                                               


DECLARE_VEC(char, String);

static void append_str_to_String(String* target, char* src_str) {
    size_t required_size = 
        ((target->count + strlen(src_str)) / target->incr_step + 1) * target->incr_step; 
    if (required_size > target->cap) {
        target->cap = required_size;
        target->items = realloc(target->items, required_size * sizeof(char));
    }
    memcpy(target->items + target->count, src_str, strlen(src_str));
    target->count += strlen(src_str);
}

static inline char* inner(String str) {
    if (str.count == str.cap) {
        str.items = realloc(str.items, (str.cap+1)*sizeof(char));
    }
    str.items[str.count] = '\0';
    return str.items;
}

#define DECLARE_MAYBE(T)                                            \
    typedef struct {                                                \
        bool some;                                                  \
        T value;                                                    \
    } Maybe##T;