diff options
Diffstat (limited to 'source/kit/string_builder.h')
-rw-r--r-- | source/kit/string_builder.h | 55 |
1 files changed, 53 insertions, 2 deletions
diff --git a/source/kit/string_builder.h b/source/kit/string_builder.h index 322f6e2..5cc12ee 100644 --- a/source/kit/string_builder.h +++ b/source/kit/string_builder.h @@ -23,7 +23,11 @@ typedef KIT_DA(char) kit_str_builder_t; static kit_str_builder_t kit_str_build(kit_str_t s, kit_allocator_t *alloc) { + assert(s.size >= 0 && (s.size == 0 || s.values != NULL)); kit_str_builder_t builder; + memset(&builder, 0, sizeof builder); + if (s.size < 0 || (s.size != 0 && s.values == NULL)) + return builder; KIT_DA_INIT(builder, s.size, alloc); assert(builder.size == s.size); if (builder.size == s.size) @@ -31,8 +35,19 @@ static kit_str_builder_t kit_str_build(kit_str_t s, return builder; } -static kit_status_t kit_str_append(kit_str_builder_t *a, - kit_str_t b) { +static kit_str_builder_t kit_substr_build(kit_str_t s, i64 index, + i64 size, + kit_allocator_t *alloc) { + assert(index + size <= s.size); + if (index + size > s.size) { + kit_str_builder_t builder; + memset(&builder, 0, sizeof builder); + return builder; + } + return kit_str_build(kit_str(size, s.values + index), alloc); +} + +static s32 kit_str_append(kit_str_builder_t *a, kit_str_t b) { assert(a != NULL); if (a == NULL) return KIT_ERROR_INVALID_ARGUMENT; @@ -46,6 +61,38 @@ static kit_status_t kit_str_append(kit_str_builder_t *a, return KIT_OK; } +static s32 kit_str_insert(kit_str_builder_t *a, i64 index, + kit_str_t b) { + assert(a != NULL && index >= 0 && index <= a->size); + if (a == NULL || index < 0 || index > a->size) + return KIT_ERROR_INVALID_ARGUMENT; + if (b.size <= 0) + return KIT_OK; + i64 n = a->size; + KIT_DA_RESIZE(*a, n + b.size); + if (a->size != n + b.size) + return KIT_ERROR_BAD_ALLOC; + if (index < n) + memmove(a->values + (index + b.size), a->values + index, + n - index); + memcpy(a->values + index, b.values, b.size); + return KIT_OK; +} + +static s32 kit_str_erase(kit_str_builder_t *a, i64 index, i64 size) { + assert(a != NULL && index >= 0 && size >= 0 && + index + size <= a->size); + if (a == NULL || index < 0 || size < 0 || index + size > a->size) + return KIT_ERROR_INVALID_ARGUMENT; + if (size <= 0) + return KIT_OK; + if (index + size < a->size) + memmove(a->values + index, a->values + (index + size), + a->size - index - size); + KIT_DA_RESIZE(*a, a->size - size); + return KIT_OK; +} + #ifdef __GNUC__ # pragma GCC pop_options # pragma GCC diagnostic pop @@ -57,7 +104,11 @@ static kit_status_t kit_str_append(kit_str_builder_t *a, #ifndef KIT_DISABLE_SHORT_NAMES # define str_builder_t kit_str_builder_t +# define str_build kit_str_build +# define substr_build kit_substr_build # define str_append kit_str_append +# define str_insert kit_str_insert +# define str_erase kit_str_erase #endif #endif |