From 41e6f42bea2e8ae328c8e80adb05af1b1cbf32b8 Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 27 Jan 2025 10:51:04 -0500 Subject: [PATCH 1/2] make regex result accept optional string --- include/ada/url_pattern-inl.h | 5 +++-- include/ada/url_pattern.h | 8 +++++--- include/ada/url_pattern_regex.h | 4 ++-- src/url_pattern_regex.cpp | 7 ++++--- 4 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/ada/url_pattern-inl.h b/include/ada/url_pattern-inl.h index 2a371f63a..7330e98ea 100644 --- a/include/ada/url_pattern-inl.h +++ b/include/ada/url_pattern-inl.h @@ -39,7 +39,8 @@ std::string url_pattern_component::to_string() const { template url_pattern_component_result url_pattern_component::create_component_match_result( - std::string_view input, 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. @@ -47,7 +48,7 @@ url_pattern_component::create_component_match_result( url_pattern_component_result{.input = std::string(input), .groups = {}}; // If input is empty, then groups will always be empty. - if (input.empty() || exec_result.empty()) { + if (input.empty()) { return result; } diff --git a/include/ada/url_pattern.h b/include/ada/url_pattern.h index 2be1354c3..241136ecd 100644 --- a/include/ada/url_pattern.h +++ b/include/ada/url_pattern.h @@ -133,7 +133,7 @@ inline url_pattern_compile_component_options // specification. struct url_pattern_component_result { std::string input; - std::unordered_map groups; + std::unordered_map> groups; bool operator==(const url_pattern_component_result&) const; @@ -142,7 +142,8 @@ struct url_pattern_component_result { std::ostream* os) { *os << "input: '" << result.input << "', group: "; for (const auto& group : result.groups) { - *os << "(" << group.first << ", " << group.second << ") "; + *os << "(" << group.first << ", " << group.second.value_or("undefined") + << ") "; } } #endif // ADA_TESTING @@ -172,7 +173,8 @@ 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, std::vector&& exec_result); + std::string_view input, + std::vector>&& exec_result); std::string to_string() const; diff --git a/include/ada/url_pattern_regex.h b/include/ada/url_pattern_regex.h index 4116cf863..47a6301bf 100644 --- a/include/ada/url_pattern_regex.h +++ b/include/ada/url_pattern_regex.h @@ -24,7 +24,7 @@ concept regex_concept = requires(T t, std::string_view pattern, // Function to perform regex search { T::regex_search(input, std::declval()) - } -> std::same_as>>; + } -> std::same_as>>>; // Function to match regex pattern { @@ -44,7 +44,7 @@ class std_regex_provider { using regex_type = std::regex; static std::optional create_instance(std::string_view pattern, bool ignore_case); - static std::optional> regex_search( + static std::optional>> regex_search( std::string_view input, const regex_type& pattern); static bool regex_match(std::string_view input, const regex_type& pattern); }; diff --git a/src/url_pattern_regex.cpp b/src/url_pattern_regex.cpp index 431058f87..ec62edf2c 100644 --- a/src/url_pattern_regex.cpp +++ b/src/url_pattern_regex.cpp @@ -19,8 +19,9 @@ std::optional std_regex_provider::create_instance( } } -std::optional> std_regex_provider::regex_search( - std::string_view input, const std::regex& pattern) { +std::optional>> +std_regex_provider::regex_search(std::string_view input, + const std::regex& pattern) { std::string input_str( input.begin(), input.end()); // Convert string_view to string for regex_search @@ -29,7 +30,7 @@ std::optional> std_regex_provider::regex_search( std::regex_constants::match_any)) { return std::nullopt; } - std::vector matches; + std::vector> matches; if (match_result.empty()) { return matches; } From 3fa7fd66c71327bfe1caab75b5bcbb14b8c4e71e Mon Sep 17 00:00:00 2001 From: Yagiz Nizipli Date: Mon, 27 Jan 2025 11:11:23 -0500 Subject: [PATCH 2/2] use correct reserve on create_component_match_result --- include/ada/url_pattern-inl.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/ada/url_pattern-inl.h b/include/ada/url_pattern-inl.h index 7330e98ea..1823d4ac8 100644 --- a/include/ada/url_pattern-inl.h +++ b/include/ada/url_pattern-inl.h @@ -53,7 +53,7 @@ url_pattern_component::create_component_match_result( } // Optimization: Let's reserve the size. - result.groups.reserve(exec_result.size() - 1); + result.groups.reserve(exec_result.size()); // We explicitly start iterating from 0 even though the spec // says we should start from 1. This case is handled by the