-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathyyjson.c
More file actions
11065 lines (9998 loc) · 401 KB
/
Copy pathyyjson.c
File metadata and controls
11065 lines (9998 loc) · 401 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
/*==============================================================================
Copyright (c) 2020 YaoYuan <ibireme@gmail.com>
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 "yyjson.h"
#include <math.h> /* for `HUGE_VAL/INFINIY/NAN` macros, no libm required */
/*==============================================================================
* MARK: - Warning Suppress (Private)
*============================================================================*/
#if defined(__clang__)
# pragma clang diagnostic ignored "-Wunused-function"
# pragma clang diagnostic ignored "-Wunused-parameter"
# pragma clang diagnostic ignored "-Wunused-label"
# pragma clang diagnostic ignored "-Wunused-macros"
# pragma clang diagnostic ignored "-Wunused-variable"
#elif defined(__GNUC__)
# pragma GCC diagnostic ignored "-Wunused-function"
# pragma GCC diagnostic ignored "-Wunused-parameter"
# pragma GCC diagnostic ignored "-Wunused-label"
# pragma GCC diagnostic ignored "-Wunused-macros"
# pragma GCC diagnostic ignored "-Wunused-variable"
#elif defined(_MSC_VER)
# pragma warning(disable:4100) /* unreferenced formal parameter */
# pragma warning(disable:4101) /* unreferenced variable */
# pragma warning(disable:4102) /* unreferenced label */
# pragma warning(disable:4127) /* conditional expression is constant */
# pragma warning(disable:4706) /* assignment within conditional expression */
#endif
/*==============================================================================
* MARK: - Version (Public)
*============================================================================*/
uint32_t yyjson_version(void) {
return YYJSON_VERSION_HEX;
}
/*==============================================================================
* MARK: - Flags (Private)
*============================================================================*/
/* msvc intrinsic */
#if YYJSON_MSC_VER >= 1400
# include <intrin.h>
# if defined(_M_AMD64) || defined(_M_ARM64)
# define MSC_HAS_BIT_SCAN_64 1
# pragma intrinsic(_BitScanForward64)
# pragma intrinsic(_BitScanReverse64)
# else
# define MSC_HAS_BIT_SCAN_64 0
# endif
# if defined(_M_AMD64) || defined(_M_ARM64) || \
defined(_M_IX86) || defined(_M_ARM)
# define MSC_HAS_BIT_SCAN 1
# pragma intrinsic(_BitScanForward)
# pragma intrinsic(_BitScanReverse)
# else
# define MSC_HAS_BIT_SCAN 0
# endif
# if defined(_M_AMD64)
# define MSC_HAS_UMUL128 1
# pragma intrinsic(_umul128)
# else
# define MSC_HAS_UMUL128 0
# endif
#else
# define MSC_HAS_BIT_SCAN_64 0
# define MSC_HAS_BIT_SCAN 0
# define MSC_HAS_UMUL128 0
#endif
/* gcc builtin */
#if yyjson_has_builtin(__builtin_clzll) || yyjson_gcc_available(3, 4, 0)
# define GCC_HAS_CLZLL 1
#else
# define GCC_HAS_CLZLL 0
#endif
#if yyjson_has_builtin(__builtin_ctzll) || yyjson_gcc_available(3, 4, 0)
# define GCC_HAS_CTZLL 1
#else
# define GCC_HAS_CTZLL 0
#endif
/* int128 type */
#if defined(__SIZEOF_INT128__) && (__SIZEOF_INT128__ == 16) && \
(defined(__GNUC__) || defined(__clang__) || defined(__INTEL_COMPILER))
# define YYJSON_HAS_INT128 1
#else
# define YYJSON_HAS_INT128 0
#endif
/* IEEE 754 floating-point binary representation */
#if defined(__STDC_IEC_559__) || defined(__STDC_IEC_60559_BFP__)
# define YYJSON_HAS_IEEE_754 1
#elif FLT_RADIX == 2 && \
FLT_MANT_DIG == 24 && FLT_DIG == 6 && \
FLT_MIN_EXP == -125 && FLT_MAX_EXP == 128 && \
FLT_MIN_10_EXP == -37 && FLT_MAX_10_EXP == 38 && \
DBL_MANT_DIG == 53 && DBL_DIG == 15 && \
DBL_MIN_EXP == -1021 && DBL_MAX_EXP == 1024 && \
DBL_MIN_10_EXP == -307 && DBL_MAX_10_EXP == 308
# define YYJSON_HAS_IEEE_754 1
#else
# define YYJSON_HAS_IEEE_754 0
# undef YYJSON_DISABLE_FAST_FP_CONV
# define YYJSON_DISABLE_FAST_FP_CONV 1
#endif
/*
Correct rounding in double number computations.
On the x86 architecture, some compilers may use x87 FPU instructions for
floating-point arithmetic. The x87 FPU loads all floating point number as
80-bit double-extended precision internally, then rounds the result to original
precision, which may produce inaccurate results. For a more detailed
explanation, see the paper: https://arxiv.org/abs/cs/0701192
Here are some examples of double precision calculation error:
2877.0 / 1e6 == 0.002877, but x87 returns 0.0028770000000000002
43683.0 * 1e21 == 4.3683e25, but x87 returns 4.3683000000000004e25
Here are some examples of compiler flags to generate x87 instructions on x86:
clang -m32 -mno-sse
gcc/icc -m32 -mfpmath=387
msvc /arch:SSE or /arch:IA32
If we are sure that there's no similar error described above, we can define the
YYJSON_DOUBLE_MATH_CORRECT as 1 to enable the fast path calculation. This is
not an accurate detection, it's just try to avoid the error at compile-time.
An accurate detection can be done at run-time:
bool is_double_math_correct(void) {
volatile double r = 43683.0;
r *= 1e21;
return r == 4.3683e25;
}
See also: utils.h in https://github.com/google/double-conversion/
*/
#if !defined(FLT_EVAL_METHOD) && defined(__FLT_EVAL_METHOD__)
# define FLT_EVAL_METHOD __FLT_EVAL_METHOD__
#endif
#if defined(FLT_EVAL_METHOD) && FLT_EVAL_METHOD != 0 && FLT_EVAL_METHOD != 1
# define YYJSON_DOUBLE_MATH_CORRECT 0
#elif defined(i386) || defined(__i386) || defined(__i386__) || \
defined(_X86_) || defined(__X86__) || defined(_M_IX86) || \
defined(__I86__) || defined(__IA32__) || defined(__THW_INTEL)
# if (defined(_MSC_VER) && defined(_M_IX86_FP) && _M_IX86_FP == 2) || \
(defined(__SSE2_MATH__) && __SSE2_MATH__)
# define YYJSON_DOUBLE_MATH_CORRECT 1
# else
# define YYJSON_DOUBLE_MATH_CORRECT 0
# endif
#elif defined(__mc68000__) || defined(__pnacl__) || defined(__native_client__)
# define YYJSON_DOUBLE_MATH_CORRECT 0
#else
# define YYJSON_DOUBLE_MATH_CORRECT 1
#endif
/*
Detect the endianness at compile-time.
YYJSON_ENDIAN == YYJSON_BIG_ENDIAN
YYJSON_ENDIAN == YYJSON_LITTLE_ENDIAN
*/
#define YYJSON_BIG_ENDIAN 4321
#define YYJSON_LITTLE_ENDIAN 1234
#if yyjson_has_include(<sys/types.h>)
# include <sys/types.h> /* POSIX */
#endif
#if yyjson_has_include(<endian.h>)
# include <endian.h> /* Linux */
#elif yyjson_has_include(<sys/endian.h>)
# include <sys/endian.h> /* BSD, Android */
#elif yyjson_has_include(<machine/endian.h>)
# include <machine/endian.h> /* BSD, Darwin */
#endif
#if defined(BYTE_ORDER) && BYTE_ORDER
# if defined(BIG_ENDIAN) && (BYTE_ORDER == BIG_ENDIAN)
# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
# elif defined(LITTLE_ENDIAN) && (BYTE_ORDER == LITTLE_ENDIAN)
# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
# endif
#elif defined(__BYTE_ORDER) && __BYTE_ORDER
# if defined(__BIG_ENDIAN) && (__BYTE_ORDER == __BIG_ENDIAN)
# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
# elif defined(__LITTLE_ENDIAN) && (__BYTE_ORDER == __LITTLE_ENDIAN)
# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
# endif
#elif defined(__BYTE_ORDER__) && __BYTE_ORDER__
# if defined(__ORDER_BIG_ENDIAN__) && \
(__BYTE_ORDER__ == __ORDER_BIG_ENDIAN__)
# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
# elif defined(__ORDER_LITTLE_ENDIAN__) && \
(__BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__)
# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
# endif
#elif (defined(__LITTLE_ENDIAN__) && __LITTLE_ENDIAN__ == 1) || \
defined(__i386) || defined(__i386__) || \
defined(_X86_) || defined(__X86__) || \
defined(_M_IX86) || defined(__THW_INTEL__) || \
defined(__x86_64) || defined(__x86_64__) || \
defined(__amd64) || defined(__amd64__) || \
defined(_M_AMD64) || defined(_M_X64) || \
defined(_M_ARM) || defined(_M_ARM64) || \
defined(__ARMEL__) || defined(__THUMBEL__) || defined(__AARCH64EL__) || \
defined(_MIPSEL) || defined(__MIPSEL) || defined(__MIPSEL__) || \
defined(__EMSCRIPTEN__) || defined(__wasm__) || \
defined(__loongarch__)
# define YYJSON_ENDIAN YYJSON_LITTLE_ENDIAN
#elif (defined(__BIG_ENDIAN__) && __BIG_ENDIAN__ == 1) || \
defined(__ARMEB__) || defined(__THUMBEB__) || defined(__AARCH64EB__) || \
defined(_MIPSEB) || defined(__MIPSEB) || defined(__MIPSEB__) || \
defined(__or1k__) || defined(__OR1K__)
# define YYJSON_ENDIAN YYJSON_BIG_ENDIAN
#else
# define YYJSON_ENDIAN 0 /* unknown endian, detect at run-time */
#endif
/*
This macro controls how yyjson handles unaligned memory accesses.
By default, yyjson uses `memcpy()` for memory copying. This allows the compiler
to optimize the code and emit unaligned memory access instructions when
supported by the target architecture.
However, on some older compilers or architectures where `memcpy()` is not
well-optimized and may result in unnecessary function calls, defining this
macro as 1 may help. In such cases, yyjson switches to manual byte-by-byte
access, which can potentially improve performance.
An example of the generated assembly code for ARM can be found here:
https://godbolt.org/z/334jjhxPT
This flag is already enabled for common architectures in the following code,
so manual configuration is usually unnecessary. If unsure, you can check the
generated assembly or run benchmarks to make an informed decision.
*/
#ifndef YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
# if defined(__ia64) || defined(_IA64) || defined(__IA64__) || \
defined(__ia64__) || defined(_M_IA64) || defined(__itanium__)
# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* Itanium */
# elif (defined(__arm__) || defined(__arm64__) || defined(__aarch64__)) && \
(defined(__GNUC__) || defined(__clang__)) && \
(!defined(__ARM_FEATURE_UNALIGNED) || !__ARM_FEATURE_UNALIGNED)
# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* ARM */
# elif defined(__sparc) || defined(__sparc__)
# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* SPARC */
# elif defined(__mips) || defined(__mips__) || defined(__MIPS__)
# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* MIPS */
# elif defined(__m68k__) || defined(M68000)
# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 1 /* M68K */
# else
# define YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS 0
# endif
#endif
/*
Estimated initial ratio of the JSON data (data_size / value_count).
For example:
data: {"id":12345678,"name":"Harry"}
data_size: 30
value_count: 5
ratio: 6
yyjson uses dynamic memory with a growth factor of 1.5 when reading and writing
JSON, the ratios below are used to determine the initial memory size.
A too large ratio will waste memory, and a too small ratio will cause multiple
memory growths and degrade performance. Currently, these ratios are generated
with some commonly used JSON datasets.
*/
#define YYJSON_READER_ESTIMATED_PRETTY_RATIO 16
#define YYJSON_READER_ESTIMATED_MINIFY_RATIO 6
#define YYJSON_WRITER_ESTIMATED_PRETTY_RATIO 32
#define YYJSON_WRITER_ESTIMATED_MINIFY_RATIO 18
/* The initial and maximum size of the memory pool's chunk in yyjson_mut_doc. */
#define YYJSON_MUT_DOC_STR_POOL_INIT_SIZE 0x100
#define YYJSON_MUT_DOC_STR_POOL_MAX_SIZE 0x10000000
#define YYJSON_MUT_DOC_VAL_POOL_INIT_SIZE (0x10 * sizeof(yyjson_mut_val))
#define YYJSON_MUT_DOC_VAL_POOL_MAX_SIZE (0x1000000 * sizeof(yyjson_mut_val))
/* The minimum size of the dynamic allocator's chunk. */
#define YYJSON_ALC_DYN_MIN_SIZE 0x1000
/* Default value for compile-time options. */
#ifndef YYJSON_DISABLE_READER
#define YYJSON_DISABLE_READER 0
#endif
#ifndef YYJSON_DISABLE_WRITER
#define YYJSON_DISABLE_WRITER 0
#endif
#ifndef YYJSON_DISABLE_INCR_READER
#define YYJSON_DISABLE_INCR_READER 0
#endif
#ifndef YYJSON_DISABLE_UTILS
#define YYJSON_DISABLE_UTILS 0
#endif
#ifndef YYJSON_DISABLE_FAST_FP_CONV
#define YYJSON_DISABLE_FAST_FP_CONV 0
#endif
#ifndef YYJSON_DISABLE_NON_STANDARD
#define YYJSON_DISABLE_NON_STANDARD 0
#endif
#ifndef YYJSON_DISABLE_UTF8_VALIDATION
#define YYJSON_DISABLE_UTF8_VALIDATION 0
#endif
/*==============================================================================
* MARK: - Macros (Private)
*============================================================================*/
/* Macros used for loop unrolling and other purpose. */
#define repeat2(x) { x x }
#define repeat4(x) { x x x x }
#define repeat8(x) { x x x x x x x x }
#define repeat16(x) { x x x x x x x x x x x x x x x x }
#define repeat2_incr(x) { x(0) x(1) }
#define repeat4_incr(x) { x(0) x(1) x(2) x(3) }
#define repeat8_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) }
#define repeat16_incr(x) { x(0) x(1) x(2) x(3) x(4) x(5) x(6) x(7) \
x(8) x(9) x(10) x(11) x(12) x(13) x(14) x(15) }
#define repeat_in_1_18(x) { x(1) x(2) x(3) x(4) x(5) x(6) x(7) x(8) \
x(9) x(10) x(11) x(12) x(13) x(14) x(15) x(16) \
x(17) x(18) }
/* Macros used to provide branch prediction information for compiler. */
#undef likely
#define likely(x) yyjson_likely(x)
#undef unlikely
#define unlikely(x) yyjson_unlikely(x)
/* Macros used to provide inline information for compiler. */
#undef static_inline
#define static_inline static yyjson_inline
#undef static_noinline
#define static_noinline static yyjson_noinline
/* Macros for min and max. */
#undef yyjson_min
#define yyjson_min(x, y) ((x) < (y) ? (x) : (y))
#undef yyjson_max
#define yyjson_max(x, y) ((x) > (y) ? (x) : (y))
/* Used to write u64 literal for C89 which doesn't support "ULL" suffix. */
#undef U64
#define U64(hi, lo) ((((u64)hi##UL) << 32U) + lo##UL)
#undef U32
#define U32(hi) ((u32)(hi##UL))
/* Used to cast away (remove) const qualifier. */
#define constcast(type) (type)(void *)(size_t)(const void *)
/*
Compiler barriers for single variables.
These macros inform GCC that a read or write access to the given memory
location will occur, preventing certain compiler optimizations or reordering
around the access to 'val'. They do not emit any actual instructions.
This is useful when GCC's default optimization strategies are suboptimal and
precise control over memory access patterns is required.
These barriers are not needed when using Clang or MSVC.
*/
#if YYJSON_IS_REAL_GCC
# define gcc_load_barrier(val) __asm__ volatile(""::"m"(val))
# define gcc_store_barrier(val) __asm__ volatile("":"=m"(val))
# define gcc_full_barrier(val) __asm__ volatile("":"=m"(val):"m"(val))
#else
# define gcc_load_barrier(val)
# define gcc_store_barrier(val)
# define gcc_full_barrier(val)
#endif
/*==============================================================================
* MARK: - Constants (Private)
*============================================================================*/
/* Common error messages. */
#define MSG_FOPEN "failed to open file"
#define MSG_FREAD "failed to read file"
#define MSG_FWRITE "failed to write file"
#define MSG_FCLOSE "failed to close file"
#define MSG_MALLOC "failed to allocate memory"
#define MSG_CHAR_T "invalid literal, expected 'true'"
#define MSG_CHAR_F "invalid literal, expected 'false'"
#define MSG_CHAR_N "invalid literal, expected 'null'"
#define MSG_CHAR "unexpected character, expected a JSON value"
#define MSG_ARR_END "unexpected character, expected ',' or ']'"
#define MSG_OBJ_KEY "unexpected character, expected a string key"
#define MSG_OBJ_SEP "unexpected character, expected ':' after key"
#define MSG_OBJ_END "unexpected character, expected ',' or '}'"
#define MSG_GARBAGE "unexpected content after document"
#define MSG_NOT_END "unexpected end of data"
#define MSG_COMMENT "unclosed multiline comment"
#define MSG_COMMA "trailing comma is not allowed"
#define MSG_NAN_INF "nan or inf number is not allowed"
#define MSG_ERR_TYPE "invalid JSON value type"
#define MSG_ERR_BOM "UTF-8 byte order mark (BOM) is not supported"
#define MSG_ERR_UTF8 "invalid utf-8 encoding in string"
#define MSG_ERR_UTF16 "UTF-16 encoding is not supported"
#define MSG_ERR_UTF32 "UTF-32 encoding is not supported"
/* U64 constant values */
#undef U64_MAX
#define U64_MAX U64(0xFFFFFFFF, 0xFFFFFFFF)
#undef I64_MAX
#define I64_MAX U64(0x7FFFFFFF, 0xFFFFFFFF)
#undef USIZE_MAX
#define USIZE_MAX ((usize)(~(usize)0))
/* Maximum number of digits for reading u32/u64/usize safety (not overflow). */
#undef U32_SAFE_DIG
#define U32_SAFE_DIG 9 /* u32 max is 4294967295, 10 digits */
#undef U64_SAFE_DIG
#define U64_SAFE_DIG 19 /* u64 max is 18446744073709551615, 20 digits */
#undef USIZE_SAFE_DIG
#define USIZE_SAFE_DIG (sizeof(usize) == 8 ? U64_SAFE_DIG : U32_SAFE_DIG)
/* Inf bits (positive) */
#define F64_BITS_INF U64(0x7FF00000, 0x00000000)
/* NaN bits (quiet NaN, no payload, no sign) */
#if defined(__hppa__) || (defined(__mips__) && !defined(__mips_nan2008))
#define F64_BITS_NAN U64(0x7FF7FFFF, 0xFFFFFFFF)
#else
#define F64_BITS_NAN U64(0x7FF80000, 0x00000000)
#endif
/* maximum significant digits count in decimal when reading double number */
#define F64_MAX_DEC_DIG 768
/* maximum decimal power of double number (1.7976931348623157e308) */
#define F64_MAX_DEC_EXP 308
/* minimum decimal power of double number (4.9406564584124654e-324) */
#define F64_MIN_DEC_EXP (-324)
/* maximum binary power of double number */
#define F64_MAX_BIN_EXP 1024
/* minimum binary power of double number */
#define F64_MIN_BIN_EXP (-1021)
/* float/double number bits */
#define F32_BITS 32
#define F64_BITS 64
/* float/double number exponent part bits */
#define F32_EXP_BITS 8
#define F64_EXP_BITS 11
/* float/double number significand part bits */
#define F32_SIG_BITS 23
#define F64_SIG_BITS 52
/* float/double number significand part bits (with 1 hidden bit) */
#define F32_SIG_FULL_BITS 24
#define F64_SIG_FULL_BITS 53
/* float/double number significand bit mask */
#define F32_SIG_MASK U32(0x007FFFFF)
#define F64_SIG_MASK U64(0x000FFFFF, 0xFFFFFFFF)
/* float/double number exponent bit mask */
#define F32_EXP_MASK U32(0x7F800000)
#define F64_EXP_MASK U64(0x7FF00000, 0x00000000)
/* float/double number exponent bias */
#define F32_EXP_BIAS 127
#define F64_EXP_BIAS 1023
/* float/double number significant digits count in decimal */
#define F32_DEC_DIG 9
#define F64_DEC_DIG 17
/* buffer length required for float/double number writer */
#define FP_BUF_LEN 40
/* maximum length of a number in incremental parsing */
#define INCR_NUM_MAX_LEN 1024
/*==============================================================================
* MARK: - Types (Private)
*============================================================================*/
/** Type define for primitive types. */
typedef float f32;
typedef double f64;
typedef int8_t i8;
typedef uint8_t u8;
typedef int16_t i16;
typedef uint16_t u16;
typedef int32_t i32;
typedef uint32_t u32;
typedef int64_t i64;
typedef uint64_t u64;
typedef size_t usize;
/** 128-bit integer, used by floating-point number reader and writer. */
#if YYJSON_HAS_INT128
__extension__ typedef __int128 i128;
__extension__ typedef unsigned __int128 u128;
#endif
/** 16/32/64-bit vector */
typedef struct v16 { char c[2]; } v16;
typedef struct v32 { char c[4]; } v32;
typedef struct v64 { char c[8]; } v64;
/** 16/32/64-bit vector union */
typedef union v16_uni { v16 v; u16 u; } v16_uni;
typedef union v32_uni { v32 v; u32 u; } v32_uni;
typedef union v64_uni { v64 v; u64 u; } v64_uni;
/*==============================================================================
* MARK: - Load/Store Utils (Private)
*============================================================================*/
#define byte_move_idx(x) ((char *)dst)[x] = ((const char *)src)[x];
#define byte_move_src(x) ((char *)tmp)[x] = ((const char *)src)[x];
#define byte_move_dst(x) ((char *)dst)[x] = ((const char *)tmp)[x];
/** Same as `memcpy(dst, src, 2)`, no overlap. */
static_inline void byte_copy_2(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
memcpy(dst, src, 2);
#else
repeat2_incr(byte_move_idx)
#endif
}
/** Same as `memcpy(dst, src, 4)`, no overlap. */
static_inline void byte_copy_4(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
memcpy(dst, src, 4);
#else
repeat4_incr(byte_move_idx)
#endif
}
/** Same as `memcpy(dst, src, 8)`, no overlap. */
static_inline void byte_copy_8(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
memcpy(dst, src, 8);
#else
repeat8_incr(byte_move_idx)
#endif
}
/** Same as `memcpy(dst, src, 16)`, no overlap. */
static_inline void byte_copy_16(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
memcpy(dst, src, 16);
#else
repeat16_incr(byte_move_idx)
#endif
}
/** Same as `memmove(dst, src, 2)`, allows overlap. */
static_inline void byte_move_2(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
u16 tmp;
memcpy(&tmp, src, 2);
memcpy(dst, &tmp, 2);
#else
char tmp[2];
repeat2_incr(byte_move_src)
repeat2_incr(byte_move_dst)
#endif
}
/** Same as `memmove(dst, src, 4)`, allows overlap. */
static_inline void byte_move_4(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
u32 tmp;
memcpy(&tmp, src, 4);
memcpy(dst, &tmp, 4);
#else
char tmp[4];
repeat4_incr(byte_move_src)
repeat4_incr(byte_move_dst)
#endif
}
/** Same as `memmove(dst, src, 8)`, allows overlap. */
static_inline void byte_move_8(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
u64 tmp;
memcpy(&tmp, src, 8);
memcpy(dst, &tmp, 8);
#else
char tmp[8];
repeat8_incr(byte_move_src)
repeat8_incr(byte_move_dst)
#endif
}
/** Same as `memmove(dst, src, 16)`, allows overlap. */
static_inline void byte_move_16(void *dst, const void *src) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
char *pdst = (char *)dst;
const char *psrc = (const char *)src;
u64 tmp1, tmp2;
memcpy(&tmp1, psrc, 8);
memcpy(&tmp2, psrc + 8, 8);
memcpy(pdst, &tmp1, 8);
memcpy(pdst + 8, &tmp2, 8);
#else
char tmp[16];
repeat16_incr(byte_move_src)
repeat16_incr(byte_move_dst)
#endif
}
/** Same as `memmove(dst, src, n)`, but only `dst <= src` and `n <= 16`. */
static_inline void byte_move_forward(void *dst, void *src, usize n) {
char *d = (char *)dst, *s = (char *)src;
n += (n % 2); /* round up to even */
if (n == 16) { byte_move_16(d, s); return; }
if (n >= 8) { byte_move_8(d, s); n -= 8; d += 8; s += 8; }
if (n >= 4) { byte_move_4(d, s); n -= 4; d += 4; s += 4; }
if (n >= 2) { byte_move_2(d, s); }
}
/** Same as `memcmp(buf, pat, 2) == 0`. */
static_inline bool byte_match_2(void *buf, const char *pat) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
v16_uni u1, u2;
memcpy(&u1, buf, 2);
memcpy(&u2, pat, 2);
return u1.u == u2.u;
#else
return ((char *)buf)[0] == ((const char *)pat)[0] &&
((char *)buf)[1] == ((const char *)pat)[1];
#endif
}
/** Same as `memcmp(buf, pat, 4) == 0`. */
static_inline bool byte_match_4(void *buf, const char *pat) {
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
v32_uni u1, u2;
memcpy(&u1, buf, 4);
memcpy(&u2, pat, 4);
return u1.u == u2.u;
#else
return ((char *)buf)[0] == ((const char *)pat)[0] &&
((char *)buf)[1] == ((const char *)pat)[1] &&
((char *)buf)[2] == ((const char *)pat)[2] &&
((char *)buf)[3] == ((const char *)pat)[3];
#endif
}
/** Loads 2 bytes from `src` as a u16 (native-endian). */
static_inline u16 byte_load_2(const void *src) {
v16_uni uni;
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
memcpy(&uni, src, 2);
#else
uni.v.c[0] = ((const char *)src)[0];
uni.v.c[1] = ((const char *)src)[1];
#endif
return uni.u;
}
/** Loads 3 bytes from `src` as a u32 (native-endian). */
static_inline u32 byte_load_3(const void *src) {
v32_uni uni;
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
memcpy(&uni, src, 2);
uni.v.c[2] = ((const char *)src)[2];
uni.v.c[3] = 0;
#else
uni.v.c[0] = ((const char *)src)[0];
uni.v.c[1] = ((const char *)src)[1];
uni.v.c[2] = ((const char *)src)[2];
uni.v.c[3] = 0;
#endif
return uni.u;
}
/** Loads 4 bytes from `src` as a u32 (native-endian). */
static_inline u32 byte_load_4(const void *src) {
v32_uni uni;
#if !YYJSON_DISABLE_UNALIGNED_MEMORY_ACCESS
memcpy(&uni, src, 4);
#else
uni.v.c[0] = ((const char *)src)[0];
uni.v.c[1] = ((const char *)src)[1];
uni.v.c[2] = ((const char *)src)[2];
uni.v.c[3] = ((const char *)src)[3];
#endif
return uni.u;
}
/*==============================================================================
* MARK: - Character Utils (Private)
* These lookup tables were generated by `misc/make_tables.c`.
*============================================================================*/
/* char_table1 */
#define CHAR_TYPE_ASCII (1 << 0) /* Except: ["\], [0x00-0x1F, 0x80-0xFF] */
#define CHAR_TYPE_ASCII_SQ (1 << 1) /* Except: ['\], [0x00-0x1F, 0x80-0xFF] */
#define CHAR_TYPE_SPACE (1 << 2) /* Whitespace: [ \t\n\r] */
#define CHAR_TYPE_SPACE_EXT (1 << 3) /* Whitespace: [ \t\n\r\v\f], JSON5 */
#define CHAR_TYPE_NUM (1 << 4) /* Number: [.-+0-9] */
#define CHAR_TYPE_COMMENT (1 << 5) /* Comment: [/] */
/* char_table2 */
#define CHAR_TYPE_EOL (1 << 0) /* End of line: [\r\n] */
#define CHAR_TYPE_EOL_EXT (1 << 1) /* End of line: [\r\n], JSON5 */
#define CHAR_TYPE_ID_START (1 << 2) /* ID start: [_$A-Za-z\], U+0080+ */
#define CHAR_TYPE_ID_NEXT (1 << 3) /* ID next: [_$A-Za-z0-9\], U+0080+ */
#define CHAR_TYPE_ID_ASCII (1 << 4) /* ID next ASCII: [_$A-Za-z0-9] */
/* char_table3 */
#define CHAR_TYPE_SIGN (1 << 0) /* [-+] */
#define CHAR_TYPE_DIGIT (1 << 1) /* [0-9] */
#define CHAR_TYPE_NONZERO (1 << 2) /* [1-9] */
#define CHAR_TYPE_EXP (1 << 3) /* [eE] */
#define CHAR_TYPE_DOT (1 << 4) /* [.] */
static const u8 char_table1[256] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0C, 0x0C, 0x08, 0x08, 0x0C, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0F, 0x03, 0x02, 0x03, 0x03, 0x03, 0x03, 0x01,
0x03, 0x03, 0x03, 0x13, 0x03, 0x13, 0x13, 0x23,
0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13, 0x13,
0x13, 0x13, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x00, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x08,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
static const u8 char_table2[256] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x1C, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18, 0x18,
0x18, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x1C, 0x1C, 0x1C, 0x00, 0x0C, 0x00, 0x00, 0x1C,
0x00, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C, 0x1C,
0x1C, 0x1C, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C,
0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C
};
static const u8 char_table3[256] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x10, 0x00,
0x02, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06, 0x06,
0x06, 0x06, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
/** Match a whitespace: [ \t\n\r]. */
static_inline bool char_is_space(u8 c) {
return !!(char_table1[c] & CHAR_TYPE_SPACE);
}
/** Match an extended whitespace: [ \t\n\r\\v\\f], JSON5 whitespace. */
static_inline bool char_is_space_ext(u8 c) {
return !!(char_table1[c] & CHAR_TYPE_SPACE_EXT);
}
/** Match a JSON number: [.-+0-9]. */
static_inline bool char_is_num(u8 c) {
return !!(char_table1[c] & CHAR_TYPE_NUM);
}
/** Match an ASCII character in string: ["\], [0x00-0x1F, 0x80-0xFF]. */
static_inline bool char_is_ascii_skip(u8 c) {
return !!(char_table1[c] & CHAR_TYPE_ASCII);
}
/** Match an ASCII character single-quoted: ['\], [0x00-0x1F, 0x80-0xFF]. */
static_inline bool char_is_ascii_skip_sq(u8 c) {
return !!(char_table1[c] & CHAR_TYPE_ASCII_SQ);
}
/** Match a trivia character: extended whitespace or comment. */
static_inline bool char_is_trivia(u8 c) {
return !!(char_table1[c] & (CHAR_TYPE_SPACE_EXT | CHAR_TYPE_COMMENT));
}
/** Match a line end character: [\r\n]. */
static_inline bool char_is_eol(u8 c) {
return !!(char_table2[c] & CHAR_TYPE_EOL);
}
/** Match an extended line end character: [\r\n], JSON5 line terminator. */
static_inline bool char_is_eol_ext(u8 c) {
return !!(char_table2[c] & CHAR_TYPE_EOL_EXT);
}
/** Match an identifier name start: [_$A-Za-z\], U+0080+. */
static_inline bool char_is_id_start(u8 c) {
return !!(char_table2[c] & CHAR_TYPE_ID_START);
}
/** Match an identifier name next: [_$A-Za-z0-9\], U+0080+. */
static_inline bool char_is_id_next(u8 c) {
return !!(char_table2[c] & CHAR_TYPE_ID_NEXT);
}
/** Match an identifier name ASCII: [_$A-Za-z0-9]. */
static_inline bool char_is_id_ascii(u8 c) {
return !!(char_table2[c] & CHAR_TYPE_ID_ASCII);
}
/** Match a sign: [+-] */
static_inline bool char_is_sign(u8 d) {
return !!(char_table3[d] & CHAR_TYPE_SIGN);
}
/** Match a none-zero digit: [1-9] */
static_inline bool char_is_nonzero(u8 d) {
return !!(char_table3[d] & CHAR_TYPE_NONZERO);
}
/** Match a digit: [0-9] */
static_inline bool char_is_digit(u8 d) {
return !!(char_table3[d] & CHAR_TYPE_DIGIT);
}
/** Match an exponent sign: [eE]. */
static_inline bool char_is_exp(u8 d) {
return !!(char_table3[d] & CHAR_TYPE_EXP);
}
/** Match a floating point indicator: [.eE]. */
static_inline bool char_is_fp(u8 d) {
return !!(char_table3[d] & (CHAR_TYPE_DOT | CHAR_TYPE_EXP));
}
/** Match a digit or floating point indicator: [0-9.eE]. */
static_inline bool char_is_digit_or_fp(u8 d) {
return !!(char_table3[d] & (CHAR_TYPE_DIGIT | CHAR_TYPE_DOT |
CHAR_TYPE_EXP));
}
/** Match a JSON container: `{` or `[`. */
static_inline bool char_is_ctn(u8 c) {
return (c & 0xDF) == 0x5B; /* '[': 0x5B, '{': 0x7B */
}
/** Convert ASCII letter to lowercase; valid only for [A-Za-z]. */
static_inline u8 char_to_lower(u8 c) {
return c | 0x20;
}
/** Match UTF-8 byte order mask. */
static_inline bool is_utf8_bom(const u8 *cur) {
return byte_load_3(cur) == byte_load_3("\xEF\xBB\xBF");
}
/** Match UTF-16 byte order mask. */
static_inline bool is_utf16_bom(const u8 *cur) {
return byte_load_2(cur) == byte_load_2("\xFE\xFF") ||
byte_load_2(cur) == byte_load_2("\xFF\xFE");
}
/** Match UTF-32 byte order mask, need length check to avoid zero padding. */
static_inline bool is_utf32_bom(const u8 *cur) {
return byte_load_4(cur) == byte_load_4("\x00\x00\xFE\xFF") ||
byte_load_4(cur) == byte_load_4("\xFF\xFE\x00\x00");
}
/** Get the extended line end length. Used with `char_is_eol_ext`. */
static_inline usize ext_eol_len(const u8 *cur) {
if (cur[0] < 0x80) return 1;
if (cur[1] == 0x80 && (cur[2] == 0xA8 || cur[2] == 0xA9)) return 3;
return 0;
}
/** Get the extended whitespace length. Used with `char_is_space_ext`. */
static_inline usize ext_space_len(const u8 *cur) {
if (cur[0] < 0x80) {
return 1;
} else if (byte_load_2(cur) == byte_load_2("\xC2\xA0")) {
return 2;
} else if (byte_load_2(cur) == byte_load_2("\xE2\x80")) {
if (cur[2] >= 0x80 && cur[2] <= 0x8A) return 3;
if (cur[2] == 0xA8 || cur[2] == 0xA9 || cur[2] == 0xAF) return 3;