sb_fprint

This commit is contained in:
2025-12-12 20:09:41 +01:00
parent 65b5905f00
commit 9398c0bf0e
4 changed files with 26 additions and 43 deletions
+1
View File
@@ -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_u8(string_builder_t *this, uint8_t u8);
void sb_add_substring(string_builder_t *this, const char *string, size_t substring_length); 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); const char *sb_build(string_builder_t *this, allocator_t output_allocator);
void sb_fprint(string_builder_t *this, FILE *dest);
#ifdef CIG_IMPL #ifdef CIG_IMPL
+15
View File
@@ -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;
}
-43
View File
@@ -1,43 +0,0 @@
#define CIG_IMPL
#include "cig.h"
#include <stdio.h>
#include <assert.h>
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);
}
+10
View File
@@ -122,3 +122,13 @@ const char *sb_build(
this->tail = NULL; this->tail = NULL;
return buffer; 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);
}
}