From 023c74d8d997eccdb7dce813a84110361d126bca Mon Sep 17 00:00:00 2001 From: Abdelrahman Date: Sun, 9 Feb 2025 18:46:32 +0000 Subject: [PATCH] Add functions to dynamically allocate strings --- src/core/strings/str8/str8.c | 82 ++++++++++++++++++++++++++++++++++++ src/core/strings/str8/str8.h | 10 ++++- 2 files changed, 90 insertions(+), 2 deletions(-) diff --git a/src/core/strings/str8/str8.c b/src/core/strings/str8/str8.c index 8340228..d19e31b 100644 --- a/src/core/strings/str8/str8.c +++ b/src/core/strings/str8/str8.c @@ -1,7 +1,73 @@ #include "str8.h" #include "aliases.h" +#include "mem_allocator.h" #include +Str8 *wapp_str8_buf_alloc(const Allocator *allocator, u64 capacity) { + Str8 *str = NULL; + + if (!allocator) { + goto RETURN_STR8; + } + + str = wapp_mem_allocator_alloc(allocator, sizeof(Str8)); + if (!str) { + goto RETURN_STR8; + } + + c8 *buf = wapp_mem_allocator_alloc(allocator, sizeof(c8) * capacity); + if (!buf) { + wapp_mem_allocator_free(allocator, (void **)&str); + goto RETURN_STR8; + } + + str->buf = buf; + str->size = 0; + str->capacity = capacity; + +RETURN_STR8: + return str; +} + +Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str) { + Str8 *output = NULL; + + if (!allocator || !str) { + goto RETURN_ALLOC_CSTR; + } + + u64 length = strlen(str); + output = wapp_str8_buf_alloc(allocator, length * 2); + if (!output) { + goto RETURN_ALLOC_CSTR; + } + + output->size = length; + memcpy(output->buf, str, length); + +RETURN_ALLOC_CSTR: + return output; +} + +Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str) { + Str8 *output = NULL; + + if (!allocator || !str) { + goto RETURN_ALLOC_STR8; + } + + output = wapp_str8_buf_alloc(allocator, str->capacity); + if (!output) { + goto RETURN_ALLOC_STR8; + } + + output->size = str->size; + memcpy(output->buf, str->buf, str->size); + +RETURN_ALLOC_STR8: + return output; +} + c8 wapp_str8_get(const Str8 *str, u64 index) { if (index >= str->size) { return '\0'; @@ -18,6 +84,22 @@ void wapp_str8_set(Str8 *str, u64 index, c8 c) { str->buf[index] = c; } +Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end) { + if (start >= str->size || start >= end) { + return (Str8RO){0}; + } + + if (end > str->size) { + end = str->size; + } + + return (Str8RO){ + .capacity = end - start, + .size = end - start, + .buf = str->buf + start, + }; +} + i64 wapp_str8_find(Str8RO *str, Str8RO substr) { if (substr.size > str->size) { return -1; diff --git a/src/core/strings/str8/str8.h b/src/core/strings/str8/str8.h index 7e6f98f..cb3397b 100644 --- a/src/core/strings/str8/str8.h +++ b/src/core/strings/str8/str8.h @@ -2,6 +2,7 @@ #define STR8_H #include "aliases.h" +#include "mem_allocator.h" #include #ifdef __cplusplus @@ -32,8 +33,13 @@ typedef const Str8 Str8RO; .size = sizeof(STRING) - 1, \ .buf = (c8 *)STRING}) -c8 wapp_str8_get(Str8RO *str, u64 index); -void wapp_str8_set(Str8 *str, u64 index, c8 c); +Str8 *wapp_str8_buf_alloc(const Allocator *allocator, u64 capacity); +Str8 *wapp_str8_alloc_cstr(const Allocator *allocator, const char *str); +Str8 *wapp_str8_alloc_str8(const Allocator *allocator, Str8RO *str); + +c8 wapp_str8_get(Str8RO *str, u64 index); +void wapp_str8_set(Str8 *str, u64 index, c8 c); +Str8RO wapp_str8_substr(Str8RO *str, u64 start, u64 end); i64 wapp_str8_find(Str8RO *str, Str8RO substr); i64 wapp_str8_rfind(Str8RO *str, Str8RO substr);