summaryrefslogtreecommitdiff
path: root/userland/libc/tb
diff options
context:
space:
mode:
authorAnton Kling <anton@kling.gg>2024-10-03 15:08:45 +0200
committerAnton Kling <anton@kling.gg>2024-10-03 15:08:45 +0200
commit226d861c9ebb7f09f95665d07d9ab5c6b7ed7d6f (patch)
treed900c3437d3d65ee7bd88474586dc6dd947b14ce /userland/libc/tb
parent64b0b49a1f23da9faa680f8158cb8a12cd4d118c (diff)
libc: small changes
Diffstat (limited to 'userland/libc/tb')
-rw-r--r--userland/libc/tb/sb.c22
-rw-r--r--userland/libc/tb/sv.c21
2 files changed, 32 insertions, 11 deletions
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 <tb/sb.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>
+#include <tb/sb.h>
-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);
}
diff --git a/userland/libc/tb/sv.c b/userland/libc/tb/sv.c
index ac1c1e5..3f947ef 100644
--- a/userland/libc/tb/sv.c
+++ b/userland/libc/tb/sv.c
@@ -155,8 +155,25 @@ struct sv sv_take(struct sv s, struct sv *rest, size_t n) {
return s;
}
s.length = n;
- rest->length -= n;
- rest->s += n;
+ if (rest) {
+ rest->length -= n;
+ rest->s += n;
+ }
+ return s;
+}
+
+struct sv sv_take_end(struct sv s, struct sv *rest, size_t n) {
+ if (s.length < n) {
+ if (rest) {
+ rest->length = 0;
+ }
+ return s;
+ }
+ if (rest) {
+ rest->length = s.length - n;
+ }
+ s.s += (s.length - n);
+ s.length = n;
return s;
}