diff options
Diffstat (limited to 'userland/libc/tb')
-rw-r--r-- | userland/libc/tb/sb.c | 61 | ||||
-rw-r--r-- | userland/libc/tb/sv.c | 50 |
2 files changed, 107 insertions, 4 deletions
diff --git a/userland/libc/tb/sb.c b/userland/libc/tb/sb.c index c379082..0c36750 100644 --- a/userland/libc/tb/sb.c +++ b/userland/libc/tb/sb.c @@ -10,6 +10,7 @@ void sb_init(struct sb *ctx) { } int sb_init_capacity(struct sb *ctx, size_t starting_capacity) { + ctx->to_ignore = 0; ctx->length = 0; ctx->prebuffer = 0; ctx->string = malloc(starting_capacity); @@ -26,6 +27,7 @@ void sb_init_buffer(struct sb *ctx, char *buffer, size_t size) { ctx->capacity = size; ctx->length = 0; ctx->prebuffer = 1; + ctx->to_ignore = 0; } void sb_free(struct sb *ctx) { @@ -39,8 +41,13 @@ void sb_free(struct sb *ctx) { ctx->string = NULL; } +void sb_set_ignore(struct sb *ctx, size_t n) { + ctx->to_ignore = n; +} + void sb_reset(struct sb *ctx) { ctx->length = 0; + ctx->to_ignore = 0; } int sb_isempty(const struct sb *ctx) { @@ -63,6 +70,10 @@ int sb_increase_buffer(struct sb *ctx, size_t min) { } int sb_append_char(struct sb *ctx, char c) { + if (ctx->to_ignore > 0) { + ctx->to_ignore--; + return 1; + } if (1 > ctx->capacity - ctx->length) { if (!sb_increase_buffer(ctx, 1)) { return 0; @@ -81,6 +92,13 @@ int sb_delete_right(struct sb *ctx, size_t n) { int sb_append(struct sb *ctx, const char *s) { size_t l = strlen(s); + if (ctx->to_ignore >= l) { + ctx->to_ignore -= l; + return 1; + } + l -= ctx->to_ignore; + s += ctx->to_ignore; + ctx->to_ignore = 0; if (l > ctx->capacity - ctx->length) { if (!sb_increase_buffer(ctx, l)) { return 0; @@ -95,7 +113,14 @@ int sb_prepend_sv(struct sb *ctx, struct sv sv) { return sb_prepend_buffer(ctx, sv.s, sv.length); } -int sb_prepend_buffer(struct sb *ctx, const char *buffer, size_t length) { +int sb_prepend_buffer(struct sb *ctx, const void *buffer, size_t length) { + if (ctx->to_ignore >= length) { + ctx->to_ignore -= length; + return 1; + } + length -= ctx->to_ignore; + buffer = (void *)((uintptr_t)buffer + ctx->to_ignore); + ctx->to_ignore = 0; if (length > ctx->capacity - ctx->length) { if (!sb_increase_buffer(ctx, length)) { return 0; @@ -107,7 +132,14 @@ int sb_prepend_buffer(struct sb *ctx, const char *buffer, size_t length) { return 1; } -int sb_append_buffer(struct sb *ctx, const char *buffer, size_t length) { +int sb_append_buffer(struct sb *ctx, const void *buffer, size_t length) { + if (ctx->to_ignore >= length) { + ctx->to_ignore -= length; + return 1; + } + length -= ctx->to_ignore; + buffer = (void *)((uintptr_t)buffer + ctx->to_ignore); + ctx->to_ignore = 0; if (length > ctx->capacity - ctx->length) { if (!sb_increase_buffer(ctx, length)) { return 0; @@ -118,6 +150,31 @@ int sb_append_buffer(struct sb *ctx, const char *buffer, size_t length) { return 1; } +int sb_modify_location(struct sb *ctx, void *buffer, size_t length, + size_t offset) { + if (offset + length > ctx->length) { + return 0; + } + memcpy(ctx->string + offset, buffer, length); + return 1; +} + +int sb_reserve_buffer(struct sb *ctx, size_t *offset, size_t length) { + if (ctx->to_ignore > 0) { + return 0; + } + if (length > ctx->capacity - ctx->length) { + if (!sb_increase_buffer(ctx, length)) { + return 0; + } + } + if (offset) { + *offset = ctx->length; + } + ctx->length += length; + return 1; +} + int sb_append_sv(struct sb *ctx, struct sv sv) { return sb_append_buffer(ctx, sv.s, sv.length); } 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 <ctype.h> -#include <math.h> #include <stdlib.h> #include <string.h> #include <tb/sv.h> +#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) { |