mirror of
https://github.com/Rockbox/rockbox.git
synced 2025-10-13 18:17:39 -04:00
strcasestr smaller function for PREFER_SIZE_OVER_SPEED
tester script: https://onlinegdb.com/TbILrA5E7 Change-Id: Ie5a37db13500510e30e87a20782f13adfe2b4d78
This commit is contained in:
parent
12aea7dae6
commit
afb0e845cf
1 changed files with 42 additions and 0 deletions
|
@ -33,7 +33,48 @@
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
typedef unsigned chartype;
|
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 */
|
||||||
|
|
||||||
|
if (needle_ch != 0) {
|
||||||
|
/* find the first matching character */
|
||||||
|
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 = (char*) haystack - 1;
|
||||||
|
/* find the first non-matching character */
|
||||||
|
lcase_match:
|
||||||
|
while (ch_h == needle_ch) {
|
||||||
|
ch_h = *haystack++;
|
||||||
|
needle_ch = tolower(*needle++);
|
||||||
|
if (needle_ch == 0) /* end of needle, found match.. */
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
|
return match;
|
||||||
|
}
|
||||||
|
#else
|
||||||
char* strcasestr (const char* phaystack, const char* pneedle)
|
char* strcasestr (const char* phaystack, const char* pneedle)
|
||||||
{
|
{
|
||||||
const unsigned char *haystack, *needle;
|
const unsigned char *haystack, *needle;
|
||||||
|
@ -120,3 +161,4 @@ char* strcasestr (const char* phaystack, const char* pneedle)
|
||||||
ret0:
|
ret0:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue