From ccda74a701c778618ca12a872ee2333c72023adf Mon Sep 17 00:00:00 2001 From: rhuibertsjr Date: Sat, 13 Apr 2024 14:15:15 +0200 Subject: [PATCH] Added: Ascii string type and helpers --- src/hash-store.h | 21 +++++++++++++++++++++ src/string.c | 32 ++++++++++++++++++++++++++++++++ src/string.h | 17 +++++++++++++++++ 3 files changed, 70 insertions(+) create mode 100644 src/hash-store.h create mode 100644 src/string.c create mode 100644 src/string.h diff --git a/src/hash-store.h b/src/hash-store.h new file mode 100644 index 0000000..a3064d2 --- /dev/null +++ b/src/hash-store.h @@ -0,0 +1,21 @@ +#ifndef HASH_TABLE_H +#define HASH_TABLE_H + +typedef u32 Key; + +typedef struct Table Table; +struct Table +{ + Arena *ptr_arena; + Arena *str8_arena; + + String8 *str8_ptr; + u32 str8_count; +}; + +internal Table hash_store_initialize(void); + +internal String8 hash_store_string8_from_key(Table *table, Key index); +internal Key hash_store_string8_to_key(Table *table, String8 value); + +#endif diff --git a/src/string.c b/src/string.c new file mode 100644 index 0000000..3402fb9 --- /dev/null +++ b/src/string.c @@ -0,0 +1,32 @@ +internal String8 +str8_init(u8 *cstring, u64 length) +{ + String8 result = { cstring, length }; + return result; +} + +internal u8 +string8_match(String8 a, String8 b) +{ + u8 result = 0; + + if (a.length == b.length) + { + u64 size = MIN(a.length, b.length); + + result = 1; + for (u64 i = 0; i < size; i++ ) + { + u8 at = a.content[i]; + i8 bt = b.content[i]; + + if (at != bt) + { + result = 0; + break; + } + } + } + + return result; +} diff --git a/src/string.h b/src/string.h new file mode 100644 index 0000000..2435158 --- /dev/null +++ b/src/string.h @@ -0,0 +1,17 @@ +#ifndef STRING_H +#define STRING_H + +typedef struct String8 String8; +struct String8 +{ + u8 *content; + u64 length; +}; + +internal String8 str8_init(u8 *cstring, u64 length); +#define str8_lit(cstring) \ + str8_init((u8*)(cstring), sizeof(cstring) - 1) + +internal u8 string8_match(String8 a, String8 b); + +#endif // STRING_H