diff options
Diffstat (limited to 'source/base/base_string.h')
| -rw-r--r-- | source/base/base_string.h | 45 |
1 files changed, 38 insertions, 7 deletions
diff --git a/source/base/base_string.h b/source/base/base_string.h index 5f875e0..941dc53 100644 --- a/source/base/base_string.h +++ b/source/base/base_string.h | |||
| @@ -1,15 +1,46 @@ | |||
| 1 | #ifndef BASE_STRING_H | 1 | #ifndef BASE_STRING_H |
| 2 | #define BASE_STRING_H | 2 | #define BASE_STRING_H |
| 3 | 3 | ||
| 4 | #define _StringCast (string) | 4 | #include <string.h> |
| 5 | #define StringCast { .data (u8 *)(string), .size = (sizeof((string)) - 1)} | ||
| 6 | #define PushString (Arena, Size) StringCast{.data = PushArray((arena), PushStruct(u8), (Size)), .size = (u64)(Size)} | ||
| 7 | 5 | ||
| 8 | typedef struct string string; | 6 | #define StringLit(string) \ |
| 9 | struct 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 | typedef struct string8 string8; | ||
| 13 | struct string8 | ||
| 10 | { | 14 | { |
| 11 | u8 *data; | 15 | u8 *data; |
| 12 | u64 size; | 16 | u64 size; |
| 13 | }; | 17 | }; |
| 14 | 18 | ||
| 19 | internal b8 | ||
| 20 | string8_cmp(string8 a, string8 b) | ||
| 21 | { | ||
| 22 | if (a.size != b.size) return 0; | ||
| 23 | return (b8)(memcmp(a.data, b.data, a.size) == 0); | ||
| 24 | } | ||
| 25 | |||
| 26 | internal u64 | ||
| 27 | string8_to_u64(u8 *buf, umm len) | ||
| 28 | { | ||
| 29 | u64 value = 0; | ||
| 30 | for (umm i = 0; i < len; ++i) | ||
| 31 | { | ||
| 32 | u8 c = buf[i]; | ||
| 33 | if (c < '0' || c > '9') break; | ||
| 34 | value = value * 10 + (c - '0'); | ||
| 35 | } | ||
| 36 | return value; | ||
| 37 | } | ||
| 38 | |||
| 39 | internal void | ||
| 40 | string8_append_char(string8 *buf, u8 c) | ||
| 41 | { | ||
| 42 | buf->data[buf->size] = c; | ||
| 43 | buf->size += 1; | ||
| 44 | } | ||
| 45 | |||
| 15 | #endif /* BASE_STRING_H */ | 46 | #endif /* BASE_STRING_H */ |
