summaryrefslogtreecommitdiff
path: root/source/kit/string_builder.h
diff options
context:
space:
mode:
authorMitya Selivanov <automainint@guattari.tech>2023-12-29 06:21:33 +0100
committerMitya Selivanov <automainint@guattari.tech>2023-12-29 06:21:33 +0100
commit2d6c8fec45b23a8a28668ecf3ef281139ab778a7 (patch)
tree75d2a8538992129a83c0c2b83688289443d697e5 /source/kit/string_builder.h
parent820b171245f2f14766f3accdb0246a4e2c0d596a (diff)
downloadsaw-2d6c8fec45b23a8a28668ecf3ef281139ab778a7.zip
refactor dependencies; include dependencies source code
Diffstat (limited to 'source/kit/string_builder.h')
-rw-r--r--source/kit/string_builder.h63
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