summaryrefslogtreecommitdiff
path: root/src/std/str.c
blob: 89c76a08dd2c2a69b3b465a372cbfe94aa79f019 (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
#include "str.h"
#include <assert.h>
#include <string.h>
#include "log.h"
#include "mem.h"

Str8 Str8_create(u8* buf, size_t len) { return (Str8){ .buf = buf, .len = len }; }

Str8 Str8_cstr_view(char* string) { return Str8_create((u8*)string, strlen(string)); }

bool Str8_equals(Str8 a, Str8 b) {
  if (a.len != b.len) {
    return false;
  }

  for (size_t i = 0; i < a.len; i++) {
    if (a.buf[i] != b.buf[i]) {
      return false;
    }
  }
  return true;
}

char* Str8_to_cstr(arena* a, Str8 s) {
  bool is_null_terminated = s.buf[s.len - 1] == 0;
  size_t n_bytes = is_null_terminated ? s.len : s.len + 1;

  u8* dest = arena_alloc(a, n_bytes);

  memcpy(dest, s.buf, s.len);
  if (is_null_terminated) {
    dest[s.len] = '\0';
  }
  return (char*)dest;
}

char* Clone_cstr(arena* a, const char* s) {
  if (s == NULL) {
    WARN("Tried to clone a NULL char*");
    return NULL;
  }
  Str8 st = Str8_cstr_view(s);
  return Str8_to_cstr(a, st);
}

Str8 Str8_concat(arena* a, Str8 left, Str8 right) {
  size_t n_bytes = left.len + right.len + 1;

  u8* dest = arena_alloc(a, n_bytes);
  memcpy(dest, left.buf, left.len);
  memcpy(dest + right.len, right.buf, right.len);

  dest[n_bytes - 1] = '\0';

  return Str8_create(dest, n_bytes);
}

Str8 Str8_substr(Str8 s, u64 min, u64 max) {
  assert(min >= 0);
  assert(min < s.len);
  assert(max >= 0);
  assert(max <= s.len);
  uint8_t* start = s.buf + (ptrdiff_t)min;
  size_t new_len = max - min;
  return (Str8){ .buf = start, .len = new_len };
}

Str8 Str8_take(Str8 s, u64 first_n) { return Str8_substr(s, 0, first_n); }

Str8 Str8_drop(Str8 s, u64 last_n) { return Str8_substr(s, s.len - last_n, s.len); }

Str8 Str8_skip(Str8 s, u64 n) { return Str8_substr(s, n, s.len); }

Str8 Str8_chop(Str8 s, u64 n) { return Str8_substr(s, 0, s.len - n); }