From 635f756fa91e22f6c4fd653e5ed2a808eb7335ae Mon Sep 17 00:00:00 2001 From: Anton Kling Date: Mon, 9 Dec 2024 20:57:31 +0100 Subject: libc: sv add number parsing --- userland/libc/include/tb/sv.h | 1 + userland/libc/tb/sv.c | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+) (limited to 'userland/libc') diff --git a/userland/libc/include/tb/sv.h b/userland/libc/include/tb/sv.h index e1ca453..0ffe266 100644 --- a/userland/libc/include/tb/sv.h +++ b/userland/libc/include/tb/sv.h @@ -36,4 +36,5 @@ struct sv sv_trim_left(struct sv s, size_t n); struct sv sv_clone(struct sv s); struct sv sv_clone_from_c(const char *s); char *sv_copy_to_c(struct sv s, char *out, size_t buffer_length); +uint64_t sv_parse_unsigned_number(struct sv input, struct sv *rest); #endif diff --git a/userland/libc/tb/sv.c b/userland/libc/tb/sv.c index 4ff24fb..0d47924 100644 --- a/userland/libc/tb/sv.c +++ b/userland/libc/tb/sv.c @@ -49,6 +49,24 @@ 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 r = 0; + size_t i = 0; + for (; i < input.length; i++) { + if (!isdigit(input.s[i])) { + break; + } + r *= 10; + r += input.s[i] - '0'; + } + input.length -= i; + input.s += i; + if (rest) { + *rest = input; + } + return r; +} + struct sv sv_split_function(const struct sv input, struct sv *rest, int (*function)(int)) { struct sv r = { -- cgit v1.2.3