1
0
Fork 0
forked from len0rd/rockbox

strcasestr optimize for speed and size

revisit this and shave a bit more off on code size

Change-Id: I90546b931780c6779960129619a41cc6592c44e1
This commit is contained in:
William Wilgus 2024-12-30 15:51:20 -05:00 committed by William Wilgus
parent 6f542b6540
commit 27aff7ec8d

View file

@ -36,43 +36,41 @@ typedef unsigned chartype;
#if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
char* strcasestr (const char* haystack, const char* needle)
{
chartype needle_ch = tolower(*needle++);
chartype needle_upch = toupper(needle_ch);
chartype ch_h;
char* match = (char*)haystack; /* if needle empty return haystack */
const char* match, *needle_search;
chartype needle_ch, needle_upch, ch_h;
if (needle_ch != 0) {
if (*needle == 0) /* if needle empty return haystack */
return (char*)haystack;
while(1)
{
needle_search = needle;
needle_ch = tolower(*needle_search++);
needle_upch = toupper(needle_ch);
/* find the first matching character */
do {
do
{
ch_h = *haystack++;
if (ch_h == 0) /* end of haystack no match.. */
return NULL;
} while (ch_h != needle_ch && ch_h != needle_upch);
match = haystack;
goto case_match;
match = (char*) haystack - 1;
/* find the first non-matching character */
lcase_match:
while (ch_h == needle_ch) {
ch_h = *haystack++;
needle_ch = tolower(*needle++);
while (ch_h == needle_ch)
{
case_match:
ch_h = *match++;
needle_ch = tolower(*needle_search++);
if (needle_ch == 0) /* end of needle, found match.. */
return match;
return (char*)haystack - 1;
}
/* lcase or ucase match */
needle_upch = toupper(needle_ch);
while (ch_h == needle_upch || ch_h == needle_ch) {
ch_h = *haystack++;
needle_ch = tolower(*needle++);
if (needle_ch == 0) /* end of needle, found match.. */
return match;
if (ch_h == needle_ch)
goto lcase_match;
needle_upch = toupper(needle_ch);
}
match = NULL;
if (ch_h == needle_upch)
goto case_match;
haystack = match;
}
return match;
}
#else
char* strcasestr (const char* phaystack, const char* pneedle)