Skip to content

Commit 0f1c571

Browse files
committed
make progress
1 parent ee9559b commit 0f1c571

14 files changed

+214
-118
lines changed

include/ada/implementation.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,13 @@ bool can_parse(std::string_view input,
6161
* use ada::url_pattern_regex::std_regex_provider
6262
* @return url_pattern instance
6363
*/
64-
ada_warn_unused tl::expected<url_pattern, errors> parse_url_pattern(
64+
template <class regex_type = std::regex>
65+
ada_warn_unused tl::expected<url_pattern<regex_type>, errors> parse_url_pattern(
6566
std::variant<std::string_view, url_pattern_init> input,
6667
const std::string_view* base_url = nullptr,
6768
const url_pattern_options* options = nullptr,
68-
std::optional<url_pattern_regex::provider> regex_provider = std::nullopt);
69+
std::optional<url_pattern_regex::provider<regex_type>> regex_provider =
70+
std::nullopt);
6971

7072
/**
7173
* Computes a href string from a file path. The function assumes

include/ada/parser.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
namespace ada {
1818
struct url_aggregator;
1919
struct url;
20+
template <class regex_type>
2021
class url_pattern;
2122
struct url_pattern_options;
2223
struct url_pattern_init;
@@ -52,10 +53,11 @@ extern template url_aggregator parse_url_impl<url_aggregator>(
5253
extern template url parse_url_impl<url>(std::string_view user_input,
5354
const url* base_url);
5455

55-
tl::expected<url_pattern, errors> parse_url_pattern_impl(
56+
template <class regex_type = std::regex>
57+
tl::expected<url_pattern<regex_type>, errors> parse_url_pattern_impl(
5658
std::variant<std::string_view, url_pattern_init> input,
5759
const std::string_view* base_url, const url_pattern_options* options,
58-
url_pattern_regex::provider&& regex_provider);
60+
url_pattern_regex::provider<regex_type>&& regex_provider);
5961

6062
} // namespace ada::parser
6163

include/ada/url_aggregator.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ struct url_aggregator : url_base {
222222
friend url_aggregator parser::parse_url_impl<url_aggregator, false>(
223223
std::string_view, const url_aggregator *);
224224
// url_pattern methods
225-
friend tl::expected<url_pattern, errors> parse_url_pattern_impl(
225+
template <class regex_type>
226+
friend tl::expected<url_pattern<regex_type>, errors> parse_url_pattern_impl(
226227
std::variant<std::string_view, url_pattern_init> input,
227228
const std::string_view *base_url, const url_pattern_options *options);
228229

include/ada/url_pattern-inl.h

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "ada/common_defs.h"
99
#include "ada/url_pattern.h"
1010

11+
#include <_regex.h>
1112
#include <string_view>
1213

1314
namespace ada {
@@ -24,7 +25,8 @@ inline bool url_pattern_component_result::operator==(
2425
return input == other.input && groups == other.groups;
2526
}
2627

27-
inline std::string url_pattern_component::to_string() const {
28+
template <class regex_type>
29+
std::string url_pattern_component<regex_type>::to_string() const {
2830
#ifdef ADA_HAS_FORMAT
2931
return std::format(R"({{"pattern": "{}", "has_regexp_groups": {}}})", pattern,
3032
has_regexp_groups ? "true" : "false" //,
@@ -34,8 +36,9 @@ inline std::string url_pattern_component::to_string() const {
3436
#endif
3537
}
3638

37-
inline url_pattern_component_result
38-
url_pattern_component::create_component_match_result(
39+
template <class regex_type>
40+
url_pattern_component_result
41+
url_pattern_component<regex_type>::create_component_match_result(
3942
std::string_view input, const std::smatch& exec_result) {
4043
// Let result be a new URLPatternComponentResult.
4144
// Set result["input"] to input.
@@ -70,7 +73,8 @@ url_pattern_component::create_component_match_result(
7073
return result;
7174
}
7275

73-
inline std::string url_pattern::to_string() const {
76+
template <class regex_type>
77+
std::string url_pattern<regex_type>::to_string() const {
7478
#ifdef ADA_HAS_FORMAT
7579
return std::format(
7680
R"({{"protocol_component": "{}", "username_component": {}, "password_component": {}, "hostname_component": {}, "port_component": {}, "pathname_component": {}, "search_component": {}, "hash_component": {}, "ignore_case": {}}})",
@@ -84,42 +88,58 @@ inline std::string url_pattern::to_string() const {
8488
#endif
8589
}
8690

87-
inline std::string_view url_pattern::get_protocol() const ada_lifetime_bound {
91+
template <class regex_type>
92+
std::string_view url_pattern<regex_type>::get_protocol() const
93+
ada_lifetime_bound {
8894
// Return this's associated URL pattern's protocol component's pattern string.
8995
return protocol_component.pattern;
9096
}
91-
inline std::string_view url_pattern::get_username() const ada_lifetime_bound {
97+
template <class regex_type>
98+
std::string_view url_pattern<regex_type>::get_username() const
99+
ada_lifetime_bound {
92100
// Return this's associated URL pattern's username component's pattern string.
93101
return username_component.pattern;
94102
}
95-
inline std::string_view url_pattern::get_password() const ada_lifetime_bound {
103+
template <class regex_type>
104+
std::string_view url_pattern<regex_type>::get_password() const
105+
ada_lifetime_bound {
96106
// Return this's associated URL pattern's password component's pattern string.
97107
return password_component.pattern;
98108
}
99-
inline std::string_view url_pattern::get_hostname() const ada_lifetime_bound {
109+
template <class regex_type>
110+
std::string_view url_pattern<regex_type>::get_hostname() const
111+
ada_lifetime_bound {
100112
// Return this's associated URL pattern's hostname component's pattern string.
101113
return hostname_component.pattern;
102114
}
103-
inline std::string_view url_pattern::get_port() const ada_lifetime_bound {
115+
template <class regex_type>
116+
std::string_view url_pattern<regex_type>::get_port() const ada_lifetime_bound {
104117
// Return this's associated URL pattern's port component's pattern string.
105118
return port_component.pattern;
106119
}
107-
inline std::string_view url_pattern::get_pathname() const ada_lifetime_bound {
120+
template <class regex_type>
121+
std::string_view url_pattern<regex_type>::get_pathname() const
122+
ada_lifetime_bound {
108123
// Return this's associated URL pattern's pathname component's pattern string.
109124
return pathname_component.pattern;
110125
}
111-
inline std::string_view url_pattern::get_search() const ada_lifetime_bound {
126+
template <class regex_type>
127+
std::string_view url_pattern<regex_type>::get_search() const
128+
ada_lifetime_bound {
112129
// Return this's associated URL pattern's search component's pattern string.
113130
return search_component.pattern;
114131
}
115-
inline std::string_view url_pattern::get_hash() const ada_lifetime_bound {
132+
template <class regex_type>
133+
std::string_view url_pattern<regex_type>::get_hash() const ada_lifetime_bound {
116134
// Return this's associated URL pattern's hash component's pattern string.
117135
return hash_component.pattern;
118136
}
119-
120-
inline bool url_pattern::ignore_case() const { return ignore_case_; }
121-
122-
inline bool url_pattern::has_regexp_groups() const {
137+
template <class regex_type>
138+
bool url_pattern<regex_type>::ignore_case() const {
139+
return ignore_case_;
140+
}
141+
template <class regex_type>
142+
bool url_pattern<regex_type>::has_regexp_groups() const {
123143
// If this's associated URL pattern's has regexp groups, then return true.
124144
return protocol_component.has_regexp_groups ||
125145
username_component.has_regexp_groups ||

include/ada/url_pattern.h

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "ada/expected.h"
1010
#include "ada/url_pattern_regex.h"
1111

12+
#include <_regex.h>
1213
#include <regex>
1314
#include <string>
1415
#include <unordered_map>
@@ -19,11 +20,11 @@ namespace ada {
1920

2021
namespace parser {
2122
template <typename result_type, typename url_pattern_init,
22-
typename url_pattern_options>
23+
typename url_pattern_options, class regex_type>
2324
tl::expected<result_type, errors> parse_url_pattern_impl(
2425
std::variant<std::string_view, url_pattern_init> input,
2526
const std::string_view* base_url, const url_pattern_options* options,
26-
url_pattern_regex::provider&& regex_provider);
27+
url_pattern_regex::provider<regex_type>&& regex_provider);
2728
}
2829

2930
// Important: C++20 allows us to use concept rather than `using` or `typedef
@@ -207,37 +208,36 @@ struct url_pattern_component_result {
207208
#endif // ADA_TESTING
208209
};
209210

211+
template <class regex_type>
210212
class url_pattern_component {
211213
public:
212214
url_pattern_component() = default;
213215

214216
// This function explicitly takes a std::string because it is moved.
215217
// To avoid unnecessary copy, move each value while calling the constructor.
216-
url_pattern_component(std::string&& new_pattern, std::regex&& new_regexp,
217-
std::regex_constants::syntax_option_type new_flags,
218+
url_pattern_component(std::string&& new_pattern, regex_type&& new_regexp,
218219
std::vector<std::string>&& new_group_name_list,
219220
bool new_has_regexp_groups)
220221
: regexp(std::move(new_regexp)),
221222
pattern(std::move(new_pattern)),
222-
flags(new_flags),
223223
group_name_list(new_group_name_list),
224224
has_regexp_groups(new_has_regexp_groups) {}
225225

226226
// @see https://urlpattern.spec.whatwg.org/#compile-a-component
227227
template <url_pattern_encoding_callback F>
228228
static tl::expected<url_pattern_component, errors> compile(
229229
std::string_view input, F& encoding_callback,
230-
url_pattern_compile_component_options& options);
230+
url_pattern_compile_component_options& options,
231+
const url_pattern_regex::provider<regex_type>& regex_provider);
231232

232233
// @see https://urlpattern.spec.whatwg.org/#create-a-component-match-result
233234
url_pattern_component_result create_component_match_result(
234235
std::string_view input, const std::smatch& exec_result);
235236

236237
std::string to_string() const;
237238

238-
std::regex regexp{};
239+
regex_type regexp{};
239240
std::string pattern{};
240-
std::regex_constants::syntax_option_type flags = std::regex::ECMAScript;
241241
std::vector<std::string> group_name_list{};
242242
bool has_regexp_groups = false;
243243
};
@@ -270,9 +270,10 @@ struct url_pattern_options {
270270
// defined in https://wicg.github.io/urlpattern.
271271
// More information about the URL Pattern syntax can be found at
272272
// https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
273+
template <class regex_type = std::regex>
273274
class url_pattern {
274275
public:
275-
explicit url_pattern(url_pattern_regex::provider&& regex_provider)
276+
explicit url_pattern(url_pattern_regex::provider<regex_type>&& regex_provider)
276277
: regex_provider_(std::move(regex_provider)) {}
277278

278279
/**
@@ -319,23 +320,23 @@ class url_pattern {
319320

320321
std::string to_string() const;
321322

322-
url_pattern_component protocol_component{};
323-
url_pattern_component username_component{};
324-
url_pattern_component password_component{};
325-
url_pattern_component hostname_component{};
326-
url_pattern_component port_component{};
327-
url_pattern_component pathname_component{};
328-
url_pattern_component search_component{};
329-
url_pattern_component hash_component{};
323+
url_pattern_component<regex_type> protocol_component{};
324+
url_pattern_component<regex_type> username_component{};
325+
url_pattern_component<regex_type> password_component{};
326+
url_pattern_component<regex_type> hostname_component{};
327+
url_pattern_component<regex_type> port_component{};
328+
url_pattern_component<regex_type> pathname_component{};
329+
url_pattern_component<regex_type> search_component{};
330+
url_pattern_component<regex_type> hash_component{};
330331
bool ignore_case_ = false;
331-
url_pattern_regex::provider regex_provider_;
332+
url_pattern_regex::provider<regex_type> regex_provider_;
332333

333334
template <typename result_type, typename url_pattern_init,
334-
typename url_pattern_options>
335+
typename url_pattern_options, typename regex_provider_type>
335336
friend tl::expected<result_type, errors> parser::parse_url_pattern_impl(
336337
std::variant<std::string_view, url_pattern_init> input,
337338
const std::string_view* base_url, const url_pattern_options* options,
338-
url_pattern_regex::provider&& regex_provider);
339+
url_pattern_regex::provider<regex_provider_type>&& regex_provider);
339340
};
340341

341342
} // namespace ada

0 commit comments

Comments
 (0)