Debian Codegraph @

im/pinyin/customphrase.h

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 #ifndef _PINYIN_CUSTOMPHRASE_H_
8 #define _PINYIN_CUSTOMPHRASE_H_
9
10 #include <cstddef>
11 #include <cstdint>
12 #include <fcitx-utils/macros.h>
13 #include <functional>
14 #include <istream>
15 #include <libime/core/datrie.h>
16 #include <ostream>
17 #include <string>
18 #include <string_view>
19 #include <utility>
20 #include <vector>
21
22 namespace fcitx {
23
24 class CustomPhrase {
25 public:
26 explicit CustomPhrase(int order, std::string value)
27 : order_(order), value_(std::move(value)) {}
28 FCITX_INLINE_DEFINE_DEFAULT_DTOR_COPY_AND_MOVE_WITH_SPEC(CustomPhrase,
29 noexcept)
30
31 int order() const { return order_; }
32 const std::string &value() const { return value_; }
33 void setOrder(int order) { order_ = order; }
34 std::string &mutableValue() { return value_; }
35
36 bool isDynamic() const;
37
38 std::string evaluate(const std::function<std::string(std::string_view key)>
39 &evaluator) const;
40
41 static std::string builtinEvaluator(std::string_view key);
42
43 private:
44 int order_ = -1;
45 std::string value_;
46 };
47
48 class CustomPhraseDict {
49 public:
50 using TrieType = libime::DATrie<uint32_t>;
51 CustomPhraseDict();
52
53 void load(std::istream &in, bool loadDisabled = false);
54 void save(std::ostream &out) const;
55 void clear();
56
57 const std::vector<CustomPhrase> *lookup(std::string_view key) const;
58
59 void addPhrase(std::string_view key, std::string_view value, int order);
60 void pinPhrase(std::string_view key, std::string_view value);
61 void removePhrase(std::string_view key, std::string_view value);
62
63 template <typename T>
64 void foreach(const T &callback) {
65 std::string buf;
66 index_.foreach([this, &buf, &callback](uint32_t index, size_t len,
67 TrieType::position_type pos) {
68 index_.suffix(buf, len, pos);
69 callback(buf, data_[index]);
70 return true;
71 });
72 }
73
74 private:
75 std::vector<CustomPhrase> *getOrCreateEntry(std::string_view key);
76 TrieType index_;
77 std::vector<std::vector<CustomPhrase>> data_;
78 };
79
80 } // namespace fcitx
81
82 #endif // _PINYIN_SYMBOLDICTIONARY_H_