Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make regex result accept optional string #851

Merged
merged 2 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
7 changes: 4 additions & 3 deletions include/ada/url_pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,20 +39,21 @@ 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, std::vector<std::string>&& exec_result) {
std::string_view input,
std::vector<std::optional<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() || exec_result.empty()) {
if (input.empty()) {
return 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
Expand Down
8 changes: 5 additions & 3 deletions include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ inline url_pattern_compile_component_options
// specification.
struct url_pattern_component_result {
std::string input;
std::unordered_map<std::string, std::string> groups;
std::unordered_map<std::string, std::optional<std::string>> groups;

bool operator==(const url_pattern_component_result&) const;

Expand All @@ -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
Expand Down Expand Up @@ -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<std::string>&& exec_result);
std::string_view input,
std::vector<std::optional<std::string>>&& exec_result);

std::string to_string() const;

Expand Down
4 changes: 2 additions & 2 deletions include/ada/url_pattern_regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<typename T::regex_type&>())
} -> std::same_as<std::optional<std::vector<std::string>>>;
} -> std::same_as<std::optional<std::vector<std::optional<std::string>>>>;

// Function to match regex pattern
{
Expand All @@ -44,7 +44,7 @@ class std_regex_provider {
using regex_type = std::regex;
static std::optional<regex_type> create_instance(std::string_view pattern,
bool ignore_case);
static std::optional<std::vector<std::string>> regex_search(
static std::optional<std::vector<std::optional<std::string>>> regex_search(
std::string_view input, const regex_type& pattern);
static bool regex_match(std::string_view input, const regex_type& pattern);
};
Expand Down
7 changes: 4 additions & 3 deletions src/url_pattern_regex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ std::optional<std::regex> std_regex_provider::create_instance(
}
}

std::optional<std::vector<std::string>> std_regex_provider::regex_search(
std::string_view input, const std::regex& pattern) {
std::optional<std::vector<std::optional<std::string>>>
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
Expand All @@ -29,7 +30,7 @@ std::optional<std::vector<std::string>> std_regex_provider::regex_search(
std::regex_constants::match_any)) {
return std::nullopt;
}
std::vector<std::string> matches;
std::vector<std::optional<std::string>> matches;
if (match_result.empty()) {
return matches;
}
Expand Down
Loading