5#ifndef ADA_URL_PATTERN_INL_H
6#define ADA_URL_PATTERN_INL_H
16#if ADA_INCLUDE_URL_PATTERN
19inline bool url_pattern_init::operator==(
const url_pattern_init& other)
const {
20 return protocol == other.protocol && username == other.username &&
21 password == other.password && hostname == other.hostname &&
22 port == other.port && search == other.search && hash == other.hash &&
23 pathname == other.pathname;
26inline bool url_pattern_component_result::operator==(
27 const url_pattern_component_result& other)
const {
28 return input == other.input && groups == other.groups;
31template <url_pattern_regex::regex_concept regex_prov
ider>
32url_pattern_component_result
33url_pattern_component<regex_provider>::create_component_match_result(
35 std::vector<std::optional<std::string>>&& exec_result) {
40 url_pattern_component_result{.input = std::move(input), .groups = {}};
47 const size_t size = std::min(exec_result.size(), group_name_list.size());
48 result.groups.reserve(size);
49 for (
size_t index = 0; index < size; index++) {
50 result.groups.emplace(group_name_list[index],
51 std::move(exec_result[index]));
56template <url_pattern_regex::regex_concept regex_prov
ider>
57std::string_view url_pattern<regex_provider>::get_protocol() const
60 return protocol_component.pattern;
62template <url_pattern_regex::regex_concept regex_prov
ider>
63std::string_view url_pattern<regex_provider>::get_username() const
66 return username_component.pattern;
68template <url_pattern_regex::regex_concept regex_prov
ider>
69std::string_view url_pattern<regex_provider>::get_password() const
72 return password_component.pattern;
74template <url_pattern_regex::regex_concept regex_prov
ider>
75std::string_view url_pattern<regex_provider>::get_hostname() const
78 return hostname_component.pattern;
80template <url_pattern_regex::regex_concept regex_prov
ider>
81std::string_view url_pattern<regex_provider>::get_port() const
84 return port_component.pattern;
86template <url_pattern_regex::regex_concept regex_prov
ider>
87std::string_view url_pattern<regex_provider>::get_pathname() const
90 return pathname_component.pattern;
92template <url_pattern_regex::regex_concept regex_prov
ider>
93std::string_view url_pattern<regex_provider>::get_search() const
96 return search_component.pattern;
98template <url_pattern_regex::regex_concept regex_prov
ider>
99std::string_view url_pattern<regex_provider>::get_hash() const
102 return hash_component.pattern;
104template <url_pattern_regex::regex_concept regex_prov
ider>
105bool url_pattern<regex_provider>::ignore_case()
const {
108template <url_pattern_regex::regex_concept regex_prov
ider>
109bool url_pattern<regex_provider>::has_regexp_groups()
const {
111 return protocol_component.has_regexp_groups ||
112 username_component.has_regexp_groups ||
113 password_component.has_regexp_groups ||
114 hostname_component.has_regexp_groups ||
115 port_component.has_regexp_groups ||
116 pathname_component.has_regexp_groups ||
117 search_component.has_regexp_groups || hash_component.has_regexp_groups;
120inline bool url_pattern_part::is_regexp() const noexcept {
121 return type == url_pattern_part_type::REGEXP;
124inline std::string_view url_pattern_compile_component_options::get_delimiter()
127 return {&delimiter.value(), 1};
132inline std::string_view url_pattern_compile_component_options::get_prefix()
135 return {&prefix.value(), 1};
140template <url_pattern_regex::regex_concept regex_prov
ider>
141template <url_pattern_encoding_callback F>
142tl::expected<url_pattern_component<regex_provider>,
errors>
143url_pattern_component<regex_provider>::compile(
144 std::string_view input, F& encoding_callback,
145 url_pattern_compile_component_options& options) {
146 ada_log(
"url_pattern_component::compile input: ", input);
149 auto part_list = url_pattern_helpers::parse_pattern_string(input, options,
153 ada_log(
"parse_pattern_string failed");
154 return tl::unexpected(part_list.error());
158 const auto has_regexp = [](
const auto& part) {
return part.is_regexp(); };
159 const bool has_regexp_groups = std::ranges::any_of(*part_list, has_regexp);
161 url_pattern_component_type component_type =
162 url_pattern_component_type::REGEXP;
163 std::string exact_match_value{};
165 if (part_list->empty()) {
166 component_type = url_pattern_component_type::EMPTY;
167 }
else if (part_list->size() == 1) {
168 const auto& part = (*part_list)[0];
169 if (part.type == url_pattern_part_type::FIXED_TEXT &&
170 part.modifier == url_pattern_part_modifier::none &&
171 !options.ignore_case) {
172 component_type = url_pattern_component_type::EXACT_MATCH;
173 exact_match_value = part.value;
174 }
else if (part.type == url_pattern_part_type::FULL_WILDCARD &&
175 part.modifier == url_pattern_part_modifier::none &&
176 part.prefix.empty() && part.suffix.empty()) {
177 component_type = url_pattern_component_type::FULL_WILDCARD;
182 if (component_type != url_pattern_component_type::REGEXP) {
183 auto pattern_string =
184 url_pattern_helpers::generate_pattern_string(*part_list, options);
187 std::vector<std::string> name_list;
188 if (component_type == url_pattern_component_type::FULL_WILDCARD &&
189 !part_list->empty()) {
190 name_list.push_back((*part_list)[0].name);
192 return url_pattern_component<regex_provider>(
193 std::move(pattern_string),
typename regex_provider::regex_type{},
194 std::move(name_list), has_regexp_groups, component_type,
195 std::move(exact_match_value));
199 auto [regular_expression_string, name_list] =
200 url_pattern_helpers::generate_regular_expression_and_name_list(*part_list,
202 auto pattern_string =
203 url_pattern_helpers::generate_pattern_string(*part_list, options);
205 std::optional<typename regex_provider::regex_type> regular_expression =
206 regex_provider::create_instance(regular_expression_string,
207 options.ignore_case);
208 if (!regular_expression) {
212 return url_pattern_component<regex_provider>(
213 std::move(pattern_string), std::move(*regular_expression),
214 std::move(name_list), has_regexp_groups, component_type,
215 std::move(exact_match_value));
218template <url_pattern_regex::regex_concept regex_prov
ider>
219bool url_pattern_component<regex_provider>::fast_test(
220 std::string_view input)
const noexcept {
223 if (type == url_pattern_component_type::FULL_WILDCARD) {
226 if (type == url_pattern_component_type::EXACT_MATCH) {
227 return input == exact_match_value;
229 if (type == url_pattern_component_type::EMPTY) {
230 return input.empty();
233 return regex_provider::regex_match(input, regexp);
236template <url_pattern_regex::regex_concept regex_prov
ider>
237std::optional<std::vector<std::optional<std::string>>>
238url_pattern_component<regex_provider>::fast_match(
239 std::string_view input)
const {
241 if (type == url_pattern_component_type::FULL_WILDCARD) {
244 if (group_name_list.empty()) {
245 return std::vector<std::optional<std::string>>{};
248 return std::vector<std::optional<std::string>>{std::string(input)};
250 if (type == url_pattern_component_type::EXACT_MATCH) {
251 if (input == exact_match_value) {
252 return std::vector<std::optional<std::string>>{};
256 if (type == url_pattern_component_type::EMPTY) {
258 return std::vector<std::optional<std::string>>{};
263 return regex_provider::regex_search(input, regexp);
266template <url_pattern_regex::regex_concept regex_prov
ider>
268 const url_pattern_input& input,
const std::string_view* base_url) {
271 return match(input, base_url);
274template <url_pattern_regex::regex_concept regex_prov
ider>
275bool url_pattern<regex_provider>::test_components(
276 std::string_view protocol, std::string_view username,
277 std::string_view password, std::string_view hostname, std::string_view port,
278 std::string_view pathname, std::string_view search,
279 std::string_view hash)
const {
280 return protocol_component.fast_test(protocol) &&
281 username_component.fast_test(username) &&
282 password_component.fast_test(password) &&
283 hostname_component.fast_test(hostname) &&
284 port_component.fast_test(port) &&
285 pathname_component.fast_test(pathname) &&
286 search_component.fast_test(search) && hash_component.fast_test(hash);
289template <url_pattern_regex::regex_concept regex_prov
ider>
291 const url_pattern_input& input,
const std::string_view* base_url_string) {
293 if (std::holds_alternative<url_pattern_init>(input)) {
294 if (base_url_string) {
298 std::string protocol{}, username{}, password{}, hostname{};
299 std::string port{}, pathname{}, search{}, hash{};
301 auto apply_result = url_pattern_init::process(
302 std::get<url_pattern_init>(input), url_pattern_init::process_type::url,
303 protocol, username, password, hostname, port, pathname, search, hash);
309 std::string_view search_view = *apply_result->search;
310 if (search_view.starts_with(
"?")) {
311 search_view.remove_prefix(1);
314 return test_components(*apply_result->protocol, *apply_result->username,
315 *apply_result->password, *apply_result->hostname,
316 *apply_result->port, *apply_result->pathname,
317 search_view, *apply_result->hash);
322 if (base_url_string) {
331 base_url.has_value() ? &*base_url :
nullptr);
338 if (protocol_view.ends_with(
":")) {
339 protocol_view.remove_suffix(1);
343 if (search_view.starts_with(
"?")) {
344 search_view.remove_prefix(1);
348 if (hash_view.starts_with(
"#")) {
349 hash_view.remove_prefix(1);
358template <url_pattern_regex::regex_concept regex_prov
ider>
360 const url_pattern_input& input,
const std::string_view* base_url_string) {
361 std::string protocol{};
362 std::string username{};
363 std::string password{};
364 std::string hostname{};
366 std::string pathname{};
367 std::string search{};
372 std::vector inputs{input};
375 if (std::holds_alternative<url_pattern_init>(input)) {
377 "url_pattern::match called with url_pattern_init and base_url_string=",
380 if (base_url_string) {
381 ada_log(
"failed to match because base_url_string was given");
388 auto apply_result = url_pattern_init::process(
389 std::get<url_pattern_init>(input), url_pattern_init::process_type::url,
390 protocol, username, password, hostname, port, pathname, search, hash);
393 if (!apply_result.has_value()) {
394 ada_log(
"match returned std::nullopt because process threw");
400 protocol = std::move(apply_result->protocol.value());
404 username = std::move(apply_result->username.value());
408 password = std::move(apply_result->password.value());
412 hostname = std::move(apply_result->hostname.value());
416 port = std::move(apply_result->port.value());
420 pathname = std::move(apply_result->pathname.value());
424 if (apply_result->search->starts_with(
"?")) {
425 search = apply_result->search->substr(1);
427 search = std::move(apply_result->search.value());
433 hash = std::move(apply_result->hash.value());
441 if (base_url_string) {
447 ada_log(
"match returned std::nullopt because failed to parse base_url=",
453 inputs.emplace_back(*base_url_string);
457 base_url.has_value() ? &*base_url :
nullptr;
465 ada_log(
"match returned std::nullopt because url failed");
492 search = view.starts_with(
"?") ?
url->
get_search().substr(1) : view;
500 hash = view.starts_with(
"#") ?
url->
get_hash().substr(1) : view;
509 auto protocol_exec_result = protocol_component.fast_match(protocol);
510 if (!protocol_exec_result) {
516 auto username_exec_result = username_component.fast_match(username);
517 if (!username_exec_result) {
523 auto password_exec_result = password_component.fast_match(password);
524 if (!password_exec_result) {
530 auto hostname_exec_result = hostname_component.fast_match(hostname);
531 if (!hostname_exec_result) {
537 auto port_exec_result = port_component.fast_match(port);
538 if (!port_exec_result) {
544 auto pathname_exec_result = pathname_component.fast_match(pathname);
545 if (!pathname_exec_result) {
551 auto search_exec_result = search_component.fast_match(search);
552 if (!search_exec_result) {
558 auto hash_exec_result = hash_component.fast_match(hash);
559 if (!hash_exec_result) {
564 auto result = url_pattern_result{};
566 result.inputs = std::move(inputs);
569 result.protocol = protocol_component.create_component_match_result(
570 std::move(protocol), std::move(*protocol_exec_result));
574 result.username = username_component.create_component_match_result(
575 std::move(username), std::move(*username_exec_result));
579 result.password = password_component.create_component_match_result(
580 std::move(password), std::move(*password_exec_result));
584 result.hostname = hostname_component.create_component_match_result(
585 std::move(hostname), std::move(*hostname_exec_result));
589 result.port = port_component.create_component_match_result(
590 std::move(port), std::move(*port_exec_result));
594 result.pathname = pathname_component.create_component_match_result(
595 std::move(pathname), std::move(*pathname_exec_result));
599 result.search = search_component.create_component_match_result(
600 std::move(search), std::move(*search_exec_result));
604 result.hash = hash_component.create_component_match_result(
605 std::move(hash), std::move(*hash_exec_result));
Cross-platform compiler macros and common definitions.
#define ADA_ASSERT_TRUE(COND)
#define ada_lifetime_bound
type
Enumeration of URL scheme types.
errors
Error codes for URL parsing operations.
template ada::result< url_aggregator > parse< url_aggregator >(std::string_view input, const url_aggregator *base_url)
tl::expected< result_type, ada::errors > result
Memory-efficient URL representation using a single buffer.
Represents a parsed URL with individual string components.
std::string get_search() const
std::string get_port() const
std::string get_protocol() const
constexpr std::string_view get_pathname() const noexcept
std::string get_hostname() const
const std::string & get_password() const noexcept
const std::string & get_username() const noexcept
constexpr bool has_search() const noexcept override
std::string get_hash() const
constexpr bool has_hash() const noexcept override
URLPattern API implementation.
Declaration for the URLPattern helpers.