summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorWilliam Wilgus <wilgus.william@gmail.com>2024-12-30 15:51:20 -0500
committerWilliam Wilgus <me.theuser@yahoo.com>2024-12-30 16:42:55 -0500
commit27aff7ec8dd8be78dd0fa8a87b836605958185d5 (patch)
tree89489e2e616a19494971ee1e8a58e767910aeb6d
parent6f542b6540e95232849befab6494d79b1bb808ee (diff)
downloadrockbox-27aff7ec8d.tar.gz
rockbox-27aff7ec8d.zip
strcasestr optimize for speed and size
revisit this and shave a bit more off on code size Change-Id: I90546b931780c6779960129619a41cc6592c44e1
-rw-r--r--firmware/common/strcasestr.c48
1 files changed, 23 insertions, 25 deletions
diff --git a/firmware/common/strcasestr.c b/firmware/common/strcasestr.c
index a20951d877..44269b58ee 100644
--- a/firmware/common/strcasestr.c
+++ b/firmware/common/strcasestr.c
@@ -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)