strcasestr smaller function for PREFER_SIZE_OVER_SPEED

tester script:
https://onlinegdb.com/TbILrA5E7

Change-Id: Ie5a37db13500510e30e87a20782f13adfe2b4d78
This commit is contained in:
William Wilgus 2024-11-28 10:03:50 -05:00
parent 12aea7dae6
commit afb0e845cf

View file

@ -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