std/string: add strchr(), strrchr(), and strchrnul()

This commit is contained in:
Lephe 2021-02-15 16:43:42 +01:00
parent 553982a445
commit cb2d067967
Signed by: Lephenixnoir
GPG Key ID: 1BBA026E13FC0495
4 changed files with 40 additions and 0 deletions

View File

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

View File

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

12
src/std/string-ext.c Normal file
View File

@ -0,0 +1,12 @@
//---
// gint:std:string-ext: Extensions to the standard for <string.h>
//---
#include <gint/defs/attributes.h>
#include <gint/std/string.h>
GWEAK char *strchrnul(char const *s, int c)
{
while(*s && *s != c) s++;
return (char *)s;
}

View File

@ -4,6 +4,7 @@
#include <gint/defs/types.h>
#include <gint/defs/attributes.h>
#include <gint/std/string.h>
#include <stdarg.h>
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;
}