1#if ADA_INCLUDE_URL_PATTERN
16namespace ada::url_pattern_helpers {
18std::tuple<std::string, std::vector<std::string>>
19generate_regular_expression_and_name_list(
20 const std::vector<url_pattern_part>& part_list,
21 url_pattern_compile_component_options options) {
25 result.reserve(part_list.size() * 16);
28 std::vector<std::string> name_list{};
29 name_list.reserve(part_list.size());
32 std::string segment_wildcard_regexp;
35 for (
const url_pattern_part& part : part_list) {
37 if (part.type == url_pattern_part_type::FIXED_TEXT) {
39 if (part.modifier == url_pattern_part_modifier::none) {
40 result.append(escape_regexp_string(part.value));
44 result.append(escape_regexp_string(part.value));
46 result.append(convert_modifier_to_string(part.modifier));
53 name_list.push_back(part.name);
56 std::string_view regexp_value = part.value;
58 if (part.type == url_pattern_part_type::SEGMENT_WILDCARD) {
60 if (segment_wildcard_regexp.empty()) {
61 segment_wildcard_regexp = generate_segment_wildcard_regexp(options);
63 regexp_value = segment_wildcard_regexp;
64 }
else if (part.type == url_pattern_part_type::FULL_WILDCARD) {
70 if (part.prefix.empty() && part.suffix.empty()) {
72 if (part.modifier == url_pattern_part_modifier::none ||
73 part.modifier == url_pattern_part_modifier::optional) {
76 result.append(regexp_value);
78 result.append(convert_modifier_to_string(part.modifier));
82 result.append(regexp_value);
84 result.append(convert_modifier_to_string(part.modifier));
91 if (part.modifier == url_pattern_part_modifier::none ||
92 part.modifier == url_pattern_part_modifier::optional) {
95 result.append(escape_regexp_string(part.prefix));
97 result.append(regexp_value);
99 result.append(escape_regexp_string(part.suffix));
101 result.append(convert_modifier_to_string(part.modifier));
106 ADA_ASSERT_TRUE(part.modifier == url_pattern_part_modifier::zero_or_more ||
107 part.modifier == url_pattern_part_modifier::one_or_more);
119 result.append(escape_regexp_string(part.prefix));
123 result.append(regexp_value);
128 result.append(escape_regexp_string(part.suffix));
131 result.append(escape_regexp_string(part.prefix));
135 result.append(regexp_value);
140 result.append(escape_regexp_string(part.suffix));
145 if (part.modifier == url_pattern_part_modifier::zero_or_more) {
154 return {std::move(result), std::move(name_list)};
157bool is_ipv6_address(std::string_view input)
noexcept {
159 if (input.size() < 2)
return false;
163 if (input.front() ==
'[')
return true;
166 if (input.starts_with(
"{["))
return true;
169 return input.starts_with(
"\\[");
172std::string_view convert_modifier_to_string(
173 url_pattern_part_modifier modifier) {
176 case url_pattern_part_modifier::zero_or_more:
179 case url_pattern_part_modifier::optional:
182 case url_pattern_part_modifier::one_or_more:
190std::string generate_segment_wildcard_regexp(
191 url_pattern_compile_component_options options) {
193 std::string
result =
"[^";
196 result.append(escape_regexp_string(options.get_delimiter()));
200 ada_log(
"generate_segment_wildcard_regexp result: ", result);
207constexpr uint8_t CHAR_SCHEME = 1;
208constexpr uint8_t CHAR_UPPER = 2;
209constexpr uint8_t CHAR_SIMPLE_HOSTNAME = 4;
210constexpr uint8_t CHAR_SIMPLE_PATHNAME =
213constexpr std::array<uint8_t, 256> char_class_table = []()
consteval {
214 std::array<uint8_t, 256>
table{};
215 for (
int c =
'a'; c <=
'z'; c++)
216 table[c] = CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME | CHAR_SIMPLE_PATHNAME;
217 for (
int c =
'A'; c <=
'Z'; c++)
218 table[c] = CHAR_SCHEME | CHAR_UPPER | CHAR_SIMPLE_PATHNAME;
219 for (
int c =
'0'; c <=
'9'; c++)
220 table[c] = CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME | CHAR_SIMPLE_PATHNAME;
221 table[
'+'] = CHAR_SCHEME;
222 table[
'-'] = CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME | CHAR_SIMPLE_PATHNAME;
224 CHAR_SCHEME | CHAR_SIMPLE_HOSTNAME;
225 table[
'/'] = CHAR_SIMPLE_PATHNAME;
226 table[
'_'] = CHAR_SIMPLE_PATHNAME;
227 table[
'~'] = CHAR_SIMPLE_PATHNAME;
232tl::expected<std::string, errors> canonicalize_protocol(
233 std::string_view input) {
234 ada_log(
"canonicalize_protocol called with input=", input);
235 if (input.empty()) [[unlikely]] {
239 if (input.ends_with(
":")) {
240 input.remove_suffix(1);
244 if (scheme::is_special(input)) {
245 return std::string(input);
250 uint8_t first_flags = char_class_table[
static_cast<uint8_t
>(input[0])];
251 if (!(first_flags & CHAR_SCHEME) || input[0] ==
'+' || input[0] ==
'-' ||
252 input[0] ==
'.' || unicode::is_ascii_digit(input[0])) {
253 return tl::unexpected(errors::type_error);
256 uint8_t needs_lowercase = first_flags & CHAR_UPPER;
257 for (
size_t i = 1; i < input.size(); i++) {
258 uint8_t flags = char_class_table[
static_cast<uint8_t
>(input[i])];
259 if (!(flags & CHAR_SCHEME)) {
260 return tl::unexpected(errors::type_error);
262 needs_lowercase |= flags & CHAR_UPPER;
265 if (needs_lowercase == 0) {
266 return std::string(input);
269 std::string
result(input);
274tl::expected<std::string, errors> canonicalize_username(
275 std::string_view input) {
277 if (input.empty()) [[unlikely]] {
282 input, character_sets::USERINFO_PERCENT_ENCODE);
283 if (idx == input.size()) {
285 return std::string(input);
288 return ada::unicode::percent_encode(
289 input, character_sets::USERINFO_PERCENT_ENCODE, idx);
292tl::expected<std::string, errors> canonicalize_password(
293 std::string_view input) {
295 if (input.empty()) [[unlikely]] {
300 input, character_sets::USERINFO_PERCENT_ENCODE);
301 if (idx == input.size()) {
303 return std::string(input);
306 return ada::unicode::percent_encode(
307 input, character_sets::USERINFO_PERCENT_ENCODE, idx);
310tl::expected<std::string, errors> canonicalize_hostname(
311 std::string_view input) {
312 ada_log(
"canonicalize_hostname input=", input);
313 if (input.empty()) [[unlikely]] {
318 bool needs_processing =
false;
319 for (
char c : input) {
321 !(char_class_table[
static_cast<uint8_t
>(c)] & CHAR_SIMPLE_HOSTNAME);
323 if (!needs_processing) {
324 return std::string(input);
336 if (!url->set_hostname(input)) {
338 return tl::unexpected(errors::type_error);
341 return std::string(url->get_hostname());
344tl::expected<std::string, errors> canonicalize_ipv6_hostname(
345 std::string_view input) {
346 ada_log(
"canonicalize_ipv6_hostname input=", input);
348 if (std::ranges::any_of(input, [](
char c) {
349 return c !=
'[' && c !=
']' && c !=
':' &&
350 !unicode::is_ascii_hex_digit(c);
352 return tl::unexpected(errors::type_error);
356 auto hostname = std::string(input);
357 unicode::to_lower_ascii(hostname.data(), hostname.size());
361tl::expected<std::string, errors> canonicalize_port(
362 std::string_view port_value) {
364 if (port_value.empty()) [[unlikely]] {
369 std::string trimmed(port_value);
370 helpers::remove_ascii_tab_or_newline(trimmed);
372 if (trimmed.empty()) {
377 if (!unicode::is_ascii_digit(trimmed.front())) {
378 return tl::unexpected(errors::type_error);
382 auto first_non_digit =
383 std::ranges::find_if_not(trimmed, unicode::is_ascii_digit);
384 std::string_view digits_to_parse =
385 std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
393 if (digits_to_parse.size() == 5) {
394 if (digits_to_parse >
"65535") {
395 return tl::unexpected(errors::type_error);
397 }
else if (digits_to_parse.size() > 5) {
398 return tl::unexpected(errors::type_error);
400 if (digits_to_parse[0] ==
'0' && digits_to_parse.size() > 1) {
402 return tl::unexpected(errors::type_error);
405 return std::string(digits_to_parse);
408tl::expected<std::string, errors> canonicalize_port_with_protocol(
409 std::string_view port_value, std::string_view protocol) {
411 if (port_value.empty()) [[unlikely]] {
416 if (protocol.empty()) {
418 }
else if (protocol.ends_with(
":")) {
419 protocol.remove_suffix(1);
423 std::string trimmed(port_value);
424 helpers::remove_ascii_tab_or_newline(trimmed);
426 if (trimmed.empty()) {
431 if (!unicode::is_ascii_digit(trimmed.front())) {
432 return tl::unexpected(errors::type_error);
436 auto first_non_digit =
437 std::ranges::find_if_not(trimmed, unicode::is_ascii_digit);
438 std::string_view digits_to_parse =
439 std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
442 uint16_t parsed_port{};
443 auto result = std::from_chars(digits_to_parse.data(),
444 digits_to_parse.data() + digits_to_parse.size(),
447 if (
result.ec == std::errc::result_out_of_range) {
448 return tl::unexpected(errors::type_error);
451 if (
result.ec == std::errc()) {
453 uint16_t default_port = scheme::get_special_port(protocol);
456 if (default_port != 0 && default_port == parsed_port) {
461 return std::to_string(parsed_port);
464 return tl::unexpected(errors::type_error);
467tl::expected<std::string, errors> canonicalize_pathname(
468 std::string_view input) {
469 if (input.empty()) [[unlikely]] {
475 bool needs_processing =
false;
476 for (
char c : input) {
478 !(char_class_table[
static_cast<uint8_t
>(c)] & CHAR_SIMPLE_PATHNAME);
480 if (!needs_processing) {
481 return std::string(input);
486 const bool leading_slash = input.starts_with(
"/");
489 const auto modified_value = leading_slash ?
"" :
"/-";
490 const auto full_url =
491 std::string(
"fake://fake-url") + modified_value + std::string(input);
493 const auto pathname = url->get_pathname();
496 return leading_slash ? std::string(pathname)
497 : std::string(pathname.substr(2));
500 return tl::unexpected(errors::type_error);
503tl::expected<std::string, errors> canonicalize_opaque_pathname(
504 std::string_view input) {
506 if (input.empty()) [[unlikely]] {
516 return std::string(url->get_pathname());
519 return tl::unexpected(errors::type_error);
522tl::expected<std::string, errors> canonicalize_search(std::string_view input) {
524 if (input.empty()) [[unlikely]] {
528 std::string new_value;
529 new_value = input[0] ==
'?' ? input.substr(1) : input;
531 helpers::remove_ascii_tab_or_newline(new_value);
533 if (new_value.empty()) {
541 new_value, character_sets::QUERY_PERCENT_ENCODE);
542 if (idx == new_value.size()) {
547 return ada::unicode::percent_encode(
548 new_value, character_sets::QUERY_PERCENT_ENCODE, idx);
551tl::expected<std::string, errors> canonicalize_hash(std::string_view input) {
553 if (input.empty()) [[unlikely]] {
557 std::string new_value;
558 new_value = input[0] ==
'#' ? input.substr(1) : input;
560 helpers::remove_ascii_tab_or_newline(new_value);
562 if (new_value.empty()) {
568 new_value, character_sets::FRAGMENT_PERCENT_ENCODE);
569 if (idx == new_value.size()) {
574 return ada::unicode::percent_encode(
575 new_value, character_sets::FRAGMENT_PERCENT_ENCODE, idx);
578tl::expected<std::vector<token>,
errors> tokenize(std::string_view input,
579 token_policy policy) {
580 ada_log(
"tokenize input: ", input);
584 auto tokenizer = Tokenizer(input, policy);
586 while (tokenizer.index < tokenizer.input.size()) {
589 tokenizer.seek_and_get_next_code_point(tokenizer.index);
592 if (tokenizer.code_point ==
'*') {
595 tokenizer.add_token_with_defaults(token_type::ASTERISK);
596 ada_log(
"add ASTERISK token");
602 if (tokenizer.code_point ==
'+' || tokenizer.code_point ==
'?') {
605 tokenizer.add_token_with_defaults(token_type::OTHER_MODIFIER);
611 if (tokenizer.code_point ==
'\\') {
614 if (tokenizer.index == tokenizer.input.size() - 1) {
617 if (
auto error = tokenizer.process_tokenizing_error(
618 tokenizer.next_index, tokenizer.index)) {
619 ada_log(
"process_tokenizing_error failed");
620 return tl::unexpected(*error);
626 auto escaped_index = tokenizer.next_index;
628 tokenizer.get_next_code_point();
631 tokenizer.add_token_with_default_length(
632 token_type::ESCAPED_CHAR, tokenizer.next_index, escaped_index);
633 ada_log(
"add ESCAPED_CHAR token on next_index ", tokenizer.next_index,
634 " with escaped index ", escaped_index);
640 if (tokenizer.code_point ==
'{') {
643 tokenizer.add_token_with_defaults(token_type::OPEN);
644 ada_log(
"add OPEN token");
649 if (tokenizer.code_point ==
'}') {
652 tokenizer.add_token_with_defaults(token_type::CLOSE);
653 ada_log(
"add CLOSE token");
658 if (tokenizer.code_point ==
':') {
660 auto name_position = tokenizer.next_index;
662 auto name_start = name_position;
664 while (name_position < tokenizer.input.size()) {
667 tokenizer.seek_and_get_next_code_point(name_position);
670 bool first_code_point = name_position == name_start;
673 auto valid_code_point =
674 idna::valid_name_code_point(tokenizer.code_point, first_code_point);
675 ada_log(
"tokenizer.code_point=", uint32_t(tokenizer.code_point),
676 " first_code_point=", first_code_point,
677 " valid_code_point=", valid_code_point);
679 if (!valid_code_point)
break;
681 name_position = tokenizer.next_index;
685 if (name_position <= name_start) {
688 if (
auto error = tokenizer.process_tokenizing_error(name_start,
690 ada_log(
"process_tokenizing_error failed");
691 return tl::unexpected(*error);
699 tokenizer.add_token_with_default_length(token_type::NAME, name_position,
705 if (tokenizer.code_point ==
'(') {
709 auto regexp_position = tokenizer.next_index;
711 auto regexp_start = regexp_position;
717 while (regexp_position < tokenizer.input.size()) {
720 tokenizer.seek_and_get_next_code_point(regexp_position);
725 if (!unicode::is_ascii(tokenizer.code_point)) {
728 if (
auto process_error = tokenizer.process_tokenizing_error(
729 regexp_start, tokenizer.index)) {
730 return tl::unexpected(*process_error);
739 if (regexp_position == regexp_start && tokenizer.code_point ==
'?') {
742 if (
auto process_error = tokenizer.process_tokenizing_error(
743 regexp_start, tokenizer.index)) {
744 return tl::unexpected(*process_error);
752 if (tokenizer.code_point ==
'\\') {
754 if (regexp_position == tokenizer.input.size() - 1) {
757 if (
auto process_error = tokenizer.process_tokenizing_error(
758 regexp_start, tokenizer.index)) {
759 return tl::unexpected(*process_error);
766 tokenizer.get_next_code_point();
769 if (!unicode::is_ascii(tokenizer.code_point)) {
772 if (
auto process_error = tokenizer.process_tokenizing_error(
773 regexp_start, tokenizer.index);
774 process_error.has_value()) {
775 return tl::unexpected(*process_error);
782 regexp_position = tokenizer.next_index;
787 if (tokenizer.code_point ==
')') {
793 regexp_position = tokenizer.next_index;
797 }
else if (tokenizer.code_point ==
'(') {
803 if (regexp_position == tokenizer.input.size() - 1) {
806 if (
auto process_error = tokenizer.process_tokenizing_error(
807 regexp_start, tokenizer.index)) {
808 return tl::unexpected(*process_error);
815 auto temporary_position = tokenizer.next_index;
817 tokenizer.get_next_code_point();
819 if (tokenizer.code_point !=
'?') {
822 if (
auto process_error = tokenizer.process_tokenizing_error(
823 regexp_start, tokenizer.index)) {
824 return tl::unexpected(*process_error);
831 tokenizer.next_index = temporary_position;
834 regexp_position = tokenizer.next_index;
843 if (
auto process_error = tokenizer.process_tokenizing_error(
844 regexp_start, tokenizer.index)) {
845 return tl::unexpected(*process_error);
850 auto regexp_length = regexp_position - regexp_start - 1;
852 if (regexp_length == 0) {
855 if (
auto process_error = tokenizer.process_tokenizing_error(
856 regexp_start, tokenizer.index)) {
857 ada_log(
"process_tokenizing_error failed");
858 return tl::unexpected(*process_error);
864 tokenizer.add_token(token_type::REGEXP, regexp_position, regexp_start,
870 tokenizer.add_token_with_defaults(token_type::CHAR);
874 tokenizer.add_token_with_default_length(token_type::END, tokenizer.index,
877 ada_log(
"tokenizer.token_list size is: ", tokenizer.token_list.size());
879 return tokenizer.token_list;
883constexpr std::array<uint8_t, 256> escape_pattern_table = []()
consteval {
884 std::array<uint8_t, 256> out{};
885 for (
auto& c : {
'+',
'*',
'?',
':',
'{',
'}',
'(',
')',
'\\'}) {
891constexpr bool should_escape_pattern_char(
char c) {
892 return escape_pattern_table[
static_cast<uint8_t
>(c)];
896std::string escape_pattern_string(std::string_view input) {
897 ada_log(
"escape_pattern_string called with input=", input);
898 if (input.empty()) [[unlikely]] {
906 result.reserve(input.size() * 2);
909 for (
const char c : input) {
910 if (should_escape_pattern_char(c)) {
922constexpr std::array<uint8_t, 256> escape_regexp_table = []()
consteval {
923 std::array<uint8_t, 256> out{};
924 for (
auto& c : {
'.',
'+',
'*',
'?',
'^',
'$',
'{',
'}',
'(',
')',
'[',
']',
931constexpr bool should_escape_regexp_char(
char c) {
932 return escape_regexp_table[(uint8_t)c];
936std::string escape_regexp_string(std::string_view input) {
942 result.reserve(input.size() * 2);
943 for (
const char c : input) {
944 if (should_escape_regexp_char(c)) {
955std::string process_base_url_string(std::string_view input,
956 url_pattern_init::process_type type) {
958 if (type != url_pattern_init::process_type::pattern) {
959 return std::string(input);
962 return escape_pattern_string(input);
965constexpr bool is_absolute_pathname(
966 std::string_view input, url_pattern_init::process_type type)
noexcept {
968 if (input.empty()) [[unlikely]] {
972 if (input.starts_with(
"/"))
return true;
974 if (type == url_pattern_init::process_type::url)
return false;
976 if (input.size() < 2)
return false;
980 return input[1] ==
'/' && (input[0] ==
'\\' || input[0] ==
'{');
983std::string generate_pattern_string(
984 std::vector<url_pattern_part>& part_list,
985 url_pattern_compile_component_options& options) {
990 for (
size_t index = 0; index < part_list.size(); index++) {
993 const auto& part = part_list[index];
997 const url_pattern_part* previous_part =
998 index == 0 ? nullptr : &part_list[index - 1];
1001 const url_pattern_part* next_part =
1002 index < part_list.size() - 1 ? &part_list[index + 1] :
nullptr;
1004 if (part.type == url_pattern_part_type::FIXED_TEXT) {
1006 if (part.modifier == url_pattern_part_modifier::none) {
1009 result.append(escape_pattern_string(part.value));
1016 result.append(escape_pattern_string(part.value));
1021 result.append(convert_modifier_to_string(part.modifier));
1026 bool custom_name = !unicode::is_ascii_digit(part.name[0]);
1032 bool needs_grouping =
1033 !part.suffix.empty() ||
1034 (!part.prefix.empty() && part.prefix[0] != options.get_prefix()[0]);
1044 if (!needs_grouping && custom_name &&
1045 part.type == url_pattern_part_type::SEGMENT_WILDCARD &&
1046 part.modifier == url_pattern_part_modifier::none && next_part &&
1047 next_part->prefix.empty() && next_part->suffix.empty()) {
1049 if (next_part->type == url_pattern_part_type::FIXED_TEXT) {
1053 if (idna::valid_name_code_point(next_part->value[0],
false)) {
1054 needs_grouping =
true;
1058 needs_grouping = !next_part->name.empty() &&
1059 unicode::is_ascii_digit(next_part->name[0]);
1070 if (!needs_grouping && part.prefix.empty() && previous_part &&
1071 previous_part->type == url_pattern_part_type::FIXED_TEXT &&
1072 !options.get_prefix().empty() &&
1073 previous_part->value.at(previous_part->value.size() - 1) ==
1074 options.get_prefix()[0]) {
1075 needs_grouping =
true;
1082 if (needs_grouping) {
1088 result.append(escape_pattern_string(part.prefix));
1095 result.append(part.name);
1099 if (part.type == url_pattern_part_type::REGEXP) {
1103 result.append(part.value);
1106 }
else if (part.type == url_pattern_part_type::SEGMENT_WILDCARD &&
1113 result.append(generate_segment_wildcard_regexp(options));
1116 }
else if (part.type == url_pattern_part_type::FULL_WILDCARD) {
1127 previous_part->type == url_pattern_part_type::FIXED_TEXT ||
1128 previous_part->modifier != url_pattern_part_modifier::none ||
1129 needs_grouping || !part.prefix.empty())) {
1146 if (part.type == url_pattern_part_type::SEGMENT_WILDCARD && custom_name &&
1147 !part.suffix.empty() &&
1148 idna::valid_name_code_point(part.suffix[0],
false)) {
1154 result.append(escape_pattern_string(part.suffix));
1156 if (needs_grouping)
result.append(
"}");
1159 result.append(convert_modifier_to_string(part.modifier));
Declaration of the character sets used by unicode functions.
#define ADA_ASSERT_TRUE(COND)
Definitions for helper functions used within Ada.
bool constexpr is_ascii(std::u32string_view view)
const uint32_t table[8150][2]
ada_really_inline size_t percent_encode_index(const std::string_view input, const uint8_t character_set[])
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
URL scheme type definitions and utilities.
Definitions for all unicode specific functions.
Declaration for the URLPattern helpers.