From cb2d06796799fb3f62372857f5edfa333064ddbc Mon Sep 17 00:00:00 2001 From: Lephe Date: Mon, 15 Feb 2021 16:43:42 +0100 Subject: [PATCH] std/string: add strchr(), strrchr(), and strchrnul() --- CMakeLists.txt | 1 + include/gint/std/string.h | 9 +++++++++ src/std/string-ext.c | 12 ++++++++++++ src/std/string.c | 18 ++++++++++++++++++ 4 files changed, 40 insertions(+) create mode 100644 src/std/string-ext.c diff --git a/CMakeLists.txt b/CMakeLists.txt index c012c1d..6f8e6fc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ set(SOURCES_COMMON src/std/memset.s src/std/print.c src/std/string.c + src/std/string-ext.c src/tmu/inth-etmu.s src/tmu/inth-tmu.s src/tmu/sleep.c diff --git a/include/gint/std/string.h b/include/gint/std/string.h index 49c1b51..892c700 100644 --- a/include/gint/std/string.h +++ b/include/gint/std/string.h @@ -31,4 +31,13 @@ char *strcat(char *dest, const char *src); /* strcmp(): Compare NUL-terminated strings */ int strcmp(char const *s1, char const *s2); +/* strchr(): Find the first occurrence of a character in a string */ +char *strchr(char const *s, int c); + +/* strrchr(): Find the last occurrence of a character in a string */ +char *strrchr(char const *s, int c); + +/* strchrnul(): Like strchr(), but returns end-of-string instead of NUL */ +char *strchrnul(char const *s, int c); + #endif /* GINT_STD_STRING */ diff --git a/src/std/string-ext.c b/src/std/string-ext.c new file mode 100644 index 0000000..f6de513 --- /dev/null +++ b/src/std/string-ext.c @@ -0,0 +1,12 @@ +//--- +// gint:std:string-ext: Extensions to the standard for +//--- + +#include +#include + +GWEAK char *strchrnul(char const *s, int c) +{ + while(*s && *s != c) s++; + return (char *)s; +} diff --git a/src/std/string.c b/src/std/string.c index 0b771f7..1fbf5b1 100644 --- a/src/std/string.c +++ b/src/std/string.c @@ -4,6 +4,7 @@ #include #include +#include #include GWEAK size_t strlen(char const *str) @@ -33,3 +34,20 @@ GWEAK int strcmp(char const *s1, char const *s2) while(*s1 && *s1 == *s2) s1++, s2++; return *s1 - *s2; } + +GWEAK char *strchr(char const *s, int c) +{ + while(*s && *s != c) s++; + return (*s) ? (char *)s : NULL; +} + +GWEAK char *strrchr(char const *s, int c) +{ + char const *found = NULL; + while(*s) + { + if(*s == c) found = s; + s++; + } + return (char *)found; +}