From 5d59fbd8aa141f97e1778e9cb1648fc71c1c0a82 Mon Sep 17 00:00:00 2001
From: divverent <divverent@d7cf8633-e32d-0410-b094-e92efae38249>
Date: Sat, 4 Sep 2010 15:32:06 +0000
Subject: [PATCH] Fix u8_COM_StringLengthNoColors not counting many ASCII chars
 (up to '?')

Better handling of invalid chars in u8_COM_StringLengthNoColors

git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@10438 d7cf8633-e32d-0410-b094-e92efae38249
::stable-branch::merge=f0eda58867febcd4dc0f6c6be401d37000d136c5
---
 utf8lib.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/utf8lib.c b/utf8lib.c
index 151a1ca5..3a2fabe7 100644
--- a/utf8lib.c
+++ b/utf8lib.c
@@ -682,6 +682,7 @@ u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
 	const unsigned char *s = (const unsigned char*)_s;
 	const unsigned char *end;
 	size_t len = 0;
+	size_t bits = 0;
 
 	if (!utf8_enable.integer)
 		return COM_StringLengthNoColors(_s, size_s, valid);
@@ -734,13 +735,14 @@ u8_COM_StringLengthNoColors(const char *_s, size_t size_s, qboolean *valid)
 		}
 
 		// start of a wide character
-		if (*s & 0xC0)
+		bits = utf8_lengths[*s];
+		if (bits >= 2)
 		{
-			for (++s; *s >= 0x80 && *s <= 0xC0; ++s);
+			for (++s; bits >= 1 && *s >= 0x80 && *s < 0xC0; ++s, --bits);
 			continue;
 		}
-		// part of a wide character, we ignore that one
-		if (*s <= 0xBF)
+		// part of a wide character or invalid character, we ignore that one
+		if (bits == 0)
 			--len;
 		++s;
 	}
-- 
2.39.5