diff options
| author | 2026-11-11 05:31:06 +0000 | |
|---|---|---|
| committer | 2026-11-11 06:14:42 +0000 | |
| commit | 9e1d304cac5367c3dbb1ca14700736680bd3a804 (patch) | |
| tree | 31f328747acb934d365d423adfda144818ff5ad4 /03/src/string-builder.h | |
| download | lab-01-05-9e1d304cac5367c3dbb1ca14700736680bd3a804.tar.gz | |
Diffstat (limited to '03/src/string-builder.h')
| -rw-r--r-- | 03/src/string-builder.h | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/03/src/string-builder.h b/03/src/string-builder.h new file mode 100644 index 0000000..6cc6307 --- /dev/null +++ b/03/src/string-builder.h | |||
| @@ -0,0 +1,56 @@ | |||
| 1 | #ifndef INCLUDED_STRING_BUILDER_H | ||
| 2 | #define INCLUDED_STRING_BUILDER_H | ||
| 3 | |||
| 4 | #include <stddef.h> | ||
| 5 | #include <string.h> | ||
| 6 | #include <stdbool.h> | ||
| 7 | #include "dynamic-array.h" | ||
| 8 | |||
| 9 | struct string_builder { | ||
| 10 | union { char *items, *string; }; | ||
| 11 | union { size_t count, length; }; | ||
| 12 | size_t capacity; | ||
| 13 | }; | ||
| 14 | #define STRING_BUILDER struct string_builder * | ||
| 15 | |||
| 16 | #define SBFMT(SB) (int) (SB).length, (SB).string | ||
| 17 | |||
| 18 | #define STRING_BUILDER_APPEND_CHAR(SB, CHAR) \ | ||
| 19 | DYNAMIC_ARRAY_APPEND ((SB), (char) (CHAR)) | ||
| 20 | |||
| 21 | #define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \ | ||
| 22 | DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE) | ||
| 23 | |||
| 24 | #define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \ | ||
| 25 | ({ \ | ||
| 26 | const char *s = (CSTR); \ | ||
| 27 | size_t n = strlen (s); \ | ||
| 28 | DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \ | ||
| 29 | }) | ||
| 30 | |||
| 31 | #define STRING_BUILDER_APPEND_CSTRS(SB, ...) \ | ||
| 32 | string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL) | ||
| 33 | |||
| 34 | #define STRING_BUILDER_APPEND_STRING_VIEW(SB, SV) \ | ||
| 35 | DYNAMIC_ARRAY_APPEND_MANY(SB, (SV).begin, SVLEN (SV)); | ||
| 36 | |||
| 37 | #define STRING_BUILDER_APPEND_NULL(SB) \ | ||
| 38 | STRING_BUILDER_APPEND_CHAR (SB, '\0'); | ||
| 39 | |||
| 40 | #define STRING_BUILDER_TERMINATE(SB) ({ \ | ||
| 41 | DYNAMIC_ARRAY_ENSURE_CAPACITY (SB, 1); \ | ||
| 42 | (SB).string[(SB).length] = '\0'; \ | ||
| 43 | }) | ||
| 44 | |||
| 45 | #define STRING_BUILDER_RESET(SB) \ | ||
| 46 | DYNAMIC_ARRAY_RESET (SB) | ||
| 47 | |||
| 48 | #define STRING_BUILDER_FREE(SB) \ | ||
| 49 | DYNAMIC_ARRAY_FREE(SB) | ||
| 50 | |||
| 51 | void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n); | ||
| 52 | void string_builder_append_cstrs (STRING_BUILDER, ...); | ||
| 53 | void string_builder_printf (STRING_BUILDER, const char *format, ...); | ||
| 54 | bool string_builder_getline (STRING_BUILDER); | ||
| 55 | |||
| 56 | #endif | ||
