5#ifndef ADA_URL_PATTERN_INL_H
6#define ADA_URL_PATTERN_INL_H
21#if ADA_INCLUDE_URL_PATTERN
24inline bool url_pattern_init::operator==(
const url_pattern_init& other)
const {
25 return protocol == other.protocol && username == other.username &&
26 password == other.password && hostname == other.hostname &&
27 port == other.port && search == other.search && hash == other.hash &&
28 pathname == other.pathname;
31inline bool url_pattern_component_result::operator==(
32 const url_pattern_component_result& other)
const {
33 return input == other.input && groups == other.groups;
36inline void url_pattern_result::rebind_owned_input_views(
37 const std::pair<const char*, size_t>* from,
size_t from_size) {
40 for (
auto& value :
inputs) {
41 auto* sv = std::get_if<std::string_view>(&value);
45 for (
size_t i = 0; i < from_size && i < owned_strings_.size(); ++i) {
46 const char*
const begin = from[i].first;
47 const char*
const end = begin + from[i].second;
48 if (sv->data() >= begin && sv->data() <= end &&
49 static_cast<size_t>(end - sv->data()) >= sv->size()) {
50 const size_t off =
static_cast<size_t>(sv->data() - begin);
51 value = std::string_view(owned_strings_[i].data() + off, sv->size());
58inline void url_pattern_result::take_inputs(
59 url_pattern_result& other)
noexcept {
63 std::pair<const char*, size_t> from[2];
65 other.owned_strings_.size() < 2 ? other.owned_strings_.size() : 2;
66 for (
size_t i = 0; i < n; ++i) {
67 from[i] = {other.owned_strings_[i].data(), other.owned_strings_[i].size()};
69 inputs = std::move(other.inputs);
70 owned_strings_ = std::move(other.owned_strings_);
71 rebind_owned_input_views(from, n);
73 other.owned_strings_.clear();
76inline void url_pattern_result::store_inputs(
const url_pattern_input& input,
77 const std::string_view* base_url) {
78 owned_strings_.clear();
82 owned_strings_.reserve(2);
83 if (std::holds_alternative<std::string_view>(input)) {
84 owned_strings_.emplace_back(std::get<std::string_view>(input));
85 inputs.emplace_back(std::string_view(owned_strings_.back()));
87 inputs.emplace_back(std::get<url_pattern_init>(input));
89 if (base_url !=
nullptr) {
90 owned_strings_.emplace_back(*base_url);
91 inputs.emplace_back(std::string_view(owned_strings_.back()));
95inline url_pattern_result::url_pattern_result(
const url_pattern_result& other)
97 protocol(other.protocol),
98 username(other.username),
99 password(other.password),
100 hostname(other.hostname),
102 pathname(other.pathname),
103 search(other.search),
105 owned_strings_(other.owned_strings_) {
108 std::pair<const char*, size_t> from[2];
110 other.owned_strings_.size() < 2 ? other.owned_strings_.size() : 2;
111 for (
size_t i = 0; i < n; ++i) {
112 from[i] = {other.owned_strings_[i].data(), other.owned_strings_[i].size()};
114 rebind_owned_input_views(from, n);
117inline url_pattern_result::url_pattern_result(
118 url_pattern_result&& other) noexcept
120 protocol(std::move(other.protocol)),
121 username(std::move(other.username)),
122 password(std::move(other.password)),
123 hostname(std::move(other.hostname)),
124 port(std::move(other.port)),
125 pathname(std::move(other.pathname)),
126 search(std::move(other.search)),
127 hash(std::move(other.hash)),
132inline url_pattern_result& url_pattern_result::operator=(
133 const url_pattern_result& other) {
134 if (
this != &other) {
135 url_pattern_result tmp(other);
136 *
this = std::move(tmp);
141inline url_pattern_result& url_pattern_result::operator=(
142 url_pattern_result&& other)
noexcept {
143 if (
this != &other) {
144 protocol = std::move(other.protocol);
145 username = std::move(other.username);
146 password = std::move(other.password);
147 hostname = std::move(other.hostname);
148 port = std::move(other.port);
149 pathname = std::move(other.pathname);
150 search = std::move(other.search);
151 hash = std::move(other.hash);
157template <url_pattern_regex::regex_concept regex_prov
ider>
158url_pattern_component_result
159url_pattern_component<regex_provider>::create_component_match_result(
161 std::vector<std::optional<std::string>>&& exec_result) {
166 url_pattern_component_result{.input = std::move(input), .groups = {}};
173 const size_t size = std::min(exec_result.size(), group_name_list.size());
174 result.groups.reserve(size);
175 for (
size_t index = 0; index < size; index++) {
176 result.groups.emplace(group_name_list[index],
177 std::move(exec_result[index]));
182template <url_pattern_regex::regex_concept regex_prov
ider>
183std::string_view url_pattern<regex_provider>::get_protocol() const
186 return protocol_component.pattern;
188template <url_pattern_regex::regex_concept regex_prov
ider>
189std::string_view url_pattern<regex_provider>::get_username() const
192 return username_component.pattern;
194template <url_pattern_regex::regex_concept regex_prov
ider>
195std::string_view url_pattern<regex_provider>::get_password() const
198 return password_component.pattern;
200template <url_pattern_regex::regex_concept regex_prov
ider>
201std::string_view url_pattern<regex_provider>::get_hostname() const
204 return hostname_component.pattern;
206template <url_pattern_regex::regex_concept regex_prov
ider>
207std::string_view url_pattern<regex_provider>::get_port() const
210 return port_component.pattern;
212template <url_pattern_regex::regex_concept regex_prov
ider>
213std::string_view url_pattern<regex_provider>::get_pathname() const
216 return pathname_component.pattern;
218template <url_pattern_regex::regex_concept regex_prov
ider>
219std::string_view url_pattern<regex_provider>::get_search() const
222 return search_component.pattern;
224template <url_pattern_regex::regex_concept regex_prov
ider>
225std::string_view url_pattern<regex_provider>::get_hash() const
228 return hash_component.pattern;
230template <url_pattern_regex::regex_concept regex_prov
ider>
231bool url_pattern<regex_provider>::ignore_case()
const {
234template <url_pattern_regex::regex_concept regex_prov
ider>
235bool url_pattern<regex_provider>::has_regexp_groups()
const {
237 return protocol_component.has_regexp_groups ||
238 username_component.has_regexp_groups ||
239 password_component.has_regexp_groups ||
240 hostname_component.has_regexp_groups ||
241 port_component.has_regexp_groups ||
242 pathname_component.has_regexp_groups ||
243 search_component.has_regexp_groups || hash_component.has_regexp_groups;
246inline bool url_pattern_part::is_regexp() const noexcept {
247 return type == url_pattern_part_type::REGEXP;
250inline std::string_view url_pattern_compile_component_options::get_delimiter()
253 return {&delimiter.value(), 1};
258inline std::string_view url_pattern_compile_component_options::get_prefix()
261 return {&prefix.value(), 1};
266template <url_pattern_regex::regex_concept regex_prov
ider>
267template <url_pattern_encoding_callback F>
268tl::expected<url_pattern_component<regex_provider>,
errors>
269url_pattern_component<regex_provider>::compile(
270 std::string_view input, F& encoding_callback,
271 url_pattern_compile_component_options& options) {
272 ada_log(
"url_pattern_component::compile input: ", input);
275 auto part_list = url_pattern_helpers::parse_pattern_string(input, options,
279 ada_log(
"parse_pattern_string failed");
280 return tl::unexpected(part_list.error());
284 const auto has_regexp = [](
const auto& part) {
return part.is_regexp(); };
285 const bool has_regexp_groups = std::ranges::any_of(*part_list, has_regexp);
287 url_pattern_component_type component_type =
288 url_pattern_component_type::REGEXP;
289 std::string exact_match_value{};
291 if (part_list->empty()) {
292 component_type = url_pattern_component_type::EMPTY;
293 }
else if (part_list->size() == 1) {
294 const auto& part = (*part_list)[0];
295 if (part.type == url_pattern_part_type::FIXED_TEXT &&
296 part.modifier == url_pattern_part_modifier::none &&
297 !options.ignore_case) {
298 component_type = url_pattern_component_type::EXACT_MATCH;
299 exact_match_value = part.value;
300 }
else if (part.type == url_pattern_part_type::FULL_WILDCARD &&
301 part.modifier == url_pattern_part_modifier::none &&
302 part.prefix.empty() && part.suffix.empty()) {
303 component_type = url_pattern_component_type::FULL_WILDCARD;
308 if (component_type != url_pattern_component_type::REGEXP) {
309 auto pattern_string =
310 url_pattern_helpers::generate_pattern_string(*part_list, options);
313 std::vector<std::string> name_list;
314 if (component_type == url_pattern_component_type::FULL_WILDCARD &&
315 !part_list->empty()) {
316 name_list.push_back((*part_list)[0].name);
318 return url_pattern_component<regex_provider>(
319 std::move(pattern_string),
typename regex_provider::regex_type{},
320 std::move(name_list), has_regexp_groups, component_type,
321 std::move(exact_match_value));
325 auto [regular_expression_string, name_list] =
326 url_pattern_helpers::generate_regular_expression_and_name_list(*part_list,
328 auto pattern_string =
329 url_pattern_helpers::generate_pattern_string(*part_list, options);
331 std::optional<typename regex_provider::regex_type> regular_expression =
332 regex_provider::create_instance(regular_expression_string,
333 options.ignore_case);
334 if (!regular_expression) {
335 return tl::unexpected(errors::type_error);
338 return url_pattern_component<regex_provider>(
339 std::move(pattern_string), std::move(*regular_expression),
340 std::move(name_list), has_regexp_groups, component_type,
341 std::move(exact_match_value));
344template <url_pattern_regex::regex_concept regex_prov
ider>
345bool url_pattern_component<regex_provider>::fast_test(
346 std::string_view input)
const {
349 if (type == url_pattern_component_type::FULL_WILDCARD) {
352 if (type == url_pattern_component_type::EXACT_MATCH) {
353 return input == exact_match_value;
355 if (type == url_pattern_component_type::EMPTY) {
356 return input.empty();
359 return regex_provider::regex_match(input, regexp);
362template <url_pattern_regex::regex_concept regex_prov
ider>
363std::optional<std::vector<std::optional<std::string>>>
364url_pattern_component<regex_provider>::fast_match(
365 std::string_view input)
const {
367 if (type == url_pattern_component_type::FULL_WILDCARD) {
370 if (group_name_list.empty()) {
371 return std::vector<std::optional<std::string>>{};
374 return std::vector<std::optional<std::string>>{std::string(input)};
376 if (type == url_pattern_component_type::EXACT_MATCH) {
377 if (input == exact_match_value) {
378 return std::vector<std::optional<std::string>>{};
382 if (type == url_pattern_component_type::EMPTY) {
384 return std::vector<std::optional<std::string>>{};
389 return regex_provider::regex_search(input, regexp);
392template <url_pattern_regex::regex_concept regex_prov
ider>
393result<std::optional<url_pattern_result>> url_pattern<regex_provider>::exec(
394 const url_pattern_input& input,
const std::string_view* base_url) {
397 return match(input, base_url);
400template <url_pattern_regex::regex_concept regex_prov
ider>
401bool url_pattern<regex_provider>::test_components(
402 std::string_view protocol, std::string_view username,
403 std::string_view password, std::string_view hostname, std::string_view port,
404 std::string_view pathname, std::string_view search,
405 std::string_view hash)
const {
406 return protocol_component.fast_test(protocol) &&
407 username_component.fast_test(username) &&
408 password_component.fast_test(password) &&
409 hostname_component.fast_test(hostname) &&
410 port_component.fast_test(port) &&
411 pathname_component.fast_test(pathname) &&
412 search_component.fast_test(search) && hash_component.fast_test(hash);
415template <url_pattern_regex::regex_concept regex_prov
ider>
416result<bool> url_pattern<regex_provider>::test(
417 const url_pattern_input& input,
const std::string_view* base_url_string) {
419 if (std::holds_alternative<url_pattern_init>(input)) {
420 if (base_url_string) {
421 return tl::unexpected(errors::type_error);
424 std::string protocol{}, username{}, password{}, hostname{};
425 std::string port{}, pathname{}, search{}, hash{};
427 auto apply_result = url_pattern_init::process(
428 std::get<url_pattern_init>(input), url_pattern_init::process_type::url,
429 protocol, username, password, hostname, port, pathname, search, hash);
435 std::string_view search_view = *apply_result->search;
436 if (search_view.starts_with(
"?")) {
437 search_view.remove_prefix(1);
440 return test_components(*apply_result->protocol, *apply_result->username,
441 *apply_result->password, *apply_result->hostname,
442 *apply_result->port, *apply_result->pathname,
443 search_view, *apply_result->hash);
450 result<url_aggregator> base_url = tl::unexpected(errors::type_error);
451 if (base_url_string) {
460 base_url.has_value() ? &*base_url :
nullptr);
466 auto protocol_view =
url->get_protocol();
467 if (protocol_view.ends_with(
":")) {
468 protocol_view.remove_suffix(1);
471 auto search_view =
url->get_search();
472 if (search_view.starts_with(
"?")) {
473 search_view.remove_prefix(1);
476 auto hash_view =
url->get_hash();
477 if (hash_view.starts_with(
"#")) {
478 hash_view.remove_prefix(1);
481 return test_components(protocol_view,
url->get_username(),
482 url->get_password(),
url->get_hostname(),
483 url->get_port(),
url->get_pathname(), search_view,
487template <url_pattern_regex::regex_concept regex_prov
ider>
488result<std::optional<url_pattern_result>> url_pattern<regex_provider>::match(
489 const url_pattern_input& input,
const std::string_view* base_url_string) {
490 std::string protocol{};
491 std::string username{};
492 std::string password{};
493 std::string hostname{};
495 std::string pathname{};
496 std::string search{};
500 if (std::holds_alternative<url_pattern_init>(input)) {
502 "url_pattern::match called with url_pattern_init and base_url_string=",
505 if (base_url_string) {
506 ada_log(
"failed to match because base_url_string was given");
507 return tl::unexpected(errors::type_error);
513 auto apply_result = url_pattern_init::process(
514 std::get<url_pattern_init>(input), url_pattern_init::process_type::url,
515 protocol, username, password, hostname, port, pathname, search, hash);
518 if (!apply_result.has_value()) {
519 ada_log(
"match returned std::nullopt because process threw");
525 protocol = std::move(apply_result->protocol.value());
529 username = std::move(apply_result->username.value());
533 password = std::move(apply_result->password.value());
537 hostname = std::move(apply_result->hostname.value());
541 port = std::move(apply_result->port.value());
545 pathname = std::move(apply_result->pathname.value());
549 if (apply_result->search->starts_with(
"?")) {
550 search = apply_result->search->substr(1);
552 search = std::move(apply_result->search.value());
558 hash = std::move(apply_result->hash.value());
565 result<url_aggregator> base_url = tl::unexpected(errors::type_error);
568 if (base_url_string) {
574 ada_log(
"match returned std::nullopt because failed to parse base_url=",
580 url_aggregator* base_url_value =
581 base_url.has_value() ? &*base_url :
nullptr;
589 ada_log(
"match returned std::nullopt because url failed");
597 protocol =
url->get_protocol().substr(0,
url->get_protocol().size() - 1);
599 username =
url->get_username();
601 password =
url->get_password();
604 hostname =
url->get_hostname();
607 port =
url->get_port();
609 pathname =
url->get_pathname();
614 if (
url->has_search()) {
615 auto view =
url->get_search();
616 search = view.starts_with(
"?") ?
url->get_search().substr(1) : view;
622 if (
url->has_hash()) {
623 auto view =
url->get_hash();
624 hash = view.starts_with(
"#") ?
url->get_hash().substr(1) : view;
633 auto protocol_exec_result = protocol_component.fast_match(protocol);
634 if (!protocol_exec_result) {
640 auto username_exec_result = username_component.fast_match(username);
641 if (!username_exec_result) {
647 auto password_exec_result = password_component.fast_match(password);
648 if (!password_exec_result) {
654 auto hostname_exec_result = hostname_component.fast_match(hostname);
655 if (!hostname_exec_result) {
661 auto port_exec_result = port_component.fast_match(port);
662 if (!port_exec_result) {
668 auto pathname_exec_result = pathname_component.fast_match(pathname);
669 if (!pathname_exec_result) {
675 auto search_exec_result = search_component.fast_match(search);
676 if (!search_exec_result) {
682 auto hash_exec_result = hash_component.fast_match(hash);
683 if (!hash_exec_result) {
688 auto result = url_pattern_result{};
691 result.store_inputs(input, base_url_string);
694 result.protocol = protocol_component.create_component_match_result(
695 std::move(protocol), std::move(*protocol_exec_result));
699 result.username = username_component.create_component_match_result(
700 std::move(username), std::move(*username_exec_result));
704 result.password = password_component.create_component_match_result(
705 std::move(password), std::move(*password_exec_result));
709 result.hostname = hostname_component.create_component_match_result(
710 std::move(hostname), std::move(*hostname_exec_result));
714 result.port = port_component.create_component_match_result(
715 std::move(port), std::move(*port_exec_result));
719 result.pathname = pathname_component.create_component_match_result(
720 std::move(pathname), std::move(*pathname_exec_result));
724 result.search = search_component.create_component_match_result(
725 std::move(search), std::move(*search_exec_result));
729 result.hash = hash_component.create_component_match_result(
730 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
URLPattern API implementation.
Declaration for the URLPattern helpers.