Skip to content

Commit

Permalink
fix build errors
Browse files Browse the repository at this point in the history
# Conflicts:
#	.gitignore
  • Loading branch information
anonrig committed Jan 8, 2025
1 parent a68ae1f commit 8f8e8f2
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 45 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ benchmarks/competitors/servo-url/target

#ignore VScode
.vscode/
.idea

# bazel output
bazel-*
23 changes: 12 additions & 11 deletions include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ tl::expected<result_type, url_pattern_errors> parse_url_pattern_impl(
// Important: C++20 allows us to use concept rather than `using` or `typedef
// and allows functions with second argument, which is optional (using either
// std::nullopt or a parameter with default value)
template <typename F>
concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
{ f(sv) } -> std::same_as<tl::expected<std::string, url_pattern_errors>>;
} || requires(F f, std::string_view sv, std::string_view opt) {
{ f(sv, opt) } -> std::same_as<tl::expected<std::string, url_pattern_errors>>;
};
// template <typename F>
// concept url_pattern_encoding_callback = requires(F f, std::string_view sv) {
// { f(sv) } -> std::same_as<tl::expected<std::string, url_pattern_errors>>;
// } || requires(F f, std::string_view sv, std::string_view opt) {
// { f(sv, opt) } -> std::same_as<tl::expected<std::string,
// url_pattern_errors>>;
// };

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

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

// @see https://urlpattern.spec.whatwg.org/#create-a-component-match-result
Expand Down Expand Up @@ -249,9 +250,9 @@ struct url_pattern_options {
class url_pattern {
public:
url_pattern() = default;
explicit url_pattern(std::optional<url_pattern_input> input,
std::optional<std::string_view> base_url,
std::optional<url_pattern_options> options);
explicit url_pattern(std::optional<url_pattern_input>&& input,
std::optional<std::string_view>&& base_url,
std::optional<url_pattern_options>&& options);

// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
tl::expected<std::optional<url_pattern_result>, url_pattern_errors> exec(
Expand Down
16 changes: 8 additions & 8 deletions include/ada/url_pattern_helpers-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ inline bool is_valid_name_code_point(char cp, bool first) {
return true;
}

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

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

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

template <url_pattern_encoding_callback F>
template <typename F>
std::string url_pattern_parser<F>::consume_text() {
// Let result be the empty string.
std::string result{};
Expand All @@ -396,7 +396,7 @@ std::string url_pattern_parser<F>::consume_text() {
return result;
}

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

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

template <url_pattern_encoding_callback F>
template <typename F>
std::optional<url_pattern_errors> url_pattern_parser<F>::add_part(
std::string_view prefix, Token* name_token, Token* regexp_or_wildcard_token,
std::string_view suffix, Token* modifier_token) {
Expand Down Expand Up @@ -554,7 +554,7 @@ std::optional<url_pattern_errors> url_pattern_parser<F>::add_part(
return std::nullopt;
}

template <url_pattern_encoding_callback F>
template <typename F>
bool url_pattern_parser<F>::is_duplicate_name(std::string_view name) {
// For each part of parser’s part list:
// If part’s name is name, then return true.
Expand Down
18 changes: 10 additions & 8 deletions include/ada/url_pattern_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

#include <string>
#include <tuple>
#include <unordered_map>
#include <variant>
#include <vector>

namespace ada::url_pattern_helpers {
Expand Down Expand Up @@ -50,10 +48,10 @@ struct Token {
};

// @see https://urlpattern.spec.whatwg.org/#pattern-parser
template <url_pattern_encoding_callback F>
template <typename url_pattern_encoding_callback>
class url_pattern_parser {
public:
url_pattern_parser(F encoding_callback_,
url_pattern_parser(url_pattern_encoding_callback&& encoding_callback_,
std::string_view segment_wildcard_regexp_)
: encoding_callback(encoding_callback_),
segment_wildcard_regexp(std::string(segment_wildcard_regexp_)) {}
Expand Down Expand Up @@ -83,7 +81,7 @@ class url_pattern_parser {
bool is_duplicate_name(std::string_view name);

std::vector<Token> tokens{};
F encoding_callback;
url_pattern_encoding_callback encoding_callback;
std::string segment_wildcard_regexp;
std::vector<url_pattern_part> parts{};
std::string pending_fixed_value{};
Expand Down Expand Up @@ -260,7 +258,11 @@ tl::expected<std::string, url_pattern_errors> canonicalize_ipv6_hostname(

// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
tl::expected<std::string, url_pattern_errors> canonicalize_port(
std::string_view input, std::string_view protocol = "fake");
std::string_view input);

// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
tl::expected<std::string, url_pattern_errors> canonicalize_port_with_protocol(
std::string_view input, std::string_view protocol);

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

// @see https://urlpattern.spec.whatwg.org/#parse-a-pattern-string
template <url_pattern_encoding_callback F>
template <typename url_pattern_encoding_callback>
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
parse_pattern_string(std::string_view input,
url_pattern_compile_component_options& options,
F encoding_callback);
url_pattern_encoding_callback&& encoding_callback);

// @see https://urlpattern.spec.whatwg.org/#generate-a-pattern-string
std::string generate_pattern_string(
Expand Down
24 changes: 12 additions & 12 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -971,18 +971,6 @@ tl::expected<url_pattern, url_pattern_errors> parse_url_pattern_impl(
// Let urlPattern be a new URL pattern.
auto url_pattern_ = url_pattern{};

// Set urlPattern’s protocol component to the result of compiling a component
// given processedInit["protocol"], canonicalize a protocol, and default
// options.
auto protocol_component = url_pattern_component::compile(
processed_init->protocol.value(),
url_pattern_helpers::canonicalize_protocol,
url_pattern_compile_component_options::DEFAULT);
if (!protocol_component) {
return tl::unexpected(protocol_component.error());
}
url_pattern_.protocol_component = std::move(*protocol_component);

// Set urlPattern’s username component to the result of compiling a component
// given processedInit["username"], canonicalize a username, and default
// options.
Expand All @@ -995,6 +983,18 @@ tl::expected<url_pattern, url_pattern_errors> parse_url_pattern_impl(
}
url_pattern_.username_component = std::move(*username_component);

// Set urlPattern’s protocol component to the result of compiling a component
// given processedInit["protocol"], canonicalize a protocol, and default
// options.
auto protocol_component = url_pattern_component::compile(
processed_init->protocol.value(),
url_pattern_helpers::canonicalize_protocol,
url_pattern_compile_component_options::DEFAULT);
if (!protocol_component) {
return tl::unexpected(protocol_component.error());
}
url_pattern_.protocol_component = std::move(*protocol_component);

// Set urlPattern’s password component to the result of compiling a component
// given processedInit["password"], canonicalize a password, and default
// options.
Expand Down
7 changes: 4 additions & 3 deletions src/url_pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ tl::expected<std::string, url_pattern_errors> url_pattern_init::process_port(
}
// Return the result of running canonicalize a port given portValue and
// protocolValue.
return url_pattern_helpers::canonicalize_port(port, protocol);
return url_pattern_helpers::canonicalize_port_with_protocol(port, protocol);
}

tl::expected<std::string, url_pattern_errors>
Expand Down Expand Up @@ -411,9 +411,10 @@ tl::expected<std::string, url_pattern_errors> url_pattern_init::process_hash(
return url_pattern_helpers::canonicalize_hash(value);
}

template <url_pattern_encoding_callback F>
template <typename url_pattern_encoding_callback>
tl::expected<url_pattern_component, url_pattern_errors>
url_pattern_component::compile(std::string_view input, F encoding_callback,
url_pattern_component::compile(std::string_view input,
url_pattern_encoding_callback encoding_callback,
url_pattern_compile_component_options& options) {
// Let part list be the result of running parse a pattern string given input,
// options, and encoding callback.
Expand Down
25 changes: 22 additions & 3 deletions src/url_pattern_helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,25 @@ tl::expected<std::string, url_pattern_errors> canonicalize_ipv6_hostname(
}

tl::expected<std::string, url_pattern_errors> canonicalize_port(
std::string_view port_value) {
// If portValue is the empty string, return portValue.
if (port_value.empty()) [[unlikely]] {
return "";
}
// Let dummyURL be a new URL record.
// If protocolValue was given, then set dummyURL’s scheme to protocolValue.
// Let parseResult be the result of running basic URL parser given portValue
// with dummyURL as url and port state as state override.
auto url = ada::parse<url_aggregator>("fake://dummy.test", nullptr);
if (url && url->set_port(port_value)) {
// Return dummyURL’s port, serialized, or empty string if it is null.
return std::string(url->get_port());
}
// If parseResult is failure, then throw a TypeError.
return tl::unexpected(url_pattern_errors::type_error);
}

tl::expected<std::string, url_pattern_errors> canonicalize_port_with_protocol(
std::string_view port_value, std::string_view protocol) {
// If portValue is the empty string, return portValue.
if (port_value.empty()) [[unlikely]] {
Expand Down Expand Up @@ -854,15 +873,15 @@ constexpr bool is_absolute_pathname(std::string_view input,
return false;
}

template <url_pattern_encoding_callback F>
template <typename url_pattern_encoding_callback>
tl::expected<std::vector<url_pattern_part>, url_pattern_errors>
parse_pattern_string(std::string_view input,
url_pattern_compile_component_options& options,
F encoding_callback) {
url_pattern_encoding_callback&& encoding_callback) {
// Let parser be a new pattern parser whose encoding callback is encoding
// callback and segment wildcard regexp is the result of running generate a
// segment wildcard regexp given options.
auto parser = url_pattern_parser<F>(
auto parser = url_pattern_parser<url_pattern_encoding_callback>(
encoding_callback, generate_segment_wildcard_regexp(options));
// Set parser’s token list to the result of running tokenize given input and
// "strict".
Expand Down

0 comments on commit 8f8e8f2

Please sign in to comment.