5#ifndef ADA_URL_PATTERN_H
6#define ADA_URL_PATTERN_H
9#include "ada/expected.h"
16#include <unordered_map>
24#if ADA_INCLUDE_URL_PATTERN
27enum class url_pattern_part_type : uint8_t {
41enum class url_pattern_part_modifier : uint8_t {
55class url_pattern_part {
57 url_pattern_part(url_pattern_part_type _type, std::string&& _value,
58 url_pattern_part_modifier _modifier)
59 :
type(_type), value(std::move(_value)), modifier(_modifier) {}
61 url_pattern_part(url_pattern_part_type _type, std::string&& _value,
62 url_pattern_part_modifier _modifier, std::string&& _name,
63 std::string&& _prefix, std::string&& _suffix)
65 value(std::move(_value)),
67 name(std::move(_name)),
68 prefix(std::move(_prefix)),
69 suffix(std::move(_suffix)) {}
71 url_pattern_part_type
type;
76 url_pattern_part_modifier modifier;
84 inline bool is_regexp() const noexcept;
88struct url_pattern_compile_component_options {
89 url_pattern_compile_component_options() =
default;
90 explicit url_pattern_compile_component_options(
91 std::optional<char> new_delimiter = std::nullopt,
92 std::optional<char> new_prefix = std::nullopt)
93 : delimiter(new_delimiter), prefix(new_prefix) {}
99 bool ignore_case = false;
101 static url_pattern_compile_component_options DEFAULT;
102 static url_pattern_compile_component_options HOSTNAME;
103 static url_pattern_compile_component_options PATHNAME;
107 std::optional<
char> delimiter{};
109 std::optional<char> prefix{};
114inline url_pattern_compile_component_options
115 url_pattern_compile_component_options::DEFAULT(std::nullopt, std::nullopt);
119inline url_pattern_compile_component_options
120 url_pattern_compile_component_options::HOSTNAME(
'.', std::nullopt);
124inline url_pattern_compile_component_options
125 url_pattern_compile_component_options::PATHNAME(
'/',
'/');
132struct url_pattern_component_result {
134 std::unordered_map<std::string, std::optional<std::string>> groups;
136 bool operator==(
const url_pattern_component_result&)
const;
139 friend void PrintTo(
const url_pattern_component_result& result,
141 *os <<
"input: '" <<
result.input <<
"', group: ";
142 for (
const auto& group :
result.groups) {
143 *os <<
"(" << group.first <<
", " << group.second.value_or(
"undefined")
150template <url_pattern_regex::regex_concept regex_prov
ider>
151class url_pattern_component {
153 url_pattern_component() =
default;
157 url_pattern_component(std::string&& new_pattern,
158 typename regex_provider::regex_type&& new_regexp,
159 std::vector<std::string>&& new_group_name_list,
160 bool new_has_regexp_groups)
161 : regexp(std::move(new_regexp)),
162 pattern(std::move(new_pattern)),
163 group_name_list(std::move(new_group_name_list)),
164 has_regexp_groups(new_has_regexp_groups) {}
167 template <url_pattern_encoding_callback F>
168 static tl::expected<url_pattern_component, errors> compile(
169 std::string_view input, F& encoding_callback,
170 url_pattern_compile_component_options& options);
173 url_pattern_component_result create_component_match_result(
175 std::vector<std::optional<std::string>>&& exec_result);
178 friend void PrintTo(
const url_pattern_component& component,
180 *os <<
"pattern: '" << component.pattern
181 <<
"', has_regexp_groups: " << component.has_regexp_groups
182 <<
"group_name_list: ";
183 for (
const auto& name : component.group_name_list) {
189 typename regex_provider::regex_type regexp{};
190 std::string pattern{};
191 std::vector<std::string> group_name_list{};
192 bool has_regexp_groups =
false;
197using url_pattern_input = std::variant<std::string_view, url_pattern_init>;
202struct url_pattern_result {
203 std::vector<url_pattern_input> inputs;
204 url_pattern_component_result protocol;
205 url_pattern_component_result username;
206 url_pattern_component_result password;
207 url_pattern_component_result hostname;
208 url_pattern_component_result port;
209 url_pattern_component_result pathname;
210 url_pattern_component_result search;
211 url_pattern_component_result hash;
214struct url_pattern_options {
215 bool ignore_case =
false;
218 friend void PrintTo(
const url_pattern_options& options, std::ostream* os) {
219 *os <<
"ignore_case: '" << options.ignore_case;
232template <url_pattern_regex::regex_concept regex_prov
ider>
235 url_pattern() =
default;
241 result<std::optional<url_pattern_result>> exec(
242 const url_pattern_input& input,
243 const std::string_view* base_url =
nullptr);
249 result<bool> test(
const url_pattern_input& input,
250 const std::string_view* base_url =
nullptr);
256 result<std::optional<url_pattern_result>> match(
257 const url_pattern_input& input,
258 const std::string_view* base_url_string =
nullptr);
279 [[nodiscard]]
bool ignore_case() const;
282 [[nodiscard]]
bool has_regexp_groups() const;
285 friend void PrintTo(
const url_pattern& c, std::ostream* os) {
286 *os <<
"protocol_component: '" << c.get_protocol() <<
", ";
287 *os <<
"username_component: '" << c.get_username() <<
", ";
288 *os <<
"password_component: '" << c.get_password() <<
", ";
289 *os <<
"hostname_component: '" << c.get_hostname() <<
", ";
290 *os <<
"port_component: '" << c.get_port() <<
", ";
291 *os <<
"pathname_component: '" << c.get_pathname() <<
", ";
292 *os <<
"search_component: '" << c.get_search() <<
", ";
293 *os <<
"hash_component: '" << c.get_hash();
297 template <url_pattern_regex::regex_concept P>
298 friend tl::expected<url_pattern<P>,
errors> parser::parse_url_pattern_impl(
299 std::variant<std::string_view, url_pattern_init>&& input,
300 const std::string_view* base_url,
const url_pattern_options* options);
307 url_pattern_component<regex_provider> protocol_component{};
313 url_pattern_component<regex_provider> username_component{};
319 url_pattern_component<regex_provider> password_component{};
325 url_pattern_component<regex_provider> hostname_component{};
331 url_pattern_component<regex_provider> port_component{};
337 url_pattern_component<regex_provider> pathname_component{};
343 url_pattern_component<regex_provider> search_component{};
349 url_pattern_component<regex_provider> hash_component{};
355 bool ignore_case_ =
false;
#define ada_lifetime_bound
Definitions for user facing functions for parsing URL and it's components.
tl::expected< result_type, ada::errors > result
Definitions for the parser.
Declaration for the url_pattern_init implementation.