Skip to content

Commit b391033

Browse files
committed
improve regex matching
1 parent 4a5a5ab commit b391033

File tree

4 files changed

+20
-15
lines changed

4 files changed

+20
-15
lines changed

include/ada/url_pattern-inl.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ url_pattern_component::create_component_match_result(
4343
auto result =
4444
url_pattern_component_result{.input = std::string(input), .groups = {}};
4545

46+
// If input is empty, then groups will always be empty.
47+
if (input.empty()) {
48+
return result;
49+
}
50+
4651
// Optimization: Let's reserve the size.
4752
result.groups.reserve(exec_result.size() - 1);
4853

@@ -53,11 +58,11 @@ url_pattern_component::create_component_match_result(
5358
// Let name be component’s group name list[index - 1].
5459
// Let value be Get(execResult, ToString(index)).
5560
// Set groups[name] to value.
56-
auto match = exec_result[index];
57-
if (!match.matched || match.length() == 0) continue;
61+
auto exec = exec_result[index];
62+
if (!exec.matched) continue;
5863
result.groups.insert({
5964
group_name_list[group_index],
60-
match.str(),
65+
exec.str(),
6166
});
6267

6368
group_index++;

include/ada/url_pattern.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,8 +197,7 @@ struct url_pattern_component_result {
197197
#if ADA_TESTING
198198
friend void PrintTo(const url_pattern_component_result& result,
199199
std::ostream* os) {
200-
*os << "input: '" << result.input
201-
<< "', group: ";
200+
*os << "input: '" << result.input << "', group: ";
202201
for (const auto& group : result.groups) {
203202
*os << "(" << group.first << ", " << group.second << ") ";
204203
}

src/url_pattern.cpp

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ result<std::optional<url_pattern_result>> url_pattern::exec(
520520
}
521521

522522
result<bool> url_pattern::test(const url_pattern_input& input,
523-
std::string_view* base_url = nullptr) {
523+
std::string_view* base_url = nullptr) {
524524
// TODO: Optimization opportunity. Rather than returning `url_pattern_result`
525525
// Implement a fast path just like `can_parse()` in ada_url.
526526
// Let result be the result of match given this's associated URL pattern,
@@ -681,48 +681,48 @@ result<std::optional<url_pattern_result>> url_pattern::match(
681681
}
682682
}
683683

684-
auto regex_flags = std::regex_constants::match_continuous;
684+
auto regex_flags = std::regex_constants::match_any;
685685

686686
// Let protocolExecResult be RegExpBuiltinExec(urlPattern’s protocol
687687
// component's regular expression, protocol).
688688
std::smatch protocol_exec_result_value;
689689
auto protocol_exec_result =
690690
std::regex_search(protocol, protocol_exec_result_value,
691-
protocol_component.regexp, regex_flags);
691+
protocol_component.regexp, regex_flags);
692692

693693
// Let usernameExecResult be RegExpBuiltinExec(urlPattern’s username
694694
// component's regular expression, username).
695695
std::smatch username_exec_result_value;
696696
auto username_exec_result =
697697
std::regex_search(username, username_exec_result_value,
698-
username_component.regexp, regex_flags);
698+
username_component.regexp, regex_flags);
699699

700700
// Let passwordExecResult be RegExpBuiltinExec(urlPattern’s password
701701
// component's regular expression, password).
702702
std::smatch password_exec_result_value;
703703
auto password_exec_result =
704704
std::regex_search(password, password_exec_result_value,
705-
password_component.regexp, regex_flags);
705+
password_component.regexp, regex_flags);
706706

707707
// Let hostnameExecResult be RegExpBuiltinExec(urlPattern’s hostname
708708
// component's regular expression, hostname).
709709
std::smatch hostname_exec_result_value;
710710
auto hostname_exec_result =
711711
std::regex_search(hostname, hostname_exec_result_value,
712-
hostname_component.regexp, regex_flags);
712+
hostname_component.regexp, regex_flags);
713713

714714
// Let portExecResult be RegExpBuiltinExec(urlPattern’s port component's
715715
// regular expression, port).
716716
std::smatch port_exec_result_value;
717717
auto port_exec_result = std::regex_search(port, port_exec_result_value,
718-
port_component.regexp, regex_flags);
718+
port_component.regexp, regex_flags);
719719

720720
// Let pathnameExecResult be RegExpBuiltinExec(urlPattern’s pathname
721721
// component's regular expression, pathname).
722722
std::smatch pathname_exec_result_value;
723723
auto pathname_exec_result =
724724
std::regex_search(pathname, pathname_exec_result_value,
725-
pathname_component.regexp, regex_flags);
725+
pathname_component.regexp, regex_flags);
726726

727727
// Let searchExecResult be RegExpBuiltinExec(urlPattern’s search component's
728728
// regular expression, search).
@@ -734,7 +734,7 @@ result<std::optional<url_pattern_result>> url_pattern::match(
734734
// regular expression, hash).
735735
std::smatch hash_exec_result_value;
736736
auto hash_exec_result = std::regex_search(hash, hash_exec_result_value,
737-
hash_component.regexp, regex_flags);
737+
hash_component.regexp, regex_flags);
738738

739739
// If protocolExecResult, usernameExecResult, passwordExecResult,
740740
// hostnameExecResult, portExecResult, pathnameExecResult, searchExecResult,

tests/wpt_urlpattern_tests.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ ada::url_pattern_component_result parse_component_result(
307307
auto group_key = group.unescaped_key().value();
308308
std::string_view group_value;
309309
EXPECT_FALSE(group.value().get_string(group_value));
310-
result.groups.insert_or_assign(std::string(group_key), std::string(group_value));
310+
result.groups.insert_or_assign(std::string(group_key),
311+
std::string(group_value));
311312
}
312313
}
313314
}

0 commit comments

Comments
 (0)