From 0c2f81e5bb9e51054a069703025d40b46567ff07 Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Mon, 10 Jan 2022 21:20:55 +0100 Subject: [PATCH] string: add and test a naive memrchr (DONE) --- CMakeLists.txt | 1 + STATUS | 1 + include/string.h | 3 +++ src/libc/string/memrchr.c | 12 ++++++++++++ 4 files changed, 17 insertions(+) create mode 100644 src/libc/string/memrchr.c diff --git a/CMakeLists.txt b/CMakeLists.txt index a463886..ec8224f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -190,6 +190,7 @@ set(SOURCES src/libc/string/memcmp.c src/libc/string/memcpy.c src/libc/string/memmove.c + src/libc/string/memrchr.c src/libc/string/memset.c src/libc/string/strcasecmp.c src/libc/string/strcasestr.c diff --git a/STATUS b/STATUS index 3e02f34..c617b0b 100644 --- a/STATUS +++ b/STATUS @@ -200,6 +200,7 @@ TEST: Function/symbol/macro needs to be tested (EXT) strncasecmp - (EXT) strdup - (EXT) strndup - + (EXT) memrchr - 7.22 => GCC diff --git a/include/string.h b/include/string.h index 705308d..04268fc 100644 --- a/include/string.h +++ b/include/string.h @@ -54,6 +54,9 @@ extern size_t strxfrm(char * __restrict__ __dest, /* Search __c within the first __n characters of __s. */ extern void *memchr(void const *__s, int __c, size_t __n); +/* Search the last occurrence of __c withing the first __n bytes of __s. */ +extern void *memrchr(void const *__s, int __c, size_t __n); + /* Find the first occurrence of __c within __s. */ extern char *strchr(char const *__s, int __c); diff --git a/src/libc/string/memrchr.c b/src/libc/string/memrchr.c new file mode 100644 index 0000000..d7a9288 --- /dev/null +++ b/src/libc/string/memrchr.c @@ -0,0 +1,12 @@ +#include + +void *memrchr(void const *_s, int c, size_t n) +{ + char const *s = _s; + + for(int i = n - 1; i >= 0; i--) { + if(s[i] == c) return (char *)&s[i]; + } + + return NULL; +}