Files
vlibc/src/string/strchr.c
T

53 lines
1.0 KiB
C

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <string.h>
/*
* Return a pointer to the first occurrence of c (converted to char) in s,
* or NULL when absent. The terminating NUL is part of the string, so
* strchr(s, '\0') returns a pointer to it.
*/
char *
strchr(const char *s, int c) // NOLINT(bugprone-easily-swappable-parameters)
{
const char cc = (char)c;
for (;; s++)
{
if (*s == cc)
{
return (char *)s;
}
if (*s == '\0')
{
return NULL;
}
}
}
/*
* Return a pointer to the last occurrence of c (converted to char) in s, or
* NULL when absent. The terminating NUL is part of the string, so
* strrchr(s, '\0') returns a pointer to it.
*/
char *
strrchr(const char *s, int c) // NOLINT(bugprone-easily-swappable-parameters)
{
const char cc = (char)c;
const char *last = NULL;
for (;; s++)
{
if (*s == cc)
{
last = s;
}
if (*s == '\0')
{
return (char *)last;
}
}
}