aboutsummaryrefslogtreecommitdiff
path: root/src/string-builder.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:23:31 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 05:24:55 +0000
commit3f36aca66e1ac6c872300059e8db691a473614d4 (patch)
tree48c65f463c487d281c7c4091c7ba01dd4a2ff8f1 /src/string-builder.h
downloadlab-02-3f36aca66e1ac6c872300059e8db691a473614d4.tar.gz
publishHEADmaster
Diffstat (limited to 'src/string-builder.h')
-rw-r--r--src/string-builder.h45
1 files changed, 45 insertions, 0 deletions
diff --git a/src/string-builder.h b/src/string-builder.h
new file mode 100644
index 0000000..902619d
--- /dev/null
+++ b/src/string-builder.h
@@ -0,0 +1,45 @@
1#ifndef INCLUDED_STRING_BUILDER_H
2#define INCLUDED_STRING_BUILDER_H
3
4#include <stddef.h>
5#include <string.h>
6#include "utils.h"
7#include "dynamic-array.h"
8
9struct string_builder {
10 union { char *items, *string; };
11 size_t count;
12 size_t capacity;
13};
14#define STRING_BUILDER struct string_builder *
15
16#define STRING_BUILDER_APPEND_CHAR(SB, C) \
17 DYNAMIC_ARRAY_APPEND (SB, C)
18
19#define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \
20 DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE)
21
22#define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \
23 ({ \
24 const char *s = (CSTR); \
25 size_t n = strlen (s); \
26 DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \
27 })
28
29#define STRING_BUILDER_APPEND_CSTRS(SB, ...) \
30 string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL)
31
32#define STRING_BUILDER_APPEND_NULL(SB) \
33 DYNAMIC_ARRAY_APPEND (SB, '\0')
34
35#define STRING_BUILDER_RESET(SB) \
36 DYNAMIC_ARRAY_RESET (SB)
37
38#define STRING_BUILDER_FREE(SB) \
39 DYNAMIC_ARRAY_FREE(SB)
40
41extern void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n);
42extern void string_builder_append_cstrs (STRING_BUILDER, ...);
43extern void string_builder_printf (STRING_BUILDER, const char *format, ...);
44
45#endif