From 4e614af0b4557291ec6a3430418d079f7e14cd4d Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Wed, 22 Jan 2025 12:55:58 -0500 Subject: [PATCH] try to reuse vector values on regex_match --- include/ada/url_pattern-inl.h | 19 +++++++------------ include/ada/url_pattern.h | 2 +- src/url_pattern.cpp | 20 ++++++++++---------- src/url_pattern_regex.cpp | 8 ++++---- 4 files changed, 22 insertions(+), 27 deletions(-) diff --git a/include/ada/url_pattern-inl.h b/include/ada/url_pattern-inl.h index 27600689f..10ffa8c2e 100644 --- a/include/ada/url_pattern-inl.h +++ b/include/ada/url_pattern-inl.h @@ -38,7 +38,7 @@ std::string url_pattern_component::to_string() const { template url_pattern_component_result url_pattern_component::create_component_match_result( - std::string_view input, const std::vector& exec_result) { + std::string_view input, std::vector&& exec_result) { // Let result be a new URLPatternComponentResult. // Set result["input"] to input. // Let groups be a record. @@ -53,19 +53,14 @@ url_pattern_component::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; } diff --git a/include/ada/url_pattern.h b/include/ada/url_pattern.h index fef4fd20c..bfa9e4fdb 100644 --- a/include/ada/url_pattern.h +++ b/include/ada/url_pattern.h @@ -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& exec_result); + std::string_view input, std::vector&& exec_result); std::string to_string() const; diff --git a/src/url_pattern.cpp b/src/url_pattern.cpp index 9110174b6..7096b5019 100644 --- a/src/url_pattern.cpp +++ b/src/url_pattern.cpp @@ -733,42 +733,42 @@ result> url_pattern::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; } diff --git a/src/url_pattern_regex.cpp b/src/url_pattern_regex.cpp index 461785b4d..a83348157 100644 --- a/src/url_pattern_regex.cpp +++ b/src/url_pattern_regex.cpp @@ -30,10 +30,10 @@ std::optional> std_regex_provider::regex_search( return std::nullopt; } std::vector matches; - matches.reserve(match_result.size() - 1); - for (const auto& match : match_result) { - if (match.matched) { - matches.push_back(match.str()); + 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;