Skip to content

Commit

Permalink
Merge branch 'yagiz/add-url-pattern' into yagiz/add-url-pattern-safe-…
Browse files Browse the repository at this point in the history
…regex
  • Loading branch information
lemire authored Dec 20, 2024
2 parents e5a4ef4 + aff51cf commit 7f945ee
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 22 deletions.
6 changes: 3 additions & 3 deletions include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,16 @@ class url_pattern {

// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
tl::expected<std::optional<url_pattern_result>, url_pattern_errors> exec(
url_pattern_input input, std::string_view* base_url);
url_pattern_input&& input, std::string_view* base_url);
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-test
bool test(url_pattern_input input, std::string_view* base_url);
bool test(url_pattern_input&& input, std::string_view* base_url);

/**
* @see https://urlpattern.spec.whatwg.org/#url-pattern-match
* This function expects a valid UTF-8 string if input is a string.
*/
tl::expected<std::optional<url_pattern_result>, url_pattern_errors> match(
url_pattern_input input, std::string_view* base_url_string);
url_pattern_input&& input, std::string_view* base_url_string);

// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
std::string_view get_protocol() const ada_lifetime_bound;
Expand Down
3 changes: 2 additions & 1 deletion include/ada/url_pattern_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,8 @@ constexpr bool is_ipv6_address(std::string_view input) noexcept;

// @see
// https://urlpattern.spec.whatwg.org/#protocol-component-matches-a-special-scheme
bool protocol_component_matches_special_scheme(std::string_view input);
bool protocol_component_matches_special_scheme(
ada::url_pattern_component& input);

// @see https://urlpattern.spec.whatwg.org/#convert-a-modifier-to-a-string
std::string convert_modifier_to_string(url_pattern_part_modifier modifier);
Expand Down
2 changes: 1 addition & 1 deletion src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1060,7 +1060,7 @@ tl::expected<url_pattern, url_pattern_errors> parse_url_pattern_impl(
// If the result of running protocol component matches a special scheme given
// urlPattern’s protocol component is true, then:
if (url_pattern_helpers::protocol_component_matches_special_scheme(
url_pattern_.protocol_component.get_pattern())) {
url_pattern_.protocol_component)) {
// Let pathCompileOptions be copy of the pathname options with the ignore
// case property set to options["ignoreCase"].
auto path_compile_options = url_pattern_compile_component_options::PATHNAME;
Expand Down
30 changes: 16 additions & 14 deletions src/url_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,9 @@ url_pattern_component::compile(std::string_view input, F encoding_callback,
// Return a new component whose pattern string is pattern string, regular
// expression is regular expression, group name list is name list, and has
// regexp groups is has regexp groups.
return url_pattern_component(std::move(pattern_string), std::move(flags),
std::move(regular_expression_string),
std::move(name_list), has_regexp_groups);
return url_pattern_component(
std::move(pattern_string), std::move(regular_expression_string),
std::move(flags), std::move(name_list), has_regexp_groups);
}

namespace url_pattern_helpers {
Expand Down Expand Up @@ -721,16 +721,17 @@ std::string generate_segment_wildcard_regexp(
return result;
}

bool protocol_component_matches_special_scheme(std::string_view input) {
// TODO: Optimize this.
bool protocol_component_matches_special_scheme(
ada::url_pattern_component& component) {
auto regex = component.get_regexp();
try {
std::regex rx(input.data(), input.size());
std::regex rx(regex.data(), regex.size());
std::cmatch cmatch;
return std::regex_match("http", cmatch, rx) ||
std::regex_match("https", cmatch, rx) ||
std::regex_match("ws", cmatch, rx) ||
std::regex_match("wss", cmatch, rx) ||
std::regex_match("ftp", cmatch, rx);
std::regex_match("https", cmatch, rx) ||
std::regex_match("ws", cmatch, rx) ||
std::regex_match("wss", cmatch, rx) ||
std::regex_match("ftp", cmatch, rx);
} catch (...) {
// You probably want to log this error.
ada_log("Error while matching protocol component with special scheme");
Expand All @@ -742,14 +743,14 @@ bool protocol_component_matches_special_scheme(std::string_view input) {
} // namespace url_pattern_helpers

tl::expected<std::optional<url_pattern_result>, url_pattern_errors>
url_pattern::exec(url_pattern_input input,
url_pattern::exec(url_pattern_input&& input,
std::string_view* base_url = nullptr) {
// Return the result of match given this's associated URL pattern, input, and
// baseURL if given.
return match(input, base_url);
return match(std::move(input), base_url);
}

bool url_pattern::test(url_pattern_input input,
bool url_pattern::test(url_pattern_input&& input,
std::string_view* base_url = nullptr) {
// TODO: Optimization opportunity. Rather than returning `url_pattern_result`
// Implement a fast path just like `can_parse()` in ada_url.
Expand All @@ -763,7 +764,8 @@ bool url_pattern::test(url_pattern_input input,
}

tl::expected<std::optional<url_pattern_result>, url_pattern_errors>
url_pattern::match(url_pattern_input input, std::string_view* base_url_string) {
url_pattern::match(url_pattern_input&& input,
std::string_view* base_url_string) {
std::string protocol{};
std::string username{};
std::string password{};
Expand Down
6 changes: 3 additions & 3 deletions src/url_pattern_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ constructor_string_parser::compute_protocol_matches_special_scheme_flag() {
// If the result of running protocol component matches a special scheme given
// protocol component is true, then set parser’s protocol matches a special
// scheme flag to true.
if (protocol_component_matches_special_scheme(
protocol_component->get_pattern())) {
if (protocol_component_matches_special_scheme(*protocol_component)) {
protocol_matches_a_special_scheme_flag = true;
}
return std::nullopt;
Expand Down Expand Up @@ -1073,7 +1072,8 @@ std::string generate_pattern_string(
}
} else {
// Set needs grouping to true if next part’s name[0] is an ASCII digit.
needs_grouping = unicode::is_ascii_digit(next_part->name[0]);
needs_grouping = !next_part->name.empty() &&
unicode::is_ascii_digit(next_part->name[0]);
}
}

Expand Down

0 comments on commit 7f945ee

Please sign in to comment.