From c8bc05b10e27b11a885b9a76fb91edce7dd4c737 Mon Sep 17 00:00:00 2001 From: Benno Schulenberg <bensberg@justemail.net> Date: Fri, 5 Aug 2016 17:16:37 +0200 Subject: [PATCH] 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. --- src/chars.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/chars.c b/src/chars.c index 80904213..a78c9c8f 100644 --- a/src/chars.c +++ b/src/chars.c @@ -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; -- GitLab