summaryrefslogtreecommitdiff
path: root/userland/libc/tb/sv.c
diff options
context:
space:
mode:
Diffstat (limited to 'userland/libc/tb/sv.c')
-rw-r--r--userland/libc/tb/sv.c57
1 files changed, 52 insertions, 5 deletions
diff --git a/userland/libc/tb/sv.c b/userland/libc/tb/sv.c
index 45eb0d9..ac1c1e5 100644
--- a/userland/libc/tb/sv.c
+++ b/userland/libc/tb/sv.c
@@ -11,16 +11,46 @@ char *SV_TO_C(struct sv s) {
return c_string;
}
-struct sv sv_split_space(const struct sv input, struct sv *rest) {
+struct sv sv_next(struct sv s) {
+ if (0 == s.length) {
+ return s;
+ }
+ s.length--;
+ s.s++;
+ return s;
+}
+
+struct sv sv_skip_chars(const struct sv input, const char *chars) {
+ struct sv r = input;
+ for (; r.length > 0;) {
+ int found = 0;
+ const char *p = chars;
+ for (; *p; p++) {
+ if (*p == r.s[0]) {
+ r.s++;
+ r.length--;
+ found = 1;
+ break;
+ }
+ }
+ if (!found) {
+ break;
+ }
+ }
+ return r;
+}
+
+struct sv sv_split_function(const struct sv input, struct sv *rest,
+ int (*function)(int)) {
struct sv r = {
.s = input.s,
};
for (size_t i = 0; i < input.length; i++) {
- if (isspace(input.s[i])) {
+ if (function(input.s[i])) {
r.length = i;
if (rest) {
- rest->s += i + 1;
- rest->length -= (i + 1);
+ rest->s += i;
+ rest->length -= i;
}
return r;
}
@@ -33,6 +63,10 @@ struct sv sv_split_space(const struct sv input, struct sv *rest) {
return input;
}
+struct sv sv_split_space(const struct sv input, struct sv *rest) {
+ return sv_split_function(input, rest, isspace);
+}
+
struct sv sv_end_split_delim(const struct sv input, struct sv *rest,
char delim) {
for (size_t i = input.length - 1; i > 0; i--) {
@@ -113,6 +147,19 @@ int sv_eq(struct sv a, struct sv b) {
return 1;
}
+struct sv sv_take(struct sv s, struct sv *rest, size_t n) {
+ if (s.length < n) {
+ if (rest) {
+ rest->length = 0;
+ }
+ return s;
+ }
+ s.length = n;
+ rest->length -= n;
+ rest->s += n;
+ return s;
+}
+
struct sv sv_trim_left(struct sv s, size_t n) {
if (s.length < n) {
s.s += s.length;
@@ -135,7 +182,7 @@ struct sv sv_clone(struct sv s) {
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) {
+ if (0 == copy_len) {
return NULL;
}
if (!out) {