-
Notifications
You must be signed in to change notification settings - Fork 44
/
ajson.hpp
2181 lines (2047 loc) · 49.1 KB
/
ajson.hpp
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
#pragma once
/*
__ _____ _____ _____
__| | __| | | | JSON for ORM C++ struct
| | |__ | | | | | | version 0.2.2
|_____|_____|_____|_|___| https://github.com/lordoffox/ajson
Licensed under the boost 1.0 License
Copyright (c) 2013-2018 Evan
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, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <array>
#include <cstdint>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <exception>
#include <deque>
#include <list>
#include <map>
#include <memory>
#include <queue>
#include <string>
#include <type_traits>
#include <vector>
#include <unordered_map>
#include <ios>
#define STRINGFY_LIST(...) #__VA_ARGS__
namespace ajson
{
namespace detail
{
struct string_ref
{
char const * str = nullptr;
size_t len = 0;
bool operator == (string_ref const& rhs) const
{
if (len == rhs.len)
{
return std::memcmp(str, rhs.str, len) == 0;
}
return false;
}
};
typedef ::std::vector<string_ref> field_list;
inline void add_field(char const * pre, char const * cur, field_list& fields)
{
size_t len = cur - pre;
if (len > 0)
{
fields.emplace_back();
auto& f = fields.back();
f.str = pre;
f.len = len;
}
}
inline char const * char_table()
{
static char table[] = {
16, 16, 16, 16, 16, 16, 16, 16, 16, 17, 17, 16, 17, 17, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 17, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 16, 16, 16, 16, 16,
16, 16, 10, 11, 12, 13, 14, 15, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
16, 16, 10, 11, 12, 13, 14, 15 };
return table;
}
inline bool is_ws(char c)
{
if (c > ' ' || c <= 0)
return false;
return char_table()[(size_t)c] == 17;
}
inline char const * skip_ws(char const * str)
{
while (is_ws(*str))++str;
return str;
}
//input "v.abc,v.def,v.xyz"
//output vector<string_ref> = ["abc","def","xyz"];
inline field_list split_fields(char const * info)
{
field_list fields;
char const * pre = info = skip_ws(info);
while (*info != 0)
{
++info;
if (is_ws(*info))
{
add_field(pre, info, fields);
info = skip_ws(info);
++info;
pre = skip_ws(info);
}
else if (*info == ',')
{
add_field(pre, info, fields);
++info;
pre = skip_ws(info);
}
}
add_field(pre, info, fields);
return fields;
}
template< class T >
struct is_signed_intergral_like : std::integral_constant < bool,
(std::is_integral<T>::value) &&
std::is_signed<T>::value
> {};
template< class T >
struct is_unsigned_intergral_like : std::integral_constant < bool,
(std::is_integral<T>::value) &&
std::is_unsigned<T>::value
> {};
template < template <typename...> class U, typename T >
struct is_template_instant_of : std::false_type {};
template < template <typename...> class U, typename... args >
struct is_template_instant_of< U, U<args...> > : std::true_type{};
template<typename T>
struct is_stdstring : is_template_instant_of < std::basic_string, T >
{};
template< class T >
struct is_sequence_container : std::integral_constant < bool,
is_template_instant_of<std::deque, T>::value ||
is_template_instant_of<std::list, T>::value ||
is_template_instant_of<std::vector, T>::value ||
is_template_instant_of<std::queue, T>::value
> {};
template< class T >
struct is_associat_container : std::integral_constant < bool,
is_template_instant_of<std::map, T>::value ||
is_template_instant_of<std::unordered_map, T>::value
> {};
template< class T >
struct is_emplace_back_able : std::integral_constant < bool,
is_template_instant_of<std::deque, T>::value ||
is_template_instant_of<std::list, T>::value ||
is_template_instant_of<std::vector, T>::value
> {};
}
struct token
{
detail::string_ref str;
enum
{
t_string,
t_int,
t_uint,
t_number,
t_ctrl,
t_end,
} type;
union
{
int64_t i64;
uint64_t u64;
double d64;
} value;
bool neg = false;
};
#ifdef _MSC_VER
#define NOEXCEPT
#else
#define NOEXCEPT noexcept
#endif
class exception : public std::exception
{
std::string msg_;
public:
exception(std::string const& msg)
:msg_(msg)
{}
char const * what() const NOEXCEPT{ return msg_.c_str(); }
};
class reader
{
token cur_tok_;
size_t cur_col_ = 0;
size_t cur_line_ = 0;
size_t len_ = 0;
size_t cur_offset_ = 0;
bool end_mark_ = false;
const char * ptr_ = nullptr;
double decimal = 0.1;
int exp = 0;
inline void decimal_reset(){ decimal = 0.1; }
inline char read() const
{
if (end_mark_)
return 0;
return ptr_[cur_offset_];
}
inline void take()
{
if (end_mark_ == false)
{
++cur_offset_;
char v = ptr_[cur_offset_];
if (v != '\r')
++cur_col_;
if (len_ > 0 && cur_offset_ >= len_)
{
end_mark_ = true;
}
else if (v == 0)
{
end_mark_ = true;
}
if (v == '\n')
{
cur_col_ = 0;
++cur_line_;
}
}
}
char skip()
{
auto c = read();
while (c == ' ' || c == '\t' || c == '\r' || c == '\n')
{
take();
c = read();
}
return c;
}
void parser_quote_string()
{
take();
cur_tok_.str.str = ptr_ + cur_offset_;
auto c = read();
do
{
switch (c)
{
case 0:
case '\n':
{
error("not a valid quote string!");
break;
}
case '\\':
{
take();
c = read();
break;
}
case '"':
{
cur_tok_.str.len = ptr_ + cur_offset_ - cur_tok_.str.str;
take();
return;
}
}
take();
c = read();
} while (true);
}
void parser_string()
{
cur_tok_.str.str = ptr_ + cur_offset_;
take();
auto c = read();
do
{
switch (c)
{
case 0:
{
error("not a valid string!");
break;
}
case ' ':
case '\t':
case '\r':
case '\n':
case ',':
case '[':
case ']':
case ':':
case '{':
case '}':
{
cur_tok_.str.len = ptr_ + cur_offset_ - cur_tok_.str.str;
return;
}
}
take();
c = read();
} while (true);
}
void parser_exp_pos()
{
take();
auto c = read();
do
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
exp *= 10;
exp += (c - '0');
break;
}
default:
{
for (int i = 0; i < exp; ++i)
{
cur_tok_.value.d64 *= 10.0;
}
exp = 0;
cur_tok_.str.len = ptr_ + cur_offset_ - cur_tok_.str.str;
return;
}
}
take();
c = read();
} while (1);
}
void parser_exp_neg()
{
take();
auto c = read();
do
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
exp *= 10;
exp += (c - '0');
break;
}
default:
{
for (int i = 0; i < exp; ++i)
{
cur_tok_.value.d64 *= 0.1;
}
exp = 0;
cur_tok_.str.len = ptr_ + cur_offset_ - cur_tok_.str.str;
return;
}
}
take();
c = read();
} while (1);
}
void parser_exp()
{
take();
auto c = read();
do
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
exp *= 10;
exp += (c - '0');
parser_exp_pos();
return;
}
case '-':
{
parser_exp_neg();
return;
}
default:
{
exp = 0;
cur_tok_.str.len = ptr_ + cur_offset_ - cur_tok_.str.str;
return;
}
}
} while (1);
}
void parser_number()
{
cur_tok_.str.str = ptr_ + cur_offset_;
take();
auto c = read();
do
{
switch (c)
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
{
if (cur_tok_.type == token::t_int)
{
cur_tok_.value.i64 *= 10;
cur_tok_.value.i64 += c - '0';
}
else if (cur_tok_.type == token::t_uint)
{
cur_tok_.value.u64 *= 10;
cur_tok_.value.u64 += c - '0';
}
else if (cur_tok_.type == token::t_number)
{
cur_tok_.value.d64 += decimal * (c - '0');
decimal *= 0.1;
}
break;
}
case '.':
{
if (cur_tok_.type == token::t_int)
{
cur_tok_.type = token::t_number;
cur_tok_.value.d64 = (double)cur_tok_.value.i64;
decimal_reset();
}
else if (cur_tok_.type == token::t_uint)
{
cur_tok_.type = token::t_number;
cur_tok_.value.d64 = (double)cur_tok_.value.u64;
decimal_reset();
}
else if (cur_tok_.type == token::t_number)
{
error("not a valid number!");
}
break;
}
case 'e':
case 'E':
{
if (cur_tok_.type == token::t_int)
{
cur_tok_.type = token::t_number;
cur_tok_.value.d64 = (double)cur_tok_.value.i64;
}
else if (cur_tok_.type == token::t_uint)
{
cur_tok_.type = token::t_number;
cur_tok_.value.d64 = (double)cur_tok_.value.u64;
}
parser_exp();
}
default:
{
cur_tok_.str.len = ptr_ + cur_offset_ - cur_tok_.str.str;
return;
}
}
take();
c = read();
} while (1);
}
public:
reader(const char * ptr = nullptr, size_t len = -1)
:len_(len), ptr_(ptr)
{
if (ptr == nullptr)
{
end_mark_ = true;
}
else if (len == 0)
{
end_mark_ = true;
}
else if (ptr[0] == 0)
{
end_mark_ = true;
}
next();
}
static inline char* itoa_native(size_t val , char * buffer , size_t len)
{
buffer[len] = 0;
size_t pos = len - 1;
if (val == 0)
{
buffer[pos--] = '0';
}
while (val)
{
buffer[pos--] = (char)((val % 10) + '0');
val = val / 10;
}
++pos;
return &buffer[0] + pos;
}
inline void error(const char * message) const
{
char buffer[20];
std::string msg = "error at line :";
msg += itoa_native(cur_line_,buffer,19);
msg += " col :";
msg += itoa_native(cur_col_, buffer, 19);
msg += " msg:";
msg += message;
throw exception(msg);
}
inline token const& peek() const
{
return cur_tok_;
}
void next()
{
auto c = skip();
bool do_next = false;
cur_tok_.neg = false;
switch (c)
{
case 0:
cur_tok_.type = token::t_end;
cur_tok_.str.str = ptr_ + cur_offset_;
cur_tok_.str.len = 1;
break;
case '{':
case '}':
case '[':
case ']':
case ':':
case ',':
{
cur_tok_.type = token::t_ctrl;
cur_tok_.str.str = ptr_ + cur_offset_;
cur_tok_.str.len = 1;
take();
break;
}
case '/':
{
take();
c = read();
if (c == '/')
{
take();
c = read();
while (c != '\n' && c != 0)
{
take();
c = read();
}
do_next = true;
break;
}
else if (c == '*')
{
take();
c = read();
do
{
while (c != '*')
{
if (c == 0)
{
return;
}
take();
c = read();
}
take();
c = read();
if (c == '/')
{
take();
do_next = true;
break;
}
} while (true);
}
//error parser comment
error("not a comment!");
}
case '"':
{
cur_tok_.type = token::t_string;
parser_quote_string();
break;
}
default:
{
if (c >= '0' && c <= '9')
{
cur_tok_.type = token::t_uint;
cur_tok_.value.u64 = c - '0';
parser_number();
}
else if (c == '-')
{
cur_tok_.type = token::t_int;
cur_tok_.value.i64 = 0;
cur_tok_.neg = true;
parser_number();
}
else
{
cur_tok_.type = token::t_string;
parser_string();
}
}
}
if (do_next == false)
return;
next();
}
inline bool expect(char c)
{
return cur_tok_.str.str[0] == c;
}
};
template <typename alloc_ty>
struct ajson_string_stream
{
private:
alloc_ty alloc;
public:
enum { good, read_overflow };
char * m_header_ptr;
char * m_read_ptr;
char * m_write_ptr;
char * m_tail_ptr;
int m_status;
std::size_t m_length;
enum{ INIT_BUFF_SIZE = 1024 };
ajson_string_stream() :m_status(good), m_length(INIT_BUFF_SIZE)
{
this->m_header_ptr = this->alloc.allocate(INIT_BUFF_SIZE);
this->m_read_ptr = this->m_header_ptr;
this->m_write_ptr = this->m_header_ptr;
this->m_tail_ptr = this->m_header_ptr + m_length;
}
~ajson_string_stream()
{
this->alloc.deallocate(m_header_ptr, this->m_length);
}
inline std::size_t read(char * buffer, std::size_t len)
{
if (this->m_read_ptr + len > this->m_tail_ptr)
{
m_status = read_overflow;
return 0;
}
std::memcpy(buffer, this->m_read_ptr, len);
this->m_read_ptr += len;
return len;
}
inline std::size_t growpup(std::size_t want_size)
{
std::size_t new_size = ((want_size + INIT_BUFF_SIZE - 1) / INIT_BUFF_SIZE)*INIT_BUFF_SIZE;
std::size_t write_pos = this->m_write_ptr - this->m_header_ptr;
std::size_t read_pos = this->m_read_ptr - this->m_header_ptr;
char * temp = this->m_header_ptr;
this->m_header_ptr = this->alloc.allocate(new_size);
std::memcpy(this->m_header_ptr, temp, this->m_length);
this->alloc.deallocate(temp, this->m_length);
this->m_length = new_size;
this->m_write_ptr = this->m_header_ptr + write_pos;
this->m_read_ptr = this->m_header_ptr + read_pos;
this->m_tail_ptr = this->m_header_ptr + m_length;
return new_size;
}
inline std::size_t write(const char * buffer, std::size_t len)
{
std::size_t writed_len = this->m_write_ptr + len - this->m_header_ptr;
if (writed_len > this->m_length)
{
this->growpup(writed_len);
}
std::memcpy((void*)this->m_write_ptr, buffer, len);
this->m_write_ptr += len;
return len;
}
inline void put(char c)
{
std::size_t writed_len = this->m_write_ptr + 1 - this->m_header_ptr;
if (writed_len > this->m_length)
{
this->growpup(writed_len);
}
*this->m_write_ptr = c;
++this->m_write_ptr;
}
inline bool bad()const{ return m_status != good; }
inline ajson_string_stream& seekp(int offset, int seek_dir)
{
switch (seek_dir)
{
case std::ios::beg:
{
if (offset < 0)
{
offset = 0;
}
this->m_write_ptr = this->m_header_ptr + offset;
break;
}
case std::ios::cur:
{
if (offset < 0)
{
offset = offset + int(this->m_write_ptr - this->m_header_ptr);
if (offset < 0)
{
offset = 0;
}
this->m_write_ptr = this->m_header_ptr + offset;
}
else
{
if (this->m_write_ptr + offset > this->m_tail_ptr)
{
this->m_write_ptr = this->m_tail_ptr;
}
}
break;
}
case std::ios::end:
{
if (offset < 0)
{
offset = offset + int(this->m_write_ptr - this->m_header_ptr);
if (offset < 0)
{
offset = 0;
}
this->m_write_ptr = this->m_header_ptr + offset;
}
break;
}
}
return *this;
}
inline void clear()
{
this->m_read_ptr = this->m_header_ptr;
this->m_write_ptr = this->m_header_ptr;
}
inline const char * data() const
{
return this->m_header_ptr;
}
std::basic_string<char, std::char_traits<char>, alloc_ty> str()
{
std::basic_string<char, std::char_traits<char>, alloc_ty> s(this->m_header_ptr, this->write_length());
return s;
}
inline ::std::size_t read_length() const
{
return this->m_read_ptr - this->m_header_ptr;
}
inline ::std::size_t write_length() const
{
return this->m_write_ptr - this->m_header_ptr;
}
};
typedef ajson_string_stream<std::allocator<char> > string_stream;
struct ajson_file_stream
{
public:
enum { good, read_overflow, file_error };
FILE * m_f;
int m_status;
enum{ INIT_AMSG_BUFF_SIZE = 1024 };
ajson_file_stream(const char * filename) :m_f(NULL), m_status(good)
{
this->m_f = std::fopen(filename, "w");
if (NULL == this->m_f)
{
this->m_status = file_error;
}
}
~ajson_file_stream()
{
if (this->m_f)
{
fclose(m_f);
}
}
inline std::size_t read(char * buffer, std::size_t len)
{
std::size_t rlen = std::fread(buffer, len, 1, this->m_f);
return rlen;
}
inline std::size_t write(const char * buffer, std::size_t len)
{
std::size_t wlen = std::fwrite(buffer, len, 1, this->m_f);
return wlen;
}
inline void put(char c)
{
std::fputc(c, this->m_f);
}
inline bool bad(){ return m_status != good; }
inline int seekp(size_t offset, int seek_dir)
{
return std::fseek(this->m_f, (int)offset, seek_dir);
}
inline void clear()
{
std::fseek(this->m_f, 0, SEEK_SET);
}
};
template<typename stream_ty>
struct lite_write
{
stream_ty& s_;
lite_write(stream_ty& s) :s_(s){}
inline void write_bool(bool v)
{
if (v)
s_.put('1');
else
s_.put('0');
}
inline void write_liter(char const * str, size_t len)
{
s_.write(str, len);
}
inline uint8_t decode(uint8_t& state, uint32_t& codep, const uint8_t byte)
{
static const std::array<uint8_t, 400> utf8d =
{
{
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 00..1F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 20..3F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 40..5F
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, // 60..7F
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, 9, // 80..9F
7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, // A0..BF
8, 8, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C0..DF
0xA, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x3, 0x4, 0x3, 0x3, // E0..EF
0xB, 0x6, 0x6, 0x6, 0x5, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, 0x8, // F0..FF
0x0, 0x1, 0x2, 0x3, 0x5, 0x8, 0x7, 0x1, 0x1, 0x1, 0x4, 0x6, 0x1, 0x1, 0x1, 0x1, // s0..s0
1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, // s1..s2
1, 2, 1, 1, 1, 1, 1, 2, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // s3..s4
1, 2, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, // s5..s6
1, 3, 1, 1, 1, 1, 1, 3, 1, 3, 1, 1, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // s7..s8
}
};
const uint8_t type = utf8d[byte];
codep = (state != 0)
? (byte & 0x3fu) | (codep << 6)
: static_cast<uint32_t>(0xff >> type) & (byte);
state = utf8d[256u + state * 16u + type];
return state;
}
inline void write_str(char const * str, size_t len)
{
static char const * hex_table = "0123456789ABCDEF";
put('"');
char const * ptr = str;
char const * end = ptr + len;
uint32_t codepoint;
uint8_t state = 0;
while (ptr < end)
{
char c = *ptr;
if (c == 0)
break;
++ptr;
decode(state, codepoint, (uint8_t)c);
if (state== 0)
{
switch (codepoint)
{
case 0x08: // backspace
{
put('\\');
put('b');
break;
}
case 0x09: // horizontal tab
{
put('\\');
put('t');
break;
}
case 0x0A: // newline
{
put('\\');
put('n');
break;
}
case 0x0C: // formfeed
{
put('\\');
put('f');