12#ifndef ADA_URL_PATTERN_H
13#define ADA_URL_PATTERN_H
16#include "ada/expected.h"
25#include <unordered_map>
34#if ADA_INCLUDE_URL_PATTERN
37enum class url_pattern_part_type : uint8_t {
53enum class url_pattern_component_type : uint8_t {
64enum class url_pattern_part_modifier : uint8_t {
78class url_pattern_part {
80 url_pattern_part(url_pattern_part_type _type, std::string&& _value,
81 url_pattern_part_modifier _modifier)
82 :
type(_type), value(std::move(_value)), modifier(_modifier) {}
84 url_pattern_part(url_pattern_part_type _type, std::string&& _value,
85 url_pattern_part_modifier _modifier, std::string&& _name,
86 std::string&& _prefix, std::string&& _suffix)
88 value(std::move(_value)),
90 name(std::move(_name)),
91 prefix(std::move(_prefix)),
92 suffix(std::move(_suffix)) {}
94 url_pattern_part_type
type;
99 url_pattern_part_modifier modifier;
103 std::string prefix{};
105 std::string suffix{};
107 inline bool is_regexp() const noexcept;
111struct url_pattern_compile_component_options {
112 url_pattern_compile_component_options() =
default;
113 explicit url_pattern_compile_component_options(
114 std::optional<char> new_delimiter = std::nullopt,
115 std::optional<char> new_prefix = std::nullopt) noexcept
116 : delimiter(new_delimiter), prefix(new_prefix) {}
122 bool ignore_case = false;
124 static url_pattern_compile_component_options DEFAULT;
125 static url_pattern_compile_component_options HOSTNAME;
126 static url_pattern_compile_component_options PATHNAME;
130 std::optional<
char> delimiter{};
132 std::optional<char> prefix{};
137inline url_pattern_compile_component_options
138 url_pattern_compile_component_options::DEFAULT(std::nullopt, std::nullopt);
142inline url_pattern_compile_component_options
143 url_pattern_compile_component_options::HOSTNAME(
'.', std::nullopt);
147inline url_pattern_compile_component_options
148 url_pattern_compile_component_options::PATHNAME(
'/',
'/');
155struct url_pattern_component_result {
157 std::unordered_map<std::string, std::optional<std::string>> groups;
159 bool operator==(
const url_pattern_component_result&)
const;
162 friend void PrintTo(
const url_pattern_component_result& result,
164 *os <<
"input: '" <<
result.input <<
"', group: ";
165 for (
const auto& group :
result.groups) {
166 *os <<
"(" << group.first <<
", " << group.second.value_or(
"undefined")
173template <url_pattern_regex::regex_concept regex_prov
ider>
174class url_pattern_component {
176 url_pattern_component() =
default;
180 url_pattern_component(std::string&& new_pattern,
181 typename regex_provider::regex_type&& new_regexp,
182 std::vector<std::string>&& new_group_name_list,
183 bool new_has_regexp_groups,
184 url_pattern_component_type new_type,
185 std::string&& new_exact_match_value = {})
186 : regexp(std::move(new_regexp)),
187 pattern(std::move(new_pattern)),
188 group_name_list(std::move(new_group_name_list)),
189 exact_match_value(std::move(new_exact_match_value)),
190 has_regexp_groups(new_has_regexp_groups),
194 template <url_pattern_encoding_callback F>
195 static tl::expected<url_pattern_component, errors> compile(
196 std::string_view input, F& encoding_callback,
197 url_pattern_compile_component_options& options);
200 url_pattern_component_result create_component_match_result(
202 std::vector<std::optional<std::string>>&& exec_result);
207 bool fast_test(std::string_view input)
const;
212 std::optional<std::vector<std::optional<std::string>>> fast_match(
213 std::string_view input)
const;
216 friend void PrintTo(
const url_pattern_component& component,
218 *os <<
"pattern: '" << component.pattern
219 <<
"', has_regexp_groups: " << component.has_regexp_groups
220 <<
"group_name_list: ";
221 for (
const auto& name : component.group_name_list) {
227 typename regex_provider::regex_type regexp{};
228 std::string pattern{};
229 std::vector<std::string> group_name_list{};
231 std::string exact_match_value{};
232 bool has_regexp_groups =
false;
234 url_pattern_component_type
type = url_pattern_component_type::REGEXP;
239using url_pattern_input = std::variant<std::string_view, url_pattern_init>;
248struct url_pattern_result {
249 std::vector<url_pattern_input>
inputs;
250 url_pattern_component_result protocol;
251 url_pattern_component_result username;
252 url_pattern_component_result password;
253 url_pattern_component_result hostname;
254 url_pattern_component_result port;
255 url_pattern_component_result pathname;
256 url_pattern_component_result search;
257 url_pattern_component_result hash;
259 url_pattern_result() =
default;
260 url_pattern_result(
const url_pattern_result& other);
261 url_pattern_result(url_pattern_result&& other)
noexcept;
262 url_pattern_result& operator=(
const url_pattern_result& other);
263 url_pattern_result& operator=(url_pattern_result&& other)
noexcept;
264 ~url_pattern_result() =
default;
269 std::vector<std::string> owned_strings_{};
273 void rebind_owned_input_views(
const std::pair<const char*, size_t>* from,
278 void take_inputs(url_pattern_result& other)
noexcept;
279 void store_inputs(
const url_pattern_input& input,
280 const std::string_view* base_url =
nullptr);
282 template <url_pattern_regex::regex_concept regex_prov
ider>
283 friend class url_pattern;
286struct url_pattern_options {
287 bool ignore_case =
false;
290 friend void PrintTo(
const url_pattern_options& options, std::ostream* os) {
291 *os <<
"ignore_case: '" << options.ignore_case;
318template <url_pattern_regex::regex_concept regex_prov
ider>
321 url_pattern() =
default;
327 result<std::optional<url_pattern_result>> exec(
328 const url_pattern_input& input,
329 const std::string_view* base_url =
nullptr);
335 result<bool> test(
const url_pattern_input& input,
336 const std::string_view* base_url =
nullptr);
342 result<std::optional<url_pattern_result>> match(
343 const url_pattern_input& input,
344 const std::string_view* base_url_string =
nullptr);
365 [[nodiscard]]
bool ignore_case() const;
368 [[nodiscard]]
bool has_regexp_groups() const;
371 [[nodiscard]]
bool test_components(
372 std::string_view protocol, std::string_view username,
373 std::string_view password, std::string_view hostname,
374 std::string_view port, std::string_view pathname, std::string_view search,
375 std::string_view hash) const;
378 friend void PrintTo(
const url_pattern& c, std::ostream* os) {
379 *os <<
"protocol_component: '" << c.get_protocol() <<
", ";
380 *os <<
"username_component: '" << c.get_username() <<
", ";
381 *os <<
"password_component: '" << c.get_password() <<
", ";
382 *os <<
"hostname_component: '" << c.get_hostname() <<
", ";
383 *os <<
"port_component: '" << c.get_port() <<
", ";
384 *os <<
"pathname_component: '" << c.get_pathname() <<
", ";
385 *os <<
"search_component: '" << c.get_search() <<
", ";
386 *os <<
"hash_component: '" << c.get_hash();
390 template <url_pattern_regex::regex_concept P>
391 friend tl::expected<url_pattern<P>,
errors> parser::parse_url_pattern_impl(
392 std::variant<std::string_view, url_pattern_init>&& input,
393 const std::string_view* base_url,
const url_pattern_options* options);
400 url_pattern_component<regex_provider> protocol_component{};
406 url_pattern_component<regex_provider> username_component{};
412 url_pattern_component<regex_provider> password_component{};
418 url_pattern_component<regex_provider> hostname_component{};
424 url_pattern_component<regex_provider> port_component{};
430 url_pattern_component<regex_provider> pathname_component{};
436 url_pattern_component<regex_provider> search_component{};
442 url_pattern_component<regex_provider> hash_component{};
448 bool ignore_case_ =
false;
#define ada_lifetime_bound
User-facing functions for URL parsing and manipulation.
type
Enumeration of URL scheme types.
url_search_params url_search_params_iter< T, Type >::EMPTY
errors
Error codes for URL parsing operations.
tl::expected< result_type, ada::errors > result
Low-level URL parsing functions.
Declaration for the url_pattern_init implementation.