Commit c8bc05b1 authored by Benno Schulenberg's avatar Benno Schulenberg
Browse files

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.
parent ee57cbfa
Showing with 8 additions and 8 deletions
+8 -8
......@@ -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;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment