Ada 4.1.1
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
implementation-inl.h
Go to the documentation of this file.
1
4#ifndef ADA_IMPLEMENTATION_INL_H
5#define ADA_IMPLEMENTATION_INL_H
6
8
9#include "ada/expected.h"
10#include "ada/implementation.h"
12
13#include <cstddef>
14#include <span>
15#include <string>
16#include <string_view>
17#include <utility>
18#include <variant>
19#include <vector>
20
21namespace ada {
22
23#if ADA_INCLUDE_URL_PATTERN
24template <url_pattern_regex::regex_concept regex_provider>
25ada_warn_unused tl::expected<url_pattern<regex_provider>, errors>
26parse_url_pattern(std::variant<std::string_view, url_pattern_init>&& input,
27 const std::string_view* base_url,
28 const url_pattern_options* options) {
29 return parser::parse_url_pattern_impl<regex_provider>(std::move(input),
30 base_url, options);
31}
32
33template <url_pattern_regex::regex_concept regex_provider>
34ada_warn_unused tl::expected<url_pattern_list<regex_provider>, errors>
35parse_url_pattern_list(std::span<const std::string_view> pathname_patterns,
36 const std::string_view* base_url,
37 const url_pattern_options* options) {
38 std::vector<std::string> processed;
39 processed.reserve(pathname_patterns.size());
40 for (const std::string_view pattern : pathname_patterns) {
41 if (base_url == nullptr) {
42 processed.emplace_back(pattern);
43 continue;
44 }
45 // With a base URL, each pattern is processed exactly as the pathname of
46 // a URLPatternInit would be: a relative pattern is resolved against the
47 // base URL's path (and an unparsable base URL is a type error).
48 url_pattern_init init{};
49 init.pathname = std::string(pattern);
50 init.base_url = std::string(*base_url);
51 auto result = url_pattern_init::process(
52 init, url_pattern_init::process_type::pattern);
53 if (!result) {
54 return tl::unexpected(result.error());
55 }
56 processed.push_back(std::move(result->pathname).value_or(std::string{}));
57 }
58 const bool ignore_case = options != nullptr && options->ignore_case;
59 auto list = url_pattern_list<regex_provider>::create(std::move(processed),
60 ignore_case);
61 if (!list) {
62 return list;
63 }
64 // Routes outside the static/":param"/"*" subset are compiled as a
65 // URLPattern pathname component through the provider, with the same
66 // options parse_url_pattern would use (ignore_case included).
67 auto compile_options = url_pattern_compile_component_options::PATHNAME;
68 compile_options.ignore_case = ignore_case;
69 const url_pattern_list_detail::route_record* routes =
70 list->compiled_.template section<url_pattern_list_detail::route_record>(
71 list->compiled_.routes_offset);
72 for (size_t i = 0; i < list->patterns_.size(); i++) {
73 if (routes[i].regexp_component < 0) {
74 continue;
75 }
76 auto component = url_pattern_component<regex_provider>::compile(
77 list->patterns_[i], url_pattern_helpers::canonicalize_pathname,
78 compile_options);
79 if (!component) {
80 return tl::unexpected(component.error());
81 }
82 list->compiled_.group_names[i] = component->group_name_list;
83 list->regexp_components_.push_back(std::move(*component));
84 }
85 return list;
86}
87
88template <url_pattern_regex::regex_concept regex_provider>
89ada_warn_unused tl::expected<url_pattern_list<regex_provider>, errors>
90parse_url_pattern_list(std::span<const url_pattern<regex_provider>> patterns) {
91 std::vector<std::string> texts;
92 texts.reserve(patterns.size());
93 const bool ignore_case = !patterns.empty() && patterns[0].ignore_case();
94 for (const url_pattern<regex_provider>& pattern : patterns) {
95 if (pattern.ignore_case() != ignore_case) {
96 return tl::unexpected(errors::type_error); // one flag per list
97 }
98 texts.emplace_back(pattern.get_pathname());
99 }
100 auto list =
101 url_pattern_list<regex_provider>::create(std::move(texts), ignore_case);
102 if (!list) {
103 return list;
104 }
105 // Routes outside the subset reuse the pattern's already compiled pathname
106 // component: no second create_instance through the provider.
107 const url_pattern_list_detail::route_record* routes =
108 list->compiled_.template section<url_pattern_list_detail::route_record>(
109 list->compiled_.routes_offset);
110 for (size_t i = 0; i < patterns.size(); i++) {
111 if (routes[i].regexp_component < 0) {
112 continue;
113 }
114 list->compiled_.group_names[i] =
115 patterns[i].pathname_component.group_name_list;
116 list->regexp_components_.push_back(patterns[i].pathname_component);
117 }
118 return list;
119}
120#endif // ADA_INCLUDE_URL_PATTERN
121
122} // namespace ada
123
124#endif // ADA_IMPLEMENTATION_INL_H
#define ada_warn_unused
Definition common_defs.h:89
User-facing functions for URL parsing and manipulation.
errors
Error codes for URL parsing operations.
Definition errors.h:17
@ type_error
Definition errors.h:18
tl::expected< result_type, ada::errors > result
Compiled set-level matching over URLPattern pathname patterns.