Skip to content

Commit

Permalink
try to reuse vector values on regex_match
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 22, 2025
1 parent 367b27b commit 0eb5c99
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 26 deletions.
19 changes: 7 additions & 12 deletions include/ada/url_pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ std::string url_pattern_component<regex_provider>::to_string() const {
template <url_pattern_regex::regex_concept regex_provider>
url_pattern_component_result
url_pattern_component<regex_provider>::create_component_match_result(
std::string_view input, const std::vector<std::string>& exec_result) {
std::string_view input, std::vector<std::string>&& exec_result) {
// Let result be a new URLPatternComponentResult.
// Set result["input"] to input.
// Let groups be a record<USVString, (USVString or undefined)>.
Expand All @@ -53,19 +53,14 @@ url_pattern_component<regex_provider>::create_component_match_result(
// Optimization: Let's reserve the size.
result.groups.reserve(exec_result.size() - 1);

size_t group_index = 0;
// Let index be 1.
// While index is less than Get(execResult, "length"):
for (size_t index = 1; index < exec_result.size(); index++) {
// Let name be component’s group name list[index - 1].
// Let value be Get(execResult, ToString(index)).
// Set groups[name] to value.
// We explicitly start iterating from 0 even though the spec
// says we should start from 1. This case is handled by the
// std_regex_provider.
for (size_t index = 0; index < exec_result.size(); index++) {
result.groups.insert({
group_name_list[group_index],
exec_result[index],
group_name_list[index],
std::move(exec_result[index]),
});

group_index++;
}
return result;
}
Expand Down
2 changes: 1 addition & 1 deletion include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ class url_pattern_component {

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

std::string to_string() const;

Expand Down
20 changes: 10 additions & 10 deletions src/url_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,42 +733,42 @@ result<std::optional<url_pattern_result>> url_pattern<regex_provider>::match(
// Set result["protocol"] to the result of creating a component match result
// given urlPattern’s protocol component, protocol, and protocolExecResult.
result.protocol = protocol_component.create_component_match_result(
protocol, *protocol_exec_result);
protocol, std::move(*protocol_exec_result));

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

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

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

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

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

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

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

return result;
}
Expand Down
6 changes: 3 additions & 3 deletions src/url_pattern_regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ std::optional<std::vector<std::string>> std_regex_provider::regex_search(
}
std::vector<std::string> matches;
matches.reserve(match_result.size() - 1);
for (const auto& match : match_result) {
if (match.matched) {
matches.push_back(match.str());
for (size_t i = 1; i < match_result.size(); ++i) {
if (auto entry = match_result[i]; entry.matched) {
matches.emplace_back(entry.str());
}
}
return matches;
Expand Down

0 comments on commit 0eb5c99

Please sign in to comment.