Debian Codegraph @

data/dictionary/DictionaryNonBmpTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Validate non-BMP characters in phrase and variant dictionaries.
5 */
6
7 #include "gtest/gtest.h"
8
9 #include <array>
10 #include <cstdint>
11 #include <cstdio>
12 #include <fstream>
13 #include <string>
14
15 #include "src/UTF8Util.hpp"
16 #include "tools/cpp/runfiles/runfiles.h"
17
18 using bazel::tools::cpp::runfiles::Runfiles;
19
20 namespace opencc {
21 namespace {
22
23 #ifndef OPENCC_DICTIONARY_NON_BMP_TEST_FILE
24 #error "OPENCC_DICTIONARY_NON_BMP_TEST_FILE must be defined"
25 #endif
26
27 struct NonBmpException {
28 const char* dictionary;
29 const char* character;
30 };
31
32 constexpr NonBmpException kAllowedNonBmpCharacters[] = {
33 {"STPhrases", "\xF0\xAB\x96\xAE"}, // U+2B5AE, 𫖮
34 {"STPhrases", "\xF0\xAB\x97\xA7"}, // U+2B5E7, 𫗧
35 {"STPhrases", "\xF0\xAB\x9B\xAD"}, // U+2B6ED, 𫛭
36 {"STPhrases", "\xF0\xAC\xB4\x83"}, // U+2CD03, 𬴃
37 {"STPhrases", "\xF0\xA3\x97\x8A"}, // U+235CA, 𣗊
38 {"STPhrases", "\xF0\xA3\xB2\x98"}, // U+23C98, 𣲘
39 {"TSPhrases", "\xF0\xAB\xAB\x87"}, // U+2BAC7, 𫫇
40 {"STPhrases_GeneratedFromRegionalPhrases", "\xF0\xAB\xAB\x87"}, // U+2BAC7, 𫫇
41 };
42
43 uint32_t DecodeUtf8CodePoint(const char* str, size_t length) {
44 const unsigned char* bytes = reinterpret_cast<const unsigned char*>(str);
45 if (length == 1) {
46 return bytes[0];
47 }
48 if (length == 2) {
49 return ((bytes[0] & 0x1F) << 6) | (bytes[1] & 0x3F);
50 }
51 if (length == 3) {
52 return ((bytes[0] & 0x0F) << 12) | ((bytes[1] & 0x3F) << 6) |
53 (bytes[2] & 0x3F);
54 }
55 if (length == 4) {
56 return ((bytes[0] & 0x07) << 18) | ((bytes[1] & 0x3F) << 12) |
57 ((bytes[2] & 0x3F) << 6) | (bytes[3] & 0x3F);
58 }
59 return 0;
60 }
61
62 std::string FormatCodePoint(uint32_t codePoint) {
63 char buffer[11];
64 snprintf(buffer, sizeof(buffer), "U+%04X", codePoint);
65 return buffer;
66 }
67
68 bool IsAllowedNonBmpCharacter(const std::string& dictionary,
69 const std::string& character) {
70 for (const auto& exception : kAllowedNonBmpCharacters) {
71 if (dictionary == exception.dictionary && character == exception.character) {
72 return true;
73 }
74 }
75 return false;
76 }
77
78 TEST(DictionaryNonBmpTest, OnlyAllowlistedCharactersOutsideCharacterDicts) {
79 std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
80 ASSERT_NE(nullptr, runfiles);
81
82 std::string dictionaryName = OPENCC_DICTIONARY_NON_BMP_TEST_FILE;
83 const std::string suffix = ".txt";
84 ASSERT_TRUE(dictionaryName.size() >= suffix.size());
85 ASSERT_EQ(suffix, dictionaryName.substr(dictionaryName.size() - suffix.size()));
86 dictionaryName.erase(dictionaryName.size() - suffix.size());
87
88 const std::string dictionaryFileName = runfiles->Rlocation(
89 "_main/data/dictionary/" +
90 std::string(OPENCC_DICTIONARY_NON_BMP_TEST_FILE));
91 std::ifstream ifs(UTF8Util::GetPlatformString(dictionaryFileName));
92 ASSERT_TRUE(ifs.is_open()) << dictionaryFileName;
93
94 std::string line;
95 size_t lineNumber = 0;
96 while (std::getline(ifs, line)) {
97 ++lineNumber;
98 for (const char* cursor = line.c_str(); *cursor != '\0';) {
99 const size_t length = UTF8Util::NextCharLength(cursor);
100 const uint32_t codePoint = DecodeUtf8CodePoint(cursor, length);
101 if (codePoint > 0xFFFF) {
102 const std::string character(cursor, length);
103 EXPECT_TRUE(IsAllowedNonBmpCharacter(dictionaryName, character))
104 << dictionaryName << ":" << lineNumber
105 << " contains non-BMP character " << FormatCodePoint(codePoint)
106 << ". Add it to kAllowedNonBmpCharacters only if it is intentional.";
107 }
108 cursor += length;
109 }
110 }
111 }
112
113 } // namespace
114 } // namespace opencc