Skip to content

Commit 1c910f4

Browse files
committed
try to reuse vector values on regex_match
1 parent 367b27b commit 1c910f4

File tree

4 files changed

+26
-28
lines changed

4 files changed

+26
-28
lines changed

include/ada/url_pattern-inl.h

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,34 +38,29 @@ std::string url_pattern_component<regex_provider>::to_string() const {
3838
template <url_pattern_regex::regex_concept regex_provider>
3939
url_pattern_component_result
4040
url_pattern_component<regex_provider>::create_component_match_result(
41-
std::string_view input, const std::vector<std::string>& exec_result) {
41+
std::string_view input, std::vector<std::string>&& exec_result) {
4242
// Let result be a new URLPatternComponentResult.
4343
// Set result["input"] to input.
4444
// Let groups be a record<USVString, (USVString or undefined)>.
4545
auto result =
4646
url_pattern_component_result{.input = std::string(input), .groups = {}};
4747

4848
// If input is empty, then groups will always be empty.
49-
if (input.empty()) {
49+
if (input.empty() || exec_result.empty()) {
5050
return result;
5151
}
5252

5353
// Optimization: Let's reserve the size.
5454
result.groups.reserve(exec_result.size() - 1);
5555

56-
size_t group_index = 0;
57-
// Let index be 1.
58-
// While index is less than Get(execResult, "length"):
59-
for (size_t index = 1; index < exec_result.size(); index++) {
60-
// Let name be component’s group name list[index - 1].
61-
// Let value be Get(execResult, ToString(index)).
62-
// Set groups[name] to value.
56+
// We explicitly start iterating from 0 even though the spec
57+
// says we should start from 1. This case is handled by the
58+
// std_regex_provider.
59+
for (size_t index = 0; index < exec_result.size(); index++) {
6360
result.groups.insert({
64-
group_name_list[group_index],
65-
exec_result[index],
61+
group_name_list[index],
62+
std::move(exec_result[index]),
6663
});
67-
68-
group_index++;
6964
}
7065
return result;
7166
}

include/ada/url_pattern.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ class url_pattern_component {
231231

232232
// @see https://urlpattern.spec.whatwg.org/#create-a-component-match-result
233233
url_pattern_component_result create_component_match_result(
234-
std::string_view input, const std::vector<std::string>& exec_result);
234+
std::string_view input, std::vector<std::string>&& exec_result);
235235

236236
std::string to_string() const;
237237

src/url_pattern.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -733,42 +733,42 @@ result<std::optional<url_pattern_result>> url_pattern<regex_provider>::match(
733733
// Set result["protocol"] to the result of creating a component match result
734734
// given urlPattern’s protocol component, protocol, and protocolExecResult.
735735
result.protocol = protocol_component.create_component_match_result(
736-
protocol, *protocol_exec_result);
736+
protocol, std::move(*protocol_exec_result));
737737

738738
// Set result["username"] to the result of creating a component match result
739739
// given urlPattern’s username component, username, and usernameExecResult.
740740
result.username = username_component.create_component_match_result(
741-
username, *username_exec_result);
741+
username, std::move(*username_exec_result));
742742

743743
// Set result["password"] to the result of creating a component match result
744744
// given urlPattern’s password component, password, and passwordExecResult.
745745
result.password = password_component.create_component_match_result(
746-
password, *password_exec_result);
746+
password, std::move(*password_exec_result));
747747

748748
// Set result["hostname"] to the result of creating a component match result
749749
// given urlPattern’s hostname component, hostname, and hostnameExecResult.
750750
result.hostname = hostname_component.create_component_match_result(
751-
hostname, *hostname_exec_result);
751+
hostname, std::move(*hostname_exec_result));
752752

753753
// Set result["port"] to the result of creating a component match result given
754754
// urlPattern’s port component, port, and portExecResult.
755-
result.port =
756-
port_component.create_component_match_result(port, *port_exec_result);
755+
result.port = port_component.create_component_match_result(
756+
port, std::move(*port_exec_result));
757757

758758
// Set result["pathname"] to the result of creating a component match result
759759
// given urlPattern’s pathname component, pathname, and pathnameExecResult.
760760
result.pathname = pathname_component.create_component_match_result(
761-
pathname, *pathname_exec_result);
761+
pathname, std::move(*pathname_exec_result));
762762

763763
// Set result["search"] to the result of creating a component match result
764764
// given urlPattern’s search component, search, and searchExecResult.
765765
result.search = search_component.create_component_match_result(
766-
search, *search_exec_result);
766+
search, std::move(*search_exec_result));
767767

768768
// Set result["hash"] to the result of creating a component match result given
769769
// urlPattern’s hash component, hash, and hashExecResult.
770-
result.hash =
771-
hash_component.create_component_match_result(hash, *hash_exec_result);
770+
result.hash = hash_component.create_component_match_result(
771+
hash, std::move(*hash_exec_result));
772772

773773
return result;
774774
}

src/url_pattern_regex.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,13 @@ std::optional<std::vector<std::string>> std_regex_provider::regex_search(
3030
return std::nullopt;
3131
}
3232
std::vector<std::string> matches;
33-
matches.reserve(match_result.size() - 1);
34-
for (const auto& match : match_result) {
35-
if (match.matched) {
36-
matches.push_back(match.str());
33+
if (match_result.empty()) {
34+
return matches;
35+
}
36+
matches.reserve(match_result.size());
37+
for (size_t i = 1; i < match_result.size(); ++i) {
38+
if (auto entry = match_result[i]; entry.matched) {
39+
matches.emplace_back(entry.str());
3740
}
3841
}
3942
return matches;

0 commit comments

Comments
 (0)