72 std::array<uint8_t, 256> t{};
73 for (
size_t i = 0; i < 256; ++i) {
76 for (
size_t i = 0x21; i <= 0x7E; ++i) {
80 {
'#',
'/',
':',
'<',
'>',
'?',
'@',
'[',
'\\',
']',
'^',
'|',
'%'}) {
83 t[
static_cast<uint8_t
>(
'/')] = 1;
84 t[
static_cast<uint8_t
>(
'?')] = 1;
85 t[
static_cast<uint8_t
>(
'#')] = 1;
86 t[
static_cast<uint8_t
>(
':')] = 1;
91constexpr std::array<uint8_t, 256>
k_path = []()
consteval {
92 std::array<uint8_t, 256> t{};
93 for (
size_t i = 0; i < 256; ++i) {
96 for (uint8_t c = 0x21; c <= 0x7E; ++c) {
99 for (uint8_t c : {
static_cast<uint8_t
>(
'"'),
static_cast<uint8_t
>(
'<'),
100 static_cast<uint8_t
>(
'>'),
static_cast<uint8_t
>(
'`'),
101 static_cast<uint8_t
>(
'{'),
static_cast<uint8_t
>(
'}'),
102 static_cast<uint8_t
>(
'^'),
static_cast<uint8_t
>(
'\\'),
103 static_cast<uint8_t
>(
'%')}) {
106 t[
static_cast<uint8_t
>(
'?')] = 1;
107 t[
static_cast<uint8_t
>(
'#')] = 1;
112constexpr std::array<uint8_t, 256>
k_query = []()
consteval {
113 std::array<uint8_t, 256> t{};
114 for (
size_t i = 0; i < 256; ++i) {
117 for (uint8_t c = 0x21; c <= 0x7E; ++c) {
120 for (uint8_t c : {
static_cast<uint8_t
>(
'"'),
static_cast<uint8_t
>(
'<'),
121 static_cast<uint8_t
>(
'>'),
static_cast<uint8_t
>(
'\'')}) {
124 t[
static_cast<uint8_t
>(
'#')] = 1;
129constexpr std::array<uint8_t, 256>
k_hash = []()
consteval {
130 std::array<uint8_t, 256> t{};
131 for (
size_t i = 0; i < 256; ++i) {
134 for (uint8_t c = 0x21; c <= 0x7E; ++c) {
137 for (uint8_t c : {
static_cast<uint8_t
>(
'"'),
static_cast<uint8_t
>(
'<'),
138 static_cast<uint8_t
>(
'>'),
static_cast<uint8_t
>(
'`')}) {
150 alignas(16) uint8_t
low[16]{};
151 alignas(16) uint8_t
high[16]{};
156 const std::array<uint8_t, 256>& cls) {
158 uint16_t patterns[16]{};
159 for (
int hi = 0; hi < 16; ++hi) {
160 for (
int lo = 0; lo < 16; ++lo) {
161 if (cls[
static_cast<size_t>((hi << 4) | lo)] != 0) {
162 patterns[hi] =
static_cast<uint16_t
>(patterns[hi] | (1u << lo));
167 for (
int hi = 0; hi < 16; ++hi) {
168 if (patterns[hi] == 0 || t.
high[hi] != 0) {
171 if (bits_used == 8) {
175 const uint8_t bit =
static_cast<uint8_t
>(1u << bits_used++);
176 for (
int other = hi; other < 16; ++other) {
177 if (patterns[other] == patterns[hi]) {
178 t.
high[other] =
static_cast<uint8_t
>(t.
high[other] | bit);
181 for (
int lo = 0; lo < 16; ++lo) {
182 if ((patterns[hi] & (1u << lo)) != 0) {
183 t.
low[lo] =
static_cast<uint8_t
>(t.
low[lo] | bit);
213 auto [ptr, ec] = std::to_chars(port_buf, port_buf + 5, port);
215 buffer.append(port_buf,
static_cast<size_t>(ptr - port_buf));
219#ifdef ADA_REGULAR_VISUAL_STUDIO
221 _BitScanForward(&ret, input_num);
222 return static_cast<int>(ret);
224 return __builtin_ctz(input_num);
229#ifdef ADA_REGULAR_VISUAL_STUDIO
231 _BitScanForward64(&ret, input_num);
232 return static_cast<int>(ret);
234 return __builtin_ctzll(input_num);
240 return _mm_load_si128(
reinterpret_cast<const __m128i*
>(t));
246 __m128i hi_tbl)
noexcept {
247 const __m128i nibble = _mm_set1_epi8(0x0F);
248 const __m128i lo = _mm_and_si128(w, nibble);
249 const __m128i hi = _mm_and_si128(_mm_srli_epi16(w, 4), nibble);
251 _mm_and_si128(_mm_shuffle_epi8(lo_tbl, lo), _mm_shuffle_epi8(hi_tbl, hi));
252 return _mm_movemask_epi8(_mm_cmpeq_epi8(hit, _mm_setzero_si128())) ^ 0xFFFF;
260 return _mm_or_si128(_mm_cmplt_epi8(w, _mm_set1_epi8(0x21)),
261 _mm_cmpeq_epi8(w, _mm_set1_epi8(0x7F)));
265 const __m128i ctrl = sse2_ctrl_or_nonascii(w);
266 const __m128i is_upper = _mm_and_si128(
267 _mm_cmpgt_epi8(w, _mm_set1_epi8(
static_cast<char>(
'A' - 1))),
268 _mm_cmplt_epi8(w, _mm_set1_epi8(
static_cast<char>(
'Z' + 1))));
269 const __m128i at = _mm_sub_epi8(w, _mm_set1_epi8(
'@'));
270 const __m128i at_through_caret =
271 _mm_cmpeq_epi8(_mm_min_epu8(at, _mm_set1_epi8(
'^' -
'@')), at);
272 const __m128i at_not_upper = _mm_andnot_si128(is_upper, at_through_caret);
273 const __m128i spec = _mm_or_si128(
274 _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'#')),
275 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'%'))),
276 _mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'/')),
277 _mm_cmpeq_epi8(w, _mm_set1_epi8(
':')))),
278 _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'<')),
279 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'>'))),
280 _mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'?')),
281 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'|')))));
282 return _mm_movemask_epi8(
283 _mm_or_si128(_mm_or_si128(ctrl, at_not_upper), spec));
287 const __m128i ctrl = sse2_ctrl_or_nonascii(w);
288 const __m128i spec = _mm_or_si128(
289 _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'"')),
290 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'#'))),
291 _mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'%')),
292 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'<')))),
294 _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'>')),
295 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'?'))),
296 _mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'\\')),
297 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'^')))),
298 _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'`')),
299 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'{'))),
300 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'}')))));
301 return _mm_movemask_epi8(_mm_or_si128(ctrl, spec));
305 const __m128i ctrl = sse2_ctrl_or_nonascii(w);
306 const __m128i spec = _mm_or_si128(
307 _mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'"')),
308 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'#'))),
309 _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'<')),
310 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'>'))),
311 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'\''))));
312 return _mm_movemask_epi8(_mm_or_si128(ctrl, spec));
316 const __m128i ctrl = sse2_ctrl_or_nonascii(w);
318 _mm_or_si128(_mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'"')),
319 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'<'))),
320 _mm_or_si128(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'>')),
321 _mm_cmpeq_epi8(w, _mm_set1_epi8(
'`'))));
322 return _mm_movemask_epi8(_mm_or_si128(ctrl, spec));
327 return _mm_movemask_epi8(_mm_and_si128(
328 _mm_cmpgt_epi8(w, _mm_set1_epi8(
static_cast<char>(
'A' - 1))),
329 _mm_cmplt_epi8(w, _mm_set1_epi8(
static_cast<char>(
'Z' + 1)))));
335 const uint8x8_t nib = vshrn_n_u16(vreinterpretq_u16_u8(matches), 4);
336 return vget_lane_u64(vreinterpret_u64_u8(nib), 0);
340 uint8x16_t hi_tbl)
noexcept {
341 const uint8x16_t hit =
342 vandq_u8(vqtbl1q_u8(lo_tbl, vandq_u8(w, vdupq_n_u8(0x0F))),
343 vqtbl1q_u8(hi_tbl, vshrq_n_u8(w, 4)));
344 return neon_nibble_bits(vtstq_u8(hit, hit));
348 return neon_nibble_bits(
349 vandq_u8(vcgeq_u8(w, vdupq_n_u8(
'A')), vcleq_u8(w, vdupq_n_u8(
'Z'))));
359 const size_t rem = n - host_start;
365 const uint8x16_t w = vld1q_u8(p + host_start);
366 const uint64_t at = neon_nibble_bits(vceqq_u8(w, vdupq_n_u8(
'@')));
370 const uint64_t delim = neon_nibble_bits(vceqq_u8(w, vdupq_n_u8(
'/'))) |
371 neon_nibble_bits(vceqq_u8(w, vdupq_n_u8(
'?'))) |
372 neon_nibble_bits(vceqq_u8(w, vdupq_n_u8(
'#')));
373 return delim == 0 || std::countr_zero(at) < std::countr_zero(delim);
378 _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(p + host_start));
379 const uint16_t at =
static_cast<uint16_t
>(
380 _mm_movemask_epi8(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'@'))));
384 const uint16_t delim =
static_cast<uint16_t
>(
385 _mm_movemask_epi8(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'/'))) |
386 _mm_movemask_epi8(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'?'))) |
387 _mm_movemask_epi8(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'#'))));
388 return delim == 0 || std::countr_zero(at) < std::countr_zero(delim);
391 const size_t lim = host_start + rem;
392 for (
size_t i = host_start; i < lim; ++i) {
393 const uint8_t c = p[i];
397 if (c ==
'/' || c ==
'?' || c ==
'#') {
409#ifdef ADA_PARSER_NEED_SSSE3_TARGET
410 static const bool supported = __builtin_cpu_supports(
"ssse3") != 0;
422 size_t& end,
bool& has_upper,
423 bool& has_x)
noexcept {
428 if (parser_use_ssse3() && (len - start >= 16)) {
431 const __m128i x_splat = _mm_set1_epi8(
'x');
432 auto visit = [&](
size_t at)
noexcept ->
bool {
434 _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + at));
435 const int up = sse2_uppercase(w);
436 const int xs = _mm_movemask_epi8(_mm_cmpeq_epi8(w, x_splat));
437 const int mask = ssse3_nibble_mask(w, lo_tbl, hi_tbl);
448 const int valid = (1 << hit) - 1;
449 if ((up & valid) != 0) {
452 if ((xs & valid) != 0) {
455 end = at +
static_cast<size_t>(hit);
458 for (; i + 32 <= len; i += 32) {
459 if (visit(i) || visit(i + 16)) {
463 for (; i + 16 <= len; i += 16) {
470 if (i > start && i < len) {
471 if (visit(len - 16) && end >= i) {
479 const __m128i x_splat = _mm_set1_epi8(
'x');
480 for (; i + 32 <= len; i += 32) {
481 for (
size_t off = 0; off < 32; off += 16) {
483 _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + i + off));
484 const int up = sse2_uppercase(w);
485 const int xs = _mm_movemask_epi8(_mm_cmpeq_epi8(w, x_splat));
486 const int mask = sse2_host_stop(w);
489 const int valid = (1 << hit) - 1;
490 if ((up & valid) != 0) {
493 if ((xs & valid) != 0) {
496 end = i + off +
static_cast<size_t>(hit);
507 for (; i + 16 <= len; i += 16) {
508 const __m128i w = _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + i));
509 const int up = sse2_uppercase(w);
510 const int xs = _mm_movemask_epi8(_mm_cmpeq_epi8(w, x_splat));
511 const int mask = sse2_host_stop(w);
514 const int valid = (1 << hit) - 1;
515 if ((up & valid) != 0) {
518 if ((xs & valid) != 0) {
521 end = i +
static_cast<size_t>(hit);
532 if (len - start >= 16) {
535 const uint8x16_t x_splat = vdupq_n_u8(
'x');
536 auto visit = [&](
size_t at)
noexcept ->
bool {
537 const uint8x16_t w = vld1q_u8(b + at);
538 const uint64_t up = neon_uppercase(w);
539 const uint64_t xs = neon_nibble_bits(vceqq_u8(w, x_splat));
540 const uint64_t bits = neon_table_stop(w, lo_tbl, hi_tbl);
551 const uint64_t valid = (hit == 0) ? 0 : (uint64_t{1} << (hit * 4)) - 1;
552 if ((up & valid) != 0) {
555 if ((xs & valid) != 0) {
561 for (; i + 32 <= len; i += 32) {
562 if (visit(i) || visit(i + 16)) {
566 for (; i + 16 <= len; i += 16) {
571 if (i > start && i < len) {
572 if (visit(len - 16) && end >= i) {
580 for (; i < len; ++i) {
581 const uint8_t c = b[i];
590 if (c >=
'A' && c <=
'Z') {
592 }
else if (c ==
'x') {
601 const uint8_t* b,
size_t pos,
size_t run_start,
602 bool& maybe_dot_segment)
noexcept {
603 if (pos == run_start || b[pos - 1] ==
'/') {
604 maybe_dot_segment =
true;
608#if ADA_PARSER_SSSE3 || ADA_SSE2
613 size_t run_start, __m128i w,
615 bool& maybe_dot_segment)
noexcept {
616 if (maybe_dot_segment || valid_mask == 0) {
620 _mm_movemask_epi8(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'.'))) & valid_mask;
625 _mm_movemask_epi8(_mm_cmpeq_epi8(w, _mm_set1_epi8(
'/'))) & valid_mask;
626 if (((dots & 1) != 0 && (i == run_start || b[i - 1] ==
'/')) ||
627 (dots & (slashes << 1)) != 0) {
628 maybe_dot_segment =
true;
637 const uint8_t* b,
size_t i,
size_t run_start, uint8x16_t w,
638 uint64_t valid_bits,
bool& maybe_dot_segment)
noexcept {
639 if (maybe_dot_segment || valid_bits == 0) {
642 const uint64_t dots =
643 neon_nibble_bits(vceqq_u8(w, vdupq_n_u8(
'.'))) & valid_bits;
647 const uint64_t slashes =
648 neon_nibble_bits(vceqq_u8(w, vdupq_n_u8(
'/'))) & valid_bits;
649 if (((dots & 0xF) != 0 && (i == run_start || b[i - 1] ==
'/')) ||
650 (dots & (slashes << 4)) != 0) {
651 maybe_dot_segment =
true;
658 bool& maybe_dot_segment)
noexcept {
659 const size_t run_start = i;
661 if (parser_use_ssse3() && (i + 16 <= len)) {
664 for (; i + 32 <= len; i += 32) {
666 _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + i));
667 const int m0 = ssse3_nibble_mask(w0, lo_tbl, hi_tbl);
670 note_dots_in_window(b, i, run_start, w0, (1 << hit_bit) - 1,
672 i +=
static_cast<size_t>(hit_bit);
675 note_dots_in_window(b, i, run_start, w0, 0xFFFF, maybe_dot_segment);
677 _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + i + 16));
678 const int m1 = ssse3_nibble_mask(w1, lo_tbl, hi_tbl);
681 note_dots_in_window(b, i + 16, run_start, w1, (1 << hit_bit) - 1,
683 i += 16 +
static_cast<size_t>(hit_bit);
686 note_dots_in_window(b, i + 16, run_start, w1, 0xFFFF, maybe_dot_segment);
688 for (; i + 16 <= len; i += 16) {
690 _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + i));
691 const int mask = ssse3_nibble_mask(w, lo_tbl, hi_tbl);
694 note_dots_in_window(b, i, run_start, w, (1 << hit_bit) - 1,
696 i +=
static_cast<size_t>(hit_bit);
699 note_dots_in_window(b, i, run_start, w, 0xFFFF, maybe_dot_segment);
701 if (i > run_start && i < len) {
703 _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + len - 16));
704 const int mask = ssse3_nibble_mask(w, lo_tbl, hi_tbl);
710 for (
size_t j = i; j < hit; ++j) {
719 for (
size_t j = i; j < len; ++j) {
729 for (; i + 16 <= len; i += 16) {
730 const __m128i w = _mm_loadu_si128(
reinterpret_cast<const __m128i*
>(b + i));
731 const int mask = sse2_path_stop(w);
734 note_dots_in_window(b, i, run_start, w, (1 << hit_bit) - 1,
736 i +=
static_cast<size_t>(hit_bit);
739 note_dots_in_window(b, i, run_start, w, 0xFFFF, maybe_dot_segment);
745 for (; i + 32 <= len; i += 32) {
746 const uint8x16_t w0 = vld1q_u8(b + i);
747 const uint64_t bits0 = neon_table_stop(w0, lo_tbl, hi_tbl);
749 const size_t hit_off =
751 const uint64_t valid =
752 (hit_off == 0) ? 0 : (uint64_t{1} << (hit_off * 4)) - 1;
753 note_dots_in_window_neon(b, i, run_start, w0, valid, maybe_dot_segment);
757 note_dots_in_window_neon(b, i, run_start, w0, ~uint64_t{0},
759 const uint8x16_t w1 = vld1q_u8(b + i + 16);
760 const uint64_t bits1 = neon_table_stop(w1, lo_tbl, hi_tbl);
762 const size_t hit_off =
764 const uint64_t valid =
765 (hit_off == 0) ? 0 : (uint64_t{1} << (hit_off * 4)) - 1;
766 note_dots_in_window_neon(b, i + 16, run_start, w1, valid,
771 note_dots_in_window_neon(b, i + 16, run_start, w1, ~uint64_t{0},
774 for (; i + 16 <= len; i += 16) {
775 const uint8x16_t w = vld1q_u8(b + i);
776 const uint64_t bits = neon_table_stop(w, lo_tbl, hi_tbl);
778 const size_t hit_off =
780 const uint64_t valid =
781 (hit_off == 0) ? 0 : (uint64_t{1} << (hit_off * 4)) - 1;
782 note_dots_in_window_neon(b, i, run_start, w, valid, maybe_dot_segment);
786 note_dots_in_window_neon(b, i, run_start, w, ~uint64_t{0},
789 if (i > run_start && i < len) {
790 const uint8x16_t w = vld1q_u8(b + len - 16);
791 const uint64_t bits = neon_table_stop(w, lo_tbl, hi_tbl);
796 for (
size_t j = i; j < hit; ++j) {
805 for (
size_t j = i; j < len; ++j) {
815 for (; i < len; ++i) {
816 const uint8_t c = b[i];
817 const uint8_t cls =
k_path[c];
830#define ADA_SCAN_STOP_RUN(nibbles, cls, unused_sse2_stop) \
832 const size_t scan_start = i; \
833 if (parser_use_ssse3() && (i + 16 <= len)) { \
834 const __m128i lo_tbl = nibble_load((nibbles).low); \
835 const __m128i hi_tbl = nibble_load((nibbles).high); \
836 for (; i + 32 <= len; i += 32) { \
838 _mm_loadu_si128(reinterpret_cast<const __m128i*>(b + i)); \
839 const int m0 = ssse3_nibble_mask(w0, lo_tbl, hi_tbl); \
841 i += static_cast<size_t>( \
842 trailing_zeroes32(static_cast<uint32_t>(m0))); \
846 _mm_loadu_si128(reinterpret_cast<const __m128i*>(b + i + 16)); \
847 const int m1 = ssse3_nibble_mask(w1, lo_tbl, hi_tbl); \
849 i += 16 + static_cast<size_t>( \
850 trailing_zeroes32(static_cast<uint32_t>(m1))); \
854 for (; i + 16 <= len; i += 16) { \
856 _mm_loadu_si128(reinterpret_cast<const __m128i*>(b + i)); \
857 const int mask = ssse3_nibble_mask(w, lo_tbl, hi_tbl); \
859 i += static_cast<size_t>( \
860 trailing_zeroes32(static_cast<uint32_t>(mask))); \
864 if (i > scan_start && i < len) { \
866 _mm_loadu_si128(reinterpret_cast<const __m128i*>(b + len - 16)); \
867 const int mask = ssse3_nibble_mask(w, lo_tbl, hi_tbl); \
869 const size_t hit = len - 16 + \
870 static_cast<size_t>(trailing_zeroes32( \
871 static_cast<uint32_t>(mask))); \
881 for (; i < len; ++i) { \
882 if ((cls)[b[i]] != 0) { \
888#define ADA_SCAN_STOP_RUN(nibbles, cls, unused_sse2_stop) \
890 const size_t scan_start = i; \
891 if (i + 16 <= len) { \
892 const uint8x16_t lo_tbl = vld1q_u8((nibbles).low); \
893 const uint8x16_t hi_tbl = vld1q_u8((nibbles).high); \
894 for (; i + 32 <= len; i += 32) { \
895 const uint64_t bits0 = \
896 neon_table_stop(vld1q_u8(b + i), lo_tbl, hi_tbl); \
898 i += static_cast<size_t>(trailing_zeroes64(bits0)) >> 2; \
901 const uint64_t bits1 = \
902 neon_table_stop(vld1q_u8(b + i + 16), lo_tbl, hi_tbl); \
904 i += 16 + (static_cast<size_t>(trailing_zeroes64(bits1)) >> 2); \
908 for (; i + 16 <= len; i += 16) { \
909 const uint8x16_t w = vld1q_u8(b + i); \
910 const uint64_t bits = neon_table_stop(w, lo_tbl, hi_tbl); \
912 i += static_cast<size_t>(trailing_zeroes64(bits)) >> 2; \
916 if (i > scan_start && i < len) { \
917 const uint8x16_t w = vld1q_u8(b + len - 16); \
918 const uint64_t bits = neon_table_stop(w, lo_tbl, hi_tbl); \
921 len - 16 + (static_cast<size_t>(trailing_zeroes64(bits)) >> 2); \
931 for (; i < len; ++i) { \
932 if ((cls)[b[i]] != 0) { \
938#define ADA_SCAN_STOP_RUN(nibbles, cls, sse2_stop) \
940 for (; i + 16 <= len; i += 16) { \
942 _mm_loadu_si128(reinterpret_cast<const __m128i*>(b + i)); \
943 const int mask = sse2_stop(w); \
945 i += static_cast<size_t>( \
946 trailing_zeroes32(static_cast<uint32_t>(mask))); \
950 for (; i < len; ++i) { \
951 if ((cls)[b[i]] != 0) { \
957#define ADA_SCAN_STOP_RUN(nibbles, cls, unused_sse2_stop) \
959 for (; i < len; ++i) { \
960 if ((cls)[b[i]] != 0) { \
968 size_t len)
noexcept {
973 size_t len)
noexcept {
977#undef ADA_SCAN_STOP_RUN
984 const size_t i = (path[0] ==
'/') ? 1 : 0;
985 if (i < path.size() && path[i] ==
'.') {
986 const size_t after = i + 1;
987 if (after == path.size() || path[after] ==
'/' ||
988 (after < path.size() && path[after] ==
'.' &&
989 (after + 1 == path.size() || path[after + 1] ==
'/'))) {
993 static constexpr std::string_view slash_dot{
"/.", 2};
995 while ((p = path.find(slash_dot, p)) != std::string_view::npos) {
996 const size_t after = p + 2;
997 if (after == path.size() || path[after] ==
'/' ||
998 (after < path.size() && path[after] ==
'.' &&
999 (after + 1 == path.size() || path[after + 1] ==
'/'))) {
1012 uint32_t protocol_end,
size_t host_start,
size_t host_end,
size_t host_len,
1014 constexpr bool is_ada_url = std::is_same_v<result_type, ada::url>;
1015 constexpr bool is_aggregator =
1016 std::is_same_v<result_type, ada::url_aggregator>;
1017 static_assert(is_ada_url || is_aggregator);
1019 const size_t len = input.size();
1020 const auto* b =
reinterpret_cast<const uint8_t*
>(input.data());
1021 size_t p = host_end + 1;
1022 uint32_t port_value = 0;
1023 bool any_digit =
false;
1024 while (p < len && b[p] >=
'0' && b[p] <=
'9') {
1026 port_value = port_value * 10 +
static_cast<uint32_t
>(b[p] -
'0');
1027 if (port_value > 65535) [[unlikely]] {
1032 if (p < len && b[p] !=
'/' && b[p] !=
'?' && b[p] !=
'#') [[unlikely]] {
1037 if (any_digit && port_value != default_port) {
1038 parsed_port = port_value;
1040 const size_t authority_end = p;
1042 size_t i = authority_end;
1043 size_t path_start = host_end;
1044 size_t path_end = host_end;
1045 size_t query_start = std::string_view::npos;
1046 size_t hash_start = std::string_view::npos;
1047 bool has_path =
false;
1048 bool maybe_dot_segment =
false;
1049 bool rest_simple =
true;
1051 if (i < len && b[i] ==
'/') {
1055 scan_path_run(b, i, len, maybe_dot_segment);
1057 const uint8_t cls = k_path[b[i]];
1069 rest_simple =
false;
1070 for (; i < len; ++i) {
1075 goto scan_query_boundary;
1088 }
else if (i < len && b[i] ==
'?') {
1092 }
else if (i < len && b[i] ==
'#') {
1100 scan_query_run(b, i, len);
1107 rest_simple =
false;
1108 goto scan_query_boundary;
1113 for (; i < len; ++i) {
1123 scan_hash_run(b, i, len);
1125 rest_simple =
false;
1129 if (rest_simple && maybe_dot_segment) {
1130 const std::string_view path_body(input.data() + path_start,
1131 path_end - path_start);
1132 if (path_has_dot_segment(path_body)) {
1133 rest_simple =
false;
1142 if (!rest_simple && (unicode::is_c0_control_or_space(input.back()) ||
1143 unicode::has_tabs_or_newline(input))) {
1147 out.type = scheme_type;
1148 out.is_valid =
true;
1149 out.has_opaque_path =
false;
1153 ? (1 + port_decimal_digit_count(parsed_port))
1157 const std::string_view path_view =
1159 ? std::string_view(input.data() + path_start, path_end - path_start)
1160 : std::string_view{};
1161 auto apply_query_and_hash = [&]() {
1162 if (query_start != std::string_view::npos) {
1163 const size_t q_end =
1164 (hash_start != std::string_view::npos) ? hash_start : len;
1165 out.update_base_search(std::string_view(input.data() + query_start + 1,
1166 q_end - query_start - 1),
1169 if (hash_start != std::string_view::npos) {
1170 out.update_unencoded_base_hash(std::string_view(
1171 input.data() + hash_start + 1, len - hash_start - 1));
1174 if constexpr (is_aggregator) {
1175 out.buffer.assign(input.substr(0, host_end));
1177 append_canonical_port(out.buffer, parsed_port);
1180 unicode::to_lower_ascii(out.buffer.data() + host_start, host_len);
1182 out.components.protocol_end = protocol_end;
1183 out.components.username_end = protocol_end + 2;
1184 out.components.host_start = protocol_end + 2;
1185 out.components.host_end =
static_cast<uint32_t
>(host_end);
1186 out.components.port = parsed_port;
1187 out.components.pathname_start =
static_cast<uint32_t
>(out.buffer.size());
1190 out.parse_path(path_view);
1191 apply_query_and_hash();
1193 std::string host_str(input.substr(host_start, host_len));
1195 unicode::to_lower_ascii(host_str.data(), host_str.size());
1197 out.host = std::move(host_str);
1199 out.port =
static_cast<uint16_t
>(parsed_port);
1201 out.parse_path(path_view);
1202 apply_query_and_hash();
1207 const bool insert_slash = !has_path;
1208 if constexpr (is_aggregator) {
1209 const size_t out_len =
1210 host_end + port_bytes + (insert_slash ? 1 : 0) + (len - authority_end);
1212 if (out_len == len && !insert_slash && !omit_port_bytes) {
1213 out.buffer.assign(input);
1216 out.buffer.reserve(out_len);
1217 out.buffer.append(input.substr(0, host_end));
1219 append_canonical_port(out.buffer, parsed_port);
1222 out.buffer.push_back(
'/');
1224 if (authority_end < len) {
1225 out.buffer.append(input.substr(authority_end));
1229 unicode::to_lower_ascii(out.buffer.data() + host_start, host_len);
1231 const uint32_t pathname_start =
1232 static_cast<uint32_t
>(host_end) + port_bytes;
1233 const int32_t tail_delta =
static_cast<int32_t
>(pathname_start) +
1234 (insert_slash ? 1 : 0) -
1235 static_cast<int32_t
>(authority_end);
1236 out.components.protocol_end = protocol_end;
1237 out.components.username_end = protocol_end + 2;
1238 out.components.host_start = protocol_end + 2;
1239 out.components.host_end =
static_cast<uint32_t
>(host_end);
1240 out.components.port = parsed_port;
1241 out.components.pathname_start = pathname_start;
1242 out.components.search_start =
1243 (query_start != std::string_view::npos)
1244 ?
static_cast<uint32_t
>(
static_cast<int32_t
>(query_start) +
1247 out.components.hash_start =
1248 (hash_start != std::string_view::npos)
1249 ?
static_cast<uint32_t
>(
static_cast<int32_t
>(hash_start) +
1253 std::string host_str(input.substr(host_start, host_len));
1255 unicode::to_lower_ascii(host_str.data(), host_str.size());
1257 out.host = std::move(host_str);
1259 out.port =
static_cast<uint16_t
>(parsed_port);
1264 out.path.assign(input.data() + path_start, path_end - path_start);
1266 if (query_start != std::string_view::npos) {
1267 const size_t q_end =
1268 (hash_start != std::string_view::npos) ? hash_start : len;
1269 out.query.emplace(input.data() + query_start + 1,
1270 q_end - query_start - 1);
1272 if (hash_start != std::string_view::npos) {
1273 out.hash.emplace(input.data() + hash_start + 1, len - hash_start - 1);
1286 constexpr bool is_ada_url = std::is_same_v<result_type, ada::url>;
1287 constexpr bool is_aggregator =
1288 std::is_same_v<result_type, ada::url_aggregator>;
1289 static_assert(is_ada_url || is_aggregator);
1291 const size_t len = input.size();
1293 if (len < 6) [[unlikely]] {
1296 const auto* b =
reinterpret_cast<const uint8_t*
>(input.data());
1300 uint32_t protocol_end = 0;
1301#if (defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__) || \
1302 defined(_M_X64) || defined(_M_IX86) || defined(_M_AMD64)
1306 uint64_t first8 = 0;
1307 std::memcpy(&first8, b, 8);
1308 if (first8 == 0x2f2f3a7370747468ull) {
1312 }
else if ((first8 & 0x00ffffffffffffffull) ==
1313 0x002f2f3a70747468ull) {
1317 }
else if ((first8 & 0x0000ffffffffffffull) ==
1318 0x00002f2f3a737377ull) {
1322 }
else if ((first8 & 0x0000ffffffffffffull) ==
1323 0x00002f2f3a707466ull) {
1327 }
else if ((first8 & 0x000000ffffffffffull) ==
1328 0x0000002f2f3a7377ull) {
1335 }
else if (b[0] ==
'w' && b[1] ==
's') {
1336 if (b[2] ==
':' && b[3] ==
'/' && b[4] ==
'/') {
1340 }
else if (b[2] ==
's' && b[3] ==
':' && b[4] ==
'/' && b[5] ==
'/') {
1347 }
else if (b[0] ==
'f' && b[1] ==
't' && b[2] ==
'p' && b[3] ==
':' &&
1348 b[4] ==
'/' && b[5] ==
'/') {
1352 }
else if (len >= 7 && b[0] ==
'h' && b[1] ==
't' && b[2] ==
't' &&
1353 b[3] ==
'p' && b[4] ==
':' && b[5] ==
'/' && b[6] ==
'/') {
1362 if (len >= 8 && b[0] ==
'h' && b[1] ==
't' && b[2] ==
't' && b[3] ==
'p' &&
1363 b[4] ==
's' && b[5] ==
':' && b[6] ==
'/' && b[7] ==
'/') {
1367 }
else if (len >= 7 && b[0] ==
'h' && b[1] ==
't' && b[2] ==
't' &&
1368 b[3] ==
'p' && b[4] ==
':' && b[5] ==
'/' && b[6] ==
'/') {
1372 }
else if (b[0] ==
'w' && b[1] ==
's') {
1373 if (b[2] ==
':' && b[3] ==
'/' && b[4] ==
'/') {
1377 }
else if (len >= 6 && b[2] ==
's' && b[3] ==
':' && b[4] ==
'/' &&
1385 }
else if (b[0] ==
'f' && b[1] ==
't' && b[2] ==
'p' && b[3] ==
':' &&
1386 b[4] ==
'/' && b[5] ==
'/') {
1394 if (pos < len && (b[pos] ==
'/' || b[pos] ==
'\\')) [[unlikely]] {
1399 if (pos < len && ((b[pos] >=
'0' && b[pos] <=
'9') || b[pos] ==
'[')) {
1403 const size_t host_start = pos;
1404 bool has_upper =
false;
1406 size_t host_end = pos;
1407 if (!scan_plain_host(b, pos, len, host_end, has_upper, has_x)) {
1410 if (host_start == host_end) [[unlikely]] {
1413 const size_t host_len = host_end - host_start;
1414 if (host_len > 253) [[unlikely]] {
1418 std::string_view hv(input.data() + host_start, host_len);
1420 if (has_upper) [[unlikely]] {
1421 std::memcpy(host_buf, input.data() + host_start, host_len);
1422 unicode::to_lower_ascii(host_buf, host_len);
1423 hv = std::string_view(host_buf, host_len);
1430 static constexpr std::string_view xn{
"xn-", 3};
1431 if ((has_x || has_upper) && hv.find(xn) != std::string_view::npos)
1437 if (host_end < len && b[host_end] ==
':') [[unlikely]] {
1438 return finish_simple_absolute_with_port(input, out, scheme_type,
1439 protocol_end, host_start, host_end,
1440 host_len, has_upper);
1443 size_t i = host_end;
1444 size_t path_start = host_end;
1445 size_t path_end = host_end;
1446 size_t query_start = std::string_view::npos;
1447 size_t hash_start = std::string_view::npos;
1448 bool has_path =
false;
1449 bool maybe_dot_segment =
false;
1450 bool rest_simple =
true;
1452 if (i < len && b[i] ==
'/') {
1456 scan_path_run(b, i, len, maybe_dot_segment);
1458 const uint8_t cls = k_path[b[i]];
1470 rest_simple =
false;
1471 hash_start = input.find(
'#', i);
1472 query_start = input.substr(0, hash_start).find(
'?', i);
1473 if (query_start != std::string_view::npos) {
1474 path_end = query_start;
1476 path_end = hash_start != std::string_view::npos ? hash_start : len;
1481 }
else if (i < len && b[i] ==
'?') {
1485 }
else if (i < len && b[i] ==
'#') {
1493 scan_query_run(b, i, len);
1500 rest_simple =
false;
1501 goto scan_query_boundary;
1506 hash_start = input.find(
'#', i);
1510 scan_hash_run(b, i, len);
1512 rest_simple =
false;
1516 if (rest_simple && maybe_dot_segment) {
1517 const std::string_view path_body(input.data() + path_start,
1518 path_end - path_start);
1519 if (path_has_dot_segment(path_body)) {
1520 rest_simple =
false;
1529 if (!rest_simple && (unicode::is_c0_control_or_space(input.back()) ||
1530 unicode::has_tabs_or_newline(input))) {
1534 out.type = scheme_type;
1535 out.is_valid =
true;
1536 out.has_opaque_path =
false;
1543 const std::string_view path_view =
1545 ? std::string_view(input.data() + path_start, path_end - path_start)
1546 : std::string_view{};
1547 auto apply_query_and_hash = [&]() {
1548 if (query_start != std::string_view::npos) {
1549 const size_t q_end =
1550 (hash_start != std::string_view::npos) ? hash_start : len;
1551 out.update_base_search(std::string_view(input.data() + query_start + 1,
1552 q_end - query_start - 1),
1555 if (hash_start != std::string_view::npos) {
1556 out.update_unencoded_base_hash(std::string_view(
1557 input.data() + hash_start + 1, len - hash_start - 1));
1560 if constexpr (is_aggregator) {
1561 out.buffer.assign(input.substr(0, host_end));
1563 unicode::to_lower_ascii(out.buffer.data() + host_start, host_len);
1565 out.components.protocol_end = protocol_end;
1566 out.components.username_end = protocol_end + 2;
1567 out.components.host_start = protocol_end + 2;
1568 out.components.host_end =
static_cast<uint32_t
>(host_end);
1570 out.components.pathname_start =
static_cast<uint32_t
>(host_end);
1573 out.parse_path(path_view);
1574 apply_query_and_hash();
1576 std::string host_str(input.substr(host_start, host_len));
1578 unicode::to_lower_ascii(host_str.data(), host_str.size());
1580 out.host = std::move(host_str);
1581 out.parse_path(path_view);
1582 apply_query_and_hash();
1587 const bool need_slash = !has_path;
1588 if constexpr (is_aggregator) {
1591 out.buffer.assign(input);
1593 unicode::to_lower_ascii(out.buffer.data() + host_start, host_len);
1595 out.components.protocol_end = protocol_end;
1596 out.components.username_end = protocol_end + 2;
1597 out.components.host_start = protocol_end + 2;
1598 out.components.host_end =
static_cast<uint32_t
>(host_end);
1600 out.components.pathname_start =
static_cast<uint32_t
>(path_start);
1601 out.components.search_start = (query_start != std::string_view::npos)
1602 ?
static_cast<uint32_t
>(query_start)
1604 out.components.hash_start = (hash_start != std::string_view::npos)
1605 ?
static_cast<uint32_t
>(hash_start)
1609 out.buffer.reserve(len + 1);
1610 out.buffer.append(input.substr(0, host_end));
1611 out.buffer.push_back(
'/');
1612 if (host_end < len) {
1613 out.buffer.append(input.substr(host_end));
1616 unicode::to_lower_ascii(out.buffer.data() + host_start, host_len);
1618 out.components.protocol_end = protocol_end;
1619 out.components.username_end = protocol_end + 2;
1620 out.components.host_start = protocol_end + 2;
1621 out.components.host_end =
static_cast<uint32_t
>(host_end);
1623 out.components.pathname_start =
static_cast<uint32_t
>(host_end);
1624 out.components.search_start = (query_start != std::string_view::npos)
1625 ?
static_cast<uint32_t
>(query_start + 1)
1627 out.components.hash_start = (hash_start != std::string_view::npos)
1628 ?
static_cast<uint32_t
>(hash_start + 1)
1632 std::string host_str(input.substr(host_start, host_len));
1634 unicode::to_lower_ascii(host_str.data(), host_str.size());
1636 out.host = std::move(host_str);
1640 out.path.assign(input.data() + path_start, path_end - path_start);
1642 if (query_start != std::string_view::npos) {
1643 const size_t q_end =
1644 (hash_start != std::string_view::npos) ? hash_start : len;
1645 out.query.emplace(input.data() + query_start + 1,
1646 q_end - query_start - 1);
1648 if (hash_start != std::string_view::npos) {
1649 out.hash.emplace(input.data() + hash_start + 1, len - hash_start - 1);
1660 const result_type& base,
1662 constexpr bool is_ada_url = std::is_same_v<result_type, ada::url>;
1663 constexpr bool is_aggregator =
1664 std::is_same_v<result_type, ada::url_aggregator>;
1665 static_assert(is_ada_url || is_aggregator);
1668 base.has_opaque_path || input.empty()) {
1671 const auto* b =
reinterpret_cast<const uint8_t*
>(input.data());
1672 const size_t len = input.size();
1673 const uint8_t first = b[0];
1674 const bool path_relative = first !=
'/' && first !=
'?' && first !=
'#';
1675 if (first ==
'/' && len > 1 && (b[1] ==
'/' || b[1] ==
'\\')) {
1679 if (path_relative) {
1680 const size_t delim = input.find_first_of(
"/?#:");
1681 if (delim != std::string_view::npos && input[delim] ==
':') {
1687 size_t path_start = 0;
1688 size_t path_end = 0;
1689 size_t query_start = std::string_view::npos;
1690 size_t hash_start = std::string_view::npos;
1691 bool has_path =
false;
1692 bool maybe_dot_segment =
false;
1694 if (first ==
'/' || path_relative) {
1700 scan_path_run(b, i, len, maybe_dot_segment);
1702 const uint8_t cls = k_path[b[i]];
1708 if (i < len && b[i] ==
'?') {
1711 scan_query_run(b, i, len);
1718 scan_hash_run(b, i, len);
1723 }
else if (i < len && b[i] ==
'#') {
1726 scan_hash_run(b, i, len);
1730 }
else if (i < len) {
1733 }
else if (first ==
'?') {
1736 scan_query_run(b, i, len);
1743 scan_hash_run(b, i, len);
1751 scan_hash_run(b, i, len);
1757 if (has_path && maybe_dot_segment) {
1758 const std::string_view path_body(input.data() + path_start,
1759 path_end - path_start);
1760 if (path_has_dot_segment(path_body)) {
1766 out.is_valid =
true;
1767 out.has_opaque_path =
false;
1769 const size_t q_end =
1770 (hash_start != std::string_view::npos) ? hash_start : len;
1771 const bool has_query = query_start != std::string_view::npos;
1772 const bool has_hash = hash_start != std::string_view::npos;
1773 const std::string_view query =
1774 has_query ? std::string_view(input.data() + query_start + 1,
1775 q_end - query_start - 1)
1776 : std::string_view{};
1777 const std::string_view hash =
1778 has_hash ? std::string_view(input.data() + hash_start + 1,
1779 len - hash_start - 1)
1780 : std::string_view{};
1782 if constexpr (is_aggregator) {
1783 if (first ==
'/' || first ==
'?' || path_relative) {
1788 out.update_base_pathname(input.substr(path_start, path_end - path_start));
1789 }
else if (path_relative) {
1790 const std::string_view base_path = out.get_pathname();
1791 const size_t slash = base_path.rfind(
'/');
1792 const std::string_view prefix = (slash == std::string_view::npos)
1793 ? std::string_view(
"/")
1794 : base_path.substr(0, slash + 1);
1795 const std::string_view rel(input.data() + path_start,
1796 path_end - path_start);
1797 std::string new_path;
1798 new_path.reserve(prefix.size() + rel.size());
1799 new_path.assign(prefix);
1800 new_path.append(rel);
1801 out.update_base_pathname(new_path);
1806 out.components.search_start =
static_cast<uint32_t
>(out.buffer.size());
1808 out.buffer.append(query);
1811 out.update_unencoded_base_hash(hash);
1814 if (first ==
'/' || first ==
'?' || path_relative) {
1819 out.path.assign(input.substr(path_start, path_end - path_start));
1820 }
else if (path_relative) {
1822 const size_t slash = out.path.rfind(
'/');
1823 if (slash == std::string::npos) {
1826 out.path.resize(slash + 1);
1828 out.path.append(input.substr(path_start, path_end - path_start));
1831 out.query.emplace(query);
1834 out.hash.emplace(hash);
1842 const result_type* base_url) {
1848 constexpr bool result_type_is_ada_url = std::is_same_v<url, result_type>;
1849 constexpr bool result_type_is_ada_url_aggregator =
1850 std::is_same_v<url_aggregator, result_type>;
1851 static_assert(result_type_is_ada_url ||
1852 result_type_is_ada_url_aggregator);
1855 ada_log(
"ada::parser::parse_url('", user_input,
"' [", user_input.size(),
1856 " bytes],", (base_url !=
nullptr ? base_url->to_string() :
"null"),
1870 const auto enforce_max_input_length = [&]() {
1871 if constexpr (store_values) {
1873 if constexpr (result_type_is_ada_url_aggregator) {
1874 if (
url.buffer.size() > max_input_length) [[unlikely]] {
1889 if (user_input.size() > max_input_length) [[unlikely]] {
1895 if (base_url !=
nullptr) {
1903 if constexpr (store_values) {
1904 bool hit_fast_path =
false;
1905 if (base_url ==
nullptr) {
1908 const auto* p =
reinterpret_cast<const uint8_t*
>(user_input.data());
1909 const size_t n = user_input.size();
1910 size_t host_start = 0;
1911 if (n >= 8 && p[4] ==
':' && p[5] ==
'/' && p[6] ==
'/') {
1913 }
else if (n >= 9 && p[5] ==
':' && p[6] ==
'/' && p[7] ==
'/') {
1916 const uint8_t host_first = host_start != 0 ? p[host_start] : 0;
1917 const bool skip_ip =
1918 host_first ==
'[' || (host_first >=
'0' && host_first <=
'9');
1923 !(host_start != 0 && authority_has_at(p, host_start, n)) &&
1924 try_parse_simple_absolute(user_input,
url);
1926 hit_fast_path = try_parse_simple_relative(user_input, *base_url,
url);
1928 if (hit_fast_path) {
1929 if constexpr (result_type_is_ada_url_aggregator) {
1930 if (
url.buffer.size() > max_input_length) [[unlikely]] {
1938 if ((base_url !=
nullptr ||
1939 user_input.size() >
static_cast<size_t>(max_input_length) / 5) &&
1950 std::string tmp_buffer;
1951 std::string_view url_data;
1952 if (unicode::has_tabs_or_newline(user_input)) [[unlikely]] {
1953 tmp_buffer = user_input;
1956 helpers::remove_ascii_tab_or_newline(tmp_buffer);
1957 url_data = tmp_buffer;
1959 url_data = user_input;
1964 helpers::trim_c0_whitespace(url_data);
1966 if constexpr (result_type_is_ada_url_aggregator && store_values) {
1978 uint32_t reserve_capacity =
1980 helpers::leading_zeroes(uint32_t(1 | user_input.size()))) +
1982 url.reserve(reserve_capacity);
1986 std::optional<std::string_view> fragment = helpers::prune_hash(url_data);
1995 size_t input_position = 0;
1996 const size_t input_size = url_data.size();
2001 while (input_position <= input_size) {
2002 ada_log(
"In parsing at ", input_position,
" out of ", input_size,
2006 ada_log(
"SCHEME_START ", helpers::substring(url_data, input_position));
2009 if ((input_position != input_size) &&
2021 ada_log(
"SCHEME ", helpers::substring(url_data, input_position));
2024 while ((input_position != input_size) &&
2025 (unicode::is_alnum_plus(url_data[input_position]))) {
2029 if ((input_position != input_size) &&
2030 (url_data[input_position] ==
':')) {
2031 ada_log(
"SCHEME the scheme should be ",
2032 url_data.substr(0, input_position));
2033 if constexpr (result_type_is_ada_url) {
2034 if (!
url.parse_scheme(url_data.substr(0, input_position))) {
2039 if (!
url.parse_scheme_with_colon(
2040 url_data.substr(0, input_position + 1))) {
2056 base_url->type ==
url.type) {
2067 else if (input_position + 1 < input_size &&
2068 url_data[input_position + 1] ==
'/') {
2090 ada_log(
"NO_SCHEME ", helpers::substring(url_data, input_position));
2094 const bool c_is_hash =
2095 fragment.has_value() && input_position == input_size;
2098 if (base_url ==
nullptr || (base_url->has_opaque_path && !c_is_hash)) {
2099 ada_log(
"NO_SCHEME validation error");
2106 else if (base_url->has_opaque_path && c_is_hash) {
2107 ada_log(
"NO_SCHEME opaque base with fragment");
2108 url.copy_scheme(*base_url);
2111 if constexpr (result_type_is_ada_url) {
2112 url.path = base_url->path;
2113 url.query = base_url->query;
2115 url.update_base_pathname(base_url->get_pathname());
2116 if (base_url->has_search()) {
2120 auto s = base_url->get_search();
2121 url.update_base_search(s.empty() ? std::string_view(
"?") : s);
2124 url.update_unencoded_base_hash(*fragment);
2125 enforce_max_input_length();
2132 ada_log(
"NO_SCHEME non-file relative path");
2137 ada_log(
"NO_SCHEME file base type");
2143 ada_log(
"AUTHORITY ", helpers::substring(url_data, input_position));
2154 if (url_data.find(
'@', input_position) == std::string_view::npos) {
2158 bool at_sign_seen{
false};
2159 bool password_token_seen{
false};
2166 std::string_view view = url_data.substr(input_position);
2169 url.
is_special() ? helpers::find_authority_delimiter_special(view)
2170 : helpers::find_authority_delimiter(view);
2171 std::string_view authority_view = view.substr(0, location);
2172 size_t end_of_authority = input_position + authority_view.size();
2174 if ((end_of_authority != input_size) &&
2175 (url_data[end_of_authority] ==
'@')) {
2178 if (password_token_seen) {
2179 if constexpr (result_type_is_ada_url) {
2180 url.password +=
"%40";
2182 url.append_base_password(
"%40");
2185 if constexpr (result_type_is_ada_url) {
2186 url.username +=
"%40";
2188 url.append_base_username(
"%40");
2193 at_sign_seen =
true;
2195 if (!password_token_seen) {
2196 size_t password_token_location = authority_view.find(
':');
2197 password_token_seen =
2198 password_token_location != std::string_view::npos;
2200 if constexpr (store_values) {
2201 if (!password_token_seen) {
2202 if constexpr (result_type_is_ada_url) {
2203 url.username += unicode::percent_encode(
2207 url.append_base_username(unicode::percent_encode(
2212 if constexpr (result_type_is_ada_url) {
2213 url.username += unicode::percent_encode(
2214 authority_view.substr(0, password_token_location),
2216 url.password += unicode::percent_encode(
2217 authority_view.substr(password_token_location + 1),
2220 url.append_base_username(unicode::percent_encode(
2221 authority_view.substr(0, password_token_location),
2223 url.append_base_password(unicode::percent_encode(
2224 authority_view.substr(password_token_location + 1),
2229 }
else if constexpr (store_values) {
2230 if constexpr (result_type_is_ada_url) {
2231 url.password += unicode::percent_encode(
2234 url.append_base_password(unicode::percent_encode(
2242 else if (end_of_authority == input_size ||
2243 url_data[end_of_authority] ==
'/' ||
2244 url_data[end_of_authority] ==
'?' ||
2248 if (at_sign_seen && authority_view.empty()) {
2255 if (end_of_authority == input_size) {
2256 if constexpr (store_values) {
2257 if (fragment.has_value()) {
2258 url.update_unencoded_base_hash(*fragment);
2261 enforce_max_input_length();
2264 input_position = end_of_authority + 1;
2270 ada_log(
"SPECIAL_RELATIVE_OR_AUTHORITY ",
2271 helpers::substring(url_data, input_position));
2276 if (url_data.substr(input_position, 2) ==
"//") {
2278 input_position += 2;
2288 ada_log(
"PATH_OR_AUTHORITY ",
2289 helpers::substring(url_data, input_position));
2292 if ((input_position != input_size) &&
2293 (url_data[input_position] ==
'/')) {
2304 ada_log(
"RELATIVE_SCHEME ",
2305 helpers::substring(url_data, input_position));
2308 url.copy_scheme(*base_url);
2311 if ((input_position != input_size) &&
2313 (url_data[input_position] ==
'/')) {
2315 "RELATIVE_SCHEME if c is U+002F (/), then set state to relative "
2318 }
else if (
url.
is_special() && (input_position != input_size) &&
2319 (url_data[input_position] ==
'\\')) {
2323 "RELATIVE_SCHEME if url is special and c is U+005C, validation "
2324 "error, set state to relative slash state");
2327 ada_log(
"RELATIVE_SCHEME otherwise");
2332 if constexpr (result_type_is_ada_url) {
2333 url.username = base_url->username;
2334 url.password = base_url->password;
2335 url.host = base_url->host;
2336 url.port = base_url->port;
2339 url.path = base_url->path;
2340 url.query = base_url->query;
2342 url.update_base_authority(base_url->get_href(),
2343 base_url->get_components());
2344 url.update_host_to_base_host(base_url->get_hostname());
2345 url.update_base_port(base_url->retrieve_base_port());
2348 url.update_base_pathname(base_url->get_pathname());
2349 if (base_url->has_search()) {
2353 auto s = base_url->get_search();
2354 url.update_base_search(s.empty() ? std::string_view(
"?") : s);
2362 if ((input_position != input_size) &&
2363 (url_data[input_position] ==
'?')) {
2367 else if (input_position != input_size) {
2370 if constexpr (result_type_is_ada_url) {
2372 helpers::shorten_path(
url.path,
url.type);
2375 if (helpers::shorten_path(path,
url.type)) {
2376 url.update_base_pathname(std::move(std::string(path)));
2388 ada_log(
"RELATIVE_SLASH ",
2389 helpers::substring(url_data, input_position));
2394 (url_data[input_position] ==
'/' ||
2395 url_data[input_position] ==
'\\')) {
2400 else if ((input_position != input_size) &&
2401 (url_data[input_position] ==
'/')) {
2411 if constexpr (result_type_is_ada_url) {
2412 url.username = base_url->username;
2413 url.password = base_url->password;
2414 url.host = base_url->host;
2415 url.port = base_url->port;
2417 url.update_base_authority(base_url->get_href(),
2418 base_url->get_components());
2419 url.update_host_to_base_host(base_url->get_hostname());
2420 url.update_base_port(base_url->retrieve_base_port());
2430 ada_log(
"SPECIAL_AUTHORITY_SLASHES ",
2431 helpers::substring(url_data, input_position));
2436 if (url_data.substr(input_position, 2) ==
"//") {
2437 input_position += 2;
2443 ada_log(
"SPECIAL_AUTHORITY_IGNORE_SLASHES ",
2444 helpers::substring(url_data, input_position));
2448 while ((input_position != input_size) &&
2449 ((url_data[input_position] ==
'/') ||
2450 (url_data[input_position] ==
'\\'))) {
2458 ada_log(
"QUERY ", helpers::substring(url_data, input_position));
2459 if constexpr (store_values) {
2462 const uint8_t* query_percent_encode_set =
2468 url.update_base_search(url_data.substr(input_position),
2469 query_percent_encode_set);
2470 ada_log(
"QUERY update_base_search completed ");
2471 if (fragment.has_value()) {
2472 url.update_unencoded_base_hash(*fragment);
2475 enforce_max_input_length();
2479 ada_log(
"HOST ", helpers::substring(url_data, input_position));
2481 std::string_view host_view = url_data.substr(input_position);
2482 auto [location, found_colon] =
2483 helpers::get_host_delimiter_location(
url.
is_special(), host_view);
2484 input_position = (location != std::string_view::npos)
2485 ? input_position + location
2494 ada_log(
"HOST parsing ", host_view);
2495 if (!
url.parse_host(host_view)) {
2516 ada_log(
"HOST parsing ", host_view,
" href=",
url.
get_href());
2519 if (host_view.empty()) {
2520 url.update_base_hostname(
"");
2521 }
else if (!
url.parse_host(host_view)) {
2534 ada_log(
"OPAQUE_PATH ", helpers::substring(url_data, input_position));
2542 if constexpr (!store_values) {
2546 std::string_view view = url_data.substr(input_position);
2549 size_t location = view.find(
'?');
2550 if (location != std::string_view::npos) {
2551 view.remove_suffix(view.size() - location);
2553 input_position += location + 1;
2555 input_position = input_size + 1;
2561 if (view.ends_with(
' ')) {
2562 std::string modified_view =
2563 std::string(view.substr(0, view.size() - 1)) +
"%20";
2564 url.update_base_pathname(unicode::percent_encode(
2567 url.update_base_pathname(unicode::percent_encode(
2573 ada_log(
"PORT ", helpers::substring(url_data, input_position));
2574 std::string_view port_view = url_data.substr(input_position);
2575 input_position +=
url.parse_port(port_view,
true);
2583 ada_log(
"PATH_START ", helpers::substring(url_data, input_position));
2588 if constexpr (!store_values) {
2599 if (input_position == input_size) {
2600 if constexpr (store_values) {
2601 url.update_base_pathname(
"/");
2602 if (fragment.has_value()) {
2603 url.update_unencoded_base_hash(*fragment);
2606 enforce_max_input_length();
2612 if ((url_data[input_position] !=
'/') &&
2613 (url_data[input_position] !=
'\\')) {
2619 else if ((input_position != input_size) &&
2620 (url_data[input_position] ==
'?')) {
2624 else if (input_position != input_size) {
2629 if (url_data[input_position] !=
'/') {
2638 ada_log(
"PATH ", helpers::substring(url_data, input_position));
2643 if constexpr (!store_values) {
2646 std::string_view view = url_data.substr(input_position);
2650 size_t locofquestionmark = view.find(
'?');
2651 if (locofquestionmark != std::string_view::npos) {
2653 view.remove_suffix(view.size() - locofquestionmark);
2654 input_position += locofquestionmark + 1;
2656 input_position = input_size + 1;
2658 if constexpr (store_values) {
2659 if constexpr (result_type_is_ada_url) {
2660 helpers::parse_prepared_path(view,
url.type,
url.path);
2662 url.consume_prepared_path(view);
2669 ada_log(
"FILE_SLASH ", helpers::substring(url_data, input_position));
2672 if ((input_position != input_size) &&
2673 (url_data[input_position] ==
'/' ||
2674 url_data[input_position] ==
'\\')) {
2675 ada_log(
"FILE_SLASH c is U+002F or U+005C");
2680 ada_log(
"FILE_SLASH otherwise");
2686 if constexpr (result_type_is_ada_url) {
2687 url.host = base_url->host;
2689 url.update_host_to_base_host(base_url->get_host());
2695 if (!base_url->get_pathname().empty()) {
2697 url_data.substr(input_position))) {
2698 std::string_view first_base_url_path =
2699 base_url->get_pathname().substr(1);
2700 size_t loc = first_base_url_path.find(
'/');
2701 if (loc != std::string_view::npos) {
2702 helpers::resize(first_base_url_path, loc);
2705 first_base_url_path)) {
2706 if constexpr (result_type_is_ada_url) {
2708 url.path += first_base_url_path;
2710 url.append_base_pathname(
2711 helpers::concat(
"/", first_base_url_path));
2725 ada_log(
"FILE_HOST ", helpers::substring(url_data, input_position));
2726 std::string_view view = url_data.substr(input_position);
2728 size_t location = view.find_first_of(
"/\\?");
2729 std::string_view file_host_buffer = view.substr(
2730 0, (location != std::string_view::npos) ? location : view.size());
2734 }
else if (file_host_buffer.empty()) {
2736 if constexpr (result_type_is_ada_url) {
2739 url.update_base_hostname(
"");
2744 size_t consumed_bytes = file_host_buffer.size();
2745 input_position += consumed_bytes;
2748 if (!
url.parse_host(file_host_buffer)) {
2752 if constexpr (result_type_is_ada_url) {
2754 if (
url.host.has_value() &&
url.host.value() ==
"localhost") {
2759 url.update_base_hostname(
"");
2770 ada_log(
"FILE ", helpers::substring(url_data, input_position));
2771 std::string_view file_view = url_data.substr(input_position);
2773 url.set_protocol_as_file();
2774 if constexpr (result_type_is_ada_url) {
2778 url.update_base_hostname(
"");
2781 if (input_position != input_size &&
2782 (url_data[input_position] ==
'/' ||
2783 url_data[input_position] ==
'\\')) {
2784 ada_log(
"FILE c is U+002F or U+005C");
2792 ada_log(
"FILE base non-null");
2793 if constexpr (result_type_is_ada_url) {
2794 url.host = base_url->host;
2795 url.path = base_url->path;
2796 url.query = base_url->query;
2798 url.update_host_to_base_host(base_url->get_hostname());
2799 url.update_base_pathname(base_url->get_pathname());
2800 if (base_url->has_search()) {
2804 auto s = base_url->get_search();
2805 url.update_base_search(s.empty() ? std::string_view(
"?") : s);
2812 if (input_position != input_size && url_data[input_position] ==
'?') {
2816 else if (input_position != input_size) {
2822 if constexpr (result_type_is_ada_url) {
2823 helpers::shorten_path(
url.path,
url.type);
2826 if (helpers::shorten_path(path,
url.type)) {
2827 url.update_base_pathname(std::move(std::string(path)));
2834 url.clear_pathname();
2844 ada_log(
"FILE go to path");
2856 if constexpr (store_values) {
2857 if (fragment.has_value()) {
2858 url.update_unencoded_base_hash(*fragment);
2861 enforce_max_input_length();