Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
hide implementation detail
  • Loading branch information
anonrig committed Jan 30, 2025
commit 87a6542981143540a414a16ae94d651a8a9b6700
8 changes: 4 additions & 4 deletions include/ada/url_pattern-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,15 @@ url_pattern_component<regex_provider>::compile(

template <url_pattern_regex::regex_concept regex_provider>
result<std::optional<url_pattern_result>> url_pattern<regex_provider>::exec(
const url_pattern_input& input, std::string_view* base_url) {
const url_pattern_input& input, const std::string_view* base_url) {
// Return the result of match given this's associated URL pattern, input, and
// baseURL if given.
return match(input, base_url);
}

template <url_pattern_regex::regex_concept regex_provider>
result<bool> url_pattern<regex_provider>::test(const url_pattern_input& input,
std::string_view* base_url) {
result<bool> url_pattern<regex_provider>::test(
const url_pattern_input& input, const std::string_view* base_url) {
// TODO: Optimization opportunity. Rather than returning `url_pattern_result`
// Implement a fast path just like `can_parse()` in ada_url.
// Let result be the result of match given this's associated URL pattern,
Expand All @@ -215,7 +215,7 @@ result<bool> url_pattern<regex_provider>::test(const url_pattern_input& input,

template <url_pattern_regex::regex_concept regex_provider>
result<std::optional<url_pattern_result>> url_pattern<regex_provider>::match(
const url_pattern_input& input, std::string_view* base_url_string) {
const url_pattern_input& input, const std::string_view* base_url_string) {
std::string protocol{};
std::string username{};
std::string password{};
Expand Down
31 changes: 14 additions & 17 deletions include/ada/url_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#include "ada/implementation.h"
#include "ada/expected.h"
#include "ada/parser.h"
#include "ada/url_pattern_init.h"

#include <string>
#include <unordered_map>
Expand All @@ -18,13 +20,6 @@
#endif // ADA_TESTING

namespace ada {
namespace parser {
template <typename result_type, typename url_pattern_init,
typename url_pattern_options, typename regex_provider>
tl::expected<result_type, errors> parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);
} // namespace parser

enum class url_pattern_part_type : uint8_t {
// The part represents a simple fixed text string.
Expand Down Expand Up @@ -234,20 +229,23 @@ class url_pattern {
/**
* @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-exec
*/
result<std::optional<url_pattern_result>> exec(const url_pattern_input& input,
std::string_view* base_url);
result<std::optional<url_pattern_result>> exec(
const url_pattern_input& input,
const std::string_view* base_url = nullptr);

/**
* @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-test
*/
result<bool> test(const url_pattern_input& input, std::string_view* base_url);
result<bool> test(const url_pattern_input& input,
const std::string_view* base_url = nullptr);

/**
* @see https://urlpattern.spec.whatwg.org/#url-pattern-match
* This function expects a valid UTF-8 string if input is a string.
*/
result<std::optional<url_pattern_result>> match(
const url_pattern_input& input, std::string_view* base_url_string);
const url_pattern_input& input,
const std::string_view* base_url_string = nullptr);

// @see https://urlpattern.spec.whatwg.org/#dom-urlpattern-protocol
[[nodiscard]] std::string_view get_protocol() const ada_lifetime_bound;
Expand Down Expand Up @@ -286,6 +284,11 @@ class url_pattern {
}
#endif // ADA_TESTING

friend tl::expected<url_pattern, errors> parser::parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);

private:
url_pattern_component<regex_provider> protocol_component{};
url_pattern_component<regex_provider> username_component{};
url_pattern_component<regex_provider> password_component{};
Expand All @@ -295,12 +298,6 @@ class url_pattern {
url_pattern_component<regex_provider> search_component{};
url_pattern_component<regex_provider> hash_component{};
bool ignore_case_ = false;

template <typename result_type, typename url_pattern_init,
typename url_pattern_options, typename regex_provider_for_parse_url>
friend tl::expected<result_type, errors> parser::parse_url_pattern_impl(
std::variant<std::string_view, url_pattern_init> input,
const std::string_view* base_url, const url_pattern_options* options);
};

} // namespace ada
Expand Down
23 changes: 13 additions & 10 deletions include/ada/url_pattern_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ class Tokenizer {
std::optional<errors> process_tokenizing_error(
size_t next_position, size_t value_position) ada_warn_unused;

friend tl::expected<std::vector<token>, errors> tokenize(
std::string_view input, token_policy policy);

private:
// has an associated input, a pattern string, initially the empty string.
std::string input;
// has an associated policy, a tokenize policy, initially "strict".
Expand Down Expand Up @@ -214,6 +218,15 @@ struct constructor_string_parser {
// @see https://urlpattern.spec.whatwg.org/#is-a-port-prefix
bool is_port_prefix();

private:
// @see https://urlpattern.spec.whatwg.org/#is-a-non-special-pattern-char
bool is_non_special_pattern_char(size_t index, std::string_view value);

// @see https://urlpattern.spec.whatwg.org/#get-a-safe-token
const token* get_safe_token(size_t index);

// @see https://urlpattern.spec.whatwg.org/#make-a-component-string
std::string make_component_string();
// has an associated input, a string, which must be set upon creation.
std::string input;
// has an associated token list, a token list, which must be set upon
Expand All @@ -238,16 +251,6 @@ struct constructor_string_parser {
bool protocol_matches_a_special_scheme_flag = false;
// has an associated state, a string, initially set to "init".
State state = State::INIT;

private:
// @see https://urlpattern.spec.whatwg.org/#is-a-non-special-pattern-char
bool is_non_special_pattern_char(size_t index, std::string_view value);

// @see https://urlpattern.spec.whatwg.org/#get-a-safe-token
const token* get_safe_token(size_t index);

// @see https://urlpattern.spec.whatwg.org/#make-a-component-string
std::string make_component_string();
};

// @see https://urlpattern.spec.whatwg.org/#canonicalize-a-protocol
Expand Down
3 changes: 2 additions & 1 deletion include/ada/url_pattern_init.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

#include "ada/expected.h"
#include "ada/errors.h"

#include <string_view>
#include <string>
#include <optional>
Expand Down Expand Up @@ -104,4 +105,4 @@ struct url_pattern_init {
};
} // namespace ada

#endif // ADA_URL_PATTERN_INIT_H
#endif // ADA_URL_PATTERN_INIT_H
2 changes: 0 additions & 2 deletions include/ada/url_pattern_regex.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#ifndef ADA_URL_PATTERN_REGEX_H
#define ADA_URL_PATTERN_REGEX_H

#include <concepts>

#ifdef ADA_USE_UNSAFE_STD_REGEX_PROVIDER
#include <regex>
#endif // ADA_USE_UNSAFE_STD_REGEX_PROVIDER
Expand Down