Ada
4.1.1
Fast spec-compliant URL parser
Toggle main menu visibility
Loading...
Searching...
No Matches
url_pattern_regex.cpp
Go to the documentation of this file.
1
#if ADA_INCLUDE_URL_PATTERN
2
3
#include "
ada/url_pattern_regex.h
"
4
5
#ifdef ADA_USE_UNSAFE_STD_REGEX_PROVIDER
6
#include <cstddef>
7
#include <optional>
8
#include <regex>
9
#include <string>
10
#include <string_view>
11
#include <vector>
12
#endif
// ADA_USE_UNSAFE_STD_REGEX_PROVIDER
13
14
namespace
ada::url_pattern_regex {
15
16
#ifdef ADA_USE_UNSAFE_STD_REGEX_PROVIDER
17
std::optional<std::regex> std_regex_provider::create_instance(
18
std::string_view pattern,
bool
ignore_case) {
19
// Let flags be an empty string.
20
// If options's ignore case is true then set flags to "vi".
21
// Otherwise set flags to "v"
22
auto
flags = ignore_case
23
? std::regex::icase | std::regex_constants::ECMAScript
24
: std::regex_constants::ECMAScript;
25
try
{
26
return
std::regex(pattern.data(), pattern.size(), flags);
27
}
catch
(
const
std::regex_error& e) {
28
(void)e;
29
ada_log(
"std_regex_provider::create_instance failed:"
, e.what());
30
return
std::nullopt;
31
}
32
}
33
34
std::optional<std::vector<std::optional<std::string>>>
35
std_regex_provider::regex_search(std::string_view input,
36
const
std::regex& pattern) {
37
// Use iterator-based regex_search to avoid string allocation
38
std::match_results<std::string_view::const_iterator> match_result;
39
try
{
40
if
(!std::regex_search(input.begin(), input.end(), match_result, pattern,
41
std::regex_constants::match_any)) {
42
return
std::nullopt;
43
}
44
}
catch
(
const
std::regex_error& e) {
45
(void)e;
46
ada_log(
"std_regex_provider::regex_search failed:"
, e.what());
47
return
std::nullopt;
48
}
49
std::vector<std::optional<std::string>> matches;
50
// An empty input can still match with capture groups (e.g. "(x*)" or an
51
// optional ":a?" against ""), so the group values must be extracted here
52
// just like for a non-empty input. A successful regex_search always leaves
53
// the full match at index 0, so match_result is never empty at this point.
54
if
(match_result.empty()) {
55
return
matches;
56
}
57
matches.reserve(match_result.size());
58
for
(
size_t
i = 1; i < match_result.size(); ++i) {
59
if
(
auto
entry = match_result[i]; entry.matched) {
60
matches.emplace_back(entry.str());
61
}
else
{
62
// A capture group that did not participate in the match is undefined,
63
// not absent. Emitting nullopt keeps the result aligned by position with
64
// the component's group name list; dropping it shifts every later group
65
// onto the wrong name.
66
matches.emplace_back(std::nullopt);
67
}
68
}
69
return
matches;
70
}
71
72
bool
std_regex_provider::regex_match(std::string_view input,
73
const
std::regex& pattern) {
74
try
{
75
return
std::regex_match(input.begin(), input.end(), pattern);
76
}
catch
(
const
std::regex_error& e) {
77
(void)e;
78
ada_log(
"std_regex_provider::regex_match failed:"
, e.what());
79
return
false
;
80
}
81
}
82
83
#endif
// ADA_USE_UNSAFE_STD_REGEX_PROVIDER
84
85
}
// namespace ada::url_pattern_regex
86
87
#endif
// ADA_INCLUDE_URL_PATTERN
url_pattern_regex.h