From a5b3f00d78b2c9e38fe5dc5f42f11019dc5c121b Mon Sep 17 00:00:00 2001
From: Benno Schulenberg <bensberg@justemail.net>
Date: Tue, 24 May 2016 21:45:22 +0200
Subject: [PATCH] chars: make comparing multibyte strings twice as fast

Instead of parsing every multibyte character twice, first with
parse_mbchar() and then with mbtowc(), just let mbtowc() do all
the work.  This makes searching for a fixed string twice as fast.

This also gets rid of four variables and lots of memory allocations.
(And, more importantly: it stops nano messing up the internal state
of the multibyte-to-wide character conversion, and thus would make
the calls to mbtowc_reset() superfluous.)
---
 src/chars.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

diff --git a/src/chars.c b/src/chars.c
index 91a395c3..dc46d49d 100644
--- a/src/chars.c
+++ b/src/chars.c
@@ -542,7 +542,6 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n)
 {
 #ifdef ENABLE_UTF8
     if (use_utf8) {
-	char *mbchar1, *mbchar2;
 	wchar_t wc1, wc2;
 
 	if (s1 == s2)
@@ -550,25 +549,19 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n)
 
 	assert(s1 != NULL && s2 != NULL);
 
-	mbchar1 = charalloc(MB_CUR_MAX);
-	mbchar2 = charalloc(MB_CUR_MAX);
-
 	for (; *s1 != '\0' && *s2 != '\0' && n > 0;
 		s1 += move_mbright(s1, 0), s2 += move_mbright(s2, 0), n--) {
 	    bool bad1 = FALSE, bad2 = FALSE;
 
-	    int len1 = parse_mbchar(s1, mbchar1, NULL);
-	    int len2 = parse_mbchar(s2, mbchar2, NULL);
-
-	    if (mbtowc(&wc1, mbchar1, len1) < 0) {
+	    if (mbtowc(&wc1, s1, MB_CUR_MAX) < 0) {
 		mbtowc_reset();
-		wc1 = (unsigned char)*mbchar1;
+		wc1 = (unsigned char)*s1;
 		bad1 = TRUE;
 	    }
 
-	    if (mbtowc(&wc2, mbchar2, len2) < 0) {
+	    if (mbtowc(&wc2, s2, MB_CUR_MAX) < 0) {
 		mbtowc_reset();
-		wc2 = (unsigned char)*mbchar2;
+		wc2 = (unsigned char)*s2;
 		bad2 = TRUE;
 	    }
 
@@ -576,9 +569,6 @@ int mbstrncasecmp(const char *s1, const char *s2, size_t n)
 		break;
 	}
 
-	free(mbchar1);
-	free(mbchar2);
-
 	return (n > 0) ? towlower(wc1) - towlower(wc2) : 0;
     } else
 #endif
-- 
GitLab