Debian Codegraph @

intl/icu/source/tools/genrb/filterrb.cpp

raw
1 // © 2018 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include <iostream>
5 #include <memory>
6 #include <stack>
7
8 #include "filterrb.h"
9 #include "errmsg.h"
10
11
12 const char* PathFilter::kEInclusionNames[] = {
13 "INCLUDE",
14 "PARTIAL",
15 "EXCLUDE"
16 };
17
18
19 ResKeyPath::ResKeyPath() {}
20
21 ResKeyPath::ResKeyPath(const std::string& path, UErrorCode& status) {
22 if (path.empty() || path[0] != '/') {
23 std::cerr << "genrb error: path must start with /: " << path << std::endl;
24 status = U_PARSE_ERROR;
25 return;
26 }
27 if (path.length() == 1) {
28 return;
29 }
30 size_t i;
31 size_t j = 0;
32 while (true) {
33 i = j + 1;
34 j = path.find('/', i);
35 std::string key = path.substr(i, j - i);
36 if (key.empty()) {
37 std::cerr << "genrb error: empty subpaths and trailing slashes are not allowed: " << path << std::endl;
38 status = U_PARSE_ERROR;
39 return;
40 }
41 push(key);
42 if (j == std::string::npos) {
43 break;
44 }
45 }
46 }
47
48 void ResKeyPath::push(const std::string& key) {
49 fPath.push_back(key);
50 }
51
52 void ResKeyPath::pop() {
53 fPath.pop_back();
54 }
55
56 const std::list<std::string>& ResKeyPath::pieces() const {
57 return fPath;
58 }
59
60 std::ostream& operator<<(std::ostream& out, const ResKeyPath& value) {
61 if (value.pieces().empty()) {
62 out << "/";
63 } else for (const auto& key : value.pieces()) {
64 out << "/" << key;
65 }
66 return out;
67 }
68
69
70 PathFilter::~PathFilter() = default;
71
72
73 void SimpleRuleBasedPathFilter::addRule(const std::string& ruleLine, UErrorCode& status) {
74 if (ruleLine.empty()) {
75 std::cerr << "genrb error: empty filter rules are not allowed" << std::endl;
76 status = U_PARSE_ERROR;
77 return;
78 }
79 bool inclusionRule = false;
80 if (ruleLine[0] == '+') {
81 inclusionRule = true;
82 } else if (ruleLine[0] != '-') {
83 std::cerr << "genrb error: rules must start with + or -: " << ruleLine << std::endl;
84 status = U_PARSE_ERROR;
85 return;
86 }
87 ResKeyPath path(ruleLine.substr(1), status);
88 addRule(path, inclusionRule, status);
89 }
90
91 void SimpleRuleBasedPathFilter::addRule(const ResKeyPath& path, bool inclusionRule, UErrorCode& status) {
92 if (U_FAILURE(status)) {
93 return;
94 }
95 fRoot.applyRule(path, path.pieces().begin(), inclusionRule, status);
96 }
97
98 PathFilter::EInclusion SimpleRuleBasedPathFilter::match(const ResKeyPath& path) const {
99 const Tree* node = &fRoot;
100
101 // defaultResult "bubbles up" the nearest "definite" inclusion/exclusion rule
102 EInclusion defaultResult = INCLUDE;
103 if (node->fIncluded != PARTIAL) {
104 // rules handled here: "+/" and "-/"
105 defaultResult = node->fIncluded;
106 }
107
108 // isLeaf is whether the filter tree can provide no additional information
109 // even if additional subpaths are added to the given key
110 bool isLeaf = false;
111
112 for (const auto& key : path.pieces()) {
113 auto child = node->fChildren.find(key);
114 // Leaf case 1: input path descends outside the filter tree
115 if (child == node->fChildren.end()) {
116 if (node->fWildcard) {
117 // A wildcard pattern is present; continue checking
118 node = node->fWildcard.get();
119 } else {
120 isLeaf = true;
121 break;
122 }
123 } else {
124 node = &child->second;
125 }
126 if (node->fIncluded != PARTIAL) {
127 defaultResult = node->fIncluded;
128 }
129 }
130
131 // Leaf case 2: input path exactly matches a filter leaf
132 if (node->isLeaf()) {
133 isLeaf = true;
134 }
135
136 // Always return PARTIAL if we are not at a leaf
137 if (!isLeaf) {
138 return PARTIAL;
139 }
140
141 // If leaf node is PARTIAL, return the default
142 if (node->fIncluded == PARTIAL) {
143 return defaultResult;
144 }
145
146 return node->fIncluded;
147 }
148
149
150 SimpleRuleBasedPathFilter::Tree::Tree(const Tree& other)
151 : fIncluded(other.fIncluded), fChildren(other.fChildren) {
152 // Note: can't use the default copy assignment because of the std::unique_ptr
153 if (other.fWildcard) {
154 fWildcard = std::make_unique<Tree>(*other.fWildcard);
155 }
156 }
157
158 bool SimpleRuleBasedPathFilter::Tree::isLeaf() const {
159 return fChildren.empty() && !fWildcard;
160 }
161
162 void SimpleRuleBasedPathFilter::Tree::applyRule(
163 const ResKeyPath& path,
164 std::list<std::string>::const_iterator it,
165 bool inclusionRule,
166 UErrorCode& status) {
167
168 // Base Case
169 if (it == path.pieces().end()) {
170 if (isVerbose() && (fIncluded != PARTIAL || !isLeaf())) {
171 std::cout << "genrb info: rule on path " << path
172 << " overrides previous rules" << std::endl;
173 }
174 fIncluded = inclusionRule ? INCLUDE : EXCLUDE;
175 fChildren.clear();
176 fWildcard.reset();
177 return;
178 }
179
180 // Recursive Step
181 const auto& key = *it;
182 if (key == "*") {
183 // Case 1: Wildcard
184 if (!fWildcard) {
185 fWildcard = std::make_unique<Tree>();
186 }
187 // Apply the rule to fWildcard and also to all existing children.
188 it++;
189 fWildcard->applyRule(path, it, inclusionRule, status);
190 for (auto& child : fChildren) {
191 child.second.applyRule(path, it, inclusionRule, status);
192 }
193 it--;
194
195 } else {
196 // Case 2: Normal Key
197 auto search = fChildren.find(key);
198 if (search == fChildren.end()) {
199 if (fWildcard) {
200 // Deep-copy the existing wildcard tree into the new key
201 search = fChildren.emplace(key, Tree(*fWildcard)).first;
202 } else {
203 search = fChildren.emplace(key, Tree()).first;
204 }
205 }
206 it++;
207 search->second.applyRule(path, it, inclusionRule, status);
208 it--;
209 }
210 }
211
212 void SimpleRuleBasedPathFilter::Tree::print(std::ostream& out, int32_t indent) const {
213 for (int32_t i=0; i<indent; i++) out << "\t";
214 out << "included: " << kEInclusionNames[fIncluded] << std::endl;
215 for (const auto& child : fChildren) {
216 for (int32_t i=0; i<indent; i++) out << "\t";
217 out << child.first << ": {" << std::endl;
218 child.second.print(out, indent + 1);
219 for (int32_t i=0; i<indent; i++) out << "\t";
220 out << "}" << std::endl;
221 }
222 if (fWildcard) {
223 for (int32_t i=0; i<indent; i++) out << "\t";
224 out << "* {" << std::endl;
225 fWildcard->print(out, indent + 1);
226 for (int32_t i=0; i<indent; i++) out << "\t";
227 out << "}" << std::endl;
228 }
229 }
230
231 void SimpleRuleBasedPathFilter::print(std::ostream& out) const {
232 out << "SimpleRuleBasedPathFilter {" << std::endl;
233 fRoot.print(out, 1);
234 out << "}" << std::endl;
235 }
236
237 std::ostream& operator<<(std::ostream& out, const SimpleRuleBasedPathFilter& value) {
238 value.print(out);
239 return out;
240 }