Skip to content

Commit 74f6cc3

Browse files
committed
make progress
1 parent ee9559b commit 74f6cc3

14 files changed

+359
-180
lines changed

include/ada/implementation.h

+7-8
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,7 @@
1111

1212
#include "ada/parser.h"
1313
#include "ada/common_defs.h"
14-
#include "ada/encoding_type.h"
1514
#include "ada/url.h"
16-
#include "ada/state.h"
17-
#include "ada/url_aggregator.h"
1815
#include "ada/url_pattern_regex.h"
1916

2017
namespace ada {
@@ -61,11 +58,13 @@ bool can_parse(std::string_view input,
6158
* use ada::url_pattern_regex::std_regex_provider
6259
* @return url_pattern instance
6360
*/
64-
ada_warn_unused tl::expected<url_pattern, errors> parse_url_pattern(
65-
std::variant<std::string_view, url_pattern_init> input,
66-
const std::string_view* base_url = nullptr,
67-
const url_pattern_options* options = nullptr,
68-
std::optional<url_pattern_regex::provider> regex_provider = std::nullopt);
61+
template <class regex_provider, class regex_type>
62+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
63+
ada_warn_unused tl::expected<url_pattern<regex_provider, regex_type>, errors>
64+
parse_url_pattern(std::variant<std::string_view, url_pattern_init> input,
65+
const std::string_view* base_url = nullptr,
66+
const url_pattern_options* options = nullptr,
67+
std::optional<regex_provider> provider = std::nullopt);
6968

7069
/**
7170
* Computes a href string from a file path. The function assumes

include/ada/parser.h

+9-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
namespace ada {
1818
struct url_aggregator;
1919
struct url;
20+
template <class regex_provider, class regex_type>
21+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
2022
class url_pattern;
2123
struct url_pattern_options;
2224
struct url_pattern_init;
@@ -52,10 +54,13 @@ extern template url_aggregator parse_url_impl<url_aggregator>(
5254
extern template url parse_url_impl<url>(std::string_view user_input,
5355
const url* base_url);
5456

55-
tl::expected<url_pattern, errors> parse_url_pattern_impl(
56-
std::variant<std::string_view, url_pattern_init> input,
57-
const std::string_view* base_url, const url_pattern_options* options,
58-
url_pattern_regex::provider&& regex_provider);
57+
template <class regex_provider, class regex_type>
58+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
59+
tl::expected<url_pattern<regex_provider, regex_type>, errors>
60+
parse_url_pattern_impl(std::variant<std::string_view, url_pattern_init> input,
61+
const std::string_view* base_url,
62+
const url_pattern_options* options,
63+
regex_provider&& provider);
5964

6065
} // namespace ada::parser
6166

include/ada/url_aggregator.h

+7-3
Original file line numberDiff line numberDiff line change
@@ -222,9 +222,13 @@ 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(
226-
std::variant<std::string_view, url_pattern_init> input,
227-
const std::string_view *base_url, const url_pattern_options *options);
225+
template <class regex_provider, class regex_type>
226+
requires url_pattern_regex::derived_from_provider<regex_provider,
227+
regex_type>
228+
friend tl::expected<url_pattern<regex_provider, regex_type>, errors>
229+
parse_url_pattern_impl(std::variant<std::string_view, url_pattern_init> input,
230+
const std::string_view *base_url,
231+
const url_pattern_options *options);
228232

229233
std::string buffer{};
230234
url_components components{};

include/ada/url_pattern-inl.h

+52-17
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ inline bool url_pattern_component_result::operator==(
2424
return input == other.input && groups == other.groups;
2525
}
2626

27-
inline std::string url_pattern_component::to_string() const {
27+
template <class regex_provider, class regex_type>
28+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
29+
std::string url_pattern_component<regex_provider, regex_type>::to_string()
30+
const {
2831
#ifdef ADA_HAS_FORMAT
2932
return std::format(R"({{"pattern": "{}", "has_regexp_groups": {}}})", pattern,
3033
has_regexp_groups ? "true" : "false" //,
@@ -34,9 +37,11 @@ inline std::string url_pattern_component::to_string() const {
3437
#endif
3538
}
3639

37-
inline url_pattern_component_result
38-
url_pattern_component::create_component_match_result(
39-
std::string_view input, const std::smatch& exec_result) {
40+
template <class regex_provider, class regex_type>
41+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
42+
url_pattern_component_result url_pattern_component<regex_provider, regex_type>::
43+
create_component_match_result(std::string_view input,
44+
const std::smatch& exec_result) {
4045
// Let result be a new URLPatternComponentResult.
4146
// Set result["input"] to input.
4247
// Let groups be a record<USVString, (USVString or undefined)>.
@@ -70,7 +75,9 @@ url_pattern_component::create_component_match_result(
7075
return result;
7176
}
7277

73-
inline std::string url_pattern::to_string() const {
78+
template <class regex_provider, class regex_type>
79+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
80+
std::string url_pattern<regex_provider, regex_type>::to_string() const {
7481
#ifdef ADA_HAS_FORMAT
7582
return std::format(
7683
R"({{"protocol_component": "{}", "username_component": {}, "password_component": {}, "hostname_component": {}, "port_component": {}, "pathname_component": {}, "search_component": {}, "hash_component": {}, "ignore_case": {}}})",
@@ -84,42 +91,70 @@ inline std::string url_pattern::to_string() const {
8491
#endif
8592
}
8693

87-
inline std::string_view url_pattern::get_protocol() const ada_lifetime_bound {
94+
template <class regex_provider, class regex_type>
95+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
96+
std::string_view url_pattern<regex_provider, regex_type>::get_protocol() const
97+
ada_lifetime_bound {
8898
// Return this's associated URL pattern's protocol component's pattern string.
8999
return protocol_component.pattern;
90100
}
91-
inline std::string_view url_pattern::get_username() const ada_lifetime_bound {
101+
template <class regex_provider, class regex_type>
102+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
103+
std::string_view url_pattern<regex_provider, regex_type>::get_username() const
104+
ada_lifetime_bound {
92105
// Return this's associated URL pattern's username component's pattern string.
93106
return username_component.pattern;
94107
}
95-
inline std::string_view url_pattern::get_password() const ada_lifetime_bound {
108+
template <class regex_provider, class regex_type>
109+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
110+
std::string_view url_pattern<regex_provider, regex_type>::get_password() const
111+
ada_lifetime_bound {
96112
// Return this's associated URL pattern's password component's pattern string.
97113
return password_component.pattern;
98114
}
99-
inline std::string_view url_pattern::get_hostname() const ada_lifetime_bound {
115+
template <class regex_provider, class regex_type>
116+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
117+
std::string_view url_pattern<regex_provider, regex_type>::get_hostname() const
118+
ada_lifetime_bound {
100119
// Return this's associated URL pattern's hostname component's pattern string.
101120
return hostname_component.pattern;
102121
}
103-
inline std::string_view url_pattern::get_port() const ada_lifetime_bound {
122+
template <class regex_provider, class regex_type>
123+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
124+
std::string_view url_pattern<regex_provider, regex_type>::get_port() const
125+
ada_lifetime_bound {
104126
// Return this's associated URL pattern's port component's pattern string.
105127
return port_component.pattern;
106128
}
107-
inline std::string_view url_pattern::get_pathname() const ada_lifetime_bound {
129+
template <class regex_provider, class regex_type>
130+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
131+
std::string_view url_pattern<regex_provider, regex_type>::get_pathname() const
132+
ada_lifetime_bound {
108133
// Return this's associated URL pattern's pathname component's pattern string.
109134
return pathname_component.pattern;
110135
}
111-
inline std::string_view url_pattern::get_search() const ada_lifetime_bound {
136+
template <class regex_provider, class regex_type>
137+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
138+
std::string_view url_pattern<regex_provider, regex_type>::get_search() const
139+
ada_lifetime_bound {
112140
// Return this's associated URL pattern's search component's pattern string.
113141
return search_component.pattern;
114142
}
115-
inline std::string_view url_pattern::get_hash() const ada_lifetime_bound {
143+
template <class regex_provider, class regex_type>
144+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
145+
std::string_view url_pattern<regex_provider, regex_type>::get_hash() const
146+
ada_lifetime_bound {
116147
// Return this's associated URL pattern's hash component's pattern string.
117148
return hash_component.pattern;
118149
}
119-
120-
inline bool url_pattern::ignore_case() const { return ignore_case_; }
121-
122-
inline bool url_pattern::has_regexp_groups() const {
150+
template <class regex_provider, class regex_type>
151+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
152+
bool url_pattern<regex_provider, regex_type>::ignore_case() const {
153+
return ignore_case_;
154+
}
155+
template <class regex_provider, class regex_type>
156+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
157+
bool url_pattern<regex_provider, regex_type>::has_regexp_groups() const {
123158
// If this's associated URL pattern's has regexp groups, then return true.
124159
return protocol_component.has_regexp_groups ||
125160
username_component.has_regexp_groups ||

include/ada/url_pattern.h

+35-32
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ namespace ada {
1919

2020
namespace parser {
2121
template <typename result_type, typename url_pattern_init,
22-
typename url_pattern_options>
22+
typename url_pattern_options, typename regex_provider>
2323
tl::expected<result_type, errors> parse_url_pattern_impl(
2424
std::variant<std::string_view, url_pattern_init> input,
2525
const std::string_view* base_url, const url_pattern_options* options,
26-
url_pattern_regex::provider&& regex_provider);
26+
regex_provider&& provider);
2727
}
2828

2929
// Important: C++20 allows us to use concept rather than `using` or `typedef
@@ -207,37 +207,37 @@ struct url_pattern_component_result {
207207
#endif // ADA_TESTING
208208
};
209209

210+
template <class regex_provider, class regex_type>
211+
requires url_pattern_regex::derived_from_provider<regex_provider, 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 regex_provider& 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,10 +270,13 @@ 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_provider, class regex_type>
274+
requires url_pattern_regex::derived_from_provider<regex_provider, regex_type>
273275
class url_pattern {
274276
public:
275-
explicit url_pattern(url_pattern_regex::provider&& regex_provider)
276-
: regex_provider_(std::move(regex_provider)) {}
277+
explicit url_pattern(
278+
url_pattern_regex::provider<regex_type>&& new_regex_provider)
279+
: regex_provider_(std::move(new_regex_provider)) {}
277280

278281
/**
279282
* @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
@@ -294,48 +297,48 @@ class url_pattern {
294297
const url_pattern_input& input, std::string_view* base_url_string);
295298

296299
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
297-
std::string_view get_protocol() const ada_lifetime_bound;
300+
[[nodiscard]] std::string_view get_protocol() const ada_lifetime_bound;
298301
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-username
299-
std::string_view get_username() const ada_lifetime_bound;
302+
[[nodiscard]] std::string_view get_username() const ada_lifetime_bound;
300303
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-password
301-
std::string_view get_password() const ada_lifetime_bound;
304+
[[nodiscard]] std::string_view get_password() const ada_lifetime_bound;
302305
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-hostname
303-
std::string_view get_hostname() const ada_lifetime_bound;
306+
[[nodiscard]] std::string_view get_hostname() const ada_lifetime_bound;
304307
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-port
305-
std::string_view get_port() const ada_lifetime_bound;
308+
[[nodiscard]] std::string_view get_port() const ada_lifetime_bound;
306309
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-pathname
307-
std::string_view get_pathname() const ada_lifetime_bound;
310+
[[nodiscard]] std::string_view get_pathname() const ada_lifetime_bound;
308311
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-search
309-
std::string_view get_search() const ada_lifetime_bound;
312+
[[nodiscard]] std::string_view get_search() const ada_lifetime_bound;
310313
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-hash
311-
std::string_view get_hash() const ada_lifetime_bound;
314+
[[nodiscard]] std::string_view get_hash() const ada_lifetime_bound;
312315

313316
// If ignoreCase is true, the JavaScript regular expression created for each
314317
// pattern must use the `vi` flag. Otherwise, they must use the `v` flag.
315-
bool ignore_case() const;
318+
[[nodiscard]] bool ignore_case() const;
316319

317320
// @see https://urlpattern.spec.whatwg.org/#url-pattern-has-regexp-groups
318-
bool has_regexp_groups() const;
321+
[[nodiscard]] bool has_regexp_groups() const;
319322

320-
std::string to_string() const;
323+
[[nodiscard]] std::string to_string() const;
321324

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{};
325+
url_pattern_component<regex_provider, regex_type> protocol_component{};
326+
url_pattern_component<regex_provider, regex_type> username_component{};
327+
url_pattern_component<regex_provider, regex_type> password_component{};
328+
url_pattern_component<regex_provider, regex_type> hostname_component{};
329+
url_pattern_component<regex_provider, regex_type> port_component{};
330+
url_pattern_component<regex_provider, regex_type> pathname_component{};
331+
url_pattern_component<regex_provider, regex_type> search_component{};
332+
url_pattern_component<regex_provider, regex_type> hash_component{};
330333
bool ignore_case_ = false;
331-
url_pattern_regex::provider regex_provider_;
334+
regex_provider regex_provider_;
332335

333336
template <typename result_type, typename url_pattern_init,
334-
typename url_pattern_options>
337+
typename url_pattern_options, typename regex_provider_>
335338
friend tl::expected<result_type, errors> parser::parse_url_pattern_impl(
336339
std::variant<std::string_view, url_pattern_init> input,
337340
const std::string_view* base_url, const url_pattern_options* options,
338-
url_pattern_regex::provider&& regex_provider);
341+
regex_provider_&& provider);
339342
};
340343

341344
} // namespace ada

0 commit comments

Comments
 (0)