10template <
typename out_iter>
11void encode_json(std::string_view view, out_iter out) {
13 const char* hexvalues =
14 "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
15 for (uint8_t c : view) {
19 }
else if (c ==
'"') {
22 }
else if (c <= 0x1f) {
27 *out++ = hexvalues[2 * c];
28 *out++ = hexvalues[2 * c + 1];
40 return "Scheme Start";
50 return "Relative Scheme";
52 return "Relative Slash";
60 return "Path or Authority";
62 return "Special Authority Ignore Slashes";
64 return "Special Authority Slashes";
66 return "Special Relative or Authority";
78 return "unknown state";
83 std::string_view& input)
noexcept {
86 size_t location_of_first = input.find(
'#');
87 if (location_of_first == std::string_view::npos) {
90 std::string_view hash = input;
91 hash.remove_prefix(location_of_first + 1);
92 input.remove_suffix(input.size() - location_of_first);
102 path.find(
'/', 1) == std::string_view::npos && !path.empty()) {
104 helpers::substring(path, 1))) {
110 size_t last_delimiter = path.rfind(
'/');
111 if (last_delimiter != std::string::npos) {
112 path.erase(last_delimiter);
125 path.find(
'/', 1) == std::string_view::npos && !path.empty()) {
127 helpers::substring(path, 1))) {
134 size_t slash_loc = path.rfind(
'/');
135 if (slash_loc != std::string_view::npos) {
136 path.remove_suffix(path.size() - slash_loc);
145 std::string& input)
noexcept {
148 std::erase_if(input, ada::unicode::is_ascii_tab_or_newline);
152 size_t pos)
noexcept {
156 return input.substr(pos);
161 input.remove_suffix(input.size() - pos);
167#ifdef ADA_REGULAR_VISUAL_STUDIO
171 _BitScanForward(&ret, input_num);
174 return __builtin_ctzl(input_num);
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) \
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); \
196 std::string_view view,
size_t location)
noexcept {
198 if (view.size() - location < 16) {
199 for (
size_t i = location; i < view.size(); i++) {
200 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'\\' ||
201 view[i] ==
'?' || view[i] ==
'[') {
205 return size_t(view.size());
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);
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);
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);
240 if (i < view.size()) {
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);
252 return size_t(view.size());
256 std::string_view view,
size_t location)
noexcept {
258 if (view.size() - location < 16) {
259 for (
size_t i = location; i < view.size(); i++) {
260 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'\\' ||
261 view[i] ==
'?' || view[i] ==
'[') {
265 return size_t(view.size());
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(
'[');
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);
289 if (i < view.size()) {
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);
304 return size_t(view.length());
310 std::array<uint8_t, 256>
result{};
311 for (
int i : {
':',
'/',
'[',
'\\',
'?'}) {
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) {
322 return pos - str.begin() + location;
325 return size_t(view.size());
334 size_t location)
noexcept {
336 if (view.size() - location < 16) {
337 for (
size_t i = location; i < view.size(); i++) {
338 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'?' ||
343 return size_t(view.size());
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);
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);
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);
378 if (i < view.size()) {
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);
390 return size_t(view.size());
394 size_t location)
noexcept {
396 if (view.size() - location < 16) {
397 for (
size_t i = location; i < view.size(); i++) {
398 if (view[i] ==
':' || view[i] ==
'/' || view[i] ==
'?' ||
403 return size_t(view.size());
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(
'[');
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);
424 if (i < view.size()) {
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);
437 return size_t(view.length());
442 std::array<uint8_t, 256>
result{};
443 for (
int i : {
':',
'/',
'?',
'['}) {
450 size_t location)
noexcept {
451 auto const str = view.substr(location);
452 for (
auto pos = str.begin(); pos != str.end(); ++pos) {
454 return pos - str.begin() + location;
457 return size_t(view.size());
462 const bool is_special, std::string_view& view)
noexcept {
471 const size_t view_size = view.size();
473 bool found_colon =
false;
498 for (; location < view_size;
500 if (view[location] ==
'[') {
501 location = view.find(
']', location);
502 if (location == std::string_view::npos) {
506 location = view_size;
510 found_colon = view[location] ==
':';
519 for (; location < view_size;
521 if (view[location] ==
'[') {
522 location = view.find(
']', location);
523 if (location == std::string_view::npos) {
527 location = view_size;
531 found_colon = view[location] ==
':';
537 view.remove_suffix(view_size - location);
538 return {location, found_colon};
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);
546 while (!input.empty() && ada::unicode::is_c0_control_or_space(input.back())) {
547 input.remove_suffix(1);
554 ada_log(
"parse_prepared_path ", input);
555 uint8_t accumulator = checkers::path_signature(input);
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;
568 (special ? (accumulator == 0)
569 : ((accumulator & (need_encoding | dot_char | percent_char)) ==
571 (!may_need_slow_file_handling);
572 if (accumulator == dot_char && !may_need_slow_file_handling) {
580 if (input[0] !=
'.') {
581 size_t slashdot = input.find(
"/.");
582 if (slashdot == std::string_view::npos) {
587 !(slashdot + 2 == input.size() || input[slashdot + 2] ==
'.' ||
588 input[slashdot + 2] ==
'/');
593 ada_log(
"parse_path trivial");
604 (accumulator & (need_encoding | backslash_char | percent_char)) == 0) &&
607 ada_log(
"parse_prepared_path fast");
612 size_t previous_location = 0;
614 size_t new_location = input.find(
'/', previous_location);
617 if (new_location == std::string_view::npos) {
618 std::string_view path_view = input.substr(previous_location);
619 if (path_view ==
"..") {
626 if (path.back() ==
'/') {
631 path.resize(path.rfind(
'/') + 1);
635 if (path_view !=
".") {
636 path.append(path_view);
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);
649 }
else if (path_view !=
".") {
651 path.append(path_view);
656 ada_log(
"parse_path slow");
658 bool needs_percent_encoding = (accumulator & 1);
659 std::string path_buffer_tmp;
661 size_t location = (special && (accumulator & 2))
662 ? input.find_first_of(
"/\\")
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);
671 std::string_view path_buffer =
672 (needs_percent_encoding &&
673 ada::unicode::percent_encode<false>(
677 if (unicode::is_double_dot_path_segment(path_buffer)) {
678 if ((helpers::shorten_path(path, type) || special) &&
679 location == std::string_view::npos) {
682 }
else if (unicode::is_single_dot_path_segment(path_buffer) &&
683 (location == std::string_view::npos)) {
687 else if (!unicode::is_single_dot_path_segment(path_buffer)) {
694 path += path_buffer[0];
696 path_buffer.remove_prefix(2);
697 path.append(path_buffer);
701 path.append(path_buffer);
704 if (location == std::string_view::npos) {
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();
719template <
class url_type>
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;
727 auto path = std::string(url.get_pathname());
728 while (!path.empty() && path.back() ==
' ') {
729 path.resize(path.size() - 1);
731 url.update_base_pathname(path);
737 std::array<uint8_t, 256>
result{};
738 for (uint8_t i : {
'@',
'/',
'\\',
'?'}) {
745find_authority_delimiter_special(std::string_view view)
noexcept {
748 for (
auto pos = view.begin(); pos != view.end(); ++pos) {
750 return pos - view.begin();
753 return size_t(view.size());
758 std::array<uint8_t, 256>
result{};
759 for (uint8_t i : {
'@',
'/',
'?'}) {
766find_authority_delimiter(std::string_view view)
noexcept {
769 for (
auto pos = view.begin(); pos != view.end(); ++pos) {
771 return pos - view.begin();
774 return size_t(view.size());
783#undef ada_make_uint8x16_t
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.