aboutsummaryrefslogtreecommitdiffhomepage
path: root/common.c
blob: badb1edc4ce38211d46d14ef709aa1e078e43772 (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
void fatal_error(const char* msg, ...) {
    va_list args;
    va_start(args, msg);
    vfprintf(stderr, msg, args);
    va_end(args);
    fprintf(stderr, "\n");
    exit(1);
}

#define unreachable() fatal_error("%s:%d: unreachable", __FILE__, __LINE__)

#define unimplemented() fatal_error("%s:%d: unimplemented", __FILE__, __LINE__)

struct String {
    char* data;
    size_t len;
};
typedef struct String String;

char* string_to_cstr(const String* s) {
    char* buf = calloc(s->len + 1, sizeof(char));
    memcpy(buf, s->data, s->len);
    return buf;
}

BOOL string_equals(const String* s1, const String* s2) {
    return s1->len == s2->len && strncmp(s1->data, s2->data, s1->len) == 0;
}

BOOL string_equals_cstr(const String* s1, const char* s2) {
    size_t s2_len = strlen(s2);
    return s1->len == s2_len && strncmp(s1->data, s2, s1->len) == 0;
}