Ada 3.4.4
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern_regex.cpp
Go to the documentation of this file.
1#if ADA_INCLUDE_URL_PATTERN
2
4
5namespace ada::url_pattern_regex {
6
7#ifdef ADA_USE_UNSAFE_STD_REGEX_PROVIDER
8std::optional<std::regex> std_regex_provider::create_instance(
9 std::string_view pattern, bool ignore_case) {
10 // Let flags be an empty string.
11 // If options's ignore case is true then set flags to "vi".
12 // Otherwise set flags to "v"
13 auto flags = ignore_case
14 ? std::regex::icase | std::regex_constants::ECMAScript
15 : std::regex_constants::ECMAScript;
16 try {
17 return std::regex(pattern.data(), pattern.size(), flags);
18 } catch (const std::regex_error& e) {
19 (void)e;
20 ada_log("std_regex_provider::create_instance failed:", e.what());
21 return std::nullopt;
22 }
23}
24
25std::optional<std::vector<std::optional<std::string>>>
26std_regex_provider::regex_search(std::string_view input,
27 const std::regex& pattern) {
28 // Use iterator-based regex_search to avoid string allocation
29 std::match_results<std::string_view::const_iterator> match_result;
30 try {
31 if (!std::regex_search(input.begin(), input.end(), match_result, pattern,
32 std::regex_constants::match_any)) {
33 return std::nullopt;
34 }
35 } catch (const std::regex_error& e) {
36 (void)e;
37 ada_log("std_regex_provider::regex_search failed:", e.what());
38 return std::nullopt;
39 }
40 std::vector<std::optional<std::string>> matches;
41 // If input is empty, let's assume the result will be empty as well.
42 if (input.empty() || match_result.empty()) {
43 return matches;
44 }
45 matches.reserve(match_result.size());
46 for (size_t i = 1; i < match_result.size(); ++i) {
47 if (auto entry = match_result[i]; entry.matched) {
48 matches.emplace_back(entry.str());
49 }
50 }
51 return matches;
52}
53
54bool std_regex_provider::regex_match(std::string_view input,
55 const std::regex& pattern) {
56 try {
57 return std::regex_match(input.begin(), input.end(), pattern);
58 } catch (const std::regex_error& e) {
59 (void)e;
60 ada_log("std_regex_provider::regex_match failed:", e.what());
61 return false;
62 }
63}
64
65#endif // ADA_USE_UNSAFE_STD_REGEX_PROVIDER
66
67} // namespace ada::url_pattern_regex
68
69#endif // ADA_INCLUDE_URL_PATTERN