Skip to content

Commit

Permalink
make regex result accept optional string
Browse files Browse the repository at this point in the history
  • Loading branch information
anonrig committed Jan 27, 2025
1 parent ecdb978 commit ef487c8
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 10 deletions.
5 changes: 3 additions & 2 deletions include/ada/url_pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,16 @@ 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;
}

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

0 comments on commit ef487c8

Please sign in to comment.