From 226d861c9ebb7f09f95665d07d9ab5c6b7ed7d6f Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Thu, 3 Oct 2024 15:08:45 +0200 Subject: libc: small changes --- userland/libc/tb/sb.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'userland/libc/tb/sb.c') diff --git a/userland/libc/tb/sb.c b/userland/libc/tb/sb.c index c3fbc64..f4e10f8 100644 --- a/userland/libc/tb/sb.c +++ b/userland/libc/tb/sb.c @@ -1,12 +1,12 @@ -#include #include #include #include +#include -void sb_init(struct sb *ctx) { - ctx->string = malloc(512); +void sb_init(struct sb *ctx, size_t starting_capacity) { + ctx->string = malloc(starting_capacity); ctx->length = 0; - ctx->capacity = 512; + ctx->capacity = starting_capacity; } void sb_free(struct sb *ctx) { @@ -59,11 +59,15 @@ void sb_prepend_sv(struct sb *ctx, struct sv sv) { ctx->length += sv.length; } -void sb_append_sv(struct sb *ctx, struct sv sv) { - if (sv.length > ctx->capacity - ctx->length) { - ctx->capacity += sv.length; +void sb_append_buffer(struct sb *ctx, const char *buffer, size_t length) { + if (length > ctx->capacity - ctx->length) { + ctx->capacity += length; ctx->string = realloc(ctx->string, ctx->capacity); } - memcpy(ctx->string + ctx->length, sv.s, sv.length); - ctx->length += sv.length; + memcpy(ctx->string + ctx->length, buffer, length); + ctx->length += length; +} + +void sb_append_sv(struct sb *ctx, struct sv sv) { + sb_append_buffer(ctx, sv.s, sv.length); } -- cgit v1.2.3