LibC: Add strcasestr()

strcasestr() behaves exactly like strstr(), except it ignores case for
both inputs. This function is a nonstandard extension.
This commit is contained in:
Julian Offenhäuser 2023-02-08 11:59:03 +01:00 committed by Linus Groh
parent 25c9dfbf90
commit 463ab21305
2 changed files with 20 additions and 0 deletions

View file

@ -359,6 +359,25 @@ char* strstr(char const* haystack, char const* needle)
return const_cast<char*>(haystack);
}
// https://linux.die.net/man/3/strcasestr
char* strcasestr(char const* haystack, char const* needle)
{
char nch;
char hch;
if ((nch = *needle++) != 0) {
size_t len = strlen(needle);
do {
do {
if ((hch = *haystack++) == 0)
return nullptr;
} while (toupper(hch) != toupper(nch));
} while (strncasecmp(haystack, needle, len) != 0);
--haystack;
}
return const_cast<char*>(haystack);
}
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/strpbrk.html
char* strpbrk(char const* s, char const* accept)
{

View file

@ -43,6 +43,7 @@ __attribute__((warn_unused_result)) size_t strlcpy(char* dest, char const* src,
char* strchr(char const*, int c);
char* strchrnul(char const*, int c);
char* strstr(char const* haystack, char const* needle);
char* strcasestr(char const* haystack, char const* needle);
char* strrchr(char const*, int c);
char* index(char const* str, int ch);