summaryrefslogtreecommitdiff
path: root/src/user/lib
diff options
context:
space:
mode:
authordzwdz2022-08-22 13:43:02 +0200
committerdzwdz2022-08-22 13:43:02 +0200
commit6a4eb86c64fa42abcf9199cd968be089ee04c3bc (patch)
tree9fc38291dc469bef2bf7a10cca70459d68068990 /src/user/lib
parente35d6a4fde9a0671bc7d2527ff6b55b0ce1b4b1e (diff)
user/libc: strspn, strtok
Diffstat (limited to 'src/user/lib')
-rw-r--r--src/user/lib/include/string.h6
-rw-r--r--src/user/lib/string.c36
2 files changed, 40 insertions, 2 deletions
diff --git a/src/user/lib/include/string.h b/src/user/lib/include/string.h
index eda4e48..5cddb61 100644
--- a/src/user/lib/include/string.h
+++ b/src/user/lib/include/string.h
@@ -3,3 +3,9 @@
long strtol(const char *restrict s, char **restrict end, int base);
char *strchr(const char *s, int c);
+
+size_t strspn(const char *s, const char *accept);
+size_t strcspn(const char *s, const char *reject);
+
+char *strtok(char *restrict s, const char *restrict sep);
+char *strtok_r(char *restrict s, const char *restrict sep, char **restrict state);
diff --git a/src/user/lib/string.c b/src/user/lib/string.c
index 9c347d0..ff67399 100644
--- a/src/user/lib/string.c
+++ b/src/user/lib/string.c
@@ -48,9 +48,41 @@ long strtol(const char *restrict s, char **restrict end, int base) {
}
char *strchr(const char *s, int c) {
- while (*s) {
+ for (; *s; s++) {
if (*s == c) return (char*)s;
- s++;
}
return NULL;
}
+
+size_t strspn(const char *s, const char *accept) {
+ size_t l = 0;
+ for (; s[l] && strchr(accept, s[l]); l++);
+ return l;
+}
+
+size_t strcspn(const char *s, const char *reject) {
+ size_t l = 0;
+ for (; s[l] && !strchr(reject, s[l]); l++);
+ return l;
+}
+
+char *strtok(char *restrict s, const char *restrict sep) {
+ static char *state;
+ return strtok_r(s, sep, &state);
+}
+
+char *strtok_r(char *restrict s, const char *restrict sep, char **restrict state) {
+ char *end;
+ if (!s) s = *state;
+ s += strspn(s, sep); /* beginning of token */
+ if (!*s) return NULL;
+
+ end = s + strcspn(s, sep);
+ if (*end) {
+ *end = '\0';
+ *state = end + 1;
+ } else {
+ *state = end;
+ }
+ return s;
+}