aboutsummaryrefslogtreecommitdiff
path: root/src/string-builder.h
diff options
context:
space:
mode:
Diffstat (limited to 'src/string-builder.h')
-rw-r--r--src/string-builder.h42
1 files changed, 42 insertions, 0 deletions
diff --git a/src/string-builder.h b/src/string-builder.h
new file mode 100644
index 0000000..6834eed
--- /dev/null
+++ b/src/string-builder.h
@@ -0,0 +1,42 @@
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 char *items;
11 size_t count;
12 size_t capacity;
13};
14#define STRING_BUILDER struct string_builder *
15
16#define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \
17 DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE)
18
19#define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \
20 ({ \
21 const char *s = (CSTR); \
22 size_t n = strlen (s); \
23 DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \
24 })
25
26#define STRING_BUILDER_APPEND_CSTRS(SB, ...) \
27 string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL)
28
29#define STRING_BUILDER_APPEND_NULL(SB) \
30 DYNAMIC_ARRAY_APPEND (SB, '\0')
31
32#define STRING_BUILDER_RESET(SB) \
33 DYNAMIC_ARRAY_RESET (SB)
34
35#define STRING_BUILDER_FREE(SB) \
36 DYNAMIC_ARRAY_FREE(SB)
37
38extern void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n);
39extern void string_builder_append_cstrs (STRING_BUILDER, ...);
40extern void string_builder_printf (STRING_BUILDER, const char *format, ...);
41
42#endif