Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
url_pattern_helpers.h
Go to the documentation of this file.
1
5#ifndef ADA_URL_PATTERN_HELPERS_H
6#define ADA_URL_PATTERN_HELPERS_H
7
8#include "ada/expected.h"
9#include "ada/common_defs.h"
10#include "ada/url_pattern.h"
11
12#include <cstddef>
13#include <cstdint>
14#include <optional>
15#include <string>
16#include <string_view>
17#include <tuple>
18#include <unordered_set>
19#include <utility>
20#include <vector>
21
22#if ADA_INCLUDE_URL_PATTERN
23namespace ada {
24enum class errors : uint8_t;
25}
26
27namespace ada::url_pattern_helpers {
28
29// @see https://urlpattern.spec.whatwg.org/#token
30enum class token_type : uint8_t {
31 INVALID_CHAR, // 0
32 OPEN, // 1
33 CLOSE, // 2
34 REGEXP, // 3
35 NAME, // 4
36 CHAR, // 5
37 ESCAPED_CHAR, // 6
38 OTHER_MODIFIER, // 7
39 ASTERISK, // 8
40 END, // 9
41};
42
43#ifdef ADA_TESTING
44std::string to_string(token_type type);
45#endif // ADA_TESTING
46
47// @see https://urlpattern.spec.whatwg.org/#tokenize-policy
48enum class token_policy {
49 strict,
50 lenient,
51};
52
53// @see https://urlpattern.spec.whatwg.org/#tokens
54class token {
55 public:
56 token(token_type _type, size_t _index, std::string_view _value)
57 : type(_type), index(_index), value(_value) {}
58
59 // A token has an associated type, a string, initially "invalid-char".
60 token_type type = token_type::INVALID_CHAR;
61
62 // A token has an associated index, a number, initially 0. It is the position
63 // of the first code point in the pattern string represented by the token.
64 size_t index = 0;
65
66 // A token has an associated value, a string, initially the empty string. It
67 // contains the code points from the pattern string represented by the token.
68 std::string_view value{};
69};
70
71// @see https://urlpattern.spec.whatwg.org/#pattern-parser
72template <url_pattern_encoding_callback F>
73class url_pattern_parser {
74 public:
75 url_pattern_parser(F& encoding_callback_,
76 std::string_view segment_wildcard_regexp_)
77 : encoding_callback(encoding_callback_),
78 segment_wildcard_regexp(segment_wildcard_regexp_) {}
79
80 bool can_continue() const { return index < tokens.size(); }
81
82 // @see https://urlpattern.spec.whatwg.org/#try-to-consume-a-token
83 token* try_consume_token(token_type type);
84 // @see https://urlpattern.spec.whatwg.org/#try-to-consume-a-modifier-token
85 token* try_consume_modifier_token();
86 // @see
87 // https://urlpattern.spec.whatwg.org/#try-to-consume-a-regexp-or-wildcard-token
88 token* try_consume_regexp_or_wildcard_token(const token* name_token);
89 // @see https://urlpattern.spec.whatwg.org/#consume-text
90 std::string consume_text();
91 // @see https://urlpattern.spec.whatwg.org/#consume-a-required-token
92 bool consume_required_token(token_type type);
93 // @see
94 // https://urlpattern.spec.whatwg.org/#maybe-add-a-part-from-the-pending-fixed-value
95 std::optional<errors> maybe_add_part_from_the_pending_fixed_value()
97 // @see https://urlpattern.spec.whatwg.org/#add-a-part
98 std::optional<errors> add_part(std::string_view prefix, token* name_token,
99 token* regexp_or_wildcard_token,
100 std::string_view suyffix,
101 token* modifier_token) ada_warn_unused;
102
103 std::vector<token> tokens{};
104 F& encoding_callback;
105 std::string segment_wildcard_regexp;
106 std::vector<url_pattern_part> parts{};
107 std::string pending_fixed_value{};
108 size_t index = 0;
109 size_t next_numeric_name = 0;
110 // Names already taken by a part, for the "is a duplicate name" check in
111 // add_part. A linear scan of the part list there is O(n^2) over a pattern
112 // with n named parts (e.g. "/*" repeated); this set keeps it amortized O(1).
113 std::unordered_set<std::string> seen_names{};
114};
115
116// @see https://urlpattern.spec.whatwg.org/#tokenizer
117class Tokenizer {
118 public:
119 explicit Tokenizer(std::string_view new_input, token_policy new_policy)
120 : input(new_input), policy(new_policy) {}
121
122 // @see https://urlpattern.spec.whatwg.org/#get-the-next-code-point
123 constexpr void get_next_code_point();
124
125 // True when the most recent decoded unit was malformed UTF-8.
126 bool had_invalid_code_point() const { return invalid_code_point; }
127
128 // @see https://urlpattern.spec.whatwg.org/#seek-and-get-the-next-code-point
129 constexpr void seek_and_get_next_code_point(size_t index);
130
131 // @see https://urlpattern.spec.whatwg.org/#add-a-token
132
133 void add_token(token_type type, size_t next_position, size_t value_position,
134 size_t value_length);
135
136 // @see https://urlpattern.spec.whatwg.org/#add-a-token-with-default-length
137 void add_token_with_default_length(token_type type, size_t next_position,
138 size_t value_position);
139
140 // @see
141 // https://urlpattern.spec.whatwg.org/#add-a-token-with-default-position-and-length
142 void add_token_with_defaults(token_type type);
143
144 // @see https://urlpattern.spec.whatwg.org/#process-a-tokenizing-error
145 std::optional<errors> process_tokenizing_error(
146 size_t next_position, size_t value_position) ada_warn_unused;
147
148 friend tl::expected<std::vector<token>, errors> tokenize(
149 std::string_view input, token_policy policy);
150
151 private:
152 // has an associated input, a pattern string, initially the empty string.
153 std::string_view input;
154 // has an associated policy, a tokenize policy, initially "strict".
155 token_policy policy;
156 // has an associated token list, a token list, initially an empty list.
157 std::vector<token> token_list{};
158 // has an associated index, a number, initially 0.
159 size_t index = 0;
160 // has an associated next index, a number, initially 0.
161 size_t next_index = 0;
162 // has an associated code point, a Unicode code point, initially null.
163 char32_t code_point{};
164 // Tracks whether the last decoded code point was malformed UTF-8.
165 bool invalid_code_point = false;
166};
167
168// @see https://urlpattern.spec.whatwg.org/#constructor-string-parser
169template <url_pattern_regex::regex_concept regex_provider>
170struct constructor_string_parser {
171 explicit constructor_string_parser(std::string_view new_input,
172 std::vector<token>&& new_token_list)
173 : input(new_input), token_list(std::move(new_token_list)) {}
174 // @see https://urlpattern.spec.whatwg.org/#parse-a-constructor-string
175 static tl::expected<url_pattern_init, errors> parse(std::string_view input);
176
177 // @see https://urlpattern.spec.whatwg.org/#constructor-string-parser-state
178 enum class State {
179 INIT,
180 PROTOCOL,
181 AUTHORITY,
182 USERNAME,
183 PASSWORD,
184 HOSTNAME,
185 PORT,
186 PATHNAME,
187 SEARCH,
188 HASH,
189 DONE,
190 };
191
192 // @see
193 // https://urlpattern.spec.whatwg.org/#compute-protocol-matches-a-special-scheme-flag
194 std::optional<errors> compute_protocol_matches_special_scheme_flag();
195
196 private:
197 // @see https://urlpattern.spec.whatwg.org/#rewind
198 constexpr void rewind();
199
200 // @see https://urlpattern.spec.whatwg.org/#is-a-hash-prefix
201 constexpr bool is_hash_prefix();
202
203 // @see https://urlpattern.spec.whatwg.org/#is-a-search-prefix
204 constexpr bool is_search_prefix();
205
206 // @see https://urlpattern.spec.whatwg.org/#change-state
207 void change_state(State state, size_t skip);
208
209 // @see https://urlpattern.spec.whatwg.org/#is-a-group-open
210 constexpr bool is_group_open() const;
211
212 // @see https://urlpattern.spec.whatwg.org/#is-a-group-close
213 constexpr bool is_group_close() const;
214
215 // @see https://urlpattern.spec.whatwg.org/#is-a-protocol-suffix
216 constexpr bool is_protocol_suffix() const;
217
218 // @see https://urlpattern.spec.whatwg.org/#next-is-authority-slashes
219 constexpr bool next_is_authority_slashes() const;
220
221 // @see https://urlpattern.spec.whatwg.org/#is-an-identity-terminator
222 constexpr bool is_an_identity_terminator() const;
223
224 // @see https://urlpattern.spec.whatwg.org/#is-a-pathname-start
225 constexpr bool is_pathname_start() const;
226
227 // @see https://urlpattern.spec.whatwg.org/#is-a-password-prefix
228 constexpr bool is_password_prefix() const;
229
230 // @see https://urlpattern.spec.whatwg.org/#is-an-ipv6-open
231 constexpr bool is_an_ipv6_open() const;
232
233 // @see https://urlpattern.spec.whatwg.org/#is-an-ipv6-close
234 constexpr bool is_an_ipv6_close() const;
235
236 // @see https://urlpattern.spec.whatwg.org/#is-a-port-prefix
237 constexpr bool is_port_prefix() const;
238
239 // @see https://urlpattern.spec.whatwg.org/#is-a-non-special-pattern-char
240 constexpr bool is_non_special_pattern_char(size_t index,
241 uint32_t value) const;
242
243 // @see https://urlpattern.spec.whatwg.org/#get-a-safe-token
244 constexpr const token* get_safe_token(size_t index) const;
245
246 // @see https://urlpattern.spec.whatwg.org/#make-a-component-string
247 std::string make_component_string();
248 // has an associated input, a string, which must be set upon creation.
249 std::string_view input;
250 // has an associated token list, a token list, which must be set upon
251 // creation.
252 std::vector<token> token_list;
253 // has an associated result, a URLPatternInit, initially set to a new
254 // URLPatternInit.
255 url_pattern_init result{};
256 // has an associated component start, a number, initially set to 0.
257 size_t component_start = 0;
258 // has an associated token index, a number, initially set to 0.
259 size_t token_index = 0;
260 // has an associated token increment, a number, initially set to 1.
261 size_t token_increment = 1;
262 // has an associated group depth, a number, initially set to 0.
263 size_t group_depth = 0;
264 // has an associated hostname IPv6 bracket depth, a number, initially set to
265 // 0.
266 size_t hostname_ipv6_bracket_depth = 0;
267 // has an associated protocol matches a special scheme flag, a boolean,
268 // initially set to false.
269 bool protocol_matches_a_special_scheme_flag = false;
270 // has an associated state, a string, initially set to "init".
271 State state = State::INIT;
272};
273
274// @see https://urlpattern.spec.whatwg.org/#canonicalize-a-protocol
275tl::expected<std::string, errors> canonicalize_protocol(std::string_view input);
276
277// @see https://wicg.github.io/urlpattern/#canonicalize-a-username
278tl::expected<std::string, errors> canonicalize_username(std::string_view input);
279
280// @see https://wicg.github.io/urlpattern/#canonicalize-a-password
281tl::expected<std::string, errors> canonicalize_password(std::string_view input);
282
283// @see https://wicg.github.io/urlpattern/#canonicalize-a-password
284tl::expected<std::string, errors> canonicalize_hostname(std::string_view input);
285
286// @see https://wicg.github.io/urlpattern/#canonicalize-an-ipv6-hostname
287tl::expected<std::string, errors> canonicalize_ipv6_hostname(
288 std::string_view input);
289
290// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
291tl::expected<std::string, errors> canonicalize_port(std::string_view input);
292
293// @see https://wicg.github.io/urlpattern/#canonicalize-a-port
294tl::expected<std::string, errors> canonicalize_port_with_protocol(
295 std::string_view input, std::string_view protocol);
296
297// @see https://wicg.github.io/urlpattern/#canonicalize-a-pathname
298tl::expected<std::string, errors> canonicalize_pathname(std::string_view input);
299
300// @see https://wicg.github.io/urlpattern/#canonicalize-an-opaque-pathname
301tl::expected<std::string, errors> canonicalize_opaque_pathname(
302 std::string_view input);
303
304// @see https://wicg.github.io/urlpattern/#canonicalize-a-search
305tl::expected<std::string, errors> canonicalize_search(std::string_view input);
306
307// @see https://wicg.github.io/urlpattern/#canonicalize-a-hash
308tl::expected<std::string, errors> canonicalize_hash(std::string_view input);
309
310// @see https://urlpattern.spec.whatwg.org/#tokenize
311tl::expected<std::vector<token>, errors> tokenize(std::string_view input,
312 token_policy policy);
313
314// @see https://urlpattern.spec.whatwg.org/#process-a-base-url-string
315std::string process_base_url_string(std::string_view input,
316 url_pattern_init::process_type type);
317
318// @see https://urlpattern.spec.whatwg.org/#escape-a-pattern-string
319std::string escape_pattern_string(std::string_view input);
320
321// @see https://urlpattern.spec.whatwg.org/#escape-a-regexp-string
322std::string escape_regexp_string(std::string_view input);
323
324// @see https://urlpattern.spec.whatwg.org/#is-an-absolute-pathname
325constexpr bool is_absolute_pathname(
326 std::string_view input, url_pattern_init::process_type type) noexcept;
327
328// @see https://urlpattern.spec.whatwg.org/#parse-a-pattern-string
329template <url_pattern_encoding_callback F>
330tl::expected<std::vector<url_pattern_part>, errors> parse_pattern_string(
331 std::string_view input, url_pattern_compile_component_options& options,
332 F& encoding_callback);
333
334// @see https://urlpattern.spec.whatwg.org/#generate-a-pattern-string
335std::string generate_pattern_string(
336 std::vector<url_pattern_part>& part_list,
337 url_pattern_compile_component_options& options);
338
339// @see
340// https://urlpattern.spec.whatwg.org/#generate-a-regular-expression-and-name-list
341std::tuple<std::string, std::vector<std::string>>
342generate_regular_expression_and_name_list(
343 const std::vector<url_pattern_part>& part_list,
344 url_pattern_compile_component_options options);
345
346// @see https://urlpattern.spec.whatwg.org/#hostname-pattern-is-an-ipv6-address
347bool is_ipv6_address(std::string_view input) noexcept;
348
349// @see
350// https://urlpattern.spec.whatwg.org/#protocol-component-matches-a-special-scheme
351template <url_pattern_regex::regex_concept regex_provider>
352bool protocol_component_matches_special_scheme(
353 ada::url_pattern_component<regex_provider>& input);
354
355// @see https://urlpattern.spec.whatwg.org/#convert-a-modifier-to-a-string
356std::string_view convert_modifier_to_string(url_pattern_part_modifier modifier);
357
358// @see https://urlpattern.spec.whatwg.org/#generate-a-segment-wildcard-regexp
359std::string generate_segment_wildcard_regexp(
360 url_pattern_compile_component_options options);
361
362} // namespace ada::url_pattern_helpers
363#endif // ADA_INCLUDE_URL_PATTERN
364#endif
Cross-platform compiler macros and common definitions.
#define ada_warn_unused
Definition common_defs.h:89
type
Enumeration of URL scheme types.
Definition scheme.h:43
errors
Error codes for URL parsing operations.
Definition errors.h:17
state
States in the URL parsing state machine.
Definition state.h:27
@ AUTHORITY
Definition state.h:31
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.