tweaks: use the logic from revstrstr() also in revstrcasestr()
This elides a counter and a comparison from the central loop, and thus makes the search a tiny bit faster.master
parent
a37435141a
commit
6967fae35d
16
src/chars.c
16
src/chars.c
|
@ -507,22 +507,22 @@ char *revstrstr(const char *haystack, const char *needle,
|
||||||
char *revstrcasestr(const char *haystack, const char *needle,
|
char *revstrcasestr(const char *haystack, const char *needle,
|
||||||
const char *index)
|
const char *index)
|
||||||
{
|
{
|
||||||
size_t tail_len, needle_len;
|
size_t needle_len = strlen(needle);
|
||||||
|
size_t tail_len = strlen(index);
|
||||||
|
|
||||||
if (*needle == '\0')
|
if (needle_len == 0)
|
||||||
return (char *)index;
|
return (char *)index;
|
||||||
|
|
||||||
needle_len = strlen(needle);
|
|
||||||
|
|
||||||
if (strlen(haystack) < needle_len)
|
if (strlen(haystack) < needle_len)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
tail_len = strlen(index);
|
if (tail_len < needle_len)
|
||||||
|
index += tail_len - needle_len;
|
||||||
|
|
||||||
for (; index >= haystack; index--, tail_len++) {
|
while (index >= haystack) {
|
||||||
if (tail_len >= needle_len &&
|
if (strncasecmp(index, needle, needle_len) == 0)
|
||||||
strncasecmp(index, needle, needle_len) == 0)
|
|
||||||
return (char *)index;
|
return (char *)index;
|
||||||
|
index--;
|
||||||
}
|
}
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
Loading…
Reference in New Issue