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__) #if defined(PREFER_SIZE_OVER_SPEED) || defined(__OPTIMIZE_SIZE__)
char* strcasestr (const char* haystack, const char* needle) char* strcasestr (const char* haystack, const char* needle)
{ {
chartype needle_ch = tolower(*needle++); const char* match, *needle_search;
chartype needle_upch = toupper(needle_ch); chartype needle_ch, needle_upch, ch_h;
chartype ch_h;
char* match = (char*)haystack; /* if needle empty return haystack */
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 */ /* find the first matching character */
do { do
{
ch_h = *haystack++; ch_h = *haystack++;
if (ch_h == 0) /* end of haystack no match.. */ if (ch_h == 0) /* end of haystack no match.. */
return NULL; return NULL;
} while (ch_h != needle_ch && ch_h != needle_upch); } while (ch_h != needle_ch && ch_h != needle_upch);
match = haystack;
goto case_match;
match = (char*) haystack - 1;
/* find the first non-matching character */ /* find the first non-matching character */
lcase_match: while (ch_h == needle_ch)
while (ch_h == needle_ch) { {
ch_h = *haystack++; case_match:
needle_ch = tolower(*needle++); ch_h = *match++;
needle_ch = tolower(*needle_search++);
if (needle_ch == 0) /* end of needle, found match.. */ 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); needle_upch = toupper(needle_ch);
if (ch_h == needle_upch)
goto case_match;
haystack = match;
} }
match = NULL;
}
return match;
} }
#else #else
char* strcasestr (const char* phaystack, const char* pneedle) char* strcasestr (const char* phaystack, const char* pneedle)