5#ifndef ADA_URL_PATTERN_HELPERS_H
6#define ADA_URL_PATTERN_HELPERS_H
8#include "ada/expected.h"
16#if ADA_INCLUDE_URL_PATTERN
18enum class errors : uint8_t;
21namespace ada::url_pattern_helpers {
24enum class token_type : uint8_t {
42enum class token_policy {
50 token(token_type _type,
size_t _index, std::string_view _value)
51 :
type(_type), index(_index), value(_value) {}
54 token_type
type = token_type::INVALID_CHAR;
62 std::string_view value{};
66template <url_pattern_encoding_callback F>
67class url_pattern_parser {
69 url_pattern_parser(F& encoding_callback_,
70 std::string_view segment_wildcard_regexp_)
71 : encoding_callback(encoding_callback_),
72 segment_wildcard_regexp(segment_wildcard_regexp_) {}
74 bool can_continue()
const {
return index < tokens.size(); }
77 token* try_consume_token(token_type type);
79 token* try_consume_modifier_token();
82 token* try_consume_regexp_or_wildcard_token(
const token* name_token);
84 std::string consume_text();
86 bool consume_required_token(token_type type);
89 std::optional<errors> maybe_add_part_from_the_pending_fixed_value()
92 std::optional<errors> add_part(std::string_view prefix, token* name_token,
93 token* regexp_or_wildcard_token,
94 std::string_view suyffix,
97 std::vector<token> tokens{};
99 std::string segment_wildcard_regexp;
100 std::vector<url_pattern_part> parts{};
101 std::string pending_fixed_value{};
103 size_t next_numeric_name = 0;
109 explicit Tokenizer(std::string_view new_input, token_policy new_policy)
110 : input(new_input), policy(new_policy) {}
113 constexpr void get_next_code_point();
116 constexpr void seek_and_get_next_code_point(
size_t index);
120 void add_token(token_type type,
size_t next_position,
size_t value_position,
121 size_t value_length);
124 void add_token_with_default_length(token_type type,
size_t next_position,
125 size_t value_position);
129 void add_token_with_defaults(token_type type);
132 std::optional<errors> process_tokenizing_error(
135 friend tl::expected<std::vector<token>,
errors> tokenize(
136 std::string_view input, token_policy policy);
140 std::string_view input;
144 std::vector<token> token_list{};
148 size_t next_index = 0;
150 char32_t code_point{};
154template <url_pattern_regex::regex_concept regex_prov
ider>
155struct constructor_string_parser {
156 explicit constructor_string_parser(std::string_view new_input,
157 std::vector<token>&& new_token_list)
158 : input(new_input), token_list(std::move(new_token_list)) {}
160 static tl::expected<url_pattern_init, errors>
parse(std::string_view input);
179 std::optional<errors> compute_protocol_matches_special_scheme_flag();
183 constexpr void rewind();
186 constexpr bool is_hash_prefix();
189 constexpr bool is_search_prefix();
192 void change_state(State state,
size_t skip);
195 constexpr bool is_group_open()
const;
198 constexpr bool is_group_close()
const;
201 constexpr bool is_protocol_suffix()
const;
204 constexpr bool next_is_authority_slashes()
const;
207 constexpr bool is_an_identity_terminator()
const;
210 constexpr bool is_pathname_start()
const;
213 constexpr bool is_password_prefix()
const;
216 constexpr bool is_an_ipv6_open()
const;
219 constexpr bool is_an_ipv6_close()
const;
222 constexpr bool is_port_prefix()
const;
225 constexpr bool is_non_special_pattern_char(
size_t index,
226 uint32_t value)
const;
229 constexpr const token* get_safe_token(
size_t index)
const;
232 std::string make_component_string();
234 std::string_view input;
237 std::vector<token> token_list;
240 url_pattern_init
result{};
242 size_t component_start = 0;
244 size_t token_index = 0;
246 size_t token_increment = 1;
248 size_t group_depth = 0;
251 size_t hostname_ipv6_bracket_depth = 0;
254 bool protocol_matches_a_special_scheme_flag =
false;
256 State
state = State::INIT;
260tl::expected<std::string, errors> canonicalize_protocol(std::string_view input);
263tl::expected<std::string, errors> canonicalize_username(std::string_view input);
266tl::expected<std::string, errors> canonicalize_password(std::string_view input);
269tl::expected<std::string, errors> canonicalize_hostname(std::string_view input);
272tl::expected<std::string, errors> canonicalize_ipv6_hostname(
273 std::string_view input);
276tl::expected<std::string, errors> canonicalize_port(std::string_view input);
279tl::expected<std::string, errors> canonicalize_port_with_protocol(
280 std::string_view input, std::string_view protocol);
283tl::expected<std::string, errors> canonicalize_pathname(std::string_view input);
286tl::expected<std::string, errors> canonicalize_opaque_pathname(
287 std::string_view input);
290tl::expected<std::string, errors> canonicalize_search(std::string_view input);
293tl::expected<std::string, errors> canonicalize_hash(std::string_view input);
296tl::expected<std::vector<token>,
errors> tokenize(std::string_view input,
297 token_policy policy);
300std::string process_base_url_string(std::string_view input,
301 url_pattern_init::process_type type);
304std::string escape_pattern_string(std::string_view input);
307std::string escape_regexp_string(std::string_view input);
310constexpr bool is_absolute_pathname(
311 std::string_view input, url_pattern_init::process_type type)
noexcept;
314template <url_pattern_encoding_callback F>
315tl::expected<std::vector<url_pattern_part>,
errors> parse_pattern_string(
316 std::string_view input, url_pattern_compile_component_options& options,
317 F& encoding_callback);
320std::string generate_pattern_string(
321 std::vector<url_pattern_part>& part_list,
322 url_pattern_compile_component_options& options);
326std::tuple<std::string, std::vector<std::string>>
327generate_regular_expression_and_name_list(
328 const std::vector<url_pattern_part>& part_list,
329 url_pattern_compile_component_options options);
332bool is_ipv6_address(std::string_view input)
noexcept;
336template <url_pattern_regex::regex_concept regex_prov
ider>
337bool protocol_component_matches_special_scheme(
338 ada::url_pattern_component<regex_provider>& input);
341std::string_view convert_modifier_to_string(url_pattern_part_modifier modifier);
344std::string generate_segment_wildcard_regexp(
345 url_pattern_compile_component_options options);
Cross-platform compiler macros and common definitions.
type
Enumeration of URL scheme types.
errors
Error codes for URL parsing operations.
state
States in the URL parsing state machine.
ada_warn_unused std::string_view to_string(encoding_type type)
tl::expected< result_type, ada::errors > result
ada_warn_unused ada::result< result_type > parse(std::string_view input, const result_type *base_url=nullptr)
URLPattern API implementation.