From 1b0c0f4b45e518bf12c58902819fbc90e4834dad Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Tue, 31 Dec 2024 14:53:29 +0100 Subject: libc: Add more sv/sb functions --- userland/libc/tb/sv.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 48 insertions(+), 2 deletions(-) (limited to 'userland/libc/tb/sv.c') diff --git a/userland/libc/tb/sv.c b/userland/libc/tb/sv.c index 0d47924..a4458d5 100644 --- a/userland/libc/tb/sv.c +++ b/userland/libc/tb/sv.c @@ -1,9 +1,14 @@ #include -#include #include #include #include +#define min(a, b) (((a) < (b)) ? (a) : (b)) + +struct sv sv_init(const char *s, size_t length) { + return (struct sv){.s = s, .length = length}; +} + char *SV_TO_C(struct sv s) { char *c_string = malloc(s.length + 1); memcpy(c_string, s.s, s.length); @@ -49,7 +54,11 @@ struct sv sv_skip_chars(const struct sv input, const char *chars) { return r; } -uint64_t sv_parse_unsigned_number(struct sv input, struct sv *rest) { +uint64_t sv_parse_unsigned_number(struct sv input, struct sv *rest, + int *got_num) { + if (got_num) { + *got_num = 0; + } uint64_t r = 0; size_t i = 0; for (; i < input.length; i++) { @@ -59,6 +68,11 @@ uint64_t sv_parse_unsigned_number(struct sv input, struct sv *rest) { r *= 10; r += input.s[i] - '0'; } + if (i > 0) { + if (got_num) { + *got_num = 1; + } + } input.length -= i; input.s += i; if (rest) { @@ -189,6 +203,21 @@ struct sv sv_take(struct sv s, struct sv *rest, size_t n) { return s; } +int sv_read(struct sv s, struct sv *rest, void *buf, size_t n) { + if (s.length < n) { + if (rest) { + rest->length = 0; + } + return 0; + } + memcpy(buf, s.s, n); + if (rest) { + rest->length -= n; + rest->s += n; + } + return 1; +} + struct sv sv_take_end(struct sv s, struct sv *rest, size_t n) { if (s.length < n) { if (rest) { @@ -224,6 +253,23 @@ struct sv sv_clone(struct sv s) { return new_sv; } +int sv_try_eat(struct sv input, struct sv *rest, struct sv b) { + if (input.length < b.length) { + return 0; + } + for (size_t i = 0; i < b.length; i++) { + if (input.s[i] != b.s[i]) { + return 0; + } + } + input.s += b.length; + input.length -= b.length; + if (rest) { + *rest = input; + } + return 1; +} + char *sv_copy_to_c(struct sv s, char *out, size_t buffer_length) { int copy_len = min(s.length + 1, buffer_length); if (0 == copy_len) { -- cgit v1.2.3