Flutter Engine Uber Docs
Docs for the entire Flutter Engine repo.
 
Loading...
Searching...
No Matches
catalog.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
6
7#include <algorithm>
8#include <fstream>
9#include <vector>
10
11#include "third_party/abseil-cpp/absl/log/log.h"
12#include "third_party/abseil-cpp/absl/strings/str_cat.h"
13
14namespace fs = std::filesystem;
15
16namespace {
17bool Overlaps(std::string_view a, std::string_view b) {
18 const char* const start1 = a.data();
19 const char* const end1 = start1 + a.size();
20 const char* const start2 = b.data();
21 const char* const end2 = start2 + b.size();
22
23 return start1 < end2 && start2 < end1;
24}
25
26bool EndsWith(std::string_view str, std::string_view suffix) {
27 if (suffix.length() > str.length()) {
28 return false;
29 }
30 return str.substr(str.length() - suffix.length()) == suffix;
31}
32
33std::string IgnoreWhitespace(std::string_view input) {
34 bool in_whitespace = false;
35 std::string result = "";
36 for (size_t i = 0; i < input.size(); ++i) {
37 char current = input[i];
38 if (std::isspace(current)) {
39 if (!in_whitespace) {
40 result.append("\\s+");
41 }
42 in_whitespace = true;
43 } else {
44 result.push_back(current);
45 in_whitespace = false;
46 }
47 }
48 if (EndsWith(result, "\\s+")) {
49 result.erase(result.end() - 3, result.end());
50 }
51 return result;
52}
53
54std::optional<Catalog::Match> FindMatchForSelectedMatcher(
55 std::string_view query,
56 RE2* matcher,
57 std::string_view matcher_name) {
58 int num_groups = matcher->NumberOfCapturingGroups();
59
60 if (num_groups == 0) {
61 std::string_view match_text;
62 if (matcher->Match(query, 0, query.length(), RE2::Anchor::UNANCHORED,
63 &match_text,
64 /*nsubmatch=*/1)) {
65 return Catalog::Match::MakeWithView(matcher_name, match_text);
66 }
67 } else {
68 // This will extract all non-grouped text from a match.
69 std::vector<re2::StringPiece> submatches(num_groups + 1);
70 if (matcher->Match(query, 0, query.length(), RE2::Anchor::UNANCHORED,
71 submatches.data(), num_groups + 1)) {
72 std::string_view full_match = submatches[0];
73 const char* full_match_end = full_match.data() + full_match.size();
74
75 std::string non_group_text;
76 non_group_text.reserve(full_match.size());
77 const char* position = full_match.data();
78 for (int i = 1; i <= num_groups; ++i) {
79 std::string_view submatch = submatches[i];
80 if (submatch.data() == nullptr) {
81 continue;
82 }
83 if (submatch.data() > position) {
84 non_group_text.append(position, submatch.data() - position);
85 }
86 position = submatch.data() + submatch.size();
87 }
88 if (position != nullptr && position < full_match_end) {
89 non_group_text.append(position, full_match_end - position);
90 }
91
92 return Catalog::Match::MakeWithString(matcher_name,
93 std::move(non_group_text));
94 }
95 }
96
97 return std::nullopt;
98}
99} // namespace
100
101absl::StatusOr<Catalog> Catalog::Open(std::string_view data_dir) {
102 fs::path data_dir_path(data_dir);
103 if (!fs::exists(data_dir_path)) {
104 return absl::InvalidArgumentError(
105 absl::StrCat("Data directory doesn't exist ", data_dir));
106 }
107 fs::path licenses_path = data_dir_path / "licenses";
108 if (!fs::exists(licenses_path)) {
109 return absl::InvalidArgumentError(absl::StrCat(
110 "Licenses directory doesn't exist ", licenses_path.string()));
111 }
112
113 RE2::Set selector(RE2::Options(), RE2::Anchor::UNANCHORED);
114 std::vector<std::unique_ptr<RE2>> matchers;
115 std::vector<std::string> names;
116
117 for (const fs::path& file : fs::directory_iterator(licenses_path)) {
118 std::ifstream infile(file.string());
119 if (!infile.good()) {
120 return absl::InvalidArgumentError("Unable to open file " + file.string());
121 }
122
123 absl::StatusOr<Entry> entry = ParseEntry(infile);
124 if (!entry.ok()) {
125 return absl::InvalidArgumentError(
126 absl::StrCat("Unable to parse data entry at ", file.string(), " : ",
127 entry.status()));
128 }
129
130 std::string err;
131 selector.Add(entry->unique, &err);
132 if (!err.empty()) {
133 return absl::InvalidArgumentError(absl::StrCat(
134 "Unable to add unique key from ", file.string(), " : ", err));
135 }
136 names.emplace_back(std::move(entry->name));
137
138 auto matcher_re2 = std::make_unique<RE2>(entry->matcher);
139 if (!matcher_re2) {
140 return absl::InvalidArgumentError("Unable to make matcher.");
141 }
142
143 matchers.emplace_back(std::move(matcher_re2));
144 }
145
146 bool did_compile = selector.Compile();
147 if (!did_compile) {
148 return absl::UnknownError("Unable to compile selector.");
149 }
150
151 return Catalog(std::move(selector), std::move(matchers), std::move(names));
152}
153
154absl::StatusOr<Catalog> Catalog::Make(const std::vector<Entry>& entries) {
155 RE2::Set selector(RE2::Options(), RE2::Anchor::UNANCHORED);
156 std::vector<std::unique_ptr<RE2>> matchers;
157 std::vector<std::string> names;
158
159 for (const Entry& entry : entries) {
160 std::string err;
161 names.push_back(std::string(entry.name));
162 int idx = selector.Add(entry.unique, &err);
163 if (idx < 0) {
164 return absl::InvalidArgumentError(
165 absl::StrCat("Unable to add set entry: ", entry.unique, " ", err));
166 }
167 matchers.push_back(std::make_unique<RE2>(entry.matcher));
168 }
169
170 bool did_compile = selector.Compile();
171 if (!did_compile) {
172 return absl::OutOfRangeError("RE2::Set ran out of memory.");
173 }
174 return Catalog(std::move(selector), std::move(matchers), std::move(names));
175}
176
177Catalog::Catalog(RE2::Set selector,
178 std::vector<std::unique_ptr<RE2>> matchers,
179 std::vector<std::string> names)
180 : selector_(std::move(selector)),
181 matchers_(std::move(matchers)),
182 names_(std::move(names)) {}
183
184namespace {} // namespace
185
186absl::StatusOr<std::vector<Catalog::Match>> Catalog::FindMatch(
187 std::string_view query) const {
188 std::vector<int> selector_results;
189 if (!selector_.Match(query, &selector_results)) {
190 return absl::NotFoundError("Selector didn't match.");
191 }
192
193 std::vector<Catalog::Match> results;
194 std::vector<int> missed_results;
195 missed_results.reserve(selector_results.size());
196 std::vector<int> hit_results;
197 hit_results.reserve(selector_results.size());
198 for (int selector_result : selector_results) {
199 RE2* matcher = matchers_[selector_result].get();
200 std::optional<Match> match =
201 FindMatchForSelectedMatcher(query, matcher, names_[selector_result]);
202 if (match.has_value()) {
203 results.emplace_back(std::move(match.value()));
204 hit_results.push_back(selector_result);
205 } else {
206 missed_results.push_back(selector_result);
207 }
208 }
209 if (selector_results.size() != results.size()) {
210 std::stringstream missed;
211 for (size_t i = 0; i < missed_results.size(); ++i) {
212 if (i != 0) {
213 missed << ", ";
214 }
215 missed << names_[missed_results[i]];
216 }
217 std::stringstream hit;
218 hit << " Hit matcher(s): (";
219 for (size_t i = 0; i < hit_results.size(); ++i) {
220 if (i != 0) {
221 hit << ", ";
222 }
223 hit << names_[hit_results[i]];
224 }
225 hit << ")";
226 return absl::NotFoundError(
227 absl::StrCat("Selected matcher(s) (", missed.str(), ") didn't match.",
228 hit_results.empty() ? "" : hit.str()));
229 } else {
230 for (size_t i = 0; i < results.size(); ++i) {
231 for (size_t j = i + 1; j < results.size(); ++j) {
232 if (Overlaps(results[i].GetMatchedText(),
233 results[j].GetMatchedText())) {
234 return absl::InvalidArgumentError(absl::StrCat(
235 "Selected matchers overlap (", results[i].GetMatcher(), ", ",
236 results[j].GetMatcher(), ").\n", results[i].GetMatchedText(),
237 "\n############\n", results[j].GetMatchedText()));
238 }
239 }
240 }
241
242 return results;
243 }
244}
245
246absl::StatusOr<Catalog::Entry> Catalog::ParseEntry(std::istream& is) {
247 if (!is.good()) {
248 return absl::InvalidArgumentError("Bad stream.");
249 }
250 std::string name;
251 std::getline(is, name);
252 if (is.eof()) {
253 return absl::InvalidArgumentError("Bad stream.");
254 }
255 std::string unique;
256 std::getline(is, unique);
257 if (is.eof()) {
258 return absl::InvalidArgumentError("Bad stream.");
259 }
260
261 std::string matcher_text((std::istreambuf_iterator<char>(is)),
262 std::istreambuf_iterator<char>());
263
264 std::string ignore_whitespace_matcher = IgnoreWhitespace(matcher_text);
265
266 VLOG(4) << "matcher:" << name << ":\n" << ignore_whitespace_matcher;
267
268 return Catalog::Entry{.name = std::move(name),
269 .unique = std::move(unique),
270 .matcher = std::move(ignore_whitespace_matcher)};
271}
static Match MakeWithView(std::string_view matcher, std::string_view matched_text)
Definition catalog.h:37
static Match MakeWithString(std::string_view matcher, std::string matched_text)
Definition catalog.h:32
static absl::StatusOr< Entry > ParseEntry(std::istream &is)
VisibleForTesting.
Definition catalog.cc:246
static absl::StatusOr< Catalog > Open(std::string_view data_dir)
Definition catalog.cc:101
absl::StatusOr< std::vector< Match > > FindMatch(std::string_view query) const
Tries to identify a match for the query across the Catalog.
Definition catalog.cc:186
static absl::StatusOr< Catalog > Make(const std::vector< Entry > &entries)
Make a Catalog for testing.
Definition catalog.cc:154
static int input(yyscan_t yyscanner)
const char * name
Definition fuchsia.cc:50
Definition ref_ptr.h:261
VisibleForTesting.
Definition catalog.h:24
std::string name
Definition catalog.h:25