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