changeset 6454:7aaba1a7034c

Move IsDBCSLeadByte from GUI to StringHelpers to share between platforms. Use more complete implemementation from Scintilla.
author Neil <nyamatongwe@gmail.com>
date Sun, 13 Apr 2025 15:08:38 +1000
parents 00c34790f27f
children 8a288593b551
files gtk/GUIGTK.cxx src/GUI.h src/StringHelpers.cxx src/StringHelpers.h src/StyleWriter.cxx win32/GUIWin.cxx
diffstat 6 files changed, 38 insertions(+), 31 deletions(-) [+]
line wrap: on
line diff
--- a/gtk/GUIGTK.cxx	Sun Apr 13 09:35:52 2025 +1000
+++ b/gtk/GUIGTK.cxx	Sun Apr 13 15:08:38 2025 +1000
@@ -181,25 +181,6 @@
 	return scintilla_send_message(SCINTILLA(GetID()), msg, wParam, lParam);
 }
 
-bool IsDBCSLeadByte(int codePage, char ch) {
-	// Byte ranges found in Wikipedia articles with relevant search strings in each case
-	unsigned char uch = static_cast<unsigned char>(ch);
-	switch (codePage) {
-		case 932:
-			// Shift_jis
-			return ((uch >= 0x81) && (uch <= 0x9F)) ||
-				((uch >= 0xE0) && (uch <= 0xEF));
-		case 936:
-			// GBK
-			return (uch >= 0x81) && (uch <= 0xFE);
-		case 950:
-			// Big5
-			return (uch >= 0x81) && (uch <= 0xFE);
-		// Korean EUC-KR may be code page 949.
-	}
-	return false;
-}
-
 void SleepMilliseconds(int sleepTime) {
 	g_usleep(sleepTime * 1000);
 }
--- a/src/GUI.h	Sun Apr 13 09:35:52 2025 +1000
+++ b/src/GUI.h	Sun Apr 13 15:08:38 2025 +1000
@@ -152,8 +152,6 @@
 	intptr_t Send(unsigned int msg, uintptr_t wParam=0, intptr_t lParam=0);
 };
 
-bool IsDBCSLeadByte(int codePage, char ch);
-
 void SleepMilliseconds(int sleepTime);
 
 }
--- a/src/StringHelpers.cxx	Sun Apr 13 09:35:52 2025 +1000
+++ b/src/StringHelpers.cxx	Sun Apr 13 15:08:38 2025 +1000
@@ -323,6 +323,36 @@
 	return result;
 }
 
+bool IsDBCSLeadByte(int codePage, char ch) noexcept {
+	// Byte ranges found in Wikipedia articles with relevant search strings in each case
+	const unsigned char uch = ch;
+	switch (codePage) {
+	case 932:
+		// Shift_jis
+		return ((uch >= 0x81) && (uch <= 0x9F)) ||
+			((uch >= 0xE0) && (uch <= 0xFC));
+		// Lead bytes F0 to FC may be a Microsoft addition.
+	case 936:
+		// GBK
+		return (uch >= 0x81) && (uch <= 0xFE);
+	case 949:
+		// Korean Wansung KS C-5601-1987
+		return (uch >= 0x81) && (uch <= 0xFE);
+	case 950:
+		// Big5
+		return (uch >= 0x81) && (uch <= 0xFE);
+	case 1361:
+		// Korean Johab KS C-5601-1992
+		return
+			((uch >= 0x84) && (uch <= 0xD3)) ||
+			((uch >= 0xD8) && (uch <= 0xDE)) ||
+			((uch >= 0xE0) && (uch <= 0xF9));
+	default:
+		break;
+	}
+	return false;
+}
+
 /**
  * Convert a string into C string literal form using \a, \b, \f, \n, \r, \t, \v, and \ooo.
  */
--- a/src/StringHelpers.h	Sun Apr 13 09:35:52 2025 +1000
+++ b/src/StringHelpers.h	Sun Apr 13 15:08:38 2025 +1000
@@ -167,6 +167,10 @@
 unsigned int UTF32Character(std::string_view utf8) noexcept;
 std::string UTF8FromUTF32(unsigned int uch);
 
+// DBCS
+
+bool IsDBCSLeadByte(int codePage, char ch) noexcept;
+
 // Escape processing
 
 std::string Slash(const std::string &s, bool quoteQuotes);
--- a/src/StyleWriter.cxx	Sun Apr 13 09:35:52 2025 +1000
+++ b/src/StyleWriter.cxx	Sun Apr 13 15:08:38 2025 +1000
@@ -9,6 +9,8 @@
 
 #include <string>
 #include <string_view>
+#include <vector>
+#include <set>
 #include <chrono>
 
 #include "ScintillaTypes.h"
@@ -16,6 +18,7 @@
 #include "ScintillaStructures.h"
 
 #include "GUI.h"
+#include "StringHelpers.h"
 #include "StyleWriter.h"
 
 namespace SA = Scintilla;
@@ -30,7 +33,7 @@
 }
 
 bool TextReader::InternalIsLeadByte(char ch) const {
-	return GUI::IsDBCSLeadByte(codePage, ch);
+	return IsDBCSLeadByte(codePage, ch);
 }
 
 void TextReader::Fill(SA::Position position) {
--- a/win32/GUIWin.cxx	Sun Apr 13 09:35:52 2025 +1000
+++ b/win32/GUIWin.cxx	Sun Apr 13 15:08:38 2025 +1000
@@ -279,15 +279,6 @@
 	return ::SendMessage(static_cast<HWND>(GetID()), msg, wParam, lParam);
 }
 
-bool IsDBCSLeadByte(int codePage, char ch) {
-	if (Scintilla::CpUtf8 == codePage)
-		// For lexing, all characters >= 0x80 are treated the
-		// same so none is considered a lead byte.
-		return false;
-	else
-		return ::IsDBCSLeadByteEx(codePage, ch) != 0;
-}
-
 void SleepMilliseconds(int sleepTime) {
 	::Sleep(sleepTime);
 }