Skip to content

Commit 8f8e8f2

Browse files
committed
fix build errors
# Conflicts: # .gitignore
1 parent a68ae1f commit 8f8e8f2

7 files changed

+69
-45
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ benchmarks/competitors/servo-url/target
2525

2626
#ignore VScode
2727
.vscode/
28+
.idea
2829

2930
# bazel output
3031
bazel-*

include/ada/url_pattern.h

+12-11
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ tl::expected<result_type, url_pattern_errors> parse_url_pattern_impl(
2828
// Important: C++20 allows us to use concept rather than `using` or `typedef
2929
// and allows functions with second argument, which is optional (using either
3030
// std::nullopt or a parameter with default value)
31-
template <typename F>
32-
concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
33-
{ f(sv) } -> std::same_as<tl::expected<std::string, url_pattern_errors>>;
34-
} || requires(F f, std::string_view sv, std::string_view opt) {
35-
{ f(sv, opt) } -> std::same_as<tl::expected<std::string, url_pattern_errors>>;
36-
};
31+
// template <typename F>
32+
// concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
33+
// { f(sv) } -> std::same_as<tl::expected<std::string, url_pattern_errors>>;
34+
// } || requires(F f, std::string_view sv, std::string_view opt) {
35+
// { f(sv, opt) } -> std::same_as<tl::expected<std::string,
36+
// url_pattern_errors>>;
37+
// };
3738

3839
// A structure providing matching patterns for individual components
3940
// of a URL. When a URLPattern is created, or when a URLPattern is
@@ -194,9 +195,9 @@ class url_pattern_component {
194195
has_regexp_groups_(new_has_regexp_groups){};
195196

196197
// @see https://urlpattern.spec.whatwg.org/#compile-a-component
197-
template <url_pattern_encoding_callback F>
198+
template <typename url_pattern_encoding_callback>
198199
static tl::expected<url_pattern_component, url_pattern_errors> compile(
199-
std::string_view input, F encoding_callback,
200+
std::string_view input, url_pattern_encoding_callback encoding_callback,
200201
url_pattern_compile_component_options& options);
201202

202203
// @see https://urlpattern.spec.whatwg.org/#create-a-component-match-result
@@ -249,9 +250,9 @@ struct url_pattern_options {
249250
class url_pattern {
250251
public:
251252
url_pattern() = default;
252-
explicit url_pattern(std::optional<url_pattern_input> input,
253-
std::optional<std::string_view> base_url,
254-
std::optional<url_pattern_options> options);
253+
explicit url_pattern(std::optional<url_pattern_input>&& input,
254+
std::optional<std::string_view>&& base_url,
255+
std::optional<url_pattern_options>&& options);
255256

256257
// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
257258
tl::expected<std::optional<url_pattern_result>, url_pattern_errors> exec(

include/ada/url_pattern_helpers-inl.h

+8-8
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ inline bool is_valid_name_code_point(char cp, bool first) {
332332
return true;
333333
}
334334

335-
template <url_pattern_encoding_callback F>
335+
template <typename F>
336336
Token* url_pattern_parser<F>::try_consume_modifier_token() {
337337
// Let token be the result of running try to consume a token given parser and
338338
// "other-modifier".
@@ -346,7 +346,7 @@ Token* url_pattern_parser<F>::try_consume_modifier_token() {
346346
return token;
347347
}
348348

349-
template <url_pattern_encoding_callback F>
349+
template <typename F>
350350
Token* url_pattern_parser<F>::try_consume_regexp_or_wildcard_token(
351351
Token* name_token) {
352352
// Let token be the result of running try to consume a token given parser and
@@ -361,7 +361,7 @@ Token* url_pattern_parser<F>::try_consume_regexp_or_wildcard_token(
361361
return token;
362362
}
363363

364-
template <url_pattern_encoding_callback F>
364+
template <typename F>
365365
Token* url_pattern_parser<F>::try_consume_token(token_type type) {
366366
// Assert: parser’s index is less than parser’s token list size.
367367
ADA_ASSERT_TRUE(index < tokens.size());
@@ -375,7 +375,7 @@ Token* url_pattern_parser<F>::try_consume_token(token_type type) {
375375
return &next_token;
376376
}
377377

378-
template <url_pattern_encoding_callback F>
378+
template <typename F>
379379
std::string url_pattern_parser<F>::consume_text() {
380380
// Let result be the empty string.
381381
std::string result{};
@@ -396,7 +396,7 @@ std::string url_pattern_parser<F>::consume_text() {
396396
return result;
397397
}
398398

399-
template <url_pattern_encoding_callback F>
399+
template <typename F>
400400
tl::expected<Token, url_pattern_errors>
401401
url_pattern_parser<F>::consume_required_token(token_type type) {
402402
// Let result be the result of running try to consume a token given parser and
@@ -409,7 +409,7 @@ url_pattern_parser<F>::consume_required_token(token_type type) {
409409
return std::move(*result);
410410
}
411411

412-
template <url_pattern_encoding_callback F>
412+
template <typename F>
413413
std::optional<url_pattern_errors>
414414
url_pattern_parser<F>::maybe_add_part_from_the_pending_fixed_value() {
415415
// If parser’s pending fixed value is the empty string, then return.
@@ -433,7 +433,7 @@ url_pattern_parser<F>::maybe_add_part_from_the_pending_fixed_value() {
433433
return std::nullopt;
434434
}
435435

436-
template <url_pattern_encoding_callback F>
436+
template <typename F>
437437
std::optional<url_pattern_errors> url_pattern_parser<F>::add_part(
438438
std::string_view prefix, Token* name_token, Token* regexp_or_wildcard_token,
439439
std::string_view suffix, Token* modifier_token) {
@@ -554,7 +554,7 @@ std::optional<url_pattern_errors> url_pattern_parser<F>::add_part(
554554
return std::nullopt;
555555
}
556556

557-
template <url_pattern_encoding_callback F>
557+
template <typename F>
558558
bool url_pattern_parser<F>::is_duplicate_name(std::string_view name) {
559559
// For each part of parser’s part list:
560560
// If part’s name is name, then return true.

include/ada/url_pattern_helpers.h

+10-8
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
#include <string>
1111
#include <tuple>
12-
#include <unordered_map>
13-
#include <variant>
1412
#include <vector>
1513

1614
namespace ada::url_pattern_helpers {
@@ -50,10 +48,10 @@ struct Token {
5048
};
5149

5250
// @see https://urlpattern.spec.whatwg.org/#pattern-parser
53-
template <url_pattern_encoding_callback F>
51+
template <typename url_pattern_encoding_callback>
5452
class url_pattern_parser {
5553
public:
56-
url_pattern_parser(F encoding_callback_,
54+
url_pattern_parser(url_pattern_encoding_callback&& encoding_callback_,
5755
std::string_view segment_wildcard_regexp_)
5856
: encoding_callback(encoding_callback_),
5957
segment_wildcard_regexp(std::string(segment_wildcard_regexp_)) {}
@@ -83,7 +81,7 @@ class url_pattern_parser {
8381
bool is_duplicate_name(std::string_view name);
8482

8583
std::vector<Token> tokens{};
86-
F encoding_callback;
84+
url_pattern_encoding_callback encoding_callback;
8785
std::string segment_wildcard_regexp;
8886
std::vector<url_pattern_part> parts{};
8987
std::string pending_fixed_value{};
@@ -260,7 +258,11 @@ tl::expected<std::string, url_pattern_errors> canonicalize_ipv6_hostname(
260258

261259
// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
262260
tl::expected<std::string, url_pattern_errors> canonicalize_port(
263-
std::string_view input, std::string_view protocol = "fake");
261+
std::string_view input);
262+
263+
// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
264+
tl::expected<std::string, url_pattern_errors> canonicalize_port_with_protocol(
265+
std::string_view input, std::string_view protocol);
264266

265267
// @see https://wicg.github.io/urlpattern/#canonicalize-a-pathname
266268
tl::expected<std::string, url_pattern_errors> canonicalize_pathname(
@@ -297,11 +299,11 @@ constexpr bool is_absolute_pathname(std::string_view input,
297299
std::string_view type) noexcept;
298300

299301
// @see https://urlpattern.spec.whatwg.org/#parse-a-pattern-string
300-
template <url_pattern_encoding_callback F>
302+
template <typename url_pattern_encoding_callback>
301303
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
302304
parse_pattern_string(std::string_view input,
303305
url_pattern_compile_component_options& options,
304-
F encoding_callback);
306+
url_pattern_encoding_callback&& encoding_callback);
305307

306308
// @see https://urlpattern.spec.whatwg.org/#generate-a-pattern-string
307309
std::string generate_pattern_string(

src/parser.cpp

+12-12
Original file line numberDiff line numberDiff line change
@@ -971,18 +971,6 @@ tl::expected<url_pattern, url_pattern_errors> parse_url_pattern_impl(
971971
// Let urlPattern be a new URL pattern.
972972
auto url_pattern_ = url_pattern{};
973973

974-
// Set urlPattern’s protocol component to the result of compiling a component
975-
// given processedInit["protocol"], canonicalize a protocol, and default
976-
// options.
977-
auto protocol_component = url_pattern_component::compile(
978-
processed_init->protocol.value(),
979-
url_pattern_helpers::canonicalize_protocol,
980-
url_pattern_compile_component_options::DEFAULT);
981-
if (!protocol_component) {
982-
return tl::unexpected(protocol_component.error());
983-
}
984-
url_pattern_.protocol_component = std::move(*protocol_component);
985-
986974
// Set urlPattern’s username component to the result of compiling a component
987975
// given processedInit["username"], canonicalize a username, and default
988976
// options.
@@ -995,6 +983,18 @@ tl::expected<url_pattern, url_pattern_errors> parse_url_pattern_impl(
995983
}
996984
url_pattern_.username_component = std::move(*username_component);
997985

986+
// Set urlPattern’s protocol component to the result of compiling a component
987+
// given processedInit["protocol"], canonicalize a protocol, and default
988+
// options.
989+
auto protocol_component = url_pattern_component::compile(
990+
processed_init->protocol.value(),
991+
url_pattern_helpers::canonicalize_protocol,
992+
url_pattern_compile_component_options::DEFAULT);
993+
if (!protocol_component) {
994+
return tl::unexpected(protocol_component.error());
995+
}
996+
url_pattern_.protocol_component = std::move(*protocol_component);
997+
998998
// Set urlPattern’s password component to the result of compiling a component
999999
// given processedInit["password"], canonicalize a password, and default
10001000
// options.

src/url_pattern.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ tl::expected<std::string, url_pattern_errors> url_pattern_init::process_port(
356356
}
357357
// Return the result of running canonicalize a port given portValue and
358358
// protocolValue.
359-
return url_pattern_helpers::canonicalize_port(port, protocol);
359+
return url_pattern_helpers::canonicalize_port_with_protocol(port, protocol);
360360
}
361361

362362
tl::expected<std::string, url_pattern_errors>
@@ -411,9 +411,10 @@ tl::expected<std::string, url_pattern_errors> url_pattern_init::process_hash(
411411
return url_pattern_helpers::canonicalize_hash(value);
412412
}
413413

414-
template <url_pattern_encoding_callback F>
414+
template <typename url_pattern_encoding_callback>
415415
tl::expected<url_pattern_component, url_pattern_errors>
416-
url_pattern_component::compile(std::string_view input, F encoding_callback,
416+
url_pattern_component::compile(std::string_view input,
417+
url_pattern_encoding_callback encoding_callback,
417418
url_pattern_compile_component_options& options) {
418419
// Let part list be the result of running parse a pattern string given input,
419420
// options, and encoding callback.

src/url_pattern_helpers.cpp

+22-3
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ tl::expected<std::string, url_pattern_errors> canonicalize_ipv6_hostname(
122122
}
123123

124124
tl::expected<std::string, url_pattern_errors> canonicalize_port(
125+
std::string_view port_value) {
126+
// If portValue is the empty string, return portValue.
127+
if (port_value.empty()) [[unlikely]] {
128+
return "";
129+
}
130+
// Let dummyURL be a new URL record.
131+
// If protocolValue was given, then set dummyURL’s scheme to protocolValue.
132+
// Let parseResult be the result of running basic URL parser given portValue
133+
// with dummyURL as url and port state as state override.
134+
auto url = ada::parse<url_aggregator>("fake://dummy.test", nullptr);
135+
if (url && url->set_port(port_value)) {
136+
// Return dummyURL’s port, serialized, or empty string if it is null.
137+
return std::string(url->get_port());
138+
}
139+
// If parseResult is failure, then throw a TypeError.
140+
return tl::unexpected(url_pattern_errors::type_error);
141+
}
142+
143+
tl::expected<std::string, url_pattern_errors> canonicalize_port_with_protocol(
125144
std::string_view port_value, std::string_view protocol) {
126145
// If portValue is the empty string, return portValue.
127146
if (port_value.empty()) [[unlikely]] {
@@ -854,15 +873,15 @@ constexpr bool is_absolute_pathname(std::string_view input,
854873
return false;
855874
}
856875

857-
template <url_pattern_encoding_callback F>
876+
template <typename url_pattern_encoding_callback>
858877
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
859878
parse_pattern_string(std::string_view input,
860879
url_pattern_compile_component_options& options,
861-
F encoding_callback) {
880+
url_pattern_encoding_callback&& encoding_callback) {
862881
// Let parser be a new pattern parser whose encoding callback is encoding
863882
// callback and segment wildcard regexp is the result of running generate a
864883
// segment wildcard regexp given options.
865-
auto parser = url_pattern_parser<F>(
884+
auto parser = url_pattern_parser<url_pattern_encoding_callback>(
866885
encoding_callback, generate_segment_wildcard_regexp(options));
867886
// Set parser’s token list to the result of running tokenize given input and
868887
// "strict".

0 commit comments

Comments
 (0)