string: split strchr, strchrnul and strrchr (TEST)

This commit is contained in:
Lephenixnoir 2021-05-23 16:07:01 +02:00
parent 6021c536f7
commit bd344d5bb2
No known key found for this signature in database
GPG key ID: 1BBA026E13FC0495
5 changed files with 43 additions and 47 deletions

View file

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

16
STATUS
View file

@ -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 <tgmath.h> => GCC

View file

@ -1,40 +1,10 @@
#include <string.h>
/*
** 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;
}

View file

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

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

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