diff options
| author | nasr <nsrddyn@gmail.com> | 2026-03-26 22:35:30 +0100 |
|---|---|---|
| committer | nasr <nsrddyn@gmail.com> | 2026-04-13 17:24:42 +0200 |
| commit | dd5586abec207dd4acd16d51ce0d392c03e5e957 (patch) | |
| tree | e56573f49ebb2a3236a39148842dc80bde5a286d /source/base/base_string.h | |
feature(main): initmain
feature(main): init
Diffstat (limited to 'source/base/base_string.h')
| -rw-r--r-- | source/base/base_string.h | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/source/base/base_string.h b/source/base/base_string.h new file mode 100644 index 0000000..189b38a --- /dev/null +++ b/source/base/base_string.h | |||
| @@ -0,0 +1,59 @@ | |||
| 1 | #ifndef BASE_STRING_H | ||
| 2 | #define BASE_STRING_H | ||
| 3 | |||
| 4 | #include <string.h> | ||
| 5 | |||
| 6 | #define StringLit(string) \ | ||
| 7 | (string8){ .data = (u8 *)(string), .size = (sizeof(string) - 1) } | ||
| 8 | |||
| 9 | #define PushString(arena, size) \ | ||
| 10 | (string8){ (u8 *)PushArray((arena), u8, (size)), (u64)(size) } | ||
| 11 | |||
| 12 | #define StringFmt "%.*s" | ||
| 13 | #define ULongFmt "%lu" | ||
| 14 | #define ULLongFmt "%llu" | ||
| 15 | |||
| 16 | typedef struct string8 string8; | ||
| 17 | struct string8 | ||
| 18 | { | ||
| 19 | u8 *data; | ||
| 20 | u64 size; | ||
| 21 | }; | ||
| 22 | |||
| 23 | internal b8 | ||
| 24 | string8_cmp(string8 a, string8 b) | ||
| 25 | { | ||
| 26 | if (a.size != b.size) return 0; | ||
| 27 | return (b8)(memcmp(a.data, b.data, a.size) == 0); | ||
| 28 | } | ||
| 29 | |||
| 30 | internal u64 | ||
| 31 | string8_to_u64(u8 *buf, umm len) | ||
| 32 | { | ||
| 33 | u64 value = 0; | ||
| 34 | for (umm i = 0; i < len; ++i) | ||
| 35 | { | ||
| 36 | u8 c = buf[i]; | ||
| 37 | if (c < '0' || c > '9') break; | ||
| 38 | value = value * 10 + (c - '0'); | ||
| 39 | } | ||
| 40 | return value; | ||
| 41 | } | ||
| 42 | |||
| 43 | internal void | ||
| 44 | string8_append_char(string8 *buf, u8 c) | ||
| 45 | { | ||
| 46 | buf->data[buf->size] = c; | ||
| 47 | buf->size += 1; | ||
| 48 | } | ||
| 49 | |||
| 50 | read_only global_variable | ||
| 51 | string8 nil_string = | ||
| 52 | { | ||
| 53 | |||
| 54 | .data = NULL, | ||
| 55 | .size = 0, | ||
| 56 | |||
| 57 | }; | ||
| 58 | |||
| 59 | #endif /* BASE_STRING_H */ | ||
