diff options
| author | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-23 19:39:13 +0200 |
|---|---|---|
| committer | Martin Kagamino Lehoux <martin@lehoux.net> | 2025-07-23 19:39:26 +0200 |
| commit | 421300c27123ca647772dc6d9bd0cabf7f5f9c99 (patch) | |
| tree | d916b62056b2440aef374b21373d16272bb709ee /src/str.c | |
| parent | 53e9e371a16ae2d167f1fc2577dff2c2426eed68 (diff) | |
refactor: src & pragma once
Diffstat (limited to 'src/str.c')
| -rw-r--r-- | src/str.c | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/src/str.c b/src/str.c new file mode 100644 index 0000000..eccacd8 --- /dev/null +++ b/src/str.c @@ -0,0 +1,32 @@ +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include "../include/memory.h" +#include "../include/str.h" + +// Returns 1 if strings are equal, 0 otherwise +i32 Str_equals(Str a, Str b) { + return (a.len == b.len) && (!a.len || !memcmp(a.data, b.data, a.len)); +} + +u64 Str_hash64(Str s) { + u64 h = 0x100; + for (size i = 0; i < s.len; i++) { + h ^= s.data[i] & 255; + h *= 1111111111111111111; + } + return h; +} + +Str Str_copy(Arena *a, byte *data, size len) { + Str str = {0}; + str.len = len; + str.data = new(a, byte, len); + if (str.len) memcpy(str.data, data, len); + return str; +} + +i32 Str_fprint(FILE *w, Str s) { + return fprintf(w, "%.*s", (i32)s.len, s.data); +} |
