diff options
author | Zufu Liu <unknown> | 2021-07-03 12:59:39 +1000 |
---|---|---|
committer | Zufu Liu <unknown> | 2021-07-03 12:59:39 +1000 |
commit | 9cda372c64c8920d2e910825161a8ed882b417b3 (patch) | |
tree | ef5ed7a0110c22327c6bb09d3f6b481ddfa83284 | |
parent | 4a34a1f59a443403844c437d6d6a61c7a8019822 (diff) | |
download | scintilla-mirror-9cda372c64c8920d2e910825161a8ed882b417b3.tar.gz |
Feature [feature-requests:#1408] Use positive IsDBCSTrailByteNoExcept function
instead of negated IsDBCSTrailByteInvalid.
-rw-r--r-- | src/Document.cxx | 35 | ||||
-rw-r--r-- | src/Document.h | 1 | ||||
-rw-r--r-- | test/unit/testDocument.cxx | 12 |
3 files changed, 46 insertions, 2 deletions
diff --git a/src/Document.cxx b/src/Document.cxx index 876e95b4f..739b6266a 100644 --- a/src/Document.cxx +++ b/src/Document.cxx @@ -894,7 +894,7 @@ Document::CharacterExtracted Document::CharacterAfter(Sci::Position position) co } else { if (IsDBCSLeadByteNoExcept(leadByte)) { const unsigned char trailByte = cb.UCharAt(position + 1); - if (!IsDBCSTrailByteInvalid(trailByte)) { + if (IsDBCSTrailByteNoExcept(trailByte)) { return CharacterExtracted::DBCS(leadByte, trailByte); } } @@ -1010,7 +1010,7 @@ int SCI_METHOD Document::GetCharacterAndWidth(Sci_Position position, Sci_Positio } else { if (IsDBCSLeadByteNoExcept(leadByte)) { const unsigned char trailByte = cb.UCharAt(position + 1); - if (!IsDBCSTrailByteInvalid(trailByte)) { + if (IsDBCSTrailByteNoExcept(trailByte)) { bytesInCharacter = 2; character = (leadByte << 8) | trailByte; } else { @@ -1067,6 +1067,37 @@ bool Document::IsDBCSLeadByteNoExcept(char ch) const noexcept { return false; } +bool Document::IsDBCSTrailByteNoExcept(char ch) const noexcept { + const unsigned char trail = ch; + switch (dbcsCodePage) { + case 932: + // Shift_jis + return (trail != 0x7F) && + ((trail >= 0x40) && (trail <= 0xFC)); + case 936: + // GBK + return (trail != 0x7F) && + ((trail >= 0x40) && (trail <= 0xFE)); + case 949: + // Korean Wansung KS C-5601-1987 + return + ((trail >= 0x41) && (trail <= 0x5A)) || + ((trail >= 0x61) && (trail <= 0x7A)) || + ((trail >= 0x81) && (trail <= 0xFE)); + case 950: + // Big5 + return + ((trail >= 0x40) && (trail <= 0x7E)) || + ((trail >= 0xA1) && (trail <= 0xFE)); + case 1361: + // Korean Johab KS C-5601-1992 + return + ((trail >= 0x31) && (trail <= 0x7E)) || + ((trail >= 0x81) && (trail <= 0xFE)); + } + return false; +} + bool Document::IsDBCSLeadByteInvalid(char ch) const noexcept { const unsigned char lead = ch; switch (dbcsCodePage) { diff --git a/src/Document.h b/src/Document.h index 09d5841ed..88392c1a2 100644 --- a/src/Document.h +++ b/src/Document.h @@ -329,6 +329,7 @@ public: int SCI_METHOD CodePage() const override; bool SCI_METHOD IsDBCSLeadByte(char ch) const override; bool IsDBCSLeadByteNoExcept(char ch) const noexcept; + bool IsDBCSTrailByteNoExcept(char ch) const noexcept; bool IsDBCSLeadByteInvalid(char ch) const noexcept; bool IsDBCSTrailByteInvalid(char ch) const noexcept; int DBCSDrawBytes(std::string_view text) const noexcept; diff --git a/test/unit/testDocument.cxx b/test/unit/testDocument.cxx index cc6255caa..cecd14920 100644 --- a/test/unit/testDocument.cxx +++ b/test/unit/testDocument.cxx @@ -120,4 +120,16 @@ TEST_CASE("Document") { REQUIRE(width == 1); REQUIRE(ch == '='); } + + SECTION("CheckTrailBytes") { + Document doc(DocumentOption::Default); + const int pages[] = { 932, 936, 949, 950, 1361 }; + for (const int page : pages) { + doc.SetDBCSCodePage(page); + for (int byteVal = 0; byteVal < 0x100; byteVal++) { + char ch = static_cast<char>(byteVal); + REQUIRE(doc.IsDBCSTrailByteNoExcept(ch) != doc.IsDBCSTrailByteInvalid(ch)); + } + } + } } |