Debian Codegraph @

im/pinyin/symboldictionary.cpp

raw symbols SCIP
1 /*
2 * SPDX-FileCopyrightText: 2023-2023 CSSlayer <[email protected]>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-or-later
5 *
6 */
7 #include "symboldictionary.h"
8 #include <cstdint>
9 #include <fcitx-utils/macros.h>
10 #include <fcitx-utils/stringutils.h>
11 #include <istream>
12 #include <limits>
13 #include <optional>
14 #include <string_view>
15 #include <tuple>
16
17 namespace fcitx {
18
19 using ParseResult = std::tuple<std::string, std::string>;
20
21 std::optional<size_t> findEnclosedQuote(std::string_view str) {
22 enum class UnescapeState { NORMAL, ESCAPE };
23 auto state = UnescapeState::NORMAL;
24 for (size_t i = 1; i < str.size(); i++) {
25 switch (state) {
26 case UnescapeState::NORMAL:
27 if (str[i] == '\\') {
28 state = UnescapeState::ESCAPE;
29 } else if (str[i] == '\"') {
30 return i;
31 }
32 break;
33 case UnescapeState::ESCAPE:
34 if (str[i] == '\\' || str[i] == 'n' || str[i] == '\"') {
35 continue;
36 } else {
37 // Invalid escape.
38 return std::nullopt;
39 }
40 state = UnescapeState::NORMAL;
41 break;
42 }
43 }
44 return std::nullopt;
45 }
46
47 std::optional<ParseResult> parseSymbolLine(std::string_view line) {
48 line = stringutils::trimView(line);
49 if (line.empty()) {
50 return std::nullopt;
51 }
52 std::string_view key;
53 size_t valueStart = std::string_view::npos;
54 if (line[0] == '\"') {
55 auto quote = findEnclosedQuote(line);
56 if (!quote.has_value()) {
57 return std::nullopt;
58 }
59 auto end = *quote + 1;
60 key = line.substr(0, end);
61 valueStart = line.find_first_not_of(FCITX_WHITESPACE, end);
62 if (valueStart == end) {
63 return std::nullopt;
64 }
65 } else {
66 auto end = line.find_first_of(FCITX_WHITESPACE);
67 if (end == std::string_view::npos) {
68 return std::nullopt;
69 }
70 key = line.substr(0, end);
71 valueStart = line.find_first_not_of(FCITX_WHITESPACE, end);
72 }
73 if (valueStart == std::string_view::npos) {
74 return std::nullopt;
75 }
76 std::string_view value = line.substr(valueStart);
77 auto parsedKey = stringutils::unescapeForValue(key);
78 if (!parsedKey) {
79 return std::nullopt;
80 }
81 auto parsedValue = stringutils::unescapeForValue(value);
82 if (!parsedValue) {
83 return std::nullopt;
84 }
85 return std::make_tuple(std::move(*parsedKey), std::move(*parsedValue));
86 }
87
88 SymbolDict::SymbolDict() = default;
89
90 void SymbolDict::load(std::istream &in) {
91 clear();
92 std::string line;
93
94 while (std::getline(in, line)) {
95 auto parseResult = parseSymbolLine(line);
96 if (parseResult) {
97 auto [key, value] = *parseResult;
98 auto index = index_.exactMatchSearch(key);
99 if (TrieType::isNoValue(index)) {
100 if (data_.size() >= std::numeric_limits<int32_t>::max()) {
101 break;
102 }
103 index = data_.size();
104 index_.set(key, index);
105 data_.push_back({});
106 }
107 data_[index].push_back(value);
108 }
109 }
110 index_.shrink_tail();
111 data_.shrink_to_fit();
112 }
113
114 const std::vector<std::string> *SymbolDict::lookup(std::string_view key) const {
115 auto index = index_.exactMatchSearch(key);
116 if (TrieType::isNoValue(index)) {
117 return nullptr;
118 }
119
120 return &data_[index];
121 }
122
123 void SymbolDict::clear() {
124 index_.clear();
125 data_.clear();
126 }
127
128 } // namespace fcitx