aboutsummaryrefslogtreecommitdiff
path: root/src/string-builder.h
diff options
context:
space:
mode:
authorLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 06:02:38 +0000
committerLibravatar Martin Michalec <martin@michalec.dev>2026-11-08 06:26:07 +0000
commit6ecea40c849effd171c1d27c961f5c1ee100481f (patch)
tree99a92dafddf7d2ef0e318d927ae70c3b642e4171 /src/string-builder.h
downloadlab-03-6ecea40c849effd171c1d27c961f5c1ee100481f.tar.gz
publishHEADmaster
Diffstat (limited to 'src/string-builder.h')
-rw-r--r--src/string-builder.h48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/string-builder.h b/src/string-builder.h
new file mode 100644
index 0000000..0ba8a4e
--- /dev/null
+++ b/src/string-builder.h
@@ -0,0 +1,48 @@
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, CHAR) \
17 DYNAMIC_ARRAY_APPEND (SB, CHAR)
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_STRING_VIEW(SB, SV) \
33 DYNAMIC_ARRAY_APPEND_MANY(SB, (SV).beg, STRING_VIEW_LEN (SV));
34
35#define STRING_BUILDER_APPEND_NULL(SB) \
36 DYNAMIC_ARRAY_APPEND (SB, '\0')
37
38#define STRING_BUILDER_RESET(SB) \
39 DYNAMIC_ARRAY_RESET (SB)
40
41#define STRING_BUILDER_FREE(SB) \
42 DYNAMIC_ARRAY_FREE(SB)
43
44extern void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n);
45extern void string_builder_append_cstrs (STRING_BUILDER, ...);
46extern void string_builder_printf (STRING_BUILDER, const char *format, ...);
47
48#endif