Open Chinese Convert: DictEntry.hpp Source File

Open Chinese Convert 1.3.2.dirty A project for conversion between Traditional and Simplified Chinese

Loading...
Searching...
No Matches
DictEntry.hpp
1/*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2020 Carbo Kuo <byvoid@byvoid.com>
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19#pragma once
20
21#include <string_view>
22
23#include "Common.hpp"
24#include "Segments.hpp"
25#include "UTF8Util.hpp"
26
27namespace opencc {
32 class OPENCC_EXPORT DictEntry {
33public:
34 virtual ~DictEntry() {}
35
36 virtual std::string_view KeyView() const = 0;
37
38 virtual std::string_view GetDefaultView() const = 0;
39
40 virtual std::string Key() const { return std::string(KeyView()); }
41
42 virtual std::vector<std::string> Values() const = 0;
43
44 virtual std::string GetDefault() const { return std::string(GetDefaultView()); }
45
46 virtual size_t NumValues() const = 0;
47
48 virtual std::string ToString() const = 0;
49
50 size_t KeyLength() const { return KeyView().length(); }
51
52 bool operator<(const DictEntry& that) const { return KeyView() < that.KeyView(); }
53
54 bool operator==(const DictEntry& that) const { return KeyView() == that.KeyView(); }
55
56 static bool UPtrLessThan(const std::unique_ptr<DictEntry>& a,
57 const std::unique_ptr<DictEntry>& b) {
58 return *a < *b;
59 }
60};
61
62 class OPENCC_EXPORT NoValueDictEntry : public DictEntry {
63public:
64 NoValueDictEntry(const std::string& _key) : key(_key) {}
65
66 virtual ~NoValueDictEntry() {}
67
68 std::string Key() const override { return key; }
69 std::string_view KeyView() const override { return key; }
70 std::string GetDefault() const override { return key; }
71 std::string_view GetDefaultView() const override { return key; }
72
73 std::vector<std::string> Values() const override {
74 return std::vector<std::string>();
75 }
76
77 size_t NumValues() const override { return 0; }
78
79 std::string ToString() const override { return key; }
80
81private:
82 std::string key;
83};
84
85 class OPENCC_EXPORT SingleValueDictEntry : public DictEntry {
86public:
87 virtual std::string Value() const = 0;
88
89 virtual std::string_view ValueView() const = 0;
90
91 std::string_view GetDefaultView() const override { return ValueView(); }
92
93 std::vector<std::string> Values() const override {
94 return std::vector<std::string>{Value()};
95 }
96
97 size_t NumValues() const override { return 1; }
98
99 std::string ToString() const override {
100 return std::string(KeyView()) + "\t" + Value();
101 }
102};
103
104 class OPENCC_EXPORT StrSingleValueDictEntry : public SingleValueDictEntry {
105public:
106 StrSingleValueDictEntry(const std::string& _key, const std::string& _value)
107 : key(_key), value(_value) {}
108
109 virtual ~StrSingleValueDictEntry() {}
110
111 std::string Key() const override { return key; }
112 std::string_view KeyView() const override { return key; }
113
114 std::string Value() const override { return value; }
115 std::string GetDefault() const override { return value; }
116 std::string_view ValueView() const override { return value; }
117
118private:
119 std::string key;
120 std::string value;
121};
122
123 class OPENCC_EXPORT MultiValueDictEntry : public DictEntry {
124public:
125 virtual std::string ToString() const;
126};
127
128 class OPENCC_EXPORT StrMultiValueDictEntry : public MultiValueDictEntry {
129public:
130 StrMultiValueDictEntry(const std::string& _key,
131 const std::vector<std::string>& _values)
132 : key(_key), values(_values) {}
133
134 virtual ~StrMultiValueDictEntry() {}
135
136 std::string Key() const override { return key; }
137 std::string_view KeyView() const override { return key; }
138
139 std::string GetDefault() const override {
140 return values.empty() ? key : values[0];
141 }
142 std::string_view GetDefaultView() const override {
143 return values.empty() ? std::string_view(key) : std::string_view(values[0]);
144 }
145
146 size_t NumValues() const override { return values.size(); }
147
148 std::vector<std::string> Values() const override { return values; }
149
150private:
151 std::string key;
152 std::vector<std::string> values;
153};
154
155 class OPENCC_EXPORT DictEntryFactory {
156public:
157 static DictEntry* New(const std::string& key) {
158 return new NoValueDictEntry(key);
159 }
160
161 static DictEntry* New(const std::string& key, const std::string& value) {
162 return new StrSingleValueDictEntry(key, value);
163 }
164
165 static DictEntry* New(const std::string& key,
166 const std::vector<std::string>& values) {
167 if (values.size() == 0) {
168 return New(key);
169 } else if (values.size() == 1) {
170 return New(key, values.front());
171 }
172 return new StrMultiValueDictEntry(key, values);
173 }
174
175 static DictEntry* New(const DictEntry* entry) {
176 if (entry->NumValues() == 0) {
177 return new NoValueDictEntry(entry->Key());
178 } else if (entry->NumValues() == 1) {
179 return new StrSingleValueDictEntry(entry->Key(), entry->Values().front());
180 } else {
181 return new StrMultiValueDictEntry(entry->Key(), entry->Values());
182 }
183 }
184};
185} // namespace opencc
opencc::DictEntryFactory
Definition DictEntry.hpp:155
opencc::DictEntry
Key-values pair entry.
Definition DictEntry.hpp:32
opencc::MultiValueDictEntry
Definition DictEntry.hpp:123
opencc::NoValueDictEntry
Definition DictEntry.hpp:62
opencc::SingleValueDictEntry
Definition DictEntry.hpp:85
opencc::StrMultiValueDictEntry
Definition DictEntry.hpp:128
opencc::StrSingleValueDictEntry
Definition DictEntry.hpp:104