summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorDanny Holman <dholman@gymli.org>2024-02-16 12:39:37 -0600
committerDanny Holman <dholman@gymli.org>2024-02-16 12:39:37 -0600
commite8b9376573a1defbdc5ba496523c9e94031818a5 (patch)
treef2d536efa29136a2b835e6dc2b371be43951064e /kernel
parent4a7562c22a7365342754beed8fdebab0350bd256 (diff)
kernel: string: add more functions to string library
Add more functions to the string library to process buffers and strings. Signed-off-by: Danny Holman <dholman@gymli.org>
Diffstat (limited to 'kernel')
-rw-r--r--kernel/string.c64
1 files changed, 64 insertions, 0 deletions
diff --git a/kernel/string.c b/kernel/string.c
index 30fe40d..35dc16e 100644
--- a/kernel/string.c
+++ b/kernel/string.c
@@ -31,6 +31,16 @@ void* memcpy(void* __restrict dest, const void* __restrict src, size_t n) {
return dest;
}
+char* strcpy(char* __restrict dest, const char* __restrict src) {
+ if (strlen(src) > strlen(dest))
+ return NULL;
+ return (char*)memcpy(dest, src, strlen(src));
+}
+
+char* strcat(char* __restrict dest, const char* __restrict src) {
+ return (char*)memcpy(&dest[strlen(dest)], src, strlen(src));
+}
+
void* memmove(void* __restrict dest, const void* __restrict src, size_t n) {
if (dest == src)
return dest;
@@ -50,9 +60,63 @@ void* memset(void *str, int c, size_t n) {
return str;
}
+int strncmp(const char *str1, const char *str2, size_t n) {
+ return memcmp(str1, str2, n);
+}
+
+int strcmp(const char *str1, const char *str2) {
+ size_t str1_sz = strlen(str1);
+ size_t str2_sz = strlen(str2);
+ if (str1_sz > str2_sz)
+ return memcmp(str1, str2, str2_sz);
+ return memcmp(str1, str2, str1_sz);
+}
+
size_t strlen(const char *str) {
size_t i = 0;
while (str[i] != '\0')
i++;
return i;
}
+
+int _is_delim(char c, const char *delim) {
+ while (*delim != '\0') {
+ if (c == *delim)
+ return 1;
+ delim++;
+ }
+ return 0;
+}
+
+char* strtok(char* __restrict str, const char* __restrict delim) {
+ static char *old_str;
+
+ if (str == NULL)
+ str = old_str;
+
+ while (1) {
+ if (_is_delim(*str, delim)) {
+ str++;
+ continue;
+ }
+
+ if (*str == '\0')
+ return NULL;
+ break;
+ }
+
+ char *ret = str;
+ while (1) {
+ if (*str == '\0') {
+ old_str = str;
+ return ret;
+ }
+
+ if (_is_delim(*str, delim)) {
+ *str = '\0';
+ old_str = str + 1;
+ return ret;
+ }
+ str++;
+ }
+}