Ada 3.1.0
Fast spec-compliant URL parser
Loading...
Searching...
No Matches
helpers.cpp
Go to the documentation of this file.
1#include "ada/checkers-inl.h"
2#include "ada/common_defs.h"
3#include "ada/scheme.h"
4
5#include <cstring>
6#include <sstream>
7
8namespace ada::helpers {
9
10template <typename out_iter>
11void encode_json(std::string_view view, out_iter out) {
12 // trivial implementation. could be faster.
13 const char* hexvalues =
14 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
15 for (uint8_t c : view) {
16 if (c == '\\') {
17 *out++ = '\\';
18 *out++ = '\\';
19 } else if (c == '"') {
20 *out++ = '\\';
21 *out++ = '"';
22 } else if (c <= 0x1f) {
23 *out++ = '\\';
24 *out++ = 'u';
25 *out++ = '0';
26 *out++ = '0';
27 *out++ = hexvalues[2 * c];
28 *out++ = hexvalues[2 * c + 1];
29 } else {
30 *out++ = c;
31 }
32 }
33}
34
36 switch (s) {
38 return "Authority";
40 return "Scheme Start";
42 return "Scheme";
44 return "Host";
46 return "No Scheme";
48 return "Fragment";
50 return "Relative Scheme";
52 return "Relative Slash";
54 return "File";
56 return "File Host";
58 return "File Slash";
60 return "Path or Authority";
62 return "Special Authority Ignore Slashes";
64 return "Special Authority Slashes";
66 return "Special Relative or Authority";
68 return "Query";
70 return "Path";
72 return "Path Start";
74 return "Opaque Path";
76 return "Port";
77 default:
78 return "unknown state";
79 }
80}
81
82ada_really_inline std::optional<std::string_view> prune_hash(
83 std::string_view& input) noexcept {
84 // compiles down to 20--30 instructions including a class to memchr (C
85 // function). this function should be quite fast.
86 size_t location_of_first = input.find('#');
87 if (location_of_first == std::string_view::npos) {
88 return std::nullopt;
89 }
90 std::string_view hash = input;
91 hash.remove_prefix(location_of_first + 1);
92 input.remove_suffix(input.size() - location_of_first);
93 return hash;
94}
95
96ada_really_inline bool shorten_path(std::string& path,
97 ada::scheme::type type) noexcept {
98 // Let path be url's path.
99 // If url's scheme is "file", path's size is 1, and path[0] is a normalized
100 // Windows drive letter, then return.
101 if (type == ada::scheme::type::FILE &&
102 path.find('/', 1) == std::string_view::npos && !path.empty()) {
104 helpers::substring(path, 1))) {
105 return false;
106 }
107 }
108
109 // Remove path's last item, if any.
110 size_t last_delimiter = path.rfind('/');
111 if (last_delimiter != std::string::npos) {
112 path.erase(last_delimiter);
113 return true;
114 }
115
116 return false;
117}
118
119ada_really_inline bool shorten_path(std::string_view& path,
120 ada::scheme::type type) noexcept {
121 // Let path be url's path.
122 // If url's scheme is "file", path's size is 1, and path[0] is a normalized
123 // Windows drive letter, then return.
124 if (type == ada::scheme::type::FILE &&
125 path.find('/', 1) == std::string_view::npos && !path.empty()) {
127 helpers::substring(path, 1))) {
128 return false;
129 }
130 }
131
132 // Remove path's last item, if any.
133 if (!path.empty()) {
134 size_t slash_loc = path.rfind('/');
135 if (slash_loc != std::string_view::npos) {
136 path.remove_suffix(path.size() - slash_loc);
137 return true;
138 }
139 }
140
141 return false;
142}
143
144ada_really_inline void remove_ascii_tab_or_newline(
145 std::string& input) noexcept {
146 // if this ever becomes a performance issue, we could use an approach similar
147 // to has_tabs_or_newline
148 std::erase_if(input, ada::unicode::is_ascii_tab_or_newline);
149}
150
151ada_really_inline constexpr std::string_view substring(std::string_view input,
152 size_t pos) noexcept {
153 ADA_ASSERT_TRUE(pos <= input.size());
154 // The following is safer but unneeded if we have the above line:
155 // return pos > input.size() ? std::string_view() : input.substr(pos);
156 return input.substr(pos);
157}
158
159ada_really_inline void resize(std::string_view& input, size_t pos) noexcept {
160 ADA_ASSERT_TRUE(pos <= input.size());
161 input.remove_suffix(input.size() - pos);
162}
163
164// computes the number of trailing zeroes
165// this is a private inline function only defined in this source file.
166ada_really_inline int trailing_zeroes(uint32_t input_num) noexcept {
167#ifdef ADA_REGULAR_VISUAL_STUDIO
168 unsigned long ret;
169 // Search the mask data from least significant bit (LSB)
170 // to the most significant bit (MSB) for a set bit (1).
171 _BitScanForward(&ret, input_num);
172 return (int)ret;
173#else // ADA_REGULAR_VISUAL_STUDIO
174 return __builtin_ctzl(input_num);
175#endif // ADA_REGULAR_VISUAL_STUDIO
176}
177
178// starting at index location, this finds the next location of a character
179// :, /, \\, ? or [. If none is found, view.size() is returned.
180// For use within get_host_delimiter_location.
181#if ADA_NEON
182// The ada_make_uint8x16_t macro is necessary because Visual Studio does not
183// support direct initialization of uint8x16_t. See
184// https://developercommunity.visualstudio.com/t/error-C2078:-too-many-initializers-whe/402911?q=backend+neon
185#ifndef ada_make_uint8x16_t
186#define ada_make_uint8x16_t(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, \
187 x13, x14, x15, x16) \
188 ([=]() { \
189 static uint8_t array[16] = {x1, x2, x3, x4, x5, x6, x7, x8, \
190 x9, x10, x11, x12, x13, x14, x15, x16}; \
191 return vld1q_u8(array); \
192 }())
193#endif
194
196 std::string_view view, size_t location) noexcept {
197 // first check for short strings in which case we do it naively.
198 if (view.size() - location < 16) { // slow path
199 for (size_t i = location; i < view.size(); i++) {
200 if (view[i] == ':' || view[i] == '/' || view[i] == '\\' ||
201 view[i] == '?' || view[i] == '[') {
202 return i;
203 }
204 }
205 return size_t(view.size());
206 }
207 auto to_bitmask = [](uint8x16_t input) -> uint16_t {
208 uint8x16_t bit_mask =
209 ada_make_uint8x16_t(0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x01,
210 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80);
211 uint8x16_t minput = vandq_u8(input, bit_mask);
212 uint8x16_t tmp = vpaddq_u8(minput, minput);
213 tmp = vpaddq_u8(tmp, tmp);
214 tmp = vpaddq_u8(tmp, tmp);
215 return vgetq_lane_u16(vreinterpretq_u16_u8(tmp), 0);
216 };
217
218 // fast path for long strings (expected to be common)
219 size_t i = location;
220 uint8x16_t low_mask =
221 ada_make_uint8x16_t(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x03);
223 uint8x16_t high_mask =
224 ada_make_uint8x16_t(0x00, 0x00, 0x02, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
226 uint8x16_t fmask = vmovq_n_u8(0xf);
227 uint8x16_t zero{0};
228 for (; i + 15 < view.size(); i += 16) {
229 uint8x16_t word = vld1q_u8((const uint8_t*)view.data() + i);
230 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
231 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
232 uint8x16_t classify = vandq_u8(lowpart, highpart);
233 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
234 uint8x16_t is_zero = vceqq_u8(classify, zero);
235 uint16_t is_non_zero = ~to_bitmask(is_zero);
236 return i + trailing_zeroes(is_non_zero);
237 }
238 }
239
240 if (i < view.size()) {
241 uint8x16_t word =
242 vld1q_u8((const uint8_t*)view.data() + view.length() - 16);
243 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
244 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
245 uint8x16_t classify = vandq_u8(lowpart, highpart);
246 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
247 uint8x16_t is_zero = vceqq_u8(classify, zero);
248 uint16_t is_non_zero = ~to_bitmask(is_zero);
249 return view.length() - 16 + trailing_zeroes(is_non_zero);
250 }
251 }
252 return size_t(view.size());
253}
254#elif ADA_SSE2
256 std::string_view view, size_t location) noexcept {
257 // first check for short strings in which case we do it naively.
258 if (view.size() - location < 16) { // slow path
259 for (size_t i = location; i < view.size(); i++) {
260 if (view[i] == ':' || view[i] == '/' || view[i] == '\\' ||
261 view[i] == '?' || view[i] == '[') {
262 return i;
263 }
264 }
265 return size_t(view.size());
266 }
267 // fast path for long strings (expected to be common)
268 size_t i = location;
269 const __m128i mask1 = _mm_set1_epi8(':');
270 const __m128i mask2 = _mm_set1_epi8('/');
271 const __m128i mask3 = _mm_set1_epi8('\\');
272 const __m128i mask4 = _mm_set1_epi8('?');
273 const __m128i mask5 = _mm_set1_epi8('[');
274
275 for (; i + 15 < view.size(); i += 16) {
276 __m128i word = _mm_loadu_si128((const __m128i*)(view.data() + i));
277 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
278 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
279 __m128i m3 = _mm_cmpeq_epi8(word, mask3);
280 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
281 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
282 __m128i m = _mm_or_si128(
283 _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m3, m4)), m5);
284 int mask = _mm_movemask_epi8(m);
285 if (mask != 0) {
286 return i + trailing_zeroes(mask);
287 }
288 }
289 if (i < view.size()) {
290 __m128i word =
291 _mm_loadu_si128((const __m128i*)(view.data() + view.length() - 16));
292 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
293 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
294 __m128i m3 = _mm_cmpeq_epi8(word, mask3);
295 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
296 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
297 __m128i m = _mm_or_si128(
298 _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m3, m4)), m5);
299 int mask = _mm_movemask_epi8(m);
300 if (mask != 0) {
301 return view.length() - 16 + trailing_zeroes(mask);
302 }
303 }
304 return size_t(view.length());
305}
306#else
307// : / [ \\ ?
308static constexpr std::array<uint8_t, 256> special_host_delimiters =
309 []() consteval {
310 std::array<uint8_t, 256> result{};
311 for (int i : {':', '/', '[', '\\', '?'}) {
312 result[i] = 1;
313 }
314 return result;
315 }();
316// credit: @the-moisrex recommended a table-based approach
318 std::string_view view, size_t location) noexcept {
319 auto const str = view.substr(location);
320 for (auto pos = str.begin(); pos != str.end(); ++pos) {
321 if (special_host_delimiters[(uint8_t)*pos]) {
322 return pos - str.begin() + location;
323 }
324 }
325 return size_t(view.size());
326}
327#endif
328
329// starting at index location, this finds the next location of a character
330// :, /, ? or [. If none is found, view.size() is returned.
331// For use within get_host_delimiter_location.
332#if ADA_NEON
333ada_really_inline size_t find_next_host_delimiter(std::string_view view,
334 size_t location) noexcept {
335 // first check for short strings in which case we do it naively.
336 if (view.size() - location < 16) { // slow path
337 for (size_t i = location; i < view.size(); i++) {
338 if (view[i] == ':' || view[i] == '/' || view[i] == '?' ||
339 view[i] == '[') {
340 return i;
341 }
342 }
343 return size_t(view.size());
344 }
345 auto to_bitmask = [](uint8x16_t input) -> uint16_t {
346 uint8x16_t bit_mask =
347 ada_make_uint8x16_t(0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x01,
348 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80);
349 uint8x16_t minput = vandq_u8(input, bit_mask);
350 uint8x16_t tmp = vpaddq_u8(minput, minput);
351 tmp = vpaddq_u8(tmp, tmp);
352 tmp = vpaddq_u8(tmp, tmp);
353 return vgetq_lane_u16(vreinterpretq_u16_u8(tmp), 0);
354 };
355
356 // fast path for long strings (expected to be common)
357 size_t i = location;
358 uint8x16_t low_mask =
359 ada_make_uint8x16_t(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
360 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x03);
361 uint8x16_t high_mask =
362 ada_make_uint8x16_t(0x00, 0x00, 0x02, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
363 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
364 uint8x16_t fmask = vmovq_n_u8(0xf);
365 uint8x16_t zero{0};
366 for (; i + 15 < view.size(); i += 16) {
367 uint8x16_t word = vld1q_u8((const uint8_t*)view.data() + i);
368 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
369 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
370 uint8x16_t classify = vandq_u8(lowpart, highpart);
371 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
372 uint8x16_t is_zero = vceqq_u8(classify, zero);
373 uint16_t is_non_zero = ~to_bitmask(is_zero);
374 return i + trailing_zeroes(is_non_zero);
375 }
376 }
377
378 if (i < view.size()) {
379 uint8x16_t word =
380 vld1q_u8((const uint8_t*)view.data() + view.length() - 16);
381 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
382 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
383 uint8x16_t classify = vandq_u8(lowpart, highpart);
384 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
385 uint8x16_t is_zero = vceqq_u8(classify, zero);
386 uint16_t is_non_zero = ~to_bitmask(is_zero);
387 return view.length() - 16 + trailing_zeroes(is_non_zero);
388 }
389 }
390 return size_t(view.size());
391}
392#elif ADA_SSE2
393ada_really_inline size_t find_next_host_delimiter(std::string_view view,
394 size_t location) noexcept {
395 // first check for short strings in which case we do it naively.
396 if (view.size() - location < 16) { // slow path
397 for (size_t i = location; i < view.size(); i++) {
398 if (view[i] == ':' || view[i] == '/' || view[i] == '?' ||
399 view[i] == '[') {
400 return i;
401 }
402 }
403 return size_t(view.size());
404 }
405 // fast path for long strings (expected to be common)
406 size_t i = location;
407 const __m128i mask1 = _mm_set1_epi8(':');
408 const __m128i mask2 = _mm_set1_epi8('/');
409 const __m128i mask4 = _mm_set1_epi8('?');
410 const __m128i mask5 = _mm_set1_epi8('[');
411
412 for (; i + 15 < view.size(); i += 16) {
413 __m128i word = _mm_loadu_si128((const __m128i*)(view.data() + i));
414 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
415 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
416 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
417 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
418 __m128i m = _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m4, m5));
419 int mask = _mm_movemask_epi8(m);
420 if (mask != 0) {
421 return i + trailing_zeroes(mask);
422 }
423 }
424 if (i < view.size()) {
425 __m128i word =
426 _mm_loadu_si128((const __m128i*)(view.data() + view.length() - 16));
427 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
428 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
429 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
430 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
431 __m128i m = _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m4, m5));
432 int mask = _mm_movemask_epi8(m);
433 if (mask != 0) {
434 return view.length() - 16 + trailing_zeroes(mask);
435 }
436 }
437 return size_t(view.length());
438}
439#else
440// : / [ ?
441static constexpr std::array<uint8_t, 256> host_delimiters = []() consteval {
442 std::array<uint8_t, 256> result{};
443 for (int i : {':', '/', '?', '['}) {
444 result[i] = 1;
445 }
446 return result;
447}();
448// credit: @the-moisrex recommended a table-based approach
449ada_really_inline size_t find_next_host_delimiter(std::string_view view,
450 size_t location) noexcept {
451 auto const str = view.substr(location);
452 for (auto pos = str.begin(); pos != str.end(); ++pos) {
453 if (host_delimiters[(uint8_t)*pos]) {
454 return pos - str.begin() + location;
455 }
456 }
457 return size_t(view.size());
458}
459#endif
460
461ada_really_inline std::pair<size_t, bool> get_host_delimiter_location(
462 const bool is_special, std::string_view& view) noexcept {
471 const size_t view_size = view.size();
472 size_t location = 0;
473 bool found_colon = false;
493 if (is_special) {
494 // We move to the next delimiter.
495 location = find_next_host_delimiter_special(view, location);
496 // Unless we find '[' then we are going only going to have to call
497 // find_next_host_delimiter_special once.
498 for (; location < view_size;
499 location = find_next_host_delimiter_special(view, location)) {
500 if (view[location] == '[') {
501 location = view.find(']', location);
502 if (location == std::string_view::npos) {
503 // performance: view.find might get translated to a memchr, which
504 // has no notion of std::string_view::npos, so the code does not
505 // reflect the assembly.
506 location = view_size;
507 break;
508 }
509 } else {
510 found_colon = view[location] == ':';
511 break;
512 }
513 }
514 } else {
515 // We move to the next delimiter.
516 location = find_next_host_delimiter(view, location);
517 // Unless we find '[' then we are going only going to have to call
518 // find_next_host_delimiter_special once.
519 for (; location < view_size;
520 location = find_next_host_delimiter(view, location)) {
521 if (view[location] == '[') {
522 location = view.find(']', location);
523 if (location == std::string_view::npos) {
524 // performance: view.find might get translated to a memchr, which
525 // has no notion of std::string_view::npos, so the code does not
526 // reflect the assembly.
527 location = view_size;
528 break;
529 }
530 } else {
531 found_colon = view[location] == ':';
532 break;
533 }
534 }
535 }
536 // performance: remove_suffix may translate into a single instruction.
537 view.remove_suffix(view_size - location);
538 return {location, found_colon};
539}
540
541void trim_c0_whitespace(std::string_view& input) noexcept {
542 while (!input.empty() &&
543 ada::unicode::is_c0_control_or_space(input.front())) {
544 input.remove_prefix(1);
545 }
546 while (!input.empty() && ada::unicode::is_c0_control_or_space(input.back())) {
547 input.remove_suffix(1);
548 }
549}
550
551ada_really_inline void parse_prepared_path(std::string_view input,
553 std::string& path) {
554 ada_log("parse_prepared_path ", input);
555 uint8_t accumulator = checkers::path_signature(input);
556 // Let us first detect a trivial case.
557 // If it is special, we check that we have no dot, no %, no \ and no
558 // character needing percent encoding. Otherwise, we check that we have no %,
559 // no dot, and no character needing percent encoding.
560 constexpr uint8_t need_encoding = 1;
561 constexpr uint8_t backslash_char = 2;
562 constexpr uint8_t dot_char = 4;
563 constexpr uint8_t percent_char = 8;
564 bool special = type != ada::scheme::NOT_SPECIAL;
565 bool may_need_slow_file_handling = (type == ada::scheme::type::FILE &&
567 bool trivial_path =
568 (special ? (accumulator == 0)
569 : ((accumulator & (need_encoding | dot_char | percent_char)) ==
570 0)) &&
571 (!may_need_slow_file_handling);
572 if (accumulator == dot_char && !may_need_slow_file_handling) {
573 // '4' means that we have at least one dot, but nothing that requires
574 // percent encoding or decoding. The only part that is not trivial is
575 // that we may have single dots and double dots path segments.
576 // If we have such segments, then we either have a path that begins
577 // with '.' (easy to check), or we have the sequence './'.
578 // Note: input cannot be empty, it must at least contain one character ('.')
579 // Note: we know that '\' is not present.
580 if (input[0] != '.') {
581 size_t slashdot = input.find("/.");
582 if (slashdot == std::string_view::npos) { // common case
583 trivial_path = true;
584 } else { // uncommon
585 // only three cases matter: /./, /.. or a final /
586 trivial_path =
587 !(slashdot + 2 == input.size() || input[slashdot + 2] == '.' ||
588 input[slashdot + 2] == '/');
589 }
590 }
591 }
592 if (trivial_path) {
593 ada_log("parse_path trivial");
594 path += '/';
595 path += input;
596 return;
597 }
598 // We are going to need to look a bit at the path, but let us see if we can
599 // ignore percent encoding *and* backslashes *and* percent characters.
600 // Except for the trivial case, this is likely to capture 99% of paths out
601 // there.
602 bool fast_path =
603 (special &&
604 (accumulator & (need_encoding | backslash_char | percent_char)) == 0) &&
605 (type != ada::scheme::type::FILE);
606 if (fast_path) {
607 ada_log("parse_prepared_path fast");
608 // Here we don't need to worry about \ or percent encoding.
609 // We also do not have a file protocol. We might have dots, however,
610 // but dots must as appear as '.', and they cannot be encoded because
611 // the symbol '%' is not present.
612 size_t previous_location = 0; // We start at 0.
613 do {
614 size_t new_location = input.find('/', previous_location);
615 // std::string_view path_view = input;
616 // We process the last segment separately:
617 if (new_location == std::string_view::npos) {
618 std::string_view path_view = input.substr(previous_location);
619 if (path_view == "..") { // The path ends with ..
620 // e.g., if you receive ".." with an empty path, you go to "/".
621 if (path.empty()) {
622 path = '/';
623 return;
624 }
625 // Fast case where we have nothing to do:
626 if (path.back() == '/') {
627 return;
628 }
629 // If you have the path "/joe/myfriend",
630 // then you delete 'myfriend'.
631 path.resize(path.rfind('/') + 1);
632 return;
633 }
634 path += '/';
635 if (path_view != ".") {
636 path.append(path_view);
637 }
638 return;
639 } else {
640 // This is a non-final segment.
641 std::string_view path_view =
642 input.substr(previous_location, new_location - previous_location);
643 previous_location = new_location + 1;
644 if (path_view == "..") {
645 size_t last_delimiter = path.rfind('/');
646 if (last_delimiter != std::string::npos) {
647 path.erase(last_delimiter);
648 }
649 } else if (path_view != ".") {
650 path += '/';
651 path.append(path_view);
652 }
653 }
654 } while (true);
655 } else {
656 ada_log("parse_path slow");
657 // we have reached the general case
658 bool needs_percent_encoding = (accumulator & 1);
659 std::string path_buffer_tmp;
660 do {
661 size_t location = (special && (accumulator & 2))
662 ? input.find_first_of("/\\")
663 : input.find('/');
664 std::string_view path_view = input;
665 if (location != std::string_view::npos) {
666 path_view.remove_suffix(path_view.size() - location);
667 input.remove_prefix(location + 1);
668 }
669 // path_buffer is either path_view or it might point at a percent encoded
670 // temporary file.
671 std::string_view path_buffer =
672 (needs_percent_encoding &&
673 ada::unicode::percent_encode<false>(
674 path_view, character_sets::PATH_PERCENT_ENCODE, path_buffer_tmp))
675 ? path_buffer_tmp
676 : path_view;
677 if (unicode::is_double_dot_path_segment(path_buffer)) {
678 if ((helpers::shorten_path(path, type) || special) &&
679 location == std::string_view::npos) {
680 path += '/';
681 }
682 } else if (unicode::is_single_dot_path_segment(path_buffer) &&
683 (location == std::string_view::npos)) {
684 path += '/';
685 }
686 // Otherwise, if path_buffer is not a single-dot path segment, then:
687 else if (!unicode::is_single_dot_path_segment(path_buffer)) {
688 // If url's scheme is "file", url's path is empty, and path_buffer is a
689 // Windows drive letter, then replace the second code point in
690 // path_buffer with U+003A (:).
691 if (type == ada::scheme::type::FILE && path.empty() &&
693 path += '/';
694 path += path_buffer[0];
695 path += ':';
696 path_buffer.remove_prefix(2);
697 path.append(path_buffer);
698 } else {
699 // Append path_buffer to url's path.
700 path += '/';
701 path.append(path_buffer);
702 }
703 }
704 if (location == std::string_view::npos) {
705 return;
706 }
707 } while (true);
708 }
709}
710
711bool overlaps(std::string_view input1, const std::string& input2) noexcept {
712 ada_log("helpers::overlaps check if string_view '", input1, "' [",
713 input1.size(), " bytes] is part of string '", input2, "' [",
714 input2.size(), " bytes]");
715 return !input1.empty() && !input2.empty() && input1.data() >= input2.data() &&
716 input1.data() < input2.data() + input2.size();
717}
718
719template <class url_type>
720ada_really_inline void strip_trailing_spaces_from_opaque_path(
721 url_type& url) noexcept {
722 ada_log("helpers::strip_trailing_spaces_from_opaque_path");
723 if (!url.has_opaque_path) return;
724 if (url.has_hash()) return;
725 if (url.has_search()) return;
726
727 auto path = std::string(url.get_pathname());
728 while (!path.empty() && path.back() == ' ') {
729 path.resize(path.size() - 1);
730 }
731 url.update_base_pathname(path);
732}
733
734// @ / \\ ?
735static constexpr std::array<uint8_t, 256> authority_delimiter_special =
736 []() consteval {
737 std::array<uint8_t, 256> result{};
738 for (uint8_t i : {'@', '/', '\\', '?'}) {
739 result[i] = 1;
740 }
741 return result;
742 }();
743// credit: @the-moisrex recommended a table-based approach
745find_authority_delimiter_special(std::string_view view) noexcept {
746 // performance note: we might be able to gain further performance
747 // with SIMD instrinsics.
748 for (auto pos = view.begin(); pos != view.end(); ++pos) {
749 if (authority_delimiter_special[(uint8_t)*pos]) {
750 return pos - view.begin();
751 }
752 }
753 return size_t(view.size());
754}
755
756// @ / ?
757static constexpr std::array<uint8_t, 256> authority_delimiter = []() consteval {
758 std::array<uint8_t, 256> result{};
759 for (uint8_t i : {'@', '/', '?'}) {
760 result[i] = 1;
761 }
762 return result;
763}();
764// credit: @the-moisrex recommended a table-based approach
766find_authority_delimiter(std::string_view view) noexcept {
767 // performance note: we might be able to gain further performance
768 // with SIMD instrinsics.
769 for (auto pos = view.begin(); pos != view.end(); ++pos) {
770 if (authority_delimiter[(uint8_t)*pos]) {
771 return pos - view.begin();
772 }
773 }
774 return size_t(view.size());
775}
776
777} // namespace ada::helpers
778
779namespace ada {
783#undef ada_make_uint8x16_t
784} // namespace ada
Definitions for URL specific checkers used within Ada.
Common definitions for cross-platform compiler support.
#define ADA_ASSERT_TRUE(COND)
#define ada_unused
Definition common_defs.h:84
#define ada_warn_unused
Definition common_defs.h:85
#define ada_really_inline
Definition common_defs.h:81
constexpr uint8_t PATH_PERCENT_ENCODE[32]
constexpr bool is_normalized_windows_drive_letter(std::string_view input) noexcept
constexpr bool is_windows_drive_letter(std::string_view input) noexcept
Includes the definitions for helper functions.
ada_really_inline size_t find_next_host_delimiter(std::string_view view, size_t location) noexcept
Definition helpers.cpp:449
static constexpr std::array< uint8_t, 256 > authority_delimiter_special
Definition helpers.cpp:735
static constexpr std::array< uint8_t, 256 > host_delimiters
Definition helpers.cpp:441
ada_really_inline size_t find_next_host_delimiter_special(std::string_view view, size_t location) noexcept
Definition helpers.cpp:317
ada_unused std::string get_state(ada::state s)
Definition helpers.cpp:35
static constexpr std::array< uint8_t, 256 > authority_delimiter
Definition helpers.cpp:757
static constexpr std::array< uint8_t, 256 > special_host_delimiters
Definition helpers.cpp:308
ada_really_inline int trailing_zeroes(uint32_t input_num) noexcept
Definition helpers.cpp:166
@ NOT_SPECIAL
Definition scheme.h:30
Definition ada_idna.h:13
ada_warn_unused std::string to_string(encoding_type type)
state
Definition state.h:17
@ SPECIAL_RELATIVE_OR_AUTHORITY
Definition state.h:91
@ FILE_SLASH
Definition state.h:71
@ SCHEME
Definition state.h:31
@ QUERY
Definition state.h:96
@ SPECIAL_AUTHORITY_SLASHES
Definition state.h:86
@ FRAGMENT
Definition state.h:46
@ FILE_HOST
Definition state.h:66
@ OPAQUE_PATH
Definition state.h:111
@ RELATIVE_SLASH
Definition state.h:56
@ NO_SCHEME
Definition state.h:41
@ PATH_START
Definition state.h:106
@ RELATIVE_SCHEME
Definition state.h:51
@ SPECIAL_AUTHORITY_IGNORE_SLASHES
Definition state.h:81
@ SCHEME_START
Definition state.h:26
@ AUTHORITY
Definition state.h:21
@ PATH_OR_AUTHORITY
Definition state.h:76
tl::expected< result_type, ada::errors > result
Declarations for the URL scheme.