11template <
typename out_iter>
12void encode_json(std::string_view view, out_iter out) {
14 const char* hexvalues =
15 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
16 for (uint8_t c : view) {
20 }
else if (c ==
'"') {
23 }
else if (c <= 0x1f) {
28 *out++ = hexvalues[2 * c];
29 *out++ = hexvalues[2 * c + 1];
41 return "Scheme Start";
51 return "Relative Scheme";
53 return "Relative Slash";
61 return "Path or Authority";
63 return "Special Authority Ignore Slashes";
65 return "Special Authority Slashes";
67 return "Special Relative or Authority";
79 return "unknown state";
84 std::string_view& input)
noexcept {
87 size_t location_of_first = input.find(
'#');
88 if (location_of_first == std::string_view::npos) {
91 std::string_view hash = input;
92 hash.remove_prefix(location_of_first + 1);
93 input.remove_suffix(input.size() - location_of_first);
103 path.find(
'/', 1) == std::string_view::npos && !path.empty()) {
105 helpers::substring(path, 1))) {
111 size_t last_delimiter = path.rfind(
'/');
112 if (last_delimiter != std::string::npos) {
113 path.erase(last_delimiter);
126 path.find(
'/', 1) == std::string_view::npos && !path.empty()) {
128 helpers::substring(path, 1))) {
135 size_t slash_loc = path.rfind(
'/');
136 if (slash_loc != std::string_view::npos) {
137 path.remove_suffix(path.size() - slash_loc);
146 std::string& input)
noexcept {
149 std::erase_if(input, ada::unicode::is_ascii_tab_or_newline);
153 size_t pos)
noexcept {
157 return input.substr(pos);
162 input.remove_suffix(input.size() - pos);
168#ifdef ADA_REGULAR_VISUAL_STUDIO
172 _BitScanForward(&ret, input_num);
175 return __builtin_ctzl(input_num);
186#ifndef ada_make_uint8x16_t
187#define ada_make_uint8x16_t(x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, \
188 x13, x14, x15, x16) \
190 static uint8_t array[16] = {x1, x2, x3, x4, x5, x6, x7, x8, \
191 x9, x10, x11, x12, x13, x14, x15, x16}; \
192 return vld1q_u8(array); \
197 std::string_view view,
size_t location)
noexcept {
199 if (view.size() - location < 16) {
200 for (
size_t i = location; i < view.size(); i++) {
201 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'\\' ||
202 view[i] ==
'?' || view[i] ==
'[') {
206 return size_t(view.size());
208 auto to_bitmask = [](uint8x16_t input) -> uint16_t {
209 uint8x16_t bit_mask =
210 ada_make_uint8x16_t(0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x01,
211 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80);
212 uint8x16_t minput = vandq_u8(input, bit_mask);
213 uint8x16_t tmp = vpaddq_u8(minput, minput);
214 tmp = vpaddq_u8(tmp, tmp);
215 tmp = vpaddq_u8(tmp, tmp);
216 return vgetq_lane_u16(vreinterpretq_u16_u8(tmp), 0);
221 uint8x16_t low_mask =
222 ada_make_uint8x16_t(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
223 0x00, 0x01, 0x04, 0x04, 0x00, 0x00, 0x03);
224 uint8x16_t high_mask =
225 ada_make_uint8x16_t(0x00, 0x00, 0x02, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
226 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
227 uint8x16_t fmask = vmovq_n_u8(0xf);
229 for (; i + 15 < view.size(); i += 16) {
230 uint8x16_t word = vld1q_u8((
const uint8_t*)view.data() + i);
231 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
232 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
233 uint8x16_t classify = vandq_u8(lowpart, highpart);
234 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
235 uint8x16_t is_zero = vceqq_u8(classify, zero);
236 uint16_t is_non_zero = ~to_bitmask(is_zero);
241 if (i < view.size()) {
243 vld1q_u8((
const uint8_t*)view.data() + view.length() - 16);
244 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
245 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
246 uint8x16_t classify = vandq_u8(lowpart, highpart);
247 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
248 uint8x16_t is_zero = vceqq_u8(classify, zero);
249 uint16_t is_non_zero = ~to_bitmask(is_zero);
253 return size_t(view.size());
257 std::string_view view,
size_t location)
noexcept {
259 if (view.size() - location < 16) {
260 for (
size_t i = location; i < view.size(); i++) {
261 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'\\' ||
262 view[i] ==
'?' || view[i] ==
'[') {
266 return size_t(view.size());
270 const __m128i mask1 = _mm_set1_epi8(
':');
271 const __m128i mask2 = _mm_set1_epi8(
'/');
272 const __m128i mask3 = _mm_set1_epi8(
'\\');
273 const __m128i mask4 = _mm_set1_epi8(
'?');
274 const __m128i mask5 = _mm_set1_epi8(
'[');
276 for (; i + 15 < view.size(); i += 16) {
277 __m128i word = _mm_loadu_si128((
const __m128i*)(view.data() + i));
278 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
279 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
280 __m128i m3 = _mm_cmpeq_epi8(word, mask3);
281 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
282 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
283 __m128i m = _mm_or_si128(
284 _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m3, m4)), m5);
285 int mask = _mm_movemask_epi8(m);
290 if (i < view.size()) {
292 _mm_loadu_si128((
const __m128i*)(view.data() + view.length() - 16));
293 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
294 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
295 __m128i m3 = _mm_cmpeq_epi8(word, mask3);
296 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
297 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
298 __m128i m = _mm_or_si128(
299 _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m3, m4)), m5);
300 int mask = _mm_movemask_epi8(m);
305 return size_t(view.length());
311 std::array<uint8_t, 256>
result{};
312 for (
int i : {
':',
'/',
'[',
'\\',
'?'}) {
319 std::string_view view,
size_t location)
noexcept {
320 auto const str = view.substr(location);
321 for (
auto pos = str.begin(); pos != str.end(); ++pos) {
323 return pos - str.begin() + location;
326 return size_t(view.size());
335 size_t location)
noexcept {
337 if (view.size() - location < 16) {
338 for (
size_t i = location; i < view.size(); i++) {
339 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'?' ||
344 return size_t(view.size());
346 auto to_bitmask = [](uint8x16_t input) -> uint16_t {
347 uint8x16_t bit_mask =
348 ada_make_uint8x16_t(0x01, 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80, 0x01,
349 0x02, 0x4, 0x8, 0x10, 0x20, 0x40, 0x80);
350 uint8x16_t minput = vandq_u8(input, bit_mask);
351 uint8x16_t tmp = vpaddq_u8(minput, minput);
352 tmp = vpaddq_u8(tmp, tmp);
353 tmp = vpaddq_u8(tmp, tmp);
354 return vgetq_lane_u16(vreinterpretq_u16_u8(tmp), 0);
359 uint8x16_t low_mask =
360 ada_make_uint8x16_t(0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
361 0x00, 0x01, 0x04, 0x00, 0x00, 0x00, 0x03);
362 uint8x16_t high_mask =
363 ada_make_uint8x16_t(0x00, 0x00, 0x02, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
364 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00);
365 uint8x16_t fmask = vmovq_n_u8(0xf);
367 for (; i + 15 < view.size(); i += 16) {
368 uint8x16_t word = vld1q_u8((
const uint8_t*)view.data() + i);
369 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
370 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
371 uint8x16_t classify = vandq_u8(lowpart, highpart);
372 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
373 uint8x16_t is_zero = vceqq_u8(classify, zero);
374 uint16_t is_non_zero = ~to_bitmask(is_zero);
379 if (i < view.size()) {
381 vld1q_u8((
const uint8_t*)view.data() + view.length() - 16);
382 uint8x16_t lowpart = vqtbl1q_u8(low_mask, vandq_u8(word, fmask));
383 uint8x16_t highpart = vqtbl1q_u8(high_mask, vshrq_n_u8(word, 4));
384 uint8x16_t classify = vandq_u8(lowpart, highpart);
385 if (vmaxvq_u32(vreinterpretq_u32_u8(classify)) != 0) {
386 uint8x16_t is_zero = vceqq_u8(classify, zero);
387 uint16_t is_non_zero = ~to_bitmask(is_zero);
391 return size_t(view.size());
395 size_t location)
noexcept {
397 if (view.size() - location < 16) {
398 for (
size_t i = location; i < view.size(); i++) {
399 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'?' ||
404 return size_t(view.size());
408 const __m128i mask1 = _mm_set1_epi8(
':');
409 const __m128i mask2 = _mm_set1_epi8(
'/');
410 const __m128i mask4 = _mm_set1_epi8(
'?');
411 const __m128i mask5 = _mm_set1_epi8(
'[');
413 for (; i + 15 < view.size(); i += 16) {
414 __m128i word = _mm_loadu_si128((
const __m128i*)(view.data() + i));
415 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
416 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
417 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
418 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
419 __m128i m = _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m4, m5));
420 int mask = _mm_movemask_epi8(m);
425 if (i < view.size()) {
427 _mm_loadu_si128((
const __m128i*)(view.data() + view.length() - 16));
428 __m128i m1 = _mm_cmpeq_epi8(word, mask1);
429 __m128i m2 = _mm_cmpeq_epi8(word, mask2);
430 __m128i m4 = _mm_cmpeq_epi8(word, mask4);
431 __m128i m5 = _mm_cmpeq_epi8(word, mask5);
432 __m128i m = _mm_or_si128(_mm_or_si128(m1, m2), _mm_or_si128(m4, m5));
433 int mask = _mm_movemask_epi8(m);
438 return size_t(view.length());
443 std::array<uint8_t, 256>
result{};
444 for (
int i : {
':',
'/',
'?',
'['}) {
451 size_t location)
noexcept {
452 auto const str = view.substr(location);
453 for (
auto pos = str.begin(); pos != str.end(); ++pos) {
455 return pos - str.begin() + location;
458 return size_t(view.size());
463 const bool is_special, std::string_view& view)
noexcept {
472 const size_t view_size = view.size();
474 bool found_colon =
false;
499 for (; location < view_size;
501 if (view[location] ==
'[') {
502 location = view.find(
']', location);
503 if (location == std::string_view::npos) {
507 location = view_size;
511 found_colon = view[location] ==
':';
520 for (; location < view_size;
522 if (view[location] ==
'[') {
523 location = view.find(
']', location);
524 if (location == std::string_view::npos) {
528 location = view_size;
532 found_colon = view[location] ==
':';
538 view.remove_suffix(view_size - location);
539 return {location, found_colon};
543 while (!input.empty() &&
544 ada::unicode::is_c0_control_or_space(input.front())) {
545 input.remove_prefix(1);
547 while (!input.empty() && ada::unicode::is_c0_control_or_space(input.back())) {
548 input.remove_suffix(1);
555 ada_log(
"parse_prepared_path ", input);
556 uint8_t accumulator = checkers::path_signature(input);
561 constexpr uint8_t need_encoding = 1;
562 constexpr uint8_t backslash_char = 2;
563 constexpr uint8_t dot_char = 4;
564 constexpr uint8_t percent_char = 8;
569 (special ? (accumulator == 0)
570 : ((accumulator & (need_encoding | dot_char | percent_char)) ==
572 (!may_need_slow_file_handling);
573 if (accumulator == dot_char && !may_need_slow_file_handling) {
581 if (input[0] !=
'.') {
582 size_t slashdot = input.find(
"/.");
583 if (slashdot == std::string_view::npos) {
588 !(slashdot + 2 == input.size() || input[slashdot + 2] ==
'.' ||
589 input[slashdot + 2] ==
'/');
594 ada_log(
"parse_path trivial");
605 (accumulator & (need_encoding | backslash_char | percent_char)) == 0) &&
608 ada_log(
"parse_prepared_path fast");
613 size_t previous_location = 0;
615 size_t new_location = input.find(
'/', previous_location);
618 if (new_location == std::string_view::npos) {
619 std::string_view path_view = input.substr(previous_location);
620 if (path_view ==
"..") {
627 if (path.back() ==
'/') {
632 path.resize(path.rfind(
'/') + 1);
636 if (path_view !=
".") {
637 path.append(path_view);
642 std::string_view path_view =
643 input.substr(previous_location, new_location - previous_location);
644 previous_location = new_location + 1;
645 if (path_view ==
"..") {
646 size_t last_delimiter = path.rfind(
'/');
647 if (last_delimiter != std::string::npos) {
648 path.erase(last_delimiter);
650 }
else if (path_view !=
".") {
652 path.append(path_view);
657 ada_log(
"parse_path slow");
659 bool needs_percent_encoding = (accumulator & 1);
660 std::string path_buffer_tmp;
662 size_t location = (special && (accumulator & 2))
663 ? input.find_first_of(
"/\\")
665 std::string_view path_view = input;
666 if (location != std::string_view::npos) {
667 path_view.remove_suffix(path_view.size() - location);
668 input.remove_prefix(location + 1);
672 std::string_view path_buffer =
673 (needs_percent_encoding &&
674 ada::unicode::percent_encode<false>(
678 if (unicode::is_double_dot_path_segment(path_buffer)) {
679 if ((helpers::shorten_path(path, type) || special) &&
680 location == std::string_view::npos) {
683 }
else if (unicode::is_single_dot_path_segment(path_buffer) &&
684 (location == std::string_view::npos)) {
688 else if (!unicode::is_single_dot_path_segment(path_buffer)) {
695 path += path_buffer[0];
697 path_buffer.remove_prefix(2);
698 path.append(path_buffer);
702 path.append(path_buffer);
705 if (location == std::string_view::npos) {
712bool overlaps(std::string_view input1,
const std::string& input2)
noexcept {
713 ada_log(
"helpers::overlaps check if string_view '", input1,
"' [",
714 input1.size(),
" bytes] is part of string '", input2,
"' [",
715 input2.size(),
" bytes]");
716 return !input1.empty() && !input2.empty() && input1.data() >= input2.data() &&
717 input1.data() < input2.data() + input2.size();
720template <
class url_type>
722 url_type& url)
noexcept {
723 ada_log(
"helpers::strip_trailing_spaces_from_opaque_path");
724 if (!url.has_opaque_path)
return;
725 if (url.has_hash())
return;
726 if (url.has_search())
return;
728 auto path = std::string(url.get_pathname());
729 while (!path.empty() && path.back() ==
' ') {
730 path.resize(path.size() - 1);
732 url.update_base_pathname(path);
738 std::array<uint8_t, 256>
result{};
739 for (uint8_t i : {
'@',
'/',
'\\',
'?'}) {
746find_authority_delimiter_special(std::string_view view)
noexcept {
749 for (
auto pos = view.begin(); pos != view.end(); ++pos) {
751 return pos - view.begin();
754 return size_t(view.size());
759 std::array<uint8_t, 256>
result{};
760 for (uint8_t i : {
'@',
'/',
'?'}) {
767find_authority_delimiter(std::string_view view)
noexcept {
770 for (
auto pos = view.begin(); pos != view.end(); ++pos) {
772 return pos - view.begin();
775 return size_t(view.size());
784#undef ada_make_uint8x16_t
Includes all definitions for Ada.
Definitions for URL specific checkers used within Ada.
Common definitions for cross-platform compiler support.
#define ADA_ASSERT_TRUE(COND)
#define ada_really_inline
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
static constexpr std::array< uint8_t, 256 > authority_delimiter_special
static constexpr std::array< uint8_t, 256 > host_delimiters
ada_really_inline size_t find_next_host_delimiter_special(std::string_view view, size_t location) noexcept
ada_unused std::string get_state(ada::state s)
static constexpr std::array< uint8_t, 256 > authority_delimiter
static constexpr std::array< uint8_t, 256 > special_host_delimiters
ada_really_inline int trailing_zeroes(uint32_t input_num) noexcept
ada_warn_unused std::string to_string(encoding_type type)
@ SPECIAL_RELATIVE_OR_AUTHORITY
@ SPECIAL_AUTHORITY_SLASHES
@ SPECIAL_AUTHORITY_IGNORE_SLASHES
tl::expected< result_type, ada::errors > result
Declarations for the URL scheme.