chars: make searching case-insensitively some ten percent faster

It is quicker to do a handful of superfluous compares at the end of
each line than it is to compute and keep track of and compare the
remaining line length the whole time.

The typical line is some sixty characters long, the typical search
string ten characters -- with a shorter search string the speedup is
even higher: some fifteen percent.  Only when the string is longer
than half the average line length does searching become slower with
this new method.

All this for a UTF-8 locale.  For a C locale it makes no difference.
master
Benno Schulenberg 2016-08-05 17:16:37 +02:00
parent ee57cbfa66
commit c8bc05b10e
1 changed files with 8 additions and 8 deletions

View File

@ -553,20 +553,20 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n)
/* This function is equivalent to strcasestr(). */
char *nstrcasestr(const char *haystack, const char *needle)
{
size_t haystack_len, needle_len;
size_t needle_len;
assert(haystack != NULL && needle != NULL);
if (*needle == '\0')
return (char *)haystack;
haystack_len = strlen(haystack);
needle_len = strlen(needle);
for (; *haystack != '\0' && haystack_len >= needle_len; haystack++,
haystack_len--) {
while (*haystack != '\0') {
if (strncasecmp(haystack, needle, needle_len) == 0)
return (char *)haystack;
haystack++;
}
return NULL;
@ -578,20 +578,20 @@ char *mbstrcasestr(const char *haystack, const char *needle)
{
#ifdef ENABLE_UTF8
if (use_utf8) {
size_t haystack_len, needle_len;
size_t needle_len;
assert(haystack != NULL && needle != NULL);
if (*needle == '\0')
return (char *)haystack;
haystack_len = mbstrlen(haystack);
needle_len = mbstrlen(needle);
for (; *haystack != '\0' && haystack_len >= needle_len;
haystack += move_mbright(haystack, 0), haystack_len--) {
while (*haystack != '\0') {
if (mbstrncasecmp(haystack, needle, needle_len) == 0)
return (char *)haystack;
haystack += move_mbright(haystack, 0);
}
return NULL;