Skip to content

Commit a790af3

Browse files
committed
use std_regex_provider everywhere
1 parent 1b44d8f commit a790af3

File tree

6 files changed

+46
-59
lines changed

6 files changed

+46
-59
lines changed

include/ada/url_pattern-inl.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ std::string url_pattern_component<regex_provider>::to_string() const {
3838
template <url_pattern_regex::regex_concept regex_provider>
3939
url_pattern_component_result
4040
url_pattern_component<regex_provider>::create_component_match_result(
41-
std::string_view input, const std::smatch& exec_result) {
41+
std::string_view input, const std::vector<std::string>& exec_result) {
4242
// Let result be a new URLPatternComponentResult.
4343
// Set result["input"] to input.
4444
// Let groups be a record<USVString, (USVString or undefined)>.
@@ -60,11 +60,9 @@ url_pattern_component<regex_provider>::create_component_match_result(
6060
// Let name be component’s group name list[index - 1].
6161
// Let value be Get(execResult, ToString(index)).
6262
// Set groups[name] to value.
63-
auto exec = exec_result[index];
64-
if (!exec.matched) continue;
6563
result.groups.insert({
6664
group_name_list[group_index],
67-
exec.str(),
65+
exec_result[index],
6866
});
6967

7068
group_index++;

include/ada/url_pattern.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "ada/expected.h"
1010
#include "ada/url_pattern_regex.h"
1111

12-
#include <regex>
1312
#include <string>
1413
#include <unordered_map>
1514
#include <variant>
@@ -232,7 +231,7 @@ class url_pattern_component {
232231

233232
// @see https://urlpattern.spec.whatwg.org/#create-a-component-match-result
234233
url_pattern_component_result create_component_match_result(
235-
std::string_view input, const std::smatch& exec_result);
234+
std::string_view input, const std::vector<std::string>& exec_result);
236235

237236
std::string to_string() const;
238237

include/ada/url_pattern_regex.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ concept regex_concept = requires(T t, std::string_view pattern,
2323

2424
// Function to perform regex search
2525
{
26-
t.regex_search(input, std::declval<typename T::regex_type&>())
27-
} -> std::same_as<std::vector<std::string>>;
26+
T::regex_search(input, std::declval<typename T::regex_type&>())
27+
} -> std::same_as<std::optional<std::vector<std::string>>>;
2828

2929
// Function to match regex pattern
3030
{
31-
t.regex_match(input, std::declval<typename T::regex_type&>())
31+
T::regex_match(input, std::declval<typename T::regex_type&>())
3232
} -> std::same_as<bool>;
3333

3434
// Copy constructor
@@ -44,9 +44,9 @@ class std_regex_provider {
4444
using regex_type = std::regex;
4545
static std::optional<regex_type> create_instance(std::string_view pattern,
4646
bool ignore_case);
47-
std::vector<std::string> regex_search(std::string_view input,
48-
const regex_type& pattern);
49-
bool regex_match(std::string_view input, const regex_type& pattern);
47+
static std::optional<std::vector<std::string>> regex_search(
48+
std::string_view input, const regex_type& pattern);
49+
static bool regex_match(std::string_view input, const regex_type& pattern);
5050
};
5151

5252
} // namespace ada::url_pattern_regex

src/url_pattern.cpp

Lines changed: 21 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <algorithm>
44
#include <optional>
5-
#include <regex>
65
#include <string>
76

87
namespace ada {
@@ -678,60 +677,45 @@ result<std::optional<url_pattern_result>> url_pattern<regex_provider>::match(
678677
}
679678
}
680679

681-
auto regex_flags = std::regex_constants::match_any;
682-
683680
// Let protocolExecResult be RegExpBuiltinExec(urlPattern’s protocol
684681
// component's regular expression, protocol).
685-
std::smatch protocol_exec_result_value;
686682
auto protocol_exec_result =
687-
std::regex_search(protocol, protocol_exec_result_value,
688-
protocol_component.regexp, regex_flags);
683+
regex_provider::regex_search(protocol, protocol_component.regexp);
689684

690685
// Let usernameExecResult be RegExpBuiltinExec(urlPattern’s username
691686
// component's regular expression, username).
692-
std::smatch username_exec_result_value;
693687
auto username_exec_result =
694-
std::regex_search(username, username_exec_result_value,
695-
username_component.regexp, regex_flags);
688+
regex_provider::regex_search(username, username_component.regexp);
696689

697690
// Let passwordExecResult be RegExpBuiltinExec(urlPattern’s password
698691
// component's regular expression, password).
699-
std::smatch password_exec_result_value;
700692
auto password_exec_result =
701-
std::regex_search(password, password_exec_result_value,
702-
password_component.regexp, regex_flags);
693+
regex_provider::regex_search(password, password_component.regexp);
703694

704695
// Let hostnameExecResult be RegExpBuiltinExec(urlPattern’s hostname
705696
// component's regular expression, hostname).
706-
std::smatch hostname_exec_result_value;
707697
auto hostname_exec_result =
708-
std::regex_search(hostname, hostname_exec_result_value,
709-
hostname_component.regexp, regex_flags);
698+
regex_provider::regex_search(hostname, hostname_component.regexp);
710699

711700
// Let portExecResult be RegExpBuiltinExec(urlPattern’s port component's
712701
// regular expression, port).
713-
std::smatch port_exec_result_value;
714-
auto port_exec_result = std::regex_search(port, port_exec_result_value,
715-
port_component.regexp, regex_flags);
702+
auto port_exec_result =
703+
regex_provider::regex_search(port, port_component.regexp);
716704

717705
// Let pathnameExecResult be RegExpBuiltinExec(urlPattern’s pathname
718706
// component's regular expression, pathname).
719-
std::smatch pathname_exec_result_value;
720707
auto pathname_exec_result =
721-
std::regex_search(pathname, pathname_exec_result_value,
722-
pathname_component.regexp, regex_flags);
708+
regex_provider::regex_search(pathname, pathname_component.regexp);
723709

724710
// Let searchExecResult be RegExpBuiltinExec(urlPattern’s search component's
725711
// regular expression, search).
726-
std::smatch search_exec_result_value;
727-
auto search_exec_result = std::regex_search(
728-
search, search_exec_result_value, search_component.regexp, regex_flags);
712+
auto search_exec_result =
713+
regex_provider::regex_search(search, search_component.regexp);
729714

730715
// Let hashExecResult be RegExpBuiltinExec(urlPattern’s hash component's
731716
// regular expression, hash).
732-
std::smatch hash_exec_result_value;
733-
auto hash_exec_result = std::regex_search(hash, hash_exec_result_value,
734-
hash_component.regexp, regex_flags);
717+
auto hash_exec_result =
718+
regex_provider::regex_search(hash, hash_component.regexp);
735719

736720
// If protocolExecResult, usernameExecResult, passwordExecResult,
737721
// hostnameExecResult, portExecResult, pathnameExecResult, searchExecResult,
@@ -749,42 +733,42 @@ result<std::optional<url_pattern_result>> url_pattern<regex_provider>::match(
749733
// Set result["protocol"] to the result of creating a component match result
750734
// given urlPattern’s protocol component, protocol, and protocolExecResult.
751735
result.protocol = protocol_component.create_component_match_result(
752-
protocol, protocol_exec_result_value);
736+
protocol, *protocol_exec_result);
753737

754738
// Set result["username"] to the result of creating a component match result
755739
// given urlPattern’s username component, username, and usernameExecResult.
756740
result.username = username_component.create_component_match_result(
757-
username, username_exec_result_value);
741+
username, *username_exec_result);
758742

759743
// Set result["password"] to the result of creating a component match result
760744
// given urlPattern’s password component, password, and passwordExecResult.
761745
result.password = password_component.create_component_match_result(
762-
password, password_exec_result_value);
746+
password, *password_exec_result);
763747

764748
// Set result["hostname"] to the result of creating a component match result
765749
// given urlPattern’s hostname component, hostname, and hostnameExecResult.
766750
result.hostname = hostname_component.create_component_match_result(
767-
hostname, hostname_exec_result_value);
751+
hostname, *hostname_exec_result);
768752

769753
// Set result["port"] to the result of creating a component match result given
770754
// urlPattern’s port component, port, and portExecResult.
771-
result.port = port_component.create_component_match_result(
772-
port, port_exec_result_value);
755+
result.port =
756+
port_component.create_component_match_result(port, *port_exec_result);
773757

774758
// Set result["pathname"] to the result of creating a component match result
775759
// given urlPattern’s pathname component, pathname, and pathnameExecResult.
776760
result.pathname = pathname_component.create_component_match_result(
777-
pathname, pathname_exec_result_value);
761+
pathname, *pathname_exec_result);
778762

779763
// Set result["search"] to the result of creating a component match result
780764
// given urlPattern’s search component, search, and searchExecResult.
781765
result.search = search_component.create_component_match_result(
782-
search, search_exec_result_value);
766+
search, *search_exec_result);
783767

784768
// Set result["hash"] to the result of creating a component match result given
785769
// urlPattern’s hash component, hash, and hashExecResult.
786-
result.hash = hash_component.create_component_match_result(
787-
hash, hash_exec_result_value);
770+
result.hash =
771+
hash_component.create_component_match_result(hash, *hash_exec_result);
788772

789773
return result;
790774
}

src/url_pattern_helpers.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,10 +193,11 @@ template <url_pattern_regex::regex_concept regex_provider>
193193
bool protocol_component_matches_special_scheme(
194194
url_pattern_component<regex_provider>& component) {
195195
auto regex = component.regexp;
196-
// TODO: Use provider.regex_match
197-
return std::regex_match("http", regex) || std::regex_match("https", regex) ||
198-
std::regex_match("ws", regex) || std::regex_match("wss", regex) ||
199-
std::regex_match("ftp", regex);
196+
return regex_provider::regex_match("http", regex) ||
197+
regex_provider::regex_match("https", regex) ||
198+
regex_provider::regex_match("ws", regex) ||
199+
regex_provider::regex_match("wss", regex) ||
200+
regex_provider::regex_match("ftp", regex);
200201
}
201202

202203
template <url_pattern_regex::regex_concept regex_provider>

src/url_pattern_regex.cpp

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,22 @@ std::optional<std::regex> std_regex_provider::create_instance(
1919
}
2020
}
2121

22-
std::vector<std::string> std_regex_provider::regex_search(
22+
std::optional<std::vector<std::string>> std_regex_provider::regex_search(
2323
std::string_view input, const std::regex& pattern) {
24-
std::vector<std::string> matches;
2524
std::string input_str(
2625
input.begin(),
2726
input.end()); // Convert string_view to string for regex_search
2827
std::smatch match_result;
29-
while (std::regex_search(input_str, match_result, pattern,
30-
std::regex_constants::match_any)) {
31-
matches.push_back(match_result.str());
32-
input_str = match_result.suffix().str();
28+
if (!std::regex_search(input_str, match_result, pattern,
29+
std::regex_constants::match_any)) {
30+
return std::nullopt;
31+
}
32+
std::vector<std::string> matches;
33+
matches.reserve(match_result.size() - 1);
34+
for (const auto& match : match_result) {
35+
if (match.matched) {
36+
matches.push_back(match.str());
37+
}
3338
}
3439
return matches;
3540
}

0 commit comments

Comments
 (0)