Debian Codegraph @

data/dictionary/DictionaryCompatibilityIdeographsTest.cpp

raw symbols SCIP
1 /*
2 * Open Chinese Convert
3 *
4 * Validate that regular dictionaries do not contain Unicode CJK Compatibility
5 * Ideographs.
6 */
7
8 #include "gtest/gtest.h"
9
10 #include <cstdint>
11 #include <cstdio>
12 #include <fstream>
13 #include <memory>
14 #include <sstream>
15 #include <string>
16 #include <unordered_map>
17 #include <vector>
18
19 #include "src/UTF8Util.hpp"
20 #include "tools/cpp/runfiles/runfiles.h"
21
22 using bazel::tools::cpp::runfiles::Runfiles;
23
24 namespace opencc {
25 namespace {
26
27 #ifndef OPENCC_COMPATIBILITY_AUDIT_FILES
28 #error "OPENCC_COMPATIBILITY_AUDIT_FILES must be defined"
29 #endif
30
31 bool IsDictionaryEntry(const std::string& line) {
32 return !line.empty() && line[0] != '#';
33 }
34
35 std::vector<std::string> SplitCommaSeparated(const std::string& text) {
36 std::vector<std::string> parts;
37 size_t start = 0;
38 while (start <= text.size()) {
39 const size_t end = text.find(',', start);
40 const std::string part = text.substr(start, end - start);
41 if (!part.empty()) {
42 parts.push_back(part);
43 }
44 if (end == std::string::npos) {
45 break;
46 }
47 start = end + 1;
48 }
49 return parts;
50 }
51
52 uint32_t DecodeUtf8CodePoint(const char* str, size_t length) {
53 const unsigned char* bytes = reinterpret_cast<const unsigned char*>(str);
54 if (length == 1) {
55 return bytes[0];
56 }
57 if (length == 2) {
58 return ((bytes[0] & 0x1F) << 6) | (bytes[1] & 0x3F);
59 }
60 if (length == 3) {
61 return ((bytes[0] & 0x0F) << 12) | ((bytes[1] & 0x3F) << 6) |
62 (bytes[2] & 0x3F);
63 }
64 if (length == 4) {
65 return ((bytes[0] & 0x07) << 18) | ((bytes[1] & 0x3F) << 12) |
66 ((bytes[2] & 0x3F) << 6) | (bytes[3] & 0x3F);
67 }
68 return 0;
69 }
70
71 std::string FormatCodePoint(uint32_t codePoint) {
72 char buffer[11];
73 snprintf(buffer, sizeof(buffer), "U+%04X", codePoint);
74 return buffer;
75 }
76
77 bool IsCjkCompatibilityIdeograph(uint32_t codePoint) {
78 return (codePoint >= 0xF900 && codePoint <= 0xFAFF) ||
79 (codePoint >= 0x2F800 && codePoint <= 0x2FA1F);
80 }
81
82 std::unordered_map<std::string, std::string> LoadCompatibilityMappings(
83 const std::string& path) {
84 std::ifstream stream(UTF8Util::GetPlatformString(path));
85 EXPECT_TRUE(stream.is_open()) << path;
86
87 std::unordered_map<std::string, std::string> compatibilityMappings;
88 std::string line;
89 size_t lineNumber = 0;
90 while (std::getline(stream, line)) {
91 ++lineNumber;
92 if (!IsDictionaryEntry(line)) {
93 continue;
94 }
95 const size_t tab = line.find('\t');
96 if (tab == std::string::npos) {
97 ADD_FAILURE() << path << ":" << lineNumber
98 << " is missing a tab separator.";
99 continue;
100 }
101 compatibilityMappings.emplace(line.substr(0, tab), line.substr(tab + 1));
102 }
103 return compatibilityMappings;
104 }
105
106 void ExpectNoCompatibilityIdeographs(
107 const std::string& dictionaryName, const std::string& path,
108 const std::unordered_map<std::string, std::string>&
109 compatibilityMappings) {
110 std::ifstream stream(UTF8Util::GetPlatformString(path));
111 ASSERT_TRUE(stream.is_open()) << path;
112
113 std::string line;
114 size_t lineNumber = 0;
115 while (std::getline(stream, line)) {
116 ++lineNumber;
117 if (!IsDictionaryEntry(line)) {
118 continue;
119 }
120
121 for (const char* cursor = line.c_str(); *cursor != '\0';) {
122 const size_t length = UTF8Util::NextCharLength(cursor);
123 const uint32_t codePoint = DecodeUtf8CodePoint(cursor, length);
124 if (IsCjkCompatibilityIdeograph(codePoint)) {
125 const std::string character = UTF8Util::FromSubstr(cursor, length);
126 std::ostringstream message;
127 message << dictionaryName << ":" << lineNumber
128 << " contains CJK Compatibility Ideograph "
129 << FormatCodePoint(codePoint) << ". ";
130 const auto mapping = compatibilityMappings.find(character);
131 if (mapping == compatibilityMappings.end()) {
132 message << "No UnicodeData decomposition mapping is available; "
133 "replace it manually with the standard CJK unified "
134 "ideograph form.";
135 } else {
136 message << "Replace it with " << mapping->second << " "
137 << FormatCodePoint(DecodeUtf8CodePoint(
138 mapping->second.c_str(),
139 UTF8Util::NextCharLength(mapping->second.c_str())))
140 << ".";
141 }
142 ADD_FAILURE() << message.str();
143 }
144 cursor += length;
145 }
146 }
147 }
148
149 TEST(DictionaryCompatibilityIdeographsTest,
150 RegularDictionariesDoNotContainCompatibilityIdeographs) {
151 std::unique_ptr<Runfiles> runfiles(Runfiles::CreateForTest());
152 ASSERT_NE(nullptr, runfiles);
153
154 const std::string dictionaryDir = "_main/data/dictionary/";
155 const std::string compatibilityPath = runfiles->Rlocation(
156 dictionaryDir + "CJK_Compatibility_Ideographs.txt");
157 const std::unordered_map<std::string, std::string> compatibilityMappings =
158 LoadCompatibilityMappings(compatibilityPath);
159 ASSERT_EQ(1002, compatibilityMappings.size());
160
161 for (const std::string& dictionaryName :
162 SplitCommaSeparated(OPENCC_COMPATIBILITY_AUDIT_FILES)) {
163 const std::string dictionaryPath =
164 runfiles->Rlocation(dictionaryDir + dictionaryName);
165 ExpectNoCompatibilityIdeographs(dictionaryName, dictionaryPath,
166 compatibilityMappings);
167 }
168 }
169
170 } // namespace
171 } // namespace opencc