diff options
Diffstat (limited to 'source/kit/string_builder.h')
-rw-r--r-- | source/kit/string_builder.h | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/source/kit/string_builder.h b/source/kit/string_builder.h new file mode 100644 index 0000000..322f6e2 --- /dev/null +++ b/source/kit/string_builder.h @@ -0,0 +1,63 @@ +#ifndef KIT_STRING_BUILDER_H +#define KIT_STRING_BUILDER_H + +#include "status.h" +#include "string_ref.h" +#include "dynamic_array.h" + +#include <assert.h> + +#ifdef __cplusplus +extern "C" { +#endif + +typedef KIT_DA(char) kit_str_builder_t; + +#ifdef __GNUC__ +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunused-function" +# pragma GCC diagnostic ignored "-Wunknown-pragmas" +# pragma GCC push_options +# pragma GCC optimize("O3") +#endif + +static kit_str_builder_t kit_str_build(kit_str_t s, + kit_allocator_t *alloc) { + kit_str_builder_t builder; + KIT_DA_INIT(builder, s.size, alloc); + assert(builder.size == s.size); + if (builder.size == s.size) + memcpy(builder.values, s.values, s.size); + return builder; +} + +static kit_status_t kit_str_append(kit_str_builder_t *a, + kit_str_t b) { + assert(a != NULL); + if (a == NULL) + 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; + memcpy(a->values + n, b.values, b.size); + return KIT_OK; +} + +#ifdef __GNUC__ +# pragma GCC pop_options +# pragma GCC diagnostic pop +#endif + +#ifdef __cplusplus +} +#endif + +#ifndef KIT_DISABLE_SHORT_NAMES +# define str_builder_t kit_str_builder_t +# define str_append kit_str_append +#endif + +#endif |