forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathada.cpp
More file actions
2760 lines (2479 loc) · 112 KB
/
Copy pathada.cpp
File metadata and controls
2760 lines (2479 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* auto-generated on 2023-02-26 15:07:41 -0500. Do not edit! */
// dofile: invoked with prepath=/Users/dlemire/CVS/github/ada/src, filename=ada.cpp
/* begin file src/ada.cpp */
#include "ada.h"
// dofile: invoked with prepath=/Users/dlemire/CVS/github/ada/src, filename=checkers.cpp
/* begin file src/checkers.cpp */
#include <algorithm>
namespace ada::checkers {
ada_really_inline ada_constexpr bool is_ipv4(std::string_view view) noexcept {
size_t last_dot = view.rfind('.');
if(last_dot == view.size() - 1) {
view.remove_suffix(1);
last_dot = view.rfind('.');
}
std::string_view number = (last_dot == std::string_view::npos) ? view : view.substr(last_dot+1);
if(number.empty()) { return false; }
/** Optimization opportunity: we have basically identified the last number of the
ipv4 if we return true here. We might as well parse it and have at least one
number parsed when we get to parse_ipv4. */
if(std::all_of(number.begin(), number.end(), ada::checkers::is_digit)) { return true; }
return (checkers::has_hex_prefix(number) && std::all_of(number.begin()+2, number.end(), ada::unicode::is_lowercase_hex));
}
// for use with path_signature, we include all characters that need percent encoding.
static constexpr uint8_t path_signature_table[256] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 0, 8, 0, 0, 0, 0, 0, 0, 0, 0, 4, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
static_assert(path_signature_table[uint8_t('?')] == 1);
static_assert(path_signature_table[uint8_t('`')] == 1);
static_assert(path_signature_table[uint8_t('{')] == 1);
static_assert(path_signature_table[uint8_t('}')] == 1);
//
static_assert(path_signature_table[uint8_t(' ')] == 1);
static_assert(path_signature_table[uint8_t('?')] == 1);
static_assert(path_signature_table[uint8_t('"')] == 1);
static_assert(path_signature_table[uint8_t('#')] == 1);
static_assert(path_signature_table[uint8_t('<')] == 1);
static_assert(path_signature_table[uint8_t('>')] == 1);
//
static_assert(path_signature_table[0] == 1);
static_assert(path_signature_table[31] == 1);
static_assert(path_signature_table[127] == 1);
static_assert(path_signature_table[128] == 1);
static_assert(path_signature_table[255] == 1);
ada_really_inline constexpr uint8_t path_signature(std::string_view input) noexcept {
// The path percent-encode set is the query percent-encode set and U+003F (?), U+0060 (`), U+007B ({), and U+007D (}).
// The query percent-encode set is the C0 control percent-encode set and U+0020 SPACE, U+0022 ("), U+0023 (#), U+003C (<), and U+003E (>).
// The C0 control percent-encode set are the C0 controls and all code points greater than U+007E (~).
size_t i = 0;
uint8_t accumulator{};
for (; i + 7 < input.size(); i += 8) {
accumulator |= uint8_t(path_signature_table[uint8_t(input[i])] |
path_signature_table[uint8_t(input[i + 1])] |
path_signature_table[uint8_t(input[i + 2])] |
path_signature_table[uint8_t(input[i + 3])] |
path_signature_table[uint8_t(input[i + 4])] |
path_signature_table[uint8_t(input[i + 5])] |
path_signature_table[uint8_t(input[i + 6])] |
path_signature_table[uint8_t(input[i + 7])]);
}
for (; i < input.size(); i++) {
accumulator |= uint8_t(path_signature_table[uint8_t(input[i])]);
}
return accumulator;
}
ada_really_inline constexpr bool verify_dns_length(std::string_view input) noexcept {
if(input.back() == '.') {
if(input.size() > 254) return false;
} else if (input.size() > 253) return false;
size_t start = 0;
while (start < input.size()) {
auto dot_location = input.find('.', start);
// If not found, it's likely the end of the domain
if(dot_location == std::string_view::npos) dot_location = input.size();
auto label_size = dot_location - start;
if (label_size > 63 || label_size == 0) return false;
start = dot_location + 1;
}
return true;
}
} // namespace ada::checkers
/* end file src/checkers.cpp */
// dofile: invoked with prepath=/Users/dlemire/CVS/github/ada/src, filename=unicode.cpp
/* begin file src/unicode.cpp */
#include <algorithm>
#if ADA_HAS_ICU
// We are good.
#else
#if defined(_WIN32) && ADA_WINDOWS_TO_ASCII_FALLBACK
#ifndef __wtypes_h__
#include <wtypes.h>
#endif // __wtypes_h__
#ifndef __WINDEF_
#include <windef.h>
#endif // __WINDEF_
#include <winnls.h>
#endif //defined(_WIN32) && ADA_WINDOWS_TO_ASCII_FALLBACK
#endif // ADA_HAS_ICU
namespace ada::unicode {
constexpr bool to_lower_ascii(char * input, size_t length) noexcept {
auto broadcast = [](uint8_t v) -> uint64_t { return 0x101010101010101 * v; };
uint64_t broadcast_80 = broadcast(0x80);
uint64_t broadcast_Ap = broadcast(128 - 'A');
uint64_t broadcast_Zp = broadcast(128 - 'Z');
uint64_t non_ascii = 0;
size_t i = 0;
for (; i + 7 < length; i += 8) {
uint64_t word{};
memcpy(&word, input + i, sizeof(word));
non_ascii |= (word & broadcast_80);
word ^= (((word+broadcast_Ap)^(word+broadcast_Zp))&broadcast_80)>>2;
memcpy(input + i, &word, sizeof(word));
}
if (i < length) {
uint64_t word{};
memcpy(&word, input + i, length - i);
non_ascii |= (word & broadcast_80);
word ^= (((word+broadcast_Ap)^(word+broadcast_Zp))&broadcast_80)>>2;
memcpy(input + i, &word, length - i);
}
return non_ascii == 0;
}
ada_really_inline constexpr bool has_tabs_or_newline(std::string_view user_input) noexcept {
auto has_zero_byte = [](uint64_t v) {
return ((v - 0x0101010101010101) & ~(v)&0x8080808080808080);
};
auto broadcast = [](uint8_t v) -> uint64_t { return 0x101010101010101 * v; };
size_t i = 0;
uint64_t mask1 = broadcast('\r');
uint64_t mask2 = broadcast('\n');
uint64_t mask3 = broadcast('\t');
uint64_t running{0};
for (; i + 7 < user_input.size(); i += 8) {
uint64_t word{};
memcpy(&word, user_input.data() + i, sizeof(word));
uint64_t xor1 = word ^ mask1;
uint64_t xor2 = word ^ mask2;
uint64_t xor3 = word ^ mask3;
running |= has_zero_byte(xor1) | has_zero_byte(xor2) | has_zero_byte(xor3);
}
if (i < user_input.size()) {
uint64_t word{};
memcpy(&word, user_input.data() + i, user_input.size() - i);
uint64_t xor1 = word ^ mask1;
uint64_t xor2 = word ^ mask2;
uint64_t xor3 = word ^ mask3;
running |= has_zero_byte(xor1) | has_zero_byte(xor2) | has_zero_byte(xor3);
}
return running;
}
// A forbidden host code point is U+0000 NULL, U+0009 TAB, U+000A LF, U+000D CR, U+0020 SPACE, U+0023 (#),
// U+002F (/), U+003A (:), U+003C (<), U+003E (>), U+003F (?), U+0040 (@), U+005B ([), U+005C (\), U+005D (]),
// U+005E (^), or U+007C (|).
constexpr static bool is_forbidden_host_code_point_table[] = {
1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static_assert(sizeof(is_forbidden_host_code_point_table) == 256);
ada_really_inline constexpr bool is_forbidden_host_code_point(const char c) noexcept {
return is_forbidden_host_code_point_table[uint8_t(c)];
}
static_assert(unicode::is_forbidden_host_code_point('\0'));
static_assert(unicode::is_forbidden_host_code_point('\t'));
static_assert(unicode::is_forbidden_host_code_point('\n'));
static_assert(unicode::is_forbidden_host_code_point('\r'));
static_assert(unicode::is_forbidden_host_code_point(' '));
static_assert(unicode::is_forbidden_host_code_point('#'));
static_assert(unicode::is_forbidden_host_code_point('/'));
static_assert(unicode::is_forbidden_host_code_point(':'));
static_assert(unicode::is_forbidden_host_code_point('?'));
static_assert(unicode::is_forbidden_host_code_point('@'));
static_assert(unicode::is_forbidden_host_code_point('['));
static_assert(unicode::is_forbidden_host_code_point('?'));
static_assert(unicode::is_forbidden_host_code_point('<'));
static_assert(unicode::is_forbidden_host_code_point('>'));
static_assert(unicode::is_forbidden_host_code_point('\\'));
static_assert(unicode::is_forbidden_host_code_point(']'));
static_assert(unicode::is_forbidden_host_code_point('^'));
static_assert(unicode::is_forbidden_host_code_point('|'));
constexpr static uint8_t is_forbidden_domain_code_point_table[] = {
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
static_assert(sizeof(is_forbidden_domain_code_point_table) == 256);
ada_really_inline constexpr bool is_forbidden_domain_code_point(const char c) noexcept {
return is_forbidden_domain_code_point_table[uint8_t(c)];
}
ada_really_inline constexpr bool contains_forbidden_domain_code_point(char * input, size_t length) noexcept {
size_t i = 0;
uint8_t accumulator{};
for(; i + 4 <= length; i+=4) {
accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i])];
accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i+1])];
accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i+2])];
accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i+3])];
}
for(; i < length; i++) {
accumulator |= is_forbidden_domain_code_point_table[uint8_t(input[i])];
}
return accumulator;
}
static_assert(unicode::is_forbidden_domain_code_point('%'));
static_assert(unicode::is_forbidden_domain_code_point('\x7f'));
static_assert(unicode::is_forbidden_domain_code_point('\0'));
static_assert(unicode::is_forbidden_domain_code_point('\t'));
static_assert(unicode::is_forbidden_domain_code_point('\n'));
static_assert(unicode::is_forbidden_domain_code_point('\r'));
static_assert(unicode::is_forbidden_domain_code_point(' '));
static_assert(unicode::is_forbidden_domain_code_point('#'));
static_assert(unicode::is_forbidden_domain_code_point('/'));
static_assert(unicode::is_forbidden_domain_code_point(':'));
static_assert(unicode::is_forbidden_domain_code_point('?'));
static_assert(unicode::is_forbidden_domain_code_point('@'));
static_assert(unicode::is_forbidden_domain_code_point('['));
static_assert(unicode::is_forbidden_domain_code_point('?'));
static_assert(unicode::is_forbidden_domain_code_point('<'));
static_assert(unicode::is_forbidden_domain_code_point('>'));
static_assert(unicode::is_forbidden_domain_code_point('\\'));
static_assert(unicode::is_forbidden_domain_code_point(']'));
static_assert(unicode::is_forbidden_domain_code_point('^'));
static_assert(unicode::is_forbidden_domain_code_point('|'));
constexpr static bool is_alnum_plus_table[] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 0,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
static_assert(sizeof(is_alnum_plus_table) == 256);
ada_really_inline constexpr bool is_alnum_plus(const char c) noexcept {
return is_alnum_plus_table[uint8_t(c)];
// A table is almost surely much faster than the
// following under most compilers: return
// return (std::isalnum(c) || c == '+' || c == '-' || c == '.');
}
static_assert(unicode::is_alnum_plus('+'));
static_assert(unicode::is_alnum_plus('-'));
static_assert(unicode::is_alnum_plus('.'));
static_assert(unicode::is_alnum_plus('0'));
static_assert(unicode::is_alnum_plus('1'));
static_assert(unicode::is_alnum_plus('a'));
static_assert(unicode::is_alnum_plus('b'));
ada_really_inline constexpr bool is_ascii_hex_digit(const char c) noexcept {
return (c >= '0' && c <= '9') || (c >= 'A' && c <= 'F') || (c >= 'a' && c<= 'f');
}
ada_really_inline constexpr bool is_c0_control_or_space(const char c) noexcept {
return (unsigned char) c <= ' ';
}
ada_really_inline constexpr bool is_ascii_tab_or_newline(const char c) noexcept {
return c == '\t' || c == '\n' || c == '\r';
}
constexpr std::string_view table_is_double_dot_path_segment[] = {"..", "%2e.", ".%2e", "%2e%2e"};
ada_really_inline ada_constexpr bool is_double_dot_path_segment(std::string_view input) noexcept {
// This will catch most cases:
// The length must be 2,4 or 6.
// We divide by two and require
// that the result be between 1 and 3 inclusively.
uint64_t half_length = uint64_t(input.size())/2;
if(half_length - 1 > 2) { return false; }
// We have a string of length 2, 4 or 6.
// We now check the first character:
if((input[0] != '.') && (input[0] != '%')) { return false; }
// We are unlikely the get beyond this point.
int hash_value = (input.size() + (unsigned)(input[0])) & 3;
const std::string_view target = table_is_double_dot_path_segment[hash_value];
if(target.size() != input.size()) { return false; }
// We almost never get here.
// Optimizing the rest is relatively unimportant.
auto prefix_equal_unsafe = [](std::string_view a, std::string_view b) {
uint16_t A, B;
memcpy(&A,a.data(), sizeof(A));
memcpy(&B,b.data(), sizeof(B));
return A == B;
};
if(!prefix_equal_unsafe(input,target)) { return false; }
for(size_t i = 2; i < input.size(); i++) {
char c = input[i];
if((uint8_t((c|0x20) - 0x61) <= 25 ? (c|0x20) : c) != target[i]) { return false; }
}
return true;
// The above code might be a bit better than the code below. Compilers
// are not stupid and may use the fact that these strings have length 2,4 and 6
// and other tricks.
//return input == ".." ||
// input == ".%2e" || input == ".%2E" ||
// input == "%2e." || input == "%2E." ||
// input == "%2e%2e" || input == "%2E%2E" || input == "%2E%2e" || input == "%2e%2E";
}
ada_really_inline constexpr bool is_single_dot_path_segment(std::string_view input) noexcept {
return input == "." || input == "%2e" || input == "%2E";
}
ada_really_inline constexpr bool is_lowercase_hex(const char c) noexcept {
return (c >= '0' && c <= '9') || (c >= 'a' && c<= 'f');
}
unsigned constexpr convert_hex_to_binary(const char c) noexcept {
// this code can be optimized.
if (c <= '9') { return c - '0'; }
char del = c >= 'a' ? 'a' : 'A';
return 10 + (c - del);
}
std::string percent_decode(const std::string_view input, size_t first_percent) {
// next line is for safety only, we expect users to avoid calling percent_decode
// when first_percent is outside the range.
if(first_percent == std::string_view::npos) { return std::string(input); }
std::string dest(input.substr(0, first_percent));
dest.reserve(input.length());
const char* pointer = input.data() + first_percent;
const char* end = input.data() + input.size();
// Optimization opportunity: if the following code gets
// called often, it can be optimized quite a bit.
while (pointer < end) {
const char ch = pointer[0];
size_t remaining = end - pointer - 1;
if (ch != '%' || remaining < 2 ||
(//ch == '%' && // It is unnecessary to check that ch == '%'.
(!is_ascii_hex_digit(pointer[1]) ||
!is_ascii_hex_digit(pointer[2])))) {
dest += ch;
pointer++;
continue;
} else {
unsigned a = convert_hex_to_binary(pointer[1]);
unsigned b = convert_hex_to_binary(pointer[2]);
char c = static_cast<char>(a * 16 + b);
dest += c;
pointer += 3;
}
}
return dest;
}
std::string percent_encode(const std::string_view input, const uint8_t character_set[]) {
auto pointer = std::find_if(input.begin(), input.end(), [character_set](const char c) {
return character_sets::bit_at(character_set, c);
});
// Optimization: Don't iterate if percent encode is not required
if (pointer == input.end()) { return std::string(input); }
std::string result(input.substr(0,std::distance(input.begin(), pointer)));
result.reserve(input.length()); // in the worst case, percent encoding might produce 3 characters.
for (;pointer != input.end(); pointer++) {
if (character_sets::bit_at(character_set, *pointer)) {
result.append(character_sets::hex + uint8_t(*pointer) * 4, 3);
} else {
result += *pointer;
}
}
return result;
}
bool percent_encode(const std::string_view input, const uint8_t character_set[], std::string &out) {
auto pointer = std::find_if(input.begin(), input.end(), [character_set](const char c) {
return character_sets::bit_at(character_set, c);
});
// Optimization: Don't iterate if percent encode is not required
if (pointer == input.end()) { return false; }
out.clear();
out.append(input.data(), std::distance(input.begin(), pointer));
for (;pointer != input.end(); pointer++) {
if (character_sets::bit_at(character_set, *pointer)) {
out.append(character_sets::hex + uint8_t(*pointer) * 4, 3);
} else {
out += *pointer;
}
}
return true;
}
bool to_ascii(std::optional<std::string>& out, const std::string_view plain, const bool be_strict, size_t first_percent) {
std::string percent_decoded_buffer;
std::string_view input = plain;
if(first_percent != std::string_view::npos) {
percent_decoded_buffer = unicode::percent_decode(plain, first_percent);
input = percent_decoded_buffer;
}
#if ADA_HAS_ICU
out = std::string(255, 0);
UErrorCode status = U_ZERO_ERROR;
uint32_t options = UIDNA_CHECK_BIDI | UIDNA_CHECK_CONTEXTJ | UIDNA_NONTRANSITIONAL_TO_ASCII;
if (be_strict) {
options |= UIDNA_USE_STD3_RULES;
}
UIDNA* uidna = uidna_openUTS46(options, &status);
if (U_FAILURE(status)) {
return false;
}
UIDNAInfo info = UIDNA_INFO_INITIALIZER;
// RFC 1035 section 2.3.4.
// The domain name must be at most 255 octets.
// It cannot contain a label longer than 63 octets.
// Thus we should never need more than 255 octets, if we
// do the domain name is in error.
int32_t length = uidna_nameToASCII_UTF8(uidna,
input.data(),
int32_t(input.length()),
out.value().data(), 255,
&info,
&status);
if (status == U_BUFFER_OVERFLOW_ERROR) {
status = U_ZERO_ERROR;
out.value().resize(length);
// When be_strict is true, this should not be allowed!
length = uidna_nameToASCII_UTF8(uidna,
input.data(),
int32_t(input.length()),
out.value().data(), length,
&info,
&status);
}
// A label contains hyphen-minus ('-') in the third and fourth positions.
info.errors &= ~UIDNA_ERROR_HYPHEN_3_4;
// A label starts with a hyphen-minus ('-').
info.errors &= ~UIDNA_ERROR_LEADING_HYPHEN;
// A label ends with a hyphen-minus ('-').
info.errors &= ~UIDNA_ERROR_TRAILING_HYPHEN;
if (!be_strict) { // This seems to violate RFC 1035 section 2.3.4.
// A non-final domain name label (or the whole domain name) is empty.
info.errors &= ~UIDNA_ERROR_EMPTY_LABEL;
// A domain name label is longer than 63 bytes.
info.errors &= ~UIDNA_ERROR_LABEL_TOO_LONG;
// A domain name is longer than 255 bytes in its storage form.
info.errors &= ~UIDNA_ERROR_DOMAIN_NAME_TOO_LONG;
}
uidna_close(uidna);
if (U_FAILURE(status) || info.errors != 0 || length == 0) {
out = std::nullopt;
return false;
}
out.value().resize(length); // we possibly want to call :shrink_to_fit otherwise we use 255 bytes.
out.value().shrink_to_fit();
#elif defined(_WIN32) && ADA_WINDOWS_TO_ASCII_FALLBACK
(void)be_strict; // unused.
// Fallback on the system if ICU is not available.
// Windows function assumes UTF-16.
std::unique_ptr<char16_t[]> buffer(new char16_t[input.size()]);
auto convert = [](const char* buf, size_t len, char16_t* utf16_output) {
const uint8_t *data = reinterpret_cast<const uint8_t *>(buf);
size_t pos = 0;
char16_t* start{utf16_output};
while (pos < len) {
// try to convert the next block of 16 ASCII bytes
if (pos + 16 <= len) { // if it is safe to read 16 more bytes, check that they are ascii
uint64_t v1;
::memcpy(&v1, data + pos, sizeof(uint64_t));
uint64_t v2;
::memcpy(&v2, data + pos + sizeof(uint64_t), sizeof(uint64_t));
uint64_t v{v1 | v2};
if ((v & 0x8080808080808080) == 0) {
size_t final_pos = pos + 16;
while(pos < final_pos) {
*utf16_output++ = char16_t(buf[pos]);
pos++;
}
continue;
}
}
uint8_t leading_byte = data[pos]; // leading byte
if (leading_byte < 0b10000000) {
// converting one ASCII byte !!!
*utf16_output++ = char16_t(leading_byte);
pos++;
} else if ((leading_byte & 0b11100000) == 0b11000000) {
// We have a two-byte UTF-8, it should become
// a single UTF-16 word.
if(pos + 1 >= len) { return 0; } // minimal bound checking
if ((data[pos + 1] & 0b11000000) != 0b10000000) { return 0; }
// range check
uint32_t code_point = (leading_byte & 0b00011111) << 6 | (data[pos + 1] & 0b00111111);
if (code_point < 0x80 || 0x7ff < code_point) { return 0; }
*utf16_output++ = char16_t(code_point);
pos += 2;
} else if ((leading_byte & 0b11110000) == 0b11100000) {
// We have a three-byte UTF-8, it should become
// a single UTF-16 word.
if(pos + 2 >= len) { return 0; } // minimal bound checking
if ((data[pos + 1] & 0b11000000) != 0b10000000) { return 0; }
if ((data[pos + 2] & 0b11000000) != 0b10000000) { return 0; }
// range check
uint32_t code_point = (leading_byte & 0b00001111) << 12 |
(data[pos + 1] & 0b00111111) << 6 |
(data[pos + 2] & 0b00111111);
if (code_point < 0x800 || 0xffff < code_point ||
(0xd7ff < code_point && code_point < 0xe000)) {
return 0;
}
*utf16_output++ = char16_t(code_point);
pos += 3;
} else if ((leading_byte & 0b11111000) == 0b11110000) { // 0b11110000
// we have a 4-byte UTF-8 word.
if(pos + 3 >= len) { return 0; } // minimal bound checking
if ((data[pos + 1] & 0b11000000) != 0b10000000) { return 0; }
if ((data[pos + 2] & 0b11000000) != 0b10000000) { return 0; }
if ((data[pos + 3] & 0b11000000) != 0b10000000) { return 0; }
// range check
uint32_t code_point =
(leading_byte & 0b00000111) << 18 | (data[pos + 1] & 0b00111111) << 12 |
(data[pos + 2] & 0b00111111) << 6 | (data[pos + 3] & 0b00111111);
if (code_point <= 0xffff || 0x10ffff < code_point) { return 0; }
code_point -= 0x10000;
uint16_t high_surrogate = uint16_t(0xD800 + (code_point >> 10));
uint16_t low_surrogate = uint16_t(0xDC00 + (code_point & 0x3FF));
*utf16_output++ = char16_t(high_surrogate);
*utf16_output++ = char16_t(low_surrogate);
pos += 4;
} else {
return 0;
}
}
return int(utf16_output - start);
};
size_t codepoints = convert(input.data(), input.size(), buffer.get());
if(codepoints == 0) {
out = std::nullopt;
return false;
}
int required_buffer_size = IdnToAscii(IDN_ALLOW_UNASSIGNED, (LPCWSTR)buffer.get(), codepoints, NULL, 0);
if(required_buffer_size == 0) {
out = std::nullopt;
return false;
}
out = std::string(required_buffer_size, 0);
std::unique_ptr<char16_t[]> ascii_buffer(new char16_t[required_buffer_size]);
required_buffer_size = IdnToAscii(IDN_ALLOW_UNASSIGNED, (LPCWSTR)buffer.get(), codepoints, (LPWSTR)ascii_buffer.get(), required_buffer_size);
if(required_buffer_size == 0) {
out = std::nullopt;
return false;
}
// This will not validate the punycode, so let us work it in reverse.
int test_reverse = IdnToUnicode(IDN_ALLOW_UNASSIGNED, (LPCWSTR)ascii_buffer.get(), required_buffer_size, NULL, 0);
if(test_reverse == 0) {
out = std::nullopt;
return false;
}
out = std::string(required_buffer_size, 0);
for(size_t i = 0; i < required_buffer_size; i++) { (*out)[i] = char(ascii_buffer.get()[i]); }
#else
(void)be_strict; // unused.
out = input; // We cannot do much more for now.
#endif
return true;
}
} // namespace ada::unicode
/* end file src/unicode.cpp */
// dofile: invoked with prepath=/Users/dlemire/CVS/github/ada/src, filename=serializers.cpp
/* begin file src/serializers.cpp */
#include <array>
#include <string>
namespace ada::serializers {
void find_longest_sequence_of_ipv6_pieces(const std::array<uint16_t, 8>& address, size_t& compress, size_t& compress_length) noexcept {
for (size_t i = 0; i < 8; i++) {
if (address[i] == 0) {
size_t next = i + 1;
while (next != 8 && address[next] == 0) ++next;
const size_t count = next - i;
if (compress_length < count) {
compress_length = count;
compress = i;
if (next == 8) break;
i = next;
}
}
}
}
std::string ipv6(const std::array<uint16_t, 8>& address) noexcept {
size_t compress_length = 0; // The length of a long sequence of zeros.
size_t compress = 0; // The start of a long sequence of zeros.
find_longest_sequence_of_ipv6_pieces(address, compress, compress_length);
if (compress_length <= 1) {
// Optimization opportunity: Find a faster way then snprintf for imploding and return here.
compress = compress_length = 8;
}
std::string output(4 * 8 + 7 + 2, '\0');
size_t piece_index = 0;
char *point = output.data();
char *point_end = output.data() + output.size();
*point++ = '[';
while (true) {
if (piece_index == compress) {
*point++ = ':';
// If we skip a value initially, we need to write '::', otherwise
// a single ':' will do since it follows a previous ':'.
if(piece_index == 0) { *point++ = ':'; }
piece_index += compress_length;
if(piece_index == 8) { break; }
}
point = std::to_chars(point, point_end, address[piece_index], 16).ptr;
piece_index++;
if(piece_index == 8) { break; }
*point++ = ':';
}
*point++ = ']';
output.resize(point - output.data());
return output;
}
std::string ipv4(const uint64_t address) noexcept {
std::string output(15, '\0');
char *point = output.data();
char *point_end = output.data() + output.size();
point = std::to_chars(point, point_end, uint8_t(address >> 24)).ptr;
for (int i = 2; i >= 0; i--) {
*point++ = '.';
point = std::to_chars(point, point_end, uint8_t(address >> (i * 8))).ptr;
}
output.resize(point - output.data());
return output;
}
} // namespace ada::serializers
/* end file src/serializers.cpp */
// dofile: invoked with prepath=/Users/dlemire/CVS/github/ada/src, filename=implementation.cpp
/* begin file src/implementation.cpp */
#include <string_view>
namespace ada {
ada_warn_unused tl::expected<ada::url,ada::errors> parse(std::string_view input,
const ada::url* base_url,
ada::encoding_type encoding) {
if(encoding != encoding_type::UTF8) {
// @todo Add support for non UTF8 input
}
ada::url u = ada::parser::parse_url(input, base_url, encoding);
if(!u.is_valid) { return tl::unexpected(errors::generic_error); }
return u;
}
std::string href_from_file(std::string_view input) {
// This is going to be much faster than constructing a URL.
std::string tmp_buffer;
std::string_view internal_input;
if(unicode::has_tabs_or_newline(input)) {
tmp_buffer = input;
helpers::remove_ascii_tab_or_newline(tmp_buffer);
internal_input = tmp_buffer;
} else {
internal_input = input;
}
std::string path;
if(internal_input.empty()) {
path = "/";
} else if((internal_input[0] == '/') ||(internal_input[0] == '\\')){
helpers::parse_prepared_path(internal_input.substr(1), ada::scheme::type::FILE, path);
} else {
helpers::parse_prepared_path(internal_input, ada::scheme::type::FILE, path);
}
return "file://" + path;
}
ada_warn_unused std::string to_string(ada::encoding_type type) {
switch(type) {
case ada::encoding_type::UTF8 : return "UTF-8";
case ada::encoding_type::UTF_16LE : return "UTF-16LE";
case ada::encoding_type::UTF_16BE : return "UTF-16BE";
default: unreachable();
}
}
} // namespace ada
/* end file src/implementation.cpp */
// dofile: invoked with prepath=/Users/dlemire/CVS/github/ada/src, filename=helpers.cpp
/* begin file src/helpers.cpp */
#include <algorithm>
#include <charconv>
#include <cstring>
#include <sstream>
namespace ada::helpers {
template <typename out_iter>
void encode_json(std::string_view view, out_iter out) {
// trivial implementation. could be faster.
const char * hexvalues = "000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f";
for(uint8_t c : view) {
if(c == '\\') {
*out++ = '\\'; *out++ = '\\';
} else if(c == '"') {
*out++ = '\\'; *out++ = '"';
} else if(c <= 0x1f) {
*out++ = '\\'; *out++= 'u'; *out++= '0'; *out++= '0';
*out++ = hexvalues[2*c];
*out++ = hexvalues[2*c+1];
} else {
*out++ = c;
}
}
}
ada_unused std::string get_state(ada::state s) {
switch (s) {
case ada::state::AUTHORITY: return "Authority";
case ada::state::SCHEME_START: return "Scheme Start";
case ada::state::SCHEME: return "Scheme";
case ada::state::HOST: return "Host";
case ada::state::NO_SCHEME: return "No Scheme";
case ada::state::FRAGMENT: return "Fragment";
case ada::state::RELATIVE_SCHEME: return "Relative Scheme";
case ada::state::RELATIVE_SLASH: return "Relative Slash";
case ada::state::FILE: return "File";
case ada::state::FILE_HOST: return "File Host";
case ada::state::FILE_SLASH: return "File Slash";
case ada::state::PATH_OR_AUTHORITY: return "Path or Authority";
case ada::state::SPECIAL_AUTHORITY_IGNORE_SLASHES: return "Special Authority Ignore Slashes";
case ada::state::SPECIAL_AUTHORITY_SLASHES: return "Special Authority Slashes";
case ada::state::SPECIAL_RELATIVE_OR_AUTHORITY: return "Special Relative or Authority";
case ada::state::QUERY: return "Query";
case ada::state::PATH: return "Path";
case ada::state::PATH_START: return "Path Start";
case ada::state::OPAQUE_PATH: return "Opaque Path";
case ada::state::PORT: return "Port";
default: return "unknown state";
}
}
ada_really_inline std::optional<std::string_view> prune_fragment(std::string_view& input) noexcept {
// compiles down to 20--30 instructions including a class to memchr (C function).
// this function should be quite fast.
size_t location_of_first = input.find('#');
if(location_of_first == std::string_view::npos) { return std::nullopt; }
std::string_view fragment = input;
fragment.remove_prefix(location_of_first+1);
input.remove_suffix(input.size() - location_of_first);
return fragment;
}
ada_really_inline void shorten_path(std::string& path, ada::scheme::type type) noexcept {
size_t first_delimiter = path.find_first_of('/', 1);
// Let path be url’s path.
// If url’s scheme is "file", path’s size is 1, and path[0] is a normalized Windows drive letter, then return.
if (type == ada::scheme::type::FILE && first_delimiter == std::string_view::npos) {
if (checkers::is_normalized_windows_drive_letter(std::string_view(path.data() + 1, first_delimiter - 1))) {
return;
}
}
// Remove path’s last item, if any.
if (!path.empty()) {
path.erase(path.rfind('/'));
}
}
ada_really_inline void remove_ascii_tab_or_newline(std::string& input) noexcept {
// if this ever becomes a performance issue, we could use an approach similar to has_tabs_or_newline
input.erase(std::remove_if(input.begin(), input.end(), [](char c) {
return ada::unicode::is_ascii_tab_or_newline(c);
}), input.end());
}
ada_really_inline std::string_view substring(std::string_view input, size_t pos) noexcept {
ada_log("substring(", input, " [", input.size() ,"bytes],", pos, ")");
return pos > input.size() ? std::string_view() : input.substr(pos);
}
// Reverse the byte order.
ada_really_inline uint64_t swap_bytes(uint64_t val) noexcept {
// performance: this often compiles to a single instruction (e.g., bswap)
return ((((val) & 0xff00000000000000ull) >> 56) |
(((val) & 0x00ff000000000000ull) >> 40) |
(((val) & 0x0000ff0000000000ull) >> 24) |
(((val) & 0x000000ff00000000ull) >> 8 ) |
(((val) & 0x00000000ff000000ull) << 8 ) |
(((val) & 0x0000000000ff0000ull) << 24) |
(((val) & 0x000000000000ff00ull) << 40) |
(((val) & 0x00000000000000ffull) << 56));
}
ada_really_inline uint64_t swap_bytes_if_big_endian(uint64_t val) noexcept {
// performance: under little-endian systems (most systems), this function
// is free (just returns the input).
#if ADA_IS_BIG_ENDIAN
return swap_bytes(val);
#else
return val; // unchanged (trivial)
#endif
}
// starting at index location, this finds the next location of a character
// :, /, \\, ? or [. If none is found, view.size() is returned.
// For use within get_host_delimiter_location.
ada_really_inline size_t find_next_host_delimiter_special(std::string_view view, size_t location) noexcept {
// performance: if you plan to call find_next_host_delimiter more than once,
// you *really* want find_next_host_delimiter to be inlined, because
// otherwise, the constants may get reloaded each time (bad).
auto has_zero_byte = [](uint64_t v) {
return ((v - 0x0101010101010101) & ~(v)&0x8080808080808080);
};
auto index_of_first_set_byte = [](uint64_t v) {
return ((((v - 1) & 0x101010101010101) * 0x101010101010101) >> 56) - 1;
};
auto broadcast = [](uint8_t v) -> uint64_t { return 0x101010101010101 * v; };
size_t i = location;
uint64_t mask1 = broadcast(':');
uint64_t mask2 = broadcast('/');
uint64_t mask3 = broadcast('\\');
uint64_t mask4 = broadcast('?');
uint64_t mask5 = broadcast('[');
// This loop will get autovectorized under many optimizing compilers,
// so you get actually SIMD!
for (; i + 7 < view.size(); i += 8) {
uint64_t word{};
// performance: the next memcpy translates into a single CPU instruction.
memcpy(&word, view.data() + i, sizeof(word));
// performance: on little-endian systems (most systems), this next line is free.
word = swap_bytes_if_big_endian(word);
uint64_t xor1 = word ^ mask1;
uint64_t xor2 = word ^ mask2;
uint64_t xor3 = word ^ mask3;
uint64_t xor4 = word ^ mask4;
uint64_t xor5 = word ^ mask5;
uint64_t is_match = has_zero_byte(xor1) | has_zero_byte(xor2) | has_zero_byte(xor3) | has_zero_byte(xor4) | has_zero_byte(xor5);
if(is_match) {
return i + index_of_first_set_byte(is_match);
}
}
if (i < view.size()) {
uint64_t word{};
// performance: the next memcpy translates into a function call, but
// that is difficult to avoid. Might be a bit expensive.
memcpy(&word, view.data() + i, view.size() - i);
word = swap_bytes_if_big_endian(word);
uint64_t xor1 = word ^ mask1;
uint64_t xor2 = word ^ mask2;
uint64_t xor3 = word ^ mask3;
uint64_t xor4 = word ^ mask4;
uint64_t xor5 = word ^ mask5;
uint64_t is_match = has_zero_byte(xor1) | has_zero_byte(xor2) | has_zero_byte(xor3) | has_zero_byte(xor4) | has_zero_byte(xor5);
if(is_match) {
return i + index_of_first_set_byte(is_match);
}
}
return view.size();
}
// starting at index location, this finds the next location of a character
// :, /, ? or [. If none is found, view.size() is returned.
// For use within get_host_delimiter_location.
ada_really_inline size_t find_next_host_delimiter(std::string_view view, size_t location) noexcept {
// performance: if you plan to call find_next_host_delimiter more than once,
// you *really* want find_next_host_delimiter to be inlined, because
// otherwise, the constants may get reloaded each time (bad).
auto has_zero_byte = [](uint64_t v) {
return ((v - 0x0101010101010101) & ~(v)&0x8080808080808080);
};
auto index_of_first_set_byte = [](uint64_t v) {
return ((((v - 1) & 0x101010101010101) * 0x101010101010101) >> 56) - 1;
};
auto broadcast = [](uint8_t v) -> uint64_t { return 0x101010101010101 * v; };
size_t i = location;
uint64_t mask1 = broadcast(':');
uint64_t mask2 = broadcast('/');
uint64_t mask4 = broadcast('?');
uint64_t mask5 = broadcast('[');
// This loop will get autovectorized under many optimizing compilers,
// so you get actually SIMD!
for (; i + 7 < view.size(); i += 8) {
uint64_t word{};
// performance: the next memcpy translates into a single CPU instruction.
memcpy(&word, view.data() + i, sizeof(word));
// performance: on little-endian systems (most systems), this next line is free.
word = swap_bytes_if_big_endian(word);
uint64_t xor1 = word ^ mask1;
uint64_t xor2 = word ^ mask2;
uint64_t xor4 = word ^ mask4;
uint64_t xor5 = word ^ mask5;
uint64_t is_match = has_zero_byte(xor1) | has_zero_byte(xor2) | has_zero_byte(xor4) | has_zero_byte(xor5);
if(is_match) {
return i + index_of_first_set_byte(is_match);
}
}
if (i < view.size()) {
uint64_t word{};
// performance: the next memcpy translates into a function call, but
// that is difficult to avoid. Might be a bit expensive.
memcpy(&word, view.data() + i, view.size() - i);
// performance: on little-endian systems (most systems), this next line is free.
word = swap_bytes_if_big_endian(word);
uint64_t xor1 = word ^ mask1;
uint64_t xor2 = word ^ mask2;
uint64_t xor4 = word ^ mask4;
uint64_t xor5 = word ^ mask5;
uint64_t is_match = has_zero_byte(xor1) | has_zero_byte(xor2) | has_zero_byte(xor4) | has_zero_byte(xor5);
if(is_match) {
return i + index_of_first_set_byte(is_match);
}
}
return view.size();
}
ada_really_inline std::pair<size_t,bool> get_host_delimiter_location(const bool is_special, std::string_view& view) noexcept {
/**
* The spec at https://url.spec.whatwg.org/#hostname-state expects us to compute
* a variable called insideBrackets but this variable is only used once, to check
* whether a ':' character was found outside brackets.
* Exact text:
* "Otherwise, if c is U+003A (:) and insideBrackets is false, then:".
* It is conceptually simpler and arguably more efficient to just return a Boolean
* indicating whether ':' was found outside brackets.
*/
const size_t view_size = view.size();
size_t location = 0;
bool found_colon = false;
/**