26#ifndef ADA_URL_PATTERN_LIST_H
27#define ADA_URL_PATTERN_LIST_H
31#include "ada/expected.h"
43#if ADA_INCLUDE_URL_PATTERN
52namespace url_pattern_list_limits {
61inline constexpr uint32_t max_captures_per_route = 8;
68inline constexpr uint32_t max_fast_path_segments = 24;
75inline constexpr uint32_t max_fast_path_pathname_length = 4096;
82inline constexpr uint32_t max_trie_pattern_segments = 16;
88inline constexpr size_t max_routes = 65535;
94inline constexpr size_t max_aux_entries = 1u << 20;
117struct url_pattern_list_match_result {
124 int32_t route_index = -1;
126 uint32_t capture_count = 0;
128 bool captures_truncated =
false;
131 bool regexp_route =
false;
132 std::array<capture, url_pattern_list_limits::max_captures_per_route>
135 std::vector<std::optional<std::string>> regexp_groups{};
137 [[nodiscard]]
bool has_match() const noexcept {
return route_index >= 0; }
148namespace url_pattern_list_detail {
155 int32_t first_child = 0;
157 int32_t param_child = -1;
159 int32_t wild_route = -1;
161 int32_t terminal_route = -1;
162 uint16_t n_static = 0;
165 uint8_t dispatch = 0;
167 uint8_t has_alternative = 0;
169 uint32_t hash_index = 0;
174 uint64_t multiplier = 0;
175 uint32_t slot_base = 0;
177 uint16_t witness_plan = 0;
179 uint8_t slot_bits = 0;
180 uint8_t reserved = 0;
193 uint32_t key_offset = 0;
194 uint16_t key_length = 0;
198enum class route_mode : uint8_t {
213 uint64_t kind_sequence = 0;
215 uint32_t segment_first = 0;
216 uint32_t segment_count = 0;
219 uint32_t challenger_first = 0;
220 uint32_t challenger_count = 0;
222 int32_t regexp_component = -1;
223 uint8_t kind_length = 0;
224 route_mode mode = route_mode::sequential;
226 uint8_t n_params = 0;
228 std::array<uint8_t, url_pattern_list_limits::max_captures_per_route>
233struct segment_record {
234 uint32_t text_offset = 0;
235 uint32_t text_length = 0;
247struct compiled_routes {
248 std::vector<uint8_t> arena{};
249 std::vector<std::vector<std::string>> group_names{};
250 uint32_t nodes_offset = 0;
251 uint32_t hashes_offset = 0;
252 uint32_t edges_offset = 0;
253 uint32_t slots_offset = 0;
254 uint32_t blob_offset = 0;
255 uint32_t routes_offset = 0;
256 uint32_t segments_offset = 0;
257 uint32_t aux_offset = 0;
258 uint32_t root_index_offset = 0;
259 uint32_t n_routes = 0;
263 uint32_t n_aux_all = 0;
264 uint8_t ignore_case = 0;
266 template <
typename T>
267 [[nodiscard]]
const T* section(uint32_t offset)
const noexcept {
268 return reinterpret_cast<const T*
>(arena.data() + offset);
273struct engine_result {
274 using capture = url_pattern_list_match_result::capture;
276 uint32_t capture_count = 0;
279 bool within_fast_path =
true;
280 bool captures_truncated =
false;
281 std::array<capture, url_pattern_list_limits::max_captures_per_route>
292tl::expected<compiled_routes, errors> compile_pathname_patterns(
293 std::span<const std::string> patterns,
bool ignore_case);
303bool match_route_sequential(
const compiled_routes& tables, uint32_t route,
304 std::string_view pathname,
bool fold_input,
305 engine_result& result)
noexcept;
318bool wildcard_tail_ok(
const char* p, uint32_t n)
noexcept;
328bool match_regexp_shape(
const compiled_routes& tables, uint32_t route,
329 std::string_view pathname,
bool fold_input)
noexcept;
337constexpr bool outranks(uint64_t a_sequence, uint32_t a_length,
size_t a_index,
338 uint64_t b_sequence, uint32_t b_length,
339 size_t b_index)
noexcept {
340 if (a_sequence != b_sequence) {
341 return a_sequence < b_sequence;
343 if (a_length != b_length) {
344 return a_length < b_length;
346 return a_index < b_index;
351template <url_pattern_regex::regex_concept regex_prov
ider>
352class url_pattern_list;
365template <url_pattern_regex::regex_concept regex_prov
ider>
367parse_url_pattern_list(std::span<const std::string_view> pathname_patterns,
368 const std::string_view* base_url,
369 const url_pattern_options* options);
372template <url_pattern_regex::regex_concept regex_prov
ider>
374parse_url_pattern_list(std::span<
const url_pattern<regex_provider>> patterns);
414template <url_pattern_regex::regex_concept regex_prov
ider>
415class url_pattern_list {
417 url_pattern_list() =
default;
425 [[nodiscard]] url_pattern_list_match_result match(
426 std::string_view pathname)
const;
429 [[nodiscard]]
size_t size() const noexcept {
return patterns_.size(); }
433 [[nodiscard]] std::string_view pattern(
size_t route_index)
const
435 return patterns_[route_index];
443 [[nodiscard]]
const std::vector<std::string>& group_names(
445 return compiled_.group_names[route_index];
449 [[nodiscard]]
bool ignore_case() const noexcept {
450 return compiled_.ignore_case != 0;
453 friend tl::expected<url_pattern_list, errors>
454 parse_url_pattern_list<regex_provider>(
455 std::span<const std::string_view> pathname_patterns,
456 const std::string_view* base_url,
const url_pattern_options* options);
458 friend tl::expected<url_pattern_list, errors> parse_url_pattern_list<
459 regex_provider>(std::span<const url_pattern<regex_provider>> patterns);
464 static tl::expected<url_pattern_list, errors> create(
465 std::vector<std::string>&& pathname_patterns,
bool ignore_case);
472 uint32_t route_index, std::string_view pathname, std::string_view probe,
473 bool fold_input, url_pattern_list_detail::engine_result& best,
474 std::vector<std::optional<std::string>>& best_groups)
const;
477 std::vector<std::string> patterns_{};
479 url_pattern_list_detail::compiled_routes compiled_{};
482 std::vector<url_pattern_component<regex_provider>> regexp_components_{};
Cross-platform compiler macros and common definitions.
#define ada_lifetime_bound
Error type definitions for URL parsing.
errors
Error codes for URL parsing operations.
ada::url_pattern_regex::std_regex_provider regex_provider
URLPattern API implementation.