From 9e1d304cac5367c3dbb1ca14700736680bd3a804 Mon Sep 17 00:00:00 2001 From: Martin Michalec Date: Wed, 11 Nov 2026 08:31:06 +0300 Subject: publish --- 02/src/string-builder.h | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 02/src/string-builder.h (limited to '02/src/string-builder.h') diff --git a/02/src/string-builder.h b/02/src/string-builder.h new file mode 100644 index 0000000..18367da --- /dev/null +++ b/02/src/string-builder.h @@ -0,0 +1,56 @@ +#ifndef INCLUDED_STRING_BUILDER_H +#define INCLUDED_STRING_BUILDER_H + +#include +#include +#include +#include "dynamic-array.h" + +struct string_builder { + union { char *items, *string; }; + union { size_t count, length; }; + size_t capacity; +}; +#define STRING_BUILDER struct string_builder * + +#define SBFMT(SB) (int) (SB).length, (SB).string + +#define STRING_BUILDER_APPEND_CHAR(SB, CHAR) \ + DYNAMIC_ARRAY_APPEND (SB, (char) CHAR) + +#define STRING_BUILDER_APPEND_BUF(SB, BUF, SIZE) \ + DYNAMIC_ARRAY_APPEND_MANY (SB, BUF, SIZE) + +#define STRING_BUILDER_APPEND_CSTR(SB, CSTR) \ + ({ \ + const char *s = (CSTR); \ + size_t n = strlen (s); \ + DYNAMIC_ARRAY_APPEND_MANY(SB, s, n); \ + }) + +#define STRING_BUILDER_APPEND_CSTRS(SB, ...) \ + string_builder_append_cstrs (&(SB), __VA_ARGS__, NULL) + +#define STRING_BUILDER_APPEND_STRING_VIEW(SB, SV) \ + DYNAMIC_ARRAY_APPEND_MANY(SB, (SV).begin, SVLEN (SV)); + +#define STRING_BUILDER_APPEND_NULL(SB) \ + DYNAMIC_ARRAY_APPEND (SB, '\0') + +#define STRING_BUILDER_TERMINATE(SB) ({ \ + DYNAMIC_ARRAY_ENSURE_CAPACITY (SB, 1); \ + (SB).string[(SB).length] = '\0'; \ + }) + +#define STRING_BUILDER_RESET(SB) \ + DYNAMIC_ARRAY_RESET (SB) + +#define STRING_BUILDER_FREE(SB) \ + DYNAMIC_ARRAY_FREE(SB) + +void string_builder_append_ntimes (STRING_BUILDER, char c, size_t n); +void string_builder_append_cstrs (STRING_BUILDER, ...); +void string_builder_printf (STRING_BUILDER, const char *format, ...); +bool string_builder_getline (STRING_BUILDER); + +#endif -- cgit v1.3.1