From 9398c0bf0e695c54a180c28de0bf95303d0eb06f Mon Sep 17 00:00:00 2001 From: Ivar Fatland Date: Fri, 12 Dec 2025 20:09:41 +0100 Subject: [PATCH] sb_fprint --- cig.h | 1 + scrap/sb_print.c | 15 +++++++++++++++ scratch.c | 43 ------------------------------------------- string_builder.c | 10 ++++++++++ 4 files changed, 26 insertions(+), 43 deletions(-) create mode 100755 scrap/sb_print.c delete mode 100644 scratch.c diff --git a/cig.h b/cig.h index 5df39ac..37d35b2 100644 --- a/cig.h +++ b/cig.h @@ -309,6 +309,7 @@ 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); #ifdef CIG_IMPL diff --git a/scrap/sb_print.c b/scrap/sb_print.c new file mode 100755 index 0000000..cb7056d --- /dev/null +++ b/scrap/sb_print.c @@ -0,0 +1,15 @@ +#!/usr/bin/tcc -run + +#define CIG_IMPL +#include "../cig.h" + +int main(int argc, const char **argv) { + with_borrow(allocator) { + string_builder_t sb = sb_create(allocator); + sb_add_string(&sb, "Age: "); + sb_add_i64(&sb, 50); + sb_add_string(&sb, "\n"); + sb_fprint(&sb, stdout); + } + return 0; +} diff --git a/scratch.c b/scratch.c deleted file mode 100644 index 0d69b34..0000000 --- a/scratch.c +++ /dev/null @@ -1,43 +0,0 @@ -#define CIG_IMPL -#include "cig.h" -#include -#include - -int main() { - // test 1/////////////////////////////////////////////////////////////////////// - arena_allocator_t aalloc = arena_allocator_create(); - - for ( int i = 0; i < 10; i++ ) with_arena(&aalloc, allocator) { - allocator_alloc(allocator, 10); - } - - assert(aalloc.to_allocate == 10); - assert(borrow_allocator_count_allocations(&aalloc.borrow_allocator) == 0); - assert(dyn_array_capacity(aalloc.bytes) == 10); - - arena_allocator_destroy(&aalloc); - assert(aalloc.to_allocate == 0); - arena_allocator_destroy(&aalloc); - - // test 2 ////////////////////////////////////////////////////////////////////// - aalloc = arena_allocator_create(); - - for ( int i = 0; i < 10; i++ ) with_arena(&aalloc, allocator) { - allocator_alloc(allocator, 1000); - } - - with_arena(&aalloc, allocator) { - size_t prev_addr = 0; - prev_addr = ~prev_addr; - for (int i = 0; i < 1001; i++) { - size_t addr = (size_t)allocator_alloc(allocator, 1); - assert(addr != prev_addr); - assert(addr % MAX_ALIGN == 0); - } - } - arena_allocator_reset(&aalloc); - fprintf(stderr, "%zu\n", aalloc.to_allocate); - //assert(aalloc.to_allocate == MAX_ALIGN * 1000 + 1); - // TODO: WHAT is happening here? - arena_allocator_destroy(&aalloc); -} diff --git a/string_builder.c b/string_builder.c index 2213912..9860521 100644 --- a/string_builder.c +++ b/string_builder.c @@ -122,3 +122,13 @@ const char *sb_build( this->tail = NULL; return buffer; } + +void sb_fprint(string_builder_t *this, FILE *dest) { + for ( + string_builder_node_t *node = this->head; + node != NULL; + node = node->next + ) { + fprintf(dest, "%s", node->string); + } +}