From fae361ba0231d188c3f6ca690762b3329a75c6ab Mon Sep 17 00:00:00 2001 From: halx99 Date: Wed, 7 Jul 2021 22:35:30 +0800 Subject: [PATCH] detectNonAsciiUTF8 Don't regard null character as ascii --- cocos/base/ccUTF8.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/cocos/base/ccUTF8.cpp b/cocos/base/ccUTF8.cpp index 1c7e6f9eed..f78dc4b896 100644 --- a/cocos/base/ccUTF8.cpp +++ b/cocos/base/ccUTF8.cpp @@ -362,15 +362,17 @@ bool detectNonAsciiUTF8(const char* str, size_t len, bool* pAllCharsAreAscii) { bool allCharsAreAscii = true; bool nonAsciiUTF8Found = false; for (size_t i = 0; i < len;) { - int numByte = getNumBytesForUTF8(str[i]); - if (isLegalUTF8Sequence((const UTF8*) &str[i], (const UTF8*) &str[i] + numByte)) { - if (numByte > 1) { + auto& current = str[i]; + int numByte = getNumBytesForUTF8(current); + if (numByte > 1) { + if (isLegalUTF8Sequence((const UTF8*) ¤t, (const UTF8*) ¤t + numByte)) { nonAsciiUTF8Found = true; - allCharsAreAscii = false; break; } - } else { // not a valid utf-8 chars allCharsAreAscii = false; + } else { // not a valid utf-8 chars + if ((current & 0x80) != 0 || current == 0) + allCharsAreAscii = false; } i += numByte; }