diff options
author | Anton Kling <anton@kling.gg> | 2024-12-11 14:48:15 +0100 |
---|---|---|
committer | Anton Kling <anton@kling.gg> | 2024-12-11 14:48:15 +0100 |
commit | b343b0dae5aa51b5bd9b195936358341a943b3b2 (patch) | |
tree | 85b088fef86f9d0724a98202260fb58048b6d096 | |
parent | 916aa42260290e9e864304bc7d9395b6aa693c27 (diff) |
kernel: Add more sv functions and change interfaces
-rw-r--r-- | kernel/audio.c | 5 | ||||
-rw-r--r-- | kernel/lib/sv.c | 28 | ||||
-rw-r--r-- | kernel/lib/sv.h | 3 | ||||
-rw-r--r-- | kernel/timer.c | 2 |
4 files changed, 33 insertions, 5 deletions
diff --git a/kernel/audio.c b/kernel/audio.c index fe78938..0b1c4af 100644 --- a/kernel/audio.c +++ b/kernel/audio.c @@ -28,9 +28,10 @@ int volume_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) { (void)fd; struct sv string_view = sv_init(buffer, len); struct sv rest; - u64 volume = sv_parse_unsigned_number(string_view, &rest); + int got_num; + u64 volume = sv_parse_unsigned_number(string_view, &rest, &got_num); int i = sv_length(string_view) - sv_length(rest); - if (0 == i) { + if (!got_num) { return 0; } diff --git a/kernel/lib/sv.c b/kernel/lib/sv.c index 580e63a..2143a4b 100644 --- a/kernel/lib/sv.c +++ b/kernel/lib/sv.c @@ -54,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++) { @@ -64,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) { @@ -229,6 +238,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) { diff --git a/kernel/lib/sv.h b/kernel/lib/sv.h index b5cb0c9..bd556d4 100644 --- a/kernel/lib/sv.h +++ b/kernel/lib/sv.h @@ -28,6 +28,7 @@ struct sv sv_split_space(const struct sv input, struct sv *rest); struct sv sv_skip_chars(const struct sv input, const char *chars); struct sv sv_split_function(const struct sv input, struct sv *rest, int (*function)(int)); +int sv_try_eat(struct sv input, struct sv *rest, struct sv b); struct sv sv_take(struct sv s, struct sv *rest, size_t n); struct sv sv_take_end(struct sv s, struct sv *rest, size_t n); struct sv sv_next(struct sv s); @@ -39,5 +40,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); +uint64_t sv_parse_unsigned_number(struct sv input, struct sv *rest, int *got_num); #endif diff --git a/kernel/timer.c b/kernel/timer.c index b84c16a..6321cda 100644 --- a/kernel/timer.c +++ b/kernel/timer.c @@ -61,7 +61,7 @@ int clock_write(u8 *buffer, u64 offset, u64 len, vfs_fd_t *fd) { struct sv string_view = sv_init(buffer, len); - u64 new_value_ms = sv_parse_unsigned_number(string_view, &string_view); + u64 new_value_ms = sv_parse_unsigned_number(string_view, &string_view, NULL); i64 new_value_seconds = new_value_ms / 1000; |