diff options
author | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-25 17:36:36 +1100 |
---|---|---|
committer | Omniscient <17525998+omnisci3nce@users.noreply.github.com> | 2024-02-25 17:36:36 +1100 |
commit | 06ed6edcedf30fd8f7e036fffe8f81e8ca89d4b0 (patch) | |
tree | f5ae75e431817a53ee5725b4c0d6f3e61b15a664 /src/std/str.c | |
parent | 109b6dd1881d90915e972f0d263a032bd262adb5 (diff) | |
parent | b5b7cea24c46d28a4b72bc18ca8ccb2d532007d3 (diff) |
Merge branch 'master' of github.com:omnisci3nce/celeritas-core
Diffstat (limited to 'src/std/str.c')
-rw-r--r-- | src/std/str.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/src/std/str.c b/src/std/str.c index 27f8f68..3b2770e 100644 --- a/src/std/str.c +++ b/src/std/str.c @@ -1,4 +1,9 @@ #include "str.h" +#include <string.h> + +#include "mem.h" + +str8 str8_create(u8* buf, size_t len) { return (str8){ .buf = buf, .len = len }; } bool str8_equals(str8 a, str8 b) { if (a.len != b.len) { @@ -11,4 +16,29 @@ bool str8_equals(str8 a, str8 b) { } } 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; +} + +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); }
\ No newline at end of file |