summaryrefslogtreecommitdiff
path: root/userland
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-10-06 10:31:28 +0200
committerAnton Kling <anton@kling.gg>2024-10-06 10:31:28 +0200
commit6dc2637972f93dea8cc831d7ee225daefb74b8ab (patch)
tree6c2e2de449ea24cdf3f7fa59dece7643cc04dd35 /userland
parent0b1a1eb35169eebcedf62c14cac7f8b83b6cd0ba (diff)
libc: Add sb_prepend_buffer
Diffstat (limited to 'userland')
-rw-r--r--userland/libc/include/tb/sb.h1
-rw-r--r--userland/libc/tb/sb.c14
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) {