diff options
author | talha <talha@talhaamir.xyz> | 2024-02-15 09:58:42 +0500 |
---|---|---|
committer | talha <talha@talhaamir.xyz> | 2024-02-15 09:58:42 +0500 |
commit | ab3edaa58eed4ff73410954ca094531d49eb5844 (patch) | |
tree | 4dd8dc896baa5737153ed0d48f27881171fd3287 /strings |
added personal libraries to git tracking
Diffstat (limited to 'strings')
-rw-r--r-- | strings/strings.cpp | 31 | ||||
-rw-r--r-- | strings/strings.h | 42 | ||||
-rw-r--r-- | strings/test.cpp | 65 |
3 files changed, 138 insertions, 0 deletions
diff --git a/strings/strings.cpp b/strings/strings.cpp new file mode 100644 index 0000000..4c8486f --- /dev/null +++ b/strings/strings.cpp @@ -0,0 +1,31 @@ +u64 CstrSize(const char* cstr) +{ + u8 *iter = (u8*)cstr; + u64 size = 0; + for(;*iter != 0; *iter++) { + size += 1; + } + + return size; +} + +struct str8 Str8(u8 *string, u64 size) +{ + struct str8 result = {string, size}; + return result; +} + +struct res_str8 Str8InitCstr(struct arena *a, const char* cstr) +{ + u64 cstr_size = CstrSize(cstr); + struct res_void cstr_res = arena_alloc(a, cstr_size); + u8 *str = (u8 *)cstr_res.memory; + u64 str_size = cstr_res.bytes_count; + + MemCopy((void*)str, (void*)cstr, str_size*sizeof(u8)); + + struct res_str8 result = {}; + result.string = Str8(str, str_size); + result.bytes_count = str_size; + return result; +} diff --git a/strings/strings.h b/strings/strings.h new file mode 100644 index 0000000..dbaff7a --- /dev/null +++ b/strings/strings.h @@ -0,0 +1,42 @@ +#ifndef AMR_STR +#define AMR_STR + +#include <assert.h> +#include <string.h> + +#define MemCopy memcpy + +// @todo: the function names have been changed a bit for the interest +// of speed. Some structs and functions share the same names, so +// I need to fix this but also make sure the functions have good names + +enum StrStatus {STR_OK=0, STR_FULL}; + +struct str8 { + u8 *buffer; + u64 size; +}; + +struct str8_node { + str8_node *next; + str8 string; +}; + +struct str8_list { + str8_node *first; + str8_node *last; + u64 count; + u64 capacity; +}; + +struct res_str8 { + str8 string; + u64 bytes_count; + enum StrStatus status; +}; + +struct str8 Str8(u8 *string, u64 size); +u64 CstrSize(const char* cstr); +struct res_str8 Str8InitCstr(struct arena *a, const char* cstr, u64 size); + +#endif diff --git a/strings/test.cpp b/strings/test.cpp new file mode 100644 index 0000000..49f81c9 --- /dev/null +++ b/strings/test.cpp @@ -0,0 +1,65 @@ +#include <stdio.h> +#include "strings.h" +#include "strings.cpp" + +void TestCstrSize() +{ + u64 size = CstrSize("hello sailor\n"); + assert(size == 13 && "test_cstr_size() failed" + "the cstr size was wrong\n"); +} + +void TestEmptyCstrSize() +{ + u64 size = CstrSize(""); + assert(size == 0 && "test_empty_cstr_size() failed" + "the cstr size was wrong\n"); +} + +void TestStr8InitCstr(u8* buffer, u64 buffer_size) +{ + struct arena a = {}; + arena_init(&a, buffer, buffer_size); + struct res_str8 qres = Str8InitCstr(&a, "hello sailor"); + assert((qres.bytes_count == 12) && "Error TestStr8InitCstr:\n" + "Reason: Invalid size of allocated string\n" + "Hint: check what is happening to the bytes_count and how it is set first\n"); +} + +void TestStr8InitCstrEmpty(u8* buffer, u64 buffer_size) +{ + struct arena a = {}; + arena_init(&a, buffer, buffer_size); + struct res_str8 qres = Str8InitCstr(&a, ""); + assert((qres.string.size == 0) && "Error TestStr8InitCstrEmpty:\n" + "Reason: Invalid size of allocated string\n" + "Hint: check what is happening to the bytes_count and how it is set first\n"); +} + +void TestStr8InitCstrNoSpace(u8* buffer, u8 buffer_size) +{ + struct arena a = {}; + arena_init(&a, buffer, buffer_size); + struct res_str8 qres = Str8InitCstr(&a, "hello sailor, looks like you've got no space here on this boat!"); + assert(qres.string.size == 0 && "Error TestStr8InitStrNoSpace:\n" + "Reason: the bytes count is not 0, even though the string is larger than the supposedly available\n" + "arena space. This is an error and should not occur.\n" + "Hint: Look at what is going on in the allocation since that arena helpers handle allocations\n."); +} + +void test_strings() +{ + printf("\n======= Testing Strings Library =======\n"); + printf("===== Testing Cstring Functions =====\n"); + TestCstrSize(); + TestEmptyCstrSize(); + + u64 buffer_size = KB(64); + u8* buffer = (u8*)malloc(sizeof(u8)*buffer_size); + + TestStr8InitCstr(buffer, buffer_size); + TestStr8InitCstrEmpty(buffer, buffer_size); + TestStr8InitCstrNoSpace(buffer, 8); + printf("Cstring tests passed successfully\n"); + free(buffer); +} |