diff --git a/CMakeLists.txt b/CMakeLists.txt index 7ce0749..59be0bf 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -130,15 +130,17 @@ set(SOURCES src/libc/stdlib/strtoul.c src/libc/stdlib/strtoull.c # string - src/libc/string/strchr.c - src/libc/string/strcpy.c src/libc/string/memcpy.c - src/libc/string/strcat.c src/libc/string/memset.c + src/libc/string/strcat.c + src/libc/string/strchr.c + src/libc/string/strchrnul.c src/libc/string/strcmp.c + src/libc/string/strcpy.c src/libc/string/strdup.c src/libc/string/strlen.c - src/libc/string/strnlen.c) + src/libc/string/strnlen.c + src/libc/string/strrchr.c) if(vhex-generic IN_LIST TARGET_FOLDERS) # TODO diff --git a/STATUS b/STATUS index 60e6a7b..0d7275c 100644 --- a/STATUS +++ b/STATUS @@ -121,10 +121,10 @@ DONE: Function/symbol/macro is defined, builds, links, and is tested ! 7.21.4.4 strncmp: TODO ! 7.21.4.5 strxfrm: TODO 7.21.5.1 memchr: DONE -! 7.21.5.2 strchr: TODO +! 7.21.5.2 strchr: TEST ! 7.21.5.3 strcspn: TODO ! 7.21.5.4 strpbrk: TODO -! 7.21.5.5 strrchr: TODO +! 7.21.5.5 strrchr: TEST ! 7.21.5.6 strspn: TODO ! 7.21.5.7 strstr: TODO ! 7.21.5.8 strtok: TODO @@ -132,12 +132,12 @@ DONE: Function/symbol/macro is defined, builds, links, and is tested ! 7.21.6.2 strerror: TODO 7.21.6.3 strlen: DONE Extensions: - - strnlen: TODO - - strchrnul: TODO - - strcasecmp: TODO - - strncasecmp: TODO - - strdup: TODO - - strndup: TODO +! - strnlen: TODO +! - strchrnul: TEST +! - strcasecmp: TODO +! - strncasecmp: TODO +! - strdup: TODO +! - strndup: TODO 7.22 => GCC diff --git a/src/libc/string/strchr.c b/src/libc/string/strchr.c index 4d4af8a..5e89b1e 100644 --- a/src/libc/string/strchr.c +++ b/src/libc/string/strchr.c @@ -1,40 +1,10 @@ #include -/* -** The strchr() function returns a pointer to the first occurrence of the -** character c in the strings. -*/ -char *strchr(const char *s1, int c) +char *strchr(char const *s, int c) { - int i = -1; - while (s1[++i] != '\0' && s1[i] != c) ; - return ((s1[i] == '\0') ? NULL : (void *)&s1[i]); -} - -/* -** The strchrnul() function is like strchr() except that if c is not found in -** s, then it returns a pointer to the null byte at the end of s, rather -** than NULL. -*/ -char *strchrnul(const char *s1, int c) -{ - int i = -1; - while (s1[++i] != '\0' && s1[i] != c) ; - return ((void *)&s1[i]); -} - -/* -** The strrchr() function returns a pointer to the last occurrence of the -** character c in the strings. -*/ -char *strrchr(const char *s1, int c) -{ - void *saved; - - saved = NULL; - for (int i = 0; s1[i] != '\0'; i++) { - if (s1[i] == c) - saved = (void *)&s1[i]; + for(size_t i = 0; s[i]; i++) { + if(s[i] == c) return (char *)&s[i]; } - return (saved); + + return NULL; } diff --git a/src/libc/string/strchrnul.c b/src/libc/string/strchrnul.c new file mode 100644 index 0000000..8c9ade7 --- /dev/null +++ b/src/libc/string/strchrnul.c @@ -0,0 +1,12 @@ +#include + +char *strchrnul(const char *s, int c) +{ + size_t i; + + for(i = 0; s[i]; i++) { + if(s[i] == c) return (char *)&s[i]; + } + + return (char *)&s[i]; +} diff --git a/src/libc/string/strrchr.c b/src/libc/string/strrchr.c new file mode 100644 index 0000000..b1df1e8 --- /dev/null +++ b/src/libc/string/strrchr.c @@ -0,0 +1,12 @@ +#include + +char *strrchr(char const *s, int c) +{ + void *last = NULL; + + for(size_t i = 0; s[i]; i++) { + if(s[i] == c) last = (void *)&s[i]; + } + + return last; +}