aboutsummaryrefslogtreecommitdiffhomepage
path: root/common.c
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2025-08-17 13:18:15 +0900
committernsfisis <nsfisis@gmail.com>2025-08-18 03:52:44 +0900
commit2a7e1667f70c5381d3b939324cc647e51134b15c (patch)
treee6d6d5bc285aadbae20c99dbcf0a653f78dda4c3 /common.c
parentd90a9c83a253b71e7731a44657f998a361a41b97 (diff)
downloadducc-2a7e1667f70c5381d3b939324cc647e51134b15c.tar.gz
ducc-2a7e1667f70c5381d3b939324cc647e51134b15c.tar.zst
ducc-2a7e1667f70c5381d3b939324cc647e51134b15c.zip
refactor: direct array access with infile_*() helper functions
Diffstat (limited to 'common.c')
-rw-r--r--common.c27
1 files changed, 27 insertions, 0 deletions
diff --git a/common.c b/common.c
index 921b89b..1b89f8c 100644
--- a/common.c
+++ b/common.c
@@ -10,3 +10,30 @@ void fatal_error(const char* msg, ...) {
#define unreachable() fatal_error("%s:%d: unreachable", __FILE__, __LINE__)
#define unimplemented() fatal_error("%s:%d: unimplemented", __FILE__, __LINE__)
+
+struct StrBuilder {
+ size_t len;
+ size_t capacity;
+ char* buf;
+};
+typedef struct StrBuilder StrBuilder;
+
+void strbuilder_init(StrBuilder* b) {
+ b->len = 0;
+ b->capacity = 16;
+ b->buf = calloc(b->capacity, sizeof(char));
+}
+
+// `size` must include a trailing null byte.
+void strbuilder_reserve(StrBuilder* b, size_t size) {
+ if (size <= b->capacity)
+ return;
+ b->capacity *= 2;
+ b->buf = realloc(b->buf, b->capacity * sizeof(char));
+ memset(b->buf + b->len, 0, (b->capacity - b->len) * sizeof(char));
+}
+
+void strbuilder_append_char(StrBuilder* b, int c) {
+ strbuilder_reserve(b, b->len + 1 + 1);
+ b->buf[b->len++] = c;
+}