diff options
author | Anton Kling <anton@kling.gg> | 2024-10-06 10:31:28 +0200 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-10-06 10:31:28 +0200 |
commit | 6dc2637972f93dea8cc831d7ee225daefb74b8ab (patch) | |
tree | 6c2e2de449ea24cdf3f7fa59dece7643cc04dd35 | |
parent | 0b1a1eb35169eebcedf62c14cac7f8b83b6cd0ba (diff) |
libc: Add sb_prepend_buffer
-rw-r--r-- | userland/libc/include/tb/sb.h | 1 | ||||
-rw-r--r-- | userland/libc/tb/sb.c | 14 |
2 files changed, 10 insertions, 5 deletions
diff --git a/userland/libc/include/tb/sb.h b/userland/libc/include/tb/sb.h index 06aa084..c2fa1cf 100644 --- a/userland/libc/include/tb/sb.h +++ b/userland/libc/include/tb/sb.h @@ -22,4 +22,5 @@ void sb_append_buffer(struct sb *ctx, const char *buffer, size_t length); void sb_append_sv(struct sb *ctx, struct sv sv); void sb_prepend_sv(struct sb *ctx, struct sv sv); +void sb_prepend_buffer(struct sb *ctx, const char *buffer, size_t length); #endif diff --git a/userland/libc/tb/sb.c b/userland/libc/tb/sb.c index f4e10f8..7ba83af 100644 --- a/userland/libc/tb/sb.c +++ b/userland/libc/tb/sb.c @@ -50,13 +50,17 @@ void sb_append(struct sb *ctx, const char *s) { } void sb_prepend_sv(struct sb *ctx, struct sv sv) { - if (sv.length > ctx->capacity - ctx->length) { - ctx->capacity += sv.length; + sb_prepend_buffer(ctx, sv.s, sv.length); +} + +void sb_prepend_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); } - memmove(ctx->string + sv.length, ctx->string, ctx->length); - memcpy(ctx->string, sv.s, sv.length); - ctx->length += sv.length; + memmove(ctx->string + length, ctx->string, ctx->length); + memcpy(ctx->string, buffer, length); + ctx->length += length; } void sb_append_buffer(struct sb *ctx, const char *buffer, size_t length) { |