string: add and test a naive memrchr (DONE)

This commit is contained in:
Lephenixnoir 2022-01-10 21:20:55 +01:00
parent 909c7df815
commit 0c2f81e5bb
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
4 changed files with 17 additions and 0 deletions

View file

@ -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

1
STATUS
View file

@ -200,6 +200,7 @@ TEST: Function/symbol/macro needs to be tested
(EXT) strncasecmp -
(EXT) strdup -
(EXT) strndup -
(EXT) memrchr -
7.22 <tgmath.h> => GCC

View file

@ -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);

12
src/libc/string/memrchr.c Normal file
View file

@ -0,0 +1,12 @@
#include <string.h>
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;
}