diff options
Diffstat (limited to 'String.c')
| -rw-r--r-- | String.c | 235 |
1 files changed, 106 insertions, 129 deletions
@@ -1,12 +1,10 @@ #include "String.h" -#include "GlobalArena.h" #include "Types.h" -#include <stdarg.h> #include <stdio.h> #include <string.h> struct StringMeta { - BumpArena b; + MemoryManager *mm; usize length; }; @@ -15,44 +13,48 @@ S_metadata(String s) { return (struct StringMeta*)(s - sizeof(struct StringMeta)); } -String -S_new(BumpArena b, const char* str) { - if (NULL == b || NULL == str) +static inline void* +S_start(MemoryManager *mm, usize length) { + if (NULL == mm) return NULL; struct StringMeta sm = { - .b = b, - .length = strlen(str), + .mm = mm, + .length = length, }; - void* start = B_alloc(b, sizeof(sm) + sm.length + 1); - if (NULL == start) + void* start = NULL; + if (R_Ok != mm->alloc(*mm, &start, sizeof(sm) + sm.length + 1) || + NULL == start) + { return NULL; + } memcpy(start, &sm, sizeof(sm)); - start += sizeof(sm); - memcpy(start, str, sm.length + 1); - - return start; + return start + sizeof(sm); } String -S_newlen(BumpArena b, usize length) { - if (NULL == b) +S_new(MemoryManager *mm, const char* str) { + if (NULL == str) return NULL; - struct StringMeta sm = { - .b = b, - .length = length, - }; + usize strlength = strlen(str); - void* start = B_alloc(b, sizeof(sm) + sm.length + 1); - if (NULL == start) - return NULL; + void* start = S_start(mm, strlength); - memcpy(start, &sm, sizeof(sm)); - start += sizeof(sm); - memset(start, '\0', sm.length + 1); + if (NULL != start) + memcpy(start, str, strlength + 1); + + return start; +} + +String +S_newlen(MemoryManager *mm, usize length) { + void* start = S_start(mm, length); + + if (NULL != start) + memset(start, '\0', length + 1); return start; } @@ -63,84 +65,28 @@ S_allocated(String s) { } String -S_dup(BumpArena b, String s) { - if (NULL == b || NULL == s) +S_dup(MemoryManager *mm, String s) { + if (NULL == mm || NULL == s) return NULL; usize allocated = S_allocated(s); - void* start = B_alloc(b, allocated); - if (NULL == start) + void* start = NULL; + if (R_Ok != mm->alloc(*mm, &start, allocated) || + NULL == start) + { return NULL; + } memcpy(start, s - sizeof(struct StringMeta), allocated); - return start + sizeof(struct StringMeta); } -usize -S_length(String s) { - if (NULL == s) - return 0; - return S_metadata(s)->length; -} - -Result -S_append(String s, const char* str) { - if (NULL == s || NULL == str) - return R_Uninitialized; - - struct StringMeta* sm = S_metadata(s); - - if (!B_resizeable(sm->b, s)) - return R_OutOfBounds; - - usize strlength = strlen(str); - RETURN_NOTOK(B_resize(sm->b, sizeof(*sm) + (sm->length + strlength) + 1)); - - strcat(s, str); - sm->length += strlength; - - return R_Ok; -} - -Result -S_prepend(String s, const char* str) { - if (NULL == s) - return R_Uninitialized; - - struct StringMeta* sm = S_metadata(s); - - if (!B_resizeable(sm->b, s)) - return R_OutOfBounds; - - usize strlength = strlen(str); - RETURN_NOTOK(B_resize(sm->b, sizeof(*sm) + (sm->length + strlength) + 1)); - - for (usize i = sm->length; i > 0; --i) - s[i - 1 + strlength] = s[i - 1]; - for (usize i = 0; i < strlength; ++i) - s[i] = str[i]; - - sm->length += strlength; - - return R_Ok; -} - -usize -S_find(String s, char c) { - if (NULL == s) - return -1; - - unsigned index = 0; - while (s[index] != '\0' && s[index] != c) - ++index; - - return index; -} +String +S_vconcat(MemoryManager *mm, va_list args) { + if (NULL == mm) + return NULL; -static String -S_vconcat(BumpArena b, va_list args) { usize length = 0; va_list temp; @@ -150,10 +96,10 @@ S_vconcat(BumpArena b, va_list args) { } va_end(temp); - String result = S_newlen(b, length); + String result = S_newlen(mm, length); if (NULL == result) - return result; + return NULL; if (0 < length) { for (const char* str = va_arg(args, const char*); str != NULL; str = va_arg(args, const char*)) @@ -163,25 +109,28 @@ S_vconcat(BumpArena b, va_list args) { } String -S_concat(BumpArena b, ...) { +S_concat(MemoryManager *mm, ...) { va_list args; usize length = 0; - va_start(args, b); - String result = S_vconcat(b, args); + va_start(args, mm); + String result = S_vconcat(mm, args); va_end(args); return result; } -static String -S_vprintf(BumpArena b, const char* fmt, va_list args) { +String +S_vprintf(MemoryManager *mm, const char* fmt, va_list args) { + if (NULL == mm) + return NULL; + va_list temp; va_copy(temp, args); usize length = vsnprintf(NULL, 0, fmt, temp); va_end(temp); - String result = S_newlen(b, length); + String result = S_newlen(mm, length); if (NULL == result) return NULL; @@ -190,54 +139,82 @@ S_vprintf(BumpArena b, const char* fmt, va_list args) { } String -S_printf(BumpArena b, const char* fmt, ...) { +S_printf(MemoryManager *mm, const char* fmt, ...) { va_list args; va_start(args, fmt); - String result = S_vprintf(b, fmt, args); + String result = S_vprintf(mm, fmt, args); va_end(args); return result; } -String -SG_new(const char* str) { - return S_new(g_arena, str); +usize +S_length(String s) { + if (NULL == s) + return 0; + return S_metadata(s)->length; } -String -SG_newlen(usize length) { - return S_newlen(g_arena, length); +static inline Result +S_resize(String *s, usize length) { + if (NULL == s || NULL == *s) + return R_Uninitialized; + + struct StringMeta *sm = S_metadata(*s); + + void *start = sm; + RETURN_NOTOK( + sm->mm->realloc(*sm->mm, &start, S_allocated(*s) + length)); + *s = start + sizeof(*sm); + + return R_Ok; } -String -SG_dup(String s) { - return S_dup(g_arena, s); +Result +S_append(String *s, const char* str) { + if (NULL == str) + return R_Uninitialized; + + usize strlength = strlen(str); + + RETURN_NOTOK(S_resize(s, strlength)); + + strcat(*s, str); + S_metadata(*s)->length += strlength; + + return R_Ok; } -String -SG_concat(const char* str, ...) { - if (str == NULL) - return NULL; +Result +S_prepend(String *s, const char* str) { + if (NULL == s) + return R_Uninitialized; - va_list args; + usize strlength = strlen(str); - va_start(args, str); - String result = S_vconcat(g_arena, args); - va_end(args); + RETURN_NOTOK(S_resize(s, strlength)); - S_prepend(result, str); + struct StringMeta *sm = S_metadata(*s); - return result; + for (usize i = sm->length; i > 0; --i) + *s[i - 1 + strlength] = *s[i - 1]; + for (usize i = 0; i < strlength; ++i) + *s[i] = str[i]; + + sm->length += strlength; + + return R_Ok; } -String -SG_printf(const char* fmt, ...) { - va_list args; +usize +S_find(String s, char c) { + if (NULL == s) + return -1; - va_start(args, fmt); - String result = S_vprintf(g_arena, fmt, args); - va_end(args); + unsigned index = 0; + while (s[index] != '\0' && s[index] != c) + ++index; - return result; + return index; } |
