From c660d1ec1e7d8643cab5dfeef5561ef58da3ad78 Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Sat, 13 Dec 2025 16:16:25 +0100 Subject: [PATCH] update builder to always clear after retrieving output --- cig.h | 6 +++--- string_builder.c | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/cig.h b/cig.h index cd68e38..edbbcab 100644 --- a/cig.h +++ b/cig.h @@ -325,7 +325,7 @@ typedef struct string_builder { } string_builder_t; string_builder_t sb_create(allocator_t builder_allocator); -// assumes the provided string will live until sb_build is called. +// assumes the provided string will live until sb_build_and_clear is called. void sb_add_string(string_builder_t *this, const char *string); void sb_add_i64(string_builder_t *this, int64_t i64); void sb_add_i32(string_builder_t *this, int32_t i32); @@ -336,8 +336,8 @@ void sb_add_u32(string_builder_t *this, uint32_t u32); void sb_add_u16(string_builder_t *this, uint16_t u16); void sb_add_u8(string_builder_t *this, uint8_t u8); void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length); -const char *sb_build(string_builder_t *this, allocator_t output_allocator); -void sb_fprint(string_builder_t *this, FILE *dest); +const char *sb_build_and_clear(string_builder_t *this, allocator_t output_allocator); +void sb_fprint_and_clear(string_builder_t *this, FILE *dest); #ifdef CIG_IMPL diff --git a/string_builder.c b/string_builder.c index 9860521..84ff95c 100644 --- a/string_builder.c +++ b/string_builder.c @@ -91,7 +91,7 @@ void sb_add_substring(string_builder_t *this, const char *string, size_t substri sb_add_string(this, buffer); } -const char *sb_build( +const char *sb_build_and_clear( string_builder_t *this, allocator_t output_allocator ) { @@ -123,7 +123,7 @@ const char *sb_build( return buffer; } -void sb_fprint(string_builder_t *this, FILE *dest) { +void sb_fprint_and_clear(string_builder_t *this, FILE *dest) { for ( string_builder_node_t *node = this->head; node != NULL; @@ -131,4 +131,6 @@ void sb_fprint(string_builder_t *this, FILE *dest) { ) { fprintf(dest, "%s", node->string); } + this->head = NULL; + this->tail = NULL; }