-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathujson.cpp
3043 lines (2918 loc) · 58.9 KB
/
ujson.cpp
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
/* Generated by re2c 0.13.5 */
/*
* Copyright (c) 2014 Anders Wang Kristensen
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#include "ujson.hpp"
#include "double-conversion.h"
#include <algorithm>
#include <sstream>
#ifdef __GNUC__
#ifdef __SSE2__
#define UJSON_USE_SSE2
#endif
#endif
#ifdef _MSC_VER
#if (defined(_M_AMD64) || defined(_M_X64))
#define UJSON_USE_SSE2
#elif _M_IX86_FP==2
#define UJSON_USE_SSE2
#endif
#endif
#ifdef UJSON_USE_SSE2
#include <emmintrin.h>
#endif
// vs2013 ctp 1 supports noexcept but rest don't
#if defined _MSC_VER && _MSC_FULL_VER != 180021114
#define noexcept
#endif
const ujson::value ujson::null = ujson::value();
// --------------------------------------------------------------------------
// utf-8
//
// Code Points | 1st | 2nd | 3rd | 4th
//
// U+0000 - U+007F 00..7F
// U+0080 - U+07FF C2..DF 80..BF
// U+0800 - U+0FFF E0 A0..BF 80..BF
// U+1000 - U+CFFF E1..EC 80..BF 80..BF
// U+D000 - U+D7FF ED 80..9F 80..BF
// U+D800 - U+DFFF illformed
// U+E000 - U+FFFF EE..EF 80..BF 80..BF
// U+10000 - U+3FFFF F0 90..BF 80..BF 80..BF
// U+40000 - U+FFFFF F1..F3 80..BF 80..BF 80..BF
// U+100000 - U+10FFFF F4 80..8F 80..BF 80..BF
typedef std::pair<std::uint8_t, std::uint8_t> range_t;
struct utf8_ranges_t {
std::uint8_t upper_bound;
std::uint8_t num_ranges;
range_t ranges[3];
};
static const utf8_ranges_t utf8_ranges[] = {
{ 0x7F, 0 },
{ 0xBF, 1 }, // 80-BF invalid continuation
{ 0xC1, 1 }, // 0xC0-0xC1 invalid as first byte
{ 0xDF, 1, { { 0x80, 0xBF + 1 } } },
{ 0xE0, 2, { { 0xA0, 0xBF + 1 }, { 0x80, 0xBF + 1 } } },
{ 0xEC, 2, { { 0x80, 0xBF + 1 }, { 0x80, 0xBF + 1 } } },
{ 0xED, 2, { { 0x80, 0x9F + 1 }, { 0x80, 0xBF + 1 } } },
{ 0xEF, 2, { { 0x80, 0xBF + 1 }, { 0x80, 0xBF + 1 } } },
{ 0xF0, 3, { { 0x90, 0xBF + 1 }, { 0x80, 0xBF + 1 }, { 0x80, 0xBF + 1 } } },
{ 0xF3, 3, { { 0x80, 0xBF + 1 }, { 0x80, 0xBF + 1 }, { 0x80, 0xBF + 1 } } },
{ 0xF4, 3, { { 0x80, 0x8F + 1 }, { 0x80, 0xBF + 1 }, { 0x80, 0xBF + 1 } } },
{ 0xFF, 1 } // 0xF5-0xFF invalid as first byte
};
static const std::size_t num_utf8_ranges =
sizeof(utf8_ranges) / sizeof(utf8_ranges[0]);
bool ujson::value::is_valid_utf8(const char *ptr,
const char *end) noexcept {
while (ptr < end) {
#ifdef UJSON_USE_SSE2
// test if the next 16 chars are ascii and skip forward as much as
// possible
while (end - ptr >= 16) {
__m128i chunk =
_mm_loadu_si128(reinterpret_cast<const __m128i *>(ptr));
// signed comparison: multibyte utf-8 sequences have high bit set
// are thus negative
__m128i compare = _mm_cmplt_epi8(chunk, _mm_setzero_si128());
int mask = _mm_movemask_epi8(compare);
if (mask == 0) {
ptr += 16;
continue;
}
#ifdef __GNUC__
int index = __builtin_ffs(mask) - 1;
#else
unsigned long index = 0;
_BitScanForward(&index, mask);
#endif
assert(index >= 0 && index <= 15);
ptr += index;
break;
}
#endif
auto c = static_cast<std::uint8_t>(*ptr++);
auto it = utf8_ranges;
for (; it != utf8_ranges + num_utf8_ranges; ++it) {
if (c <= it->upper_bound)
break;
}
assert(it < utf8_ranges + num_utf8_ranges);
const auto limit = it->ranges + it->num_ranges;
for (auto rng = it->ranges; rng < limit; ++rng) {
auto d = static_cast<std::uint8_t>(*ptr++);
if (d < rng->first || d >= rng->second)
return false;
}
}
return true;
}
// convert utf8 to utf32; returns utf32 + number of bytes consumed
static std::pair<std::uint32_t, std::uint32_t>
utf8_to_utf32(const char *ptr) {
auto utf8 = reinterpret_cast<const std::uint8_t *>(ptr);
std::uint8_t first = *utf8;
std::uint32_t trailing_bytes;
if (first <= 0x7F)
trailing_bytes = 0;
else if (first <= 0xDF)
trailing_bytes = 1;
else if (first <= 0xEF)
trailing_bytes = 2;
else
trailing_bytes = 3;
std::uint32_t utf32 = 0;
switch (trailing_bytes) {
case 3:
utf32 += *utf8++;
utf32 <<= 6;
case 2:
utf32 += *utf8++;
utf32 <<= 6;
case 1:
utf32 += *utf8++;
utf32 <<= 6;
case 0:
utf32 += *utf8++;
}
static const std::uint32_t magic[] = { 0x0, 0x3080, 0xE2080, 0x3C82080 };
utf32 -= magic[trailing_bytes];
assert(utf32 <= 0x10FFFF);
return { utf32, trailing_bytes + 1 };
}
// convert utf32 code point to 1-4 bytes of utf8
static char *utf32_to_utf8(char *str, std::uint32_t cp) {
assert(cp <= 0x10FFFF);
std::size_t num_bytes;
if (cp <= 0x7F)
num_bytes = 1;
else if (cp <= 0x7FF)
num_bytes = 2;
else if (cp <= 0xFFFF)
num_bytes = 3;
else
num_bytes = 4;
static const std::uint32_t offset[] = { 0x00, 0x00, 0xC0, 0xE0, 0xF0 };
switch (num_bytes) {
case 4:
str[3] = static_cast<char>((cp | 0x80) & 0xBF);
cp >>= 6;
case 3:
str[2] = static_cast<char>((cp | 0x80) & 0xBF);
cp >>= 6;
case 2:
str[1] = static_cast<char>((cp | 0x80) & 0xBF);
cp >>= 6;
case 1:
str[0] = static_cast<char>(cp | offset[num_bytes]);
}
return str + num_bytes;
}
// convert four byte hex string to int
static std::uint32_t hex_to_int(const std::uint8_t *hex) {
static const std::uint8_t lookup[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0, 0, 10, 11,
12, 13, 14, 15, 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, 10, 11, 12, 13, 14, 15
};
assert(lookup['0' - '0'] == 0);
assert(lookup['1' - '0'] == 1);
assert(lookup['2' - '0'] == 2);
assert(lookup['3' - '0'] == 3);
assert(lookup['4' - '0'] == 4);
assert(lookup['5' - '0'] == 5);
assert(lookup['6' - '0'] == 6);
assert(lookup['7' - '0'] == 7);
assert(lookup['8' - '0'] == 8);
assert(lookup['9' - '0'] == 9);
assert(lookup['A' - '0'] == 10);
assert(lookup['B' - '0'] == 11);
assert(lookup['C' - '0'] == 12);
assert(lookup['D' - '0'] == 13);
assert(lookup['E' - '0'] == 14);
assert(lookup['F' - '0'] == 15);
assert(lookup['a' - '0'] == 10);
assert(lookup['b' - '0'] == 11);
assert(lookup['c' - '0'] == 12);
assert(lookup['d' - '0'] == 13);
assert(lookup['e' - '0'] == 14);
assert(lookup['f' - '0'] == 15);
std::uint32_t result = 0;
for (int i = 0; i < 4; ++i) {
result <<= 4;
std::uint8_t c = hex[i];
std::uint32_t h = lookup[c - '0'];
result += h;
}
return result;
}
// convert int (<=0xFFFF) to six byte escaped hex string
static void int_to_hex(std::uint32_t cp, char *str) {
assert(cp <= 0xFFFF);
static const char *hex = "0123456789ABCDEF";
str[0] = '\\';
str[1] = 'u';
str[2] = hex[(cp & 0xF000) >> 12];
str[3] = hex[(cp & 0x0F00) >> 8];
str[4] = hex[(cp & 0x00F0) >> 4];
str[5] = hex[(cp & 0x000F) >> 0];
}
// ---------------------------------------------------------------------------
// to_string
static void to_string(std::string &result, ujson::string_view input,
const ujson::to_string_options &opts) {
// upper bound on how much result can grow (happens if string consists of
// certain control characters, since they are encoded as six characters,
// \uXXXX)
const auto max_size_increase = 6 * input.length() + 2;
const auto old_size = result.size();
result.resize(old_size + max_size_increase);
char *out = &result[old_size];
*out++ = '"';
for (auto in = input.c_str(); in != input.c_str() + input.length(); ++in) {
std::uint8_t c = *in;
if (c >= 0x20) {
if (c == '"') {
*out++ = '\\';
*out++ = '"';
} else if (c == '\\') {
*out++ = '\\';
*out++ = '\\';
} else if (c <= 127) {
// ascii
*out++ = c;
} else if (opts.encoding == ujson::character_encoding::utf8) {
// utf-8 multi-byte case
*out++ = c;
} else {
// encode utf-8 multi-byte as utf-16
auto pair = utf8_to_utf32(in);
if (pair.first < 0x10000) {
int_to_hex(pair.first, out);
out += 6;
} else {
std::uint32_t cp = pair.first - 0x10000;
std::uint32_t leading = (cp >> 10) + 0xD800;
assert(leading >= 0xD800 && leading < 0xDC00);
std::uint32_t trailing = (cp & 0x3FF) + 0xDC00;
assert(trailing >= 0xDC00 && trailing < 0xE000);
int_to_hex(leading, out);
out += 6;
int_to_hex(trailing, out);
out += 6;
}
in += pair.second - 1;
}
} else {
if (c == '\b') {
*out++ = '\\';
*out++ = 'b';
} else if (c == '\f') {
*out++ = '\\';
*out++ = 'f';
} else if (c == '\n') {
*out++ = '\\';
*out++ = 'n';
} else if (c == '\r') {
*out++ = '\\';
*out++ = 'r';
} else if (c == '\t') {
*out++ = '\\';
*out++ = 't';
} else {
int_to_hex(c, out);
out += 6;
}
}
}
*out++ = '"';
// trim result to actual size
const auto new_size = out - &result[0];
assert(new_size - old_size <= max_size_increase);
result.resize(new_size);
}
static void to_string(std::string &str, double v) {
using namespace double_conversion;
const int buffer_size = 128;
char buffer[buffer_size];
StringBuilder builder(buffer, buffer_size);
auto flags = DoubleToStringConverter::NO_FLAGS;
DoubleToStringConverter d2sc(flags, nullptr, nullptr, 'e', -10, 10, 0, 0);
#ifdef NDEBUG
d2sc.ToShortest(v, &builder);
#else
bool result = d2sc.ToShortest(v, &builder);
assert(result);
#endif
str += builder.Finalize();
}
static void to_string_impl(std::string &str, ujson::value const &v,
const ujson::to_string_options &opts,
std::size_t current_indent) {
switch (v.type()) {
case ujson::value_type::null:
str += "null";
break;
case ujson::value_type::boolean:
str += bool_cast(v) ? "true" : "false";
break;
case ujson::value_type::number:
to_string(str, double_cast(v));
break;
case ujson::value_type::string:
to_string(str, string_cast(v), opts);
break;
case ujson::value_type::array: {
str += '[';
if (opts.indent_amount > 0)
str += '\n';
auto const &array = array_cast(v);
for (auto const &value : array) {
str.append(current_indent + opts.indent_amount, ' ');
to_string_impl(str, value, opts,
current_indent + opts.indent_amount);
if (&value != &array.back())
str += ',';
if (opts.indent_amount > 0)
str += '\n';
}
str.append(current_indent, ' ');
str += ']';
break;
}
case ujson::value_type::object: {
str += '{';
if (opts.indent_amount > 0)
str += '\n';
auto const &object = object_cast(v);
bool first = true;
for (auto const &kvp : object) {
if (!first) {
str += ',';
if (opts.indent_amount > 0)
str += '\n';
}
str.append(current_indent + opts.indent_amount, ' ');
to_string(str, { kvp.first.c_str(), kvp.first.length() }, opts);
if (opts.indent_amount > 0)
str += " : ";
else
str += ':';
to_string_impl(str, kvp.second, opts,
current_indent + opts.indent_amount);
first = false;
}
if (opts.indent_amount > 0 && !object.empty())
str += '\n';
str.append(current_indent, ' ');
str += '}';
break;
}
default:
assert(false);
break;
}
}
std::string ujson::to_string(value const &v,
const ujson::to_string_options &opts) {
std::string result;
to_string_impl(result, v, opts, 0);
return result;
}
std::ostream &ujson::operator<<(std::ostream &stream, value const &v) {
stream << to_string(v);
return stream;
}
//----------------------------------------------------------------------------
// exception
ujson::exception::exception(error_code error, int line)
: m_error_code(error), m_line(line) {}
const char *ujson::exception::what() const noexcept{
if (m_what.empty()) {
std::stringstream ss;
switch (m_error_code) {
case error_code::bad_cast:
ss << "Bad cast.";
break;
case error_code::bad_number:
if (m_line == -1)
ss << "Bad number.";
else
ss << "Bad number on line " << m_line << ".";
break;
case error_code::bad_string:
ss << "Bad UTF-8.";
break;
case error_code::invalid_syntax:
ss << "Invalid syntax on line " << m_line << ".";
break;
case error_code::integer_overflow:
ss << "Number out of range for integer cast.";
break;
default:
assert(false);
break;
}
m_what = ss.str();
}
return m_what.c_str();
}
ujson::error_code ujson::exception::get_error_code() const {
return m_error_code;
}
int ujson::exception::get_line() const { return m_line; }
//----------------------------------------------------------------------------
// parser
namespace {
enum token {
ujson_colon,
ujson_comma,
ujson_null,
ujson_true,
ujson_false,
ujson_number,
ujson_string,
ujson_array_begin,
ujson_array_end,
ujson_object_begin,
ujson_object_end,
ujson_eof
};
// helper class used for providing a sentinel token to the parser
// when it reads beyond the supplied buffer
class safe_ptr {
public:
safe_ptr(const std::uint8_t *ptr, const std::uint8_t *limit);
inline const std::uint8_t *ptr() const;
inline const std::uint8_t *limit() const;
inline std::uint8_t operator*() const;
inline operator const std::uint8_t *() const;
inline safe_ptr &operator++();
inline safe_ptr &operator+=(std::size_t);
inline safe_ptr &operator=(const std::uint8_t *);
private:
const std::uint8_t *m_ptr;
const std::uint8_t *m_limit;
};
class parser {
public:
parser(const std::uint8_t *ptr, std::size_t len);
token peek_token();
token read_token();
void expect(token token);
double read_double() const;
std::string read_string() const;
int line() const;
private:
token scan();
const std::uint8_t *const m_start;
const std::uint8_t *const m_limit;
bool m_peeked;
token m_current_token;
safe_ptr m_cursor;
const std::uint8_t *m_token;
};
}
//----------------------------------------------------------------------------
safe_ptr::safe_ptr(const std::uint8_t *ptr, const std::uint8_t *limit)
: m_ptr(ptr), m_limit(limit) {
assert(m_ptr <= m_limit);
}
const std::uint8_t *safe_ptr::ptr() const { return m_ptr; }
const std::uint8_t *safe_ptr::limit() const { return m_limit; }
std::uint8_t safe_ptr::operator*() const {
return m_ptr < m_limit ? *m_ptr : 0;
}
safe_ptr::operator const std::uint8_t *() const { return m_ptr; }
safe_ptr &safe_ptr::operator++() {
++m_ptr;
return *this;
}
safe_ptr &safe_ptr::operator+=(std::size_t offset) {
m_ptr += offset;
return *this;
}
safe_ptr &safe_ptr::operator=(const std::uint8_t *ptr) {
m_ptr = ptr;
return *this;
}
//----------------------------------------------------------------------------
parser::parser(const std::uint8_t *ptr, std::size_t len)
: m_start(ptr), m_limit(ptr + len), m_cursor(ptr, ptr + len) {
m_peeked = false;
}
int parser::line() const {
return static_cast<int>(std::count(m_start, m_cursor.ptr(), '\n') + 1);
}
token parser::peek_token() {
if (!m_peeked) {
m_current_token = scan();
m_peeked = true;
}
return m_current_token;
}
token parser::read_token() {
if (!m_peeked)
m_current_token = scan();
m_peeked = false;
return m_current_token;
}
void parser::expect(token token) {
if (token != read_token())
throw ujson::exception(ujson::error_code::invalid_syntax, line());
}
double parser::read_double() const {
const auto len = static_cast<int>(m_cursor.ptr() - m_token);
using namespace double_conversion;
auto flags = StringToDoubleConverter::NO_FLAGS;
StringToDoubleConverter s2dc(flags, 0.0, 0.0, nullptr, nullptr);
int processed_chars;
auto token = reinterpret_cast<const char *>(m_token);
double result = s2dc.StringToDouble(token, len, &processed_chars);
// handle invalid number
if (processed_chars != len)
throw ujson::exception(ujson::error_code::bad_number, line());
// handle overflow
if (!std::isfinite(result))
throw ujson::exception(ujson::error_code::bad_number, line());
return result;
}
std::string parser::read_string() const {
// m_token points to first double qoute and m_cursor points to last
auto in = m_token + 1;
const auto limit = m_cursor.ptr() - 1;
if (in == limit)
return "";
// limit-in is an upper bound on the size of the resulting string
std::string result(limit - in, '\0');
char *out = &result.front();
while (in < limit) {
#ifdef UJSON_USE_SSE2
while (limit - in >= 16) {
__m128i backslash = _mm_set1_epi8(0x5C);
__m128i chunk =
_mm_loadu_si128(reinterpret_cast<const __m128i *>(in));
__m128i compare = _mm_cmpeq_epi8(chunk, backslash);
int mask = _mm_movemask_epi8(compare);
if (!mask) {
in += 16;
_mm_storeu_si128(reinterpret_cast<__m128i *>(out), chunk);
out += 16;
continue;
}
#ifdef __GNUC__
int index = __builtin_ffs(mask) - 1;
#else
unsigned long index = 0;
_BitScanForward(&index, mask);
#endif
assert(index >= 0 && index <= 15);
for (decltype(index) i = 0; i < index; ++i)
*out++ = *in++;
break;
}
if (in == limit)
break;
#endif
char c = *in++;
if (c != '\\') {
*out++ = c;
} else {
c = *in++;
if (c == '\\')
*out++ = '\\';
else if (c == '"')
*out++ = '"';
else if (c == '/')
*out++ = '/';
else if (c == 'b')
*out++ = '\b';
else if (c == 'f')
*out++ = '\f';
else if (c == 'n')
*out++ = '\n';
else if (c == 'r')
*out++ = '\r';
else if (c == 't')
*out++ = '\t';
else if (c == 'u') {
std::uint32_t cp = hex_to_int(in);
in += 4;
// surrogate pair?
if (cp >= 0xD800 && cp <= 0xDBFF) {
in += 2;
std::uint32_t trailing = hex_to_int(in);
in += 4;
assert(trailing >= 0xDC00 && trailing <= 0xDFFF);
cp =
((cp - 0xD800) << 10) + (trailing - 0xDC00) + 0x10000;
}
// translate to 1-4 utf-8 chars
out = utf32_to_utf8(out, cp);
} else {
// can't happen unless regexes are wrong
assert(false);
}
}
}
result.resize(out - result.data());
return result;
}
//----------------------------------------------------------------------------
token parser::scan() {
safe_ptr marker(m_cursor, m_limit);
std:
#ifdef UJSON_USE_SSE2
while (m_limit - m_cursor.ptr() >= 16) {
__m128i chunk = _mm_loadu_si128(
reinterpret_cast<const __m128i *>(m_cursor.ptr()));
__m128i tabs = _mm_set1_epi8(0x09);
__m128i newlines = _mm_set1_epi8(0x0A);
__m128i carriage_return = _mm_set1_epi8(0x0D);
__m128i spaces = _mm_set1_epi8(0x20);
__m128i is_tabs = _mm_cmpeq_epi8(chunk, tabs);
__m128i is_newlines = _mm_cmpeq_epi8(chunk, newlines);
__m128i is_carriage_return = _mm_cmpeq_epi8(chunk, carriage_return);
__m128i is_space = _mm_cmpeq_epi8(chunk, spaces);
__m128i is_white_space =
_mm_or_si128(_mm_or_si128(is_tabs, is_newlines),
_mm_or_si128(is_carriage_return, is_space));
int mask = _mm_movemask_epi8(is_white_space);
if (mask == 0xFFFF) {
m_cursor += 16;
continue;
}
#ifdef __GNUC__
m_cursor += __builtin_ffs(~mask) - 1;
#else
unsigned long index = 0;
if (_BitScanForward(&index, ~mask))
m_cursor += index;
#endif
break;
}
#endif
m_token = m_cursor.ptr();
{
std::uint8_t yych;
unsigned int yyaccept = 0;
yych = *m_cursor;
switch (yych) {
case 0x00: goto ujson25;
case '\t':
case '\n':
case '\r':
case ' ': goto ujson23;
case '"': goto ujson22;
case ',': goto ujson4;
case '-': goto ujson18;
case '0': goto ujson19;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': goto ujson21;
case ':': goto ujson2;
case '[': goto ujson10;
case ']': goto ujson12;
case 'f': goto ujson9;
case 'n': goto ujson6;
case 't': goto ujson8;
case '{': goto ujson14;
case '}': goto ujson16;
default: goto ujson27;
}
ujson2:
++m_cursor;
{ return ujson_colon; }
ujson4:
++m_cursor;
{ return ujson_comma; }
ujson6:
yyaccept = 0;
yych = *(marker = ++m_cursor);
switch (yych) {
case 'u': goto ujson91;
default: goto ujson7;
}
ujson7:
{
throw ujson::exception(ujson::error_code::invalid_syntax, line());
}
ujson8:
yyaccept = 0;
yych = *(marker = ++m_cursor);
switch (yych) {
case 'r': goto ujson87;
default: goto ujson7;
}
ujson9:
yyaccept = 0;
yych = *(marker = ++m_cursor);
switch (yych) {
case 'a': goto ujson82;
default: goto ujson7;
}
ujson10:
++m_cursor;
{ return ujson_array_begin; }
ujson12:
++m_cursor;
{ return ujson_array_end; }
ujson14:
++m_cursor;
{ return ujson_object_begin; }
ujson16:
++m_cursor;
{ return ujson_object_end; }
ujson18:
yych = *++m_cursor;
switch (yych) {
case '0': goto ujson81;
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': goto ujson72;
default: goto ujson7;
}
ujson19:
yyaccept = 1;
yych = *(marker = ++m_cursor);
switch (yych) {
case '.': goto ujson74;
case 'E':
case 'e': goto ujson75;
default: goto ujson20;
}
ujson20:
{ return ujson_number; }
ujson21:
yyaccept = 1;
yych = *(marker = ++m_cursor);
goto ujson73;
ujson22:
yyaccept = 0;
yych = *(marker = ++m_cursor);
switch (yych) {
case ' ':
case '!':
case '"':
case '#':
case '$':
case '%':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case '-':
case '.':
case '/':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case ':':
case ';':
case '<':
case '=':
case '>':
case '?':
case '@':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
case 'I':
case 'J':
case 'K':
case 'L':
case 'M':
case 'N':
case 'O':
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
case 'U':
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
case '[':
case '\\':
case ']':
case '^':
case '_':
case '`':
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
case 'i':
case 'j':
case 'k':
case 'l':
case 'm':
case 'n':
case 'o':
case 'p':
case 'q':
case 'r':
case 's':
case 't':
case 'u':
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
case '{':
case '|':
case '}':
case '~':
case 0x7F:
case 0xC2:
case 0xC3:
case 0xC4: