Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
try to reuse vector values on regex_match
  • Loading branch information
anonrig committed Jan 22, 2025
commit 1c910f4fafe425d5241936473f28ec33bc2617dc
21 changes: 8 additions & 13 deletions include/ada/url_pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,34 +38,29 @@ 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)>.
auto result =
url_pattern_component_result{.input = std::string(input), .groups = {}};

// If input is empty, then groups will always be empty.
if (input.empty()) {
if (input.empty() || exec_result.empty()) {
return 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
11 changes: 7 additions & 4 deletions src/url_pattern_regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,13 @@ std::optional<std::vector<std::string>> std_regex_provider::regex_search(
return std::nullopt;
}
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());
if (match_result.empty()) {
return matches;
}
matches.reserve(match_result.size());
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
Loading