Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern-inl.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_PATTERN_INL_H
6#define ADA_URL_PATTERN_INL_H
7
8#include "ada/common_defs.h"
10#include "ada/url_pattern.h"
11
12#include <algorithm>
13#include <cstddef>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <utility>
18#include <variant>
19#include <vector>
20
21#if ADA_INCLUDE_URL_PATTERN
22namespace ada {
23
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;
29}
30
31inline bool url_pattern_component_result::operator==(
32 const url_pattern_component_result& other) const {
33 return input == other.input && groups == other.groups;
34}
35
36inline void url_pattern_result::rebind_owned_input_views(
37 const std::pair<const char*, size_t>* from, size_t from_size) {
38 // A view the caller pushed into `inputs` does not alias `from` and
39 // is left unchanged.
40 for (auto& value : inputs) {
41 auto* sv = std::get_if<std::string_view>(&value);
42 if (sv == nullptr) {
43 continue;
44 }
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());
52 break;
53 }
54 }
55 }
56}
57
58inline void url_pattern_result::take_inputs(
59 url_pattern_result& other) noexcept {
60 // SSO buffers change address when a std::string is moved, so the
61 // source addresses have to be captured first. store_inputs never
62 // owns more than two strings, which keeps this noexcept.
63 std::pair<const char*, size_t> from[2];
64 const size_t n =
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()};
68 }
69 inputs = std::move(other.inputs);
70 owned_strings_ = std::move(other.owned_strings_);
71 rebind_owned_input_views(from, n);
72 other.inputs.clear();
73 other.owned_strings_.clear();
74}
75
76inline void url_pattern_result::store_inputs(const url_pattern_input& input,
77 const std::string_view* base_url) {
78 owned_strings_.clear();
79 inputs.clear();
80 // inputs[] holds views into owned_strings_, so it must not reallocate
81 // (and move SSO buffers) after the first view is taken.
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()));
86 } else {
87 inputs.emplace_back(std::get<url_pattern_init>(input));
88 }
89 if (base_url != nullptr) {
90 owned_strings_.emplace_back(*base_url);
91 inputs.emplace_back(std::string_view(owned_strings_.back()));
92 }
93}
94
95inline url_pattern_result::url_pattern_result(const url_pattern_result& other)
96 : inputs(other.inputs),
97 protocol(other.protocol),
98 username(other.username),
99 password(other.password),
100 hostname(other.hostname),
101 port(other.port),
102 pathname(other.pathname),
103 search(other.search),
104 hash(other.hash),
105 owned_strings_(other.owned_strings_) {
106 // `other` is unchanged, so its buffers are still the ones inputs
107 // may point at.
108 std::pair<const char*, size_t> from[2];
109 const size_t n =
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()};
113 }
114 rebind_owned_input_views(from, n);
115}
116
117inline url_pattern_result::url_pattern_result(
118 url_pattern_result&& other) noexcept
119 : inputs(),
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)),
128 owned_strings_() {
129 take_inputs(other);
130}
131
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);
137 }
138 return *this;
139}
140
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);
152 take_inputs(other);
153 }
154 return *this;
155}
156
157template <url_pattern_regex::regex_concept regex_provider>
158url_pattern_component_result
159url_pattern_component<regex_provider>::create_component_match_result(
160 std::string&& input,
161 std::vector<std::optional<std::string>>&& exec_result) {
162 // Let result be a new URLPatternComponentResult.
163 // Set result["input"] to input.
164 // Let groups be a record<USVString, (USVString or undefined)>.
165 auto result =
166 url_pattern_component_result{.input = std::move(input), .groups = {}};
167
168 // We explicitly start iterating from 0 even though the spec
169 // says we should start from 1. This case is handled by the
170 // std_regex_provider which removes the full match from index 0.
171 // Use min() to guard against potential mismatches between
172 // exec_result size and group_name_list size.
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]));
178 }
179 return result;
180}
181
182template <url_pattern_regex::regex_concept regex_provider>
183std::string_view url_pattern<regex_provider>::get_protocol() const
185 // Return this's associated URL pattern's protocol component's pattern string.
186 return protocol_component.pattern;
187}
188template <url_pattern_regex::regex_concept regex_provider>
189std::string_view url_pattern<regex_provider>::get_username() const
191 // Return this's associated URL pattern's username component's pattern string.
192 return username_component.pattern;
193}
194template <url_pattern_regex::regex_concept regex_provider>
195std::string_view url_pattern<regex_provider>::get_password() const
197 // Return this's associated URL pattern's password component's pattern string.
198 return password_component.pattern;
199}
200template <url_pattern_regex::regex_concept regex_provider>
201std::string_view url_pattern<regex_provider>::get_hostname() const
203 // Return this's associated URL pattern's hostname component's pattern string.
204 return hostname_component.pattern;
205}
206template <url_pattern_regex::regex_concept regex_provider>
207std::string_view url_pattern<regex_provider>::get_port() const
209 // Return this's associated URL pattern's port component's pattern string.
210 return port_component.pattern;
211}
212template <url_pattern_regex::regex_concept regex_provider>
213std::string_view url_pattern<regex_provider>::get_pathname() const
215 // Return this's associated URL pattern's pathname component's pattern string.
216 return pathname_component.pattern;
217}
218template <url_pattern_regex::regex_concept regex_provider>
219std::string_view url_pattern<regex_provider>::get_search() const
221 // Return this's associated URL pattern's search component's pattern string.
222 return search_component.pattern;
223}
224template <url_pattern_regex::regex_concept regex_provider>
225std::string_view url_pattern<regex_provider>::get_hash() const
227 // Return this's associated URL pattern's hash component's pattern string.
228 return hash_component.pattern;
229}
230template <url_pattern_regex::regex_concept regex_provider>
231bool url_pattern<regex_provider>::ignore_case() const {
232 return ignore_case_;
233}
234template <url_pattern_regex::regex_concept regex_provider>
235bool url_pattern<regex_provider>::has_regexp_groups() const {
236 // If this's associated URL pattern's has regexp groups, then return true.
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;
244}
245
246inline bool url_pattern_part::is_regexp() const noexcept {
247 return type == url_pattern_part_type::REGEXP;
248}
249
250inline std::string_view url_pattern_compile_component_options::get_delimiter()
251 const {
252 if (delimiter) {
253 return {&delimiter.value(), 1};
254 }
255 return {};
256}
257
258inline std::string_view url_pattern_compile_component_options::get_prefix()
259 const {
260 if (prefix) {
261 return {&prefix.value(), 1};
262 }
263 return {};
264}
265
266template <url_pattern_regex::regex_concept regex_provider>
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);
273 // Let part list be the result of running parse a pattern string given input,
274 // options, and encoding callback.
275 auto part_list = url_pattern_helpers::parse_pattern_string(input, options,
276 encoding_callback);
277
278 if (!part_list) {
279 ada_log("parse_pattern_string failed");
280 return tl::unexpected(part_list.error());
281 }
282
283 // Detect pattern type early to potentially skip expensive regex compilation
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);
286
287 url_pattern_component_type component_type =
288 url_pattern_component_type::REGEXP;
289 std::string exact_match_value{};
290
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;
304 }
305 }
306
307 // For simple patterns, skip regex generation and compilation entirely
308 if (component_type != url_pattern_component_type::REGEXP) {
309 auto pattern_string =
310 url_pattern_helpers::generate_pattern_string(*part_list, options);
311 // For FULL_WILDCARD, we need the group name from
312 // generate_regular_expression
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);
317 }
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));
322 }
323
324 // Generate regex for complex patterns
325 auto [regular_expression_string, name_list] =
326 url_pattern_helpers::generate_regular_expression_and_name_list(*part_list,
327 options);
328 auto pattern_string =
329 url_pattern_helpers::generate_pattern_string(*part_list, options);
330
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);
336 }
337
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));
342}
343
344template <url_pattern_regex::regex_concept regex_provider>
345bool url_pattern_component<regex_provider>::fast_test(
346 std::string_view input) const {
347 // Fast path for simple patterns - avoid regex evaluation
348 // Using if-else for better branch prediction on common cases
349 if (type == url_pattern_component_type::FULL_WILDCARD) {
350 return true;
351 }
352 if (type == url_pattern_component_type::EXACT_MATCH) {
353 return input == exact_match_value;
354 }
355 if (type == url_pattern_component_type::EMPTY) {
356 return input.empty();
357 }
358 // type == REGEXP
359 return regex_provider::regex_match(input, regexp);
360}
361
362template <url_pattern_regex::regex_concept regex_provider>
363std::optional<std::vector<std::optional<std::string>>>
364url_pattern_component<regex_provider>::fast_match(
365 std::string_view input) const {
366 // Handle each type directly without redundant checks
367 if (type == url_pattern_component_type::FULL_WILDCARD) {
368 // FULL_WILDCARD always matches - capture the input (even if empty)
369 // If there's no group name, return empty groups
370 if (group_name_list.empty()) {
371 return std::vector<std::optional<std::string>>{};
372 }
373 // Capture the matched input (including empty strings)
374 return std::vector<std::optional<std::string>>{std::string(input)};
375 }
376 if (type == url_pattern_component_type::EXACT_MATCH) {
377 if (input == exact_match_value) {
378 return std::vector<std::optional<std::string>>{};
379 }
380 return std::nullopt;
381 }
382 if (type == url_pattern_component_type::EMPTY) {
383 if (input.empty()) {
384 return std::vector<std::optional<std::string>>{};
385 }
386 return std::nullopt;
387 }
388 // type == REGEXP - use regex
389 return regex_provider::regex_search(input, regexp);
390}
391
392template <url_pattern_regex::regex_concept regex_provider>
393result<std::optional<url_pattern_result>> url_pattern<regex_provider>::exec(
394 const url_pattern_input& input, const std::string_view* base_url) {
395 // Return the result of match given this's associated URL pattern, input, and
396 // baseURL if given.
397 return match(input, base_url);
398}
399
400template <url_pattern_regex::regex_concept regex_provider>
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);
413}
414
415template <url_pattern_regex::regex_concept regex_provider>
416result<bool> url_pattern<regex_provider>::test(
417 const url_pattern_input& input, const std::string_view* base_url_string) {
418 // If input is a URLPatternInit
419 if (std::holds_alternative<url_pattern_init>(input)) {
420 if (base_url_string) {
421 return tl::unexpected(errors::type_error);
422 }
423
424 std::string protocol{}, username{}, password{}, hostname{};
425 std::string port{}, pathname{}, search{}, hash{};
426
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);
430
431 if (!apply_result) {
432 return false;
433 }
434
435 std::string_view search_view = *apply_result->search;
436 if (search_view.starts_with("?")) {
437 search_view.remove_prefix(1);
438 }
439
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);
444 }
445
446 // URL string input path.
447 // Let baseURL be null. A default-constructed result holds a value (an
448 // empty URL), and a relative input would then resolve against it. Start
449 // from an error so "no base given" means no value.
450 result<url_aggregator> base_url = tl::unexpected(errors::type_error);
451 if (base_url_string) {
452 base_url = ada::parse<url_aggregator>(*base_url_string, nullptr);
453 if (!base_url) {
454 return false;
455 }
456 }
457
458 auto url =
459 ada::parse<url_aggregator>(std::get<std::string_view>(input),
460 base_url.has_value() ? &*base_url : nullptr);
461 if (!url) {
462 return false;
463 }
464
465 // Extract components as string_view
466 auto protocol_view = url->get_protocol();
467 if (protocol_view.ends_with(":")) {
468 protocol_view.remove_suffix(1);
469 }
470
471 auto search_view = url->get_search();
472 if (search_view.starts_with("?")) {
473 search_view.remove_prefix(1);
474 }
475
476 auto hash_view = url->get_hash();
477 if (hash_view.starts_with("#")) {
478 hash_view.remove_prefix(1);
479 }
480
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,
484 hash_view);
485}
486
487template <url_pattern_regex::regex_concept regex_provider>
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{};
494 std::string port{};
495 std::string pathname{};
496 std::string search{};
497 std::string hash{};
498
499 // If input is a URLPatternInit then:
500 if (std::holds_alternative<url_pattern_init>(input)) {
501 ada_log(
502 "url_pattern::match called with url_pattern_init and base_url_string=",
503 base_url_string);
504 // If baseURLString was given, throw a TypeError.
505 if (base_url_string) {
506 ada_log("failed to match because base_url_string was given");
507 return tl::unexpected(errors::type_error);
508 }
509
510 // Let applyResult be the result of process a URLPatternInit given input,
511 // "url", protocol, username, password, hostname, port, pathname, search,
512 // and hash.
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);
516
517 // If this throws an exception, catch it, and return null.
518 if (!apply_result.has_value()) {
519 ada_log("match returned std::nullopt because process threw");
520 return std::nullopt;
521 }
522
523 // Set protocol to applyResult["protocol"].
524 ADA_ASSERT_TRUE(apply_result->protocol.has_value());
525 protocol = std::move(apply_result->protocol.value());
526
527 // Set username to applyResult["username"].
528 ADA_ASSERT_TRUE(apply_result->username.has_value());
529 username = std::move(apply_result->username.value());
530
531 // Set password to applyResult["password"].
532 ADA_ASSERT_TRUE(apply_result->password.has_value());
533 password = std::move(apply_result->password.value());
534
535 // Set hostname to applyResult["hostname"].
536 ADA_ASSERT_TRUE(apply_result->hostname.has_value());
537 hostname = std::move(apply_result->hostname.value());
538
539 // Set port to applyResult["port"].
540 ADA_ASSERT_TRUE(apply_result->port.has_value());
541 port = std::move(apply_result->port.value());
542
543 // Set pathname to applyResult["pathname"].
544 ADA_ASSERT_TRUE(apply_result->pathname.has_value());
545 pathname = std::move(apply_result->pathname.value());
546
547 // Set search to applyResult["search"].
548 ADA_ASSERT_TRUE(apply_result->search.has_value());
549 if (apply_result->search->starts_with("?")) {
550 search = apply_result->search->substr(1);
551 } else {
552 search = std::move(apply_result->search.value());
553 }
554
555 // Set hash to applyResult["hash"].
556 ADA_ASSERT_TRUE(apply_result->hash.has_value());
557 ADA_ASSERT_TRUE(!apply_result->hash->starts_with("#"));
558 hash = std::move(apply_result->hash.value());
559 } else {
560 ADA_ASSERT_TRUE(std::holds_alternative<std::string_view>(input));
561
562 // Let baseURL be null. A default-constructed result holds a value (an
563 // empty URL), and a relative input would then resolve against it. Start
564 // from an error so "no base given" means no value.
565 result<url_aggregator> base_url = tl::unexpected(errors::type_error);
566
567 // If baseURLString was given, then:
568 if (base_url_string) {
569 // Let baseURL be the result of parsing baseURLString.
570 base_url = ada::parse<url_aggregator>(*base_url_string, nullptr);
571
572 // If baseURL is failure, return null.
573 if (!base_url) {
574 ada_log("match returned std::nullopt because failed to parse base_url=",
575 *base_url_string);
576 return std::nullopt;
577 }
578 }
579
580 url_aggregator* base_url_value =
581 base_url.has_value() ? &*base_url : nullptr;
582
583 // Set url to the result of parsing input given baseURL.
584 auto url = ada::parse<url_aggregator>(std::get<std::string_view>(input),
585 base_url_value);
586
587 // If url is failure, return null.
588 if (!url) {
589 ada_log("match returned std::nullopt because url failed");
590 return std::nullopt;
591 }
592
593 // Set protocol to url's scheme.
594 // IMPORTANT: Not documented on the URLPattern spec, but protocol suffix ':'
595 // is removed. Similar work was done on workerd:
596 // https://github.com/cloudflare/workerd/blob/8620d14012513a6ce04d079e401d3becac3c67bd/src/workerd/jsg/url.c%2B%2B#L2038
597 protocol = url->get_protocol().substr(0, url->get_protocol().size() - 1);
598 // Set username to url's username.
599 username = url->get_username();
600 // Set password to url's password.
601 password = url->get_password();
602 // Set hostname to url's host, serialized, or the empty string if the value
603 // is null.
604 hostname = url->get_hostname();
605 // Set port to url's port, serialized, or the empty string if the value is
606 // null.
607 port = url->get_port();
608 // Set pathname to the result of URL path serializing url.
609 pathname = url->get_pathname();
610 // Set search to url's query or the empty string if the value is null.
611 // IMPORTANT: Not documented on the URLPattern spec, but search prefix '?'
612 // is removed. Similar work was done on workerd:
613 // https://github.com/cloudflare/workerd/blob/8620d14012513a6ce04d079e401d3becac3c67bd/src/workerd/jsg/url.c%2B%2B#L2232
614 if (url->has_search()) {
615 auto view = url->get_search();
616 search = view.starts_with("?") ? url->get_search().substr(1) : view;
617 }
618 // Set hash to url's fragment or the empty string if the value is null.
619 // IMPORTANT: Not documented on the URLPattern spec, but hash prefix '#' is
620 // removed. Similar work was done on workerd:
621 // https://github.com/cloudflare/workerd/blob/8620d14012513a6ce04d079e401d3becac3c67bd/src/workerd/jsg/url.c%2B%2B#L2242
622 if (url->has_hash()) {
623 auto view = url->get_hash();
624 hash = view.starts_with("#") ? url->get_hash().substr(1) : view;
625 }
626 }
627
628 // Use fast_match which skips regex for simple patterns (EMPTY, EXACT_MATCH,
629 // FULL_WILDCARD) and only falls back to regex for complex REGEXP patterns.
630
631 // Let protocolExecResult be RegExpBuiltinExec(urlPattern's protocol
632 // component's regular expression, protocol).
633 auto protocol_exec_result = protocol_component.fast_match(protocol);
634 if (!protocol_exec_result) {
635 return std::nullopt;
636 }
637
638 // Let usernameExecResult be RegExpBuiltinExec(urlPattern's username
639 // component's regular expression, username).
640 auto username_exec_result = username_component.fast_match(username);
641 if (!username_exec_result) {
642 return std::nullopt;
643 }
644
645 // Let passwordExecResult be RegExpBuiltinExec(urlPattern's password
646 // component's regular expression, password).
647 auto password_exec_result = password_component.fast_match(password);
648 if (!password_exec_result) {
649 return std::nullopt;
650 }
651
652 // Let hostnameExecResult be RegExpBuiltinExec(urlPattern's hostname
653 // component's regular expression, hostname).
654 auto hostname_exec_result = hostname_component.fast_match(hostname);
655 if (!hostname_exec_result) {
656 return std::nullopt;
657 }
658
659 // Let portExecResult be RegExpBuiltinExec(urlPattern's port component's
660 // regular expression, port).
661 auto port_exec_result = port_component.fast_match(port);
662 if (!port_exec_result) {
663 return std::nullopt;
664 }
665
666 // Let pathnameExecResult be RegExpBuiltinExec(urlPattern's pathname
667 // component's regular expression, pathname).
668 auto pathname_exec_result = pathname_component.fast_match(pathname);
669 if (!pathname_exec_result) {
670 return std::nullopt;
671 }
672
673 // Let searchExecResult be RegExpBuiltinExec(urlPattern's search component's
674 // regular expression, search).
675 auto search_exec_result = search_component.fast_match(search);
676 if (!search_exec_result) {
677 return std::nullopt;
678 }
679
680 // Let hashExecResult be RegExpBuiltinExec(urlPattern's hash component's
681 // regular expression, hash).
682 auto hash_exec_result = hash_component.fast_match(hash);
683 if (!hash_exec_result) {
684 return std::nullopt;
685 }
686
687 // Let result be a new URLPatternResult.
688 auto result = url_pattern_result{};
689 // Set result["inputs"] to inputs. Copy URL / base URL text into the
690 // result so inputs[] string_views outlive the caller's arguments.
691 result.store_inputs(input, base_url_string);
692 // Set result["protocol"] to the result of creating a component match result
693 // given urlPattern's protocol component, protocol, and protocolExecResult.
694 result.protocol = protocol_component.create_component_match_result(
695 std::move(protocol), std::move(*protocol_exec_result));
696
697 // Set result["username"] to the result of creating a component match result
698 // given urlPattern's username component, username, and usernameExecResult.
699 result.username = username_component.create_component_match_result(
700 std::move(username), std::move(*username_exec_result));
701
702 // Set result["password"] to the result of creating a component match result
703 // given urlPattern's password component, password, and passwordExecResult.
704 result.password = password_component.create_component_match_result(
705 std::move(password), std::move(*password_exec_result));
706
707 // Set result["hostname"] to the result of creating a component match result
708 // given urlPattern's hostname component, hostname, and hostnameExecResult.
709 result.hostname = hostname_component.create_component_match_result(
710 std::move(hostname), std::move(*hostname_exec_result));
711
712 // Set result["port"] to the result of creating a component match result given
713 // urlPattern's port component, port, and portExecResult.
714 result.port = port_component.create_component_match_result(
715 std::move(port), std::move(*port_exec_result));
716
717 // Set result["pathname"] to the result of creating a component match result
718 // given urlPattern's pathname component, pathname, and pathnameExecResult.
719 result.pathname = pathname_component.create_component_match_result(
720 std::move(pathname), std::move(*pathname_exec_result));
721
722 // Set result["search"] to the result of creating a component match result
723 // given urlPattern's search component, search, and searchExecResult.
724 result.search = search_component.create_component_match_result(
725 std::move(search), std::move(*search_exec_result));
726
727 // Set result["hash"] to the result of creating a component match result given
728 // urlPattern's hash component, hash, and hashExecResult.
729 result.hash = hash_component.create_component_match_result(
730 std::move(hash), std::move(*hash_exec_result));
731
732 return result;
733}
734
735} // namespace ada
736#endif // ADA_INCLUDE_URL_PATTERN
737#endif
std::string inputs[]
Definition to_ascii.cpp:19
Cross-platform compiler macros and common definitions.
#define ADA_ASSERT_TRUE(COND)
#define ada_lifetime_bound
type
Enumeration of URL scheme types.
Definition scheme.h:43
errors
Error codes for URL parsing operations.
Definition errors.h:17
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.