-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathzzzjson.h
2761 lines (2586 loc) · 83.3 KB
/
zzzjson.h
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
#ifndef zj_JSON_H
#define zj_JSON_H
#include <stdbool.h> // 使用其 bool
#include <stdint.h> // 使用其 uint32_t
#include <stdio.h> // 使用其 sprintf 函数
#include <stdlib.h> // 使用其 atof malloc free 函数
#include <string.h> // 使用其 memcpy 函数
// API模式
// 0 经典模式( C语言 ),所有API为 zj_ 开头
// 1 短API模式( C语言 ),所有API没有 zj_ 开头,有可能会跟其它头文件冲突,因为
// C语言 没有命名空间
#ifndef zj_API_MODE
#define zj_API_MODE 1
#endif
// 是否支持 C++ 模式
#ifndef zj_CPLUSPLUS
#ifdef __cplusplus
#define zj_CPLUSPLUS 1
#else
#define zj_CPLUSPLUS 0
#endif
#endif
// C++ 支持命名空间
#if zj_CPLUSPLUS == 1
// 如果是C++模式,那么有名空间,短API开启
#define zj_API_MODE 1
namespace zj {
#endif
// 长命名的 类型 & 常量 & 用作常量的宏,详见《数据结构》
#ifndef zj_SIZE_TYPE
#define zj_SIZE_TYPE uint32_t
#endif
typedef zj_SIZE_TYPE zj_Size;
typedef enum zj_JSONType {
zj_JSONTypeBool = 1,
zj_JSONTypeArray = 2,
zj_JSONTypeObject = 3,
zj_JSONTypeString = 4,
zj_JSONTypeNull = 5,
zj_JSONTypeNumber = 6
} zj_JSONType;
// 内存配置,详见《内存配置》
#ifndef zj_DELTA
#define zj_DELTA 2
#endif
static const zj_Size zj_Delta = zj_DELTA;
#ifndef zj_ALLOCATOR_INIT_MEMSIZE
#define zj_ALLOCATOR_INIT_MEMSIZE 1024 * 4
#endif
static const zj_Size zj_AllocatorInitMemSize = zj_ALLOCATOR_INIT_MEMSIZE;
// Stringify函数初始化字符串的大小
#ifndef zj_STRING_INIT_MEMSIZE
#define zj_STRING_INIT_MEMSIZE 1024
#endif
static const zj_Size zj_StringInitMemSize = zj_STRING_INIT_MEMSIZE;
// 用zj_String作为Cache时初始化内存的大小,后面可能去掉
#ifndef zj_STRING_CACHE_INIT_MEMSIZE
#define zj_STRING_CACHE_INIT_MEMSIZE 128
#endif
static const zj_Size zj_StringCacheInitMemSize = zj_STRING_CACHE_INIT_MEMSIZE;
// 环境适配
#ifndef zj_MEMORY_MODE
#define zj_MEMORY_MODE 1
#endif
#if zj_MEMORY_MODE == 1
static inline void *zj_new(zj_Size size) { return malloc(size); }
static inline void zj_free(void *pointer) { free(pointer); }
#elif zj_MEMORY_MODE == 2
// 测试模式,主要是用来测试内存的分配数和释放数,防止内存泄漏,同时用于观察内存分配次数。
// 通过观察,可以得出较好zj_DELTA和zj_ALLOCATORINITMEMSIZE。
static zj_Size AllocMemorySize = 0, AllocMemoryCount = 0, FreeMemoryCount = 0;
static inline void *zj_new(zj_Size size) {
return AllocMemorySize += size, AllocMemoryCount += 1, malloc(size);
}
static inline void zj_free(void *ptr) { FreeMemoryCount += 1, free(ptr); }
#elif zj_MEMORY_MODE == 3
// 自定义模式,需要实现zj_New,zj_Free和zj_MemCopy三个函数
#endif
// 分支预测
#ifndef zj_EXPECT_MODE
#if defined(__GNUC__) || defined(__clang__)
#define zj_EXPECT_MODE 1
#else
#define zj_EXPECT_MODE 2
#endif
#endif
#if zj_EXPECT_MODE == 1
// gcc和clang,使用__builtin_expect
#define zj_LIKELY(x) __builtin_expect(x, 1)
#define zj_UNLIKELY(x) __builtin_expect(x, 0)
#elif zj_EXPECT_MODE == 2
// msvc 不需要分支优化
#define zj_LIKELY(x) x
#define zj_UNLIKELY(x) x
#elif zj_EXPECT_MODE == 3
// 自定义分支预测
#endif
// 长命名数据结构,详见《数据结构》
typedef struct zj_Allocator zj_Allocator;
typedef struct zj_Value zj_Value;
// 长命名数据结构,详见《API》
static inline zj_Allocator *zj_NewAllocator();
static inline void zj_ReleaseAllocator(zj_Allocator *root_alloc);
static inline zj_Value *zj_NewValue(zj_Allocator *alloc);
static inline bool zj_ParseFast(zj_Value *v, const char *s);
static inline bool zj_ParseLen(zj_Value *v, const char *s, zj_Size len);
static inline bool zj_Parse(zj_Value *v, const char *s);
static inline const char *zj_Stringify(const zj_Value *v);
static inline const char *zj_GetStrFast(const zj_Value *v, zj_Size *len);
static inline const char *zj_GetUnEscapeStr(zj_Value *v);
static inline const char *zj_GetStr(zj_Value *v);
static inline const char *zj_GetNumFast(const zj_Value *v, zj_Size *len);
static inline const char *zj_GetNumStr(zj_Value *v);
static inline const double *zj_GetNum(zj_Value *v);
static inline const double *zj_GetDouble(zj_Value *v);
static inline const int *zj_GetInt(zj_Value *v);
static inline const long *zj_GetLong(zj_Value *v);
static inline const long long *zj_GetLongLong(zj_Value *v);
static inline const bool *zj_GetBool(const zj_Value *v);
static inline bool zj_IsNull(const zj_Value *v);
static inline const char *zj_GetKey(zj_Value *v);
static inline const char *zj_GetUnEscapeKey(zj_Value *v);
static inline const char *zj_GetKeyFast(const zj_Value *v, zj_Size *len);
static inline zj_Value *zj_ObjGet(const zj_Value *v, const char *key);
static inline zj_Value *zj_ObjGetLen(const zj_Value *v, const char *key,
zj_Size len);
static inline const zj_JSONType *zj_Type(const zj_Value *v);
static inline zj_Size zj_SizeOf(const zj_Value *v);
static inline zj_Value *zj_ArrayGet(const zj_Value *v, zj_Size index);
static inline zj_Value *zj_Begin(const zj_Value *v);
static inline zj_Value *zj_Next(const zj_Value *v);
static inline zj_Value *zj_Copy(const zj_Value *v);
static inline bool zj_Move(zj_Value *v);
static inline void zj_SetNull(zj_Value *v);
static inline void zj_SetBool(zj_Value *v, bool b);
static inline bool zj_SetNumStrFast(zj_Value *v, const char *num);
static inline bool zj_SetNumStrLenFast(zj_Value *v, const char *num,
zj_Size len);
static inline bool zj_SetNumStr(zj_Value *v, const char *num);
static inline bool zj_SetNumStrLen(zj_Value *v, const char *num, zj_Size len);
static inline bool zj_SetNum(zj_Value *v, const double d);
static inline bool zj_SetDouble(zj_Value *v, const double d);
static inline bool zj_SetInt(zj_Value *v, const int n);
static inline bool zj_SetLong(zj_Value *v, const long n);
static inline bool zj_SetLongLong(zj_Value *v, const long long n);
static inline bool zj_SetStrFast(zj_Value *v, const char *str);
static inline bool zj_SetStrLenFast(zj_Value *v, const char *str, zj_Size len);
static inline bool zj_SetStr(zj_Value *v, const char *str);
static inline bool zj_SetStrLen(zj_Value *v, const char *str, zj_Size len);
static inline bool zj_SetStrEscape(zj_Value *v, const char *str);
static inline bool zj_SetStrLenEscape(zj_Value *v, const char *str,
zj_Size len);
static inline bool zj_SetKeyFast(zj_Value *v, const char *key);
static inline bool zj_SetKeyLenFast(zj_Value *v, const char *key, zj_Size len);
static inline bool zj_SetKey(zj_Value *v, const char *key);
static inline bool zj_SetKeyLen(zj_Value *v, const char *key, zj_Size len);
static inline bool zj_SetKeyEscape(zj_Value *v, const char *key);
static inline bool zj_SetKeyLenEscape(zj_Value *v, const char *key,
zj_Size len);
static inline void zj_SetArray(zj_Value *v);
static inline void zj_SetObj(zj_Value *v);
static inline bool zj_SetFast(zj_Value *v, zj_Value *vv);
static inline bool zj_Set(zj_Value *v, const zj_Value *vv);
static inline bool zj_ObjAddFast(zj_Value *v, zj_Value *vv);
static inline bool zj_ObjAdd(zj_Value *v, const zj_Value *vv);
static inline bool zj_ArrayAddFast(zj_Value *v, zj_Value *vv);
static inline bool zj_ArrayAdd(zj_Value *v, const zj_Value *vv);
static inline bool zj_ArrayDel(zj_Value *v, zj_Size index);
static inline bool zj_ObjDel(zj_Value *v, const char *key);
#if zj_API_MODE == 1
// 短命名数据结构,详见《数据结构》
typedef zj_Allocator Allocator;
typedef zj_Value Value;
typedef zj_Size Size;
typedef enum JSONType {
JSONTypeArray = zj_JSONTypeArray,
JSONTypeObject = zj_JSONTypeObject,
JSONTypeString = zj_JSONTypeString,
JSONTypeNumber = zj_JSONTypeNumber,
JSONTypeBool = zj_JSONTypeBool,
JSONTypeNull = zj_JSONTypeNull
} JSONType;
// 短命名API,详见《API》
static inline Allocator *NewAllocator() { return zj_NewAllocator(); }
static inline void ReleaseAllocator(Allocator *root_alloc) {
zj_ReleaseAllocator(root_alloc);
}
static inline Value *NewValue(Allocator *alloc) { return zj_NewValue(alloc); }
static inline bool ParseFast(Value *v, const char *s) {
return zj_ParseFast(v, s);
}
static inline bool ParseLen(Value *v, const char *s, Size len) {
return zj_ParseLen(v, s, len);
}
static inline bool Parse(Value *v, const char *s) { return zj_Parse(v, s); }
static inline const char *Stringify(const Value *v) { return zj_Stringify(v); }
static inline const char *GetStrFast(const Value *v, Size *len) {
return zj_GetStrFast(v, len);
}
static inline const char *GetUnEscapeStr(Value *v) {
return zj_GetUnEscapeStr(v);
}
static inline const char *GetStr(Value *v) { return zj_GetStr(v); }
static inline const char *GetNumFast(const Value *v, zj_Size *len) {
return zj_GetNumFast(v, len);
}
static inline const char *GetNumStr(Value *v) { return zj_GetNumStr(v); }
static inline const double *GetNum(Value *v) { return zj_GetNum(v); }
static inline const double *GetDouble(Value *v) { return zj_GetDouble(v); }
static inline const int *GetInt(Value *v) { return zj_GetInt(v); }
static inline const long *GetLong(Value *v) { return zj_GetLong(v); }
static inline const long long *GetLongLong(Value *v) {
return zj_GetLongLong(v);
}
static inline const bool *GetBool(const Value *v) { return zj_GetBool(v); }
static inline bool IsNull(const Value *v) { return zj_IsNull(v); }
static inline const char *GetKey(Value *v) { return zj_GetKey(v); }
static inline const char *GetUnEscapeKey(Value *v) {
return zj_GetUnEscapeKey(v);
}
static inline const char *GetKeyFast(const Value *v, Size *len) {
return zj_GetKeyFast(v, len);
}
static inline Value *ObjGet(const Value *v, const char *key) {
return zj_ObjGet(v, key);
}
static inline Value *ObjGetLen(const Value *v, const char *key, Size len) {
return zj_ObjGetLen(v, key, len);
}
static inline const JSONType *Type(const Value *v) {
return (const JSONType *)zj_Type(v);
}
static inline Size SizeOf(const Value *v) { return zj_SizeOf(v); }
static inline Value *ArrayGet(const Value *v, Size index) {
return zj_ArrayGet(v, index);
}
static inline Value *Begin(const Value *v) { return zj_Begin(v); }
static inline Value *Next(const Value *v) { return zj_Next(v); }
static inline Value *Copy(const Value *v) { return zj_Copy(v); }
static inline bool Move(Value *v) { return zj_Move(v); }
static inline void SetNull(Value *v) { zj_SetNull(v); }
static inline void SetBool(Value *v, bool b) { zj_SetBool(v, b); }
static inline bool SetNumStrFast(Value *v, const char *num) {
return zj_SetNumStrFast(v, num);
}
static inline bool SetNumStrLenFast(Value *v, const char *num, Size len) {
return zj_SetNumStrLenFast(v, num, len);
}
static inline bool SetNumStr(Value *v, const char *num) {
return zj_SetNumStr(v, num);
}
static inline bool SetNumStrLen(Value *v, const char *num, Size len) {
return zj_SetNumStrLen(v, num, len);
}
static inline bool SetNum(Value *v, const double d) { return zj_SetNum(v, d); }
static inline bool SetDouble(Value *v, const double d) {
return zj_SetDouble(v, d);
}
static inline bool SetInt(Value *v, const int d) { return zj_SetInt(v, d); }
static inline bool SetLong(Value *v, const long d) { return zj_SetLong(v, d); }
static inline bool SetLongLong(Value *v, const long long d) {
return zj_SetLongLong(v, d);
}
static inline bool SetStrFast(Value *v, const char *str) {
return zj_SetStrFast(v, str);
}
static inline bool SetStrLenFast(Value *v, const char *str, Size len) {
return zj_SetStrLenFast(v, str, len);
}
static inline bool SetStr(Value *v, const char *str) {
return zj_SetStr(v, str);
}
static inline bool SetStrLen(Value *v, const char *str, Size len) {
return zj_SetStrLen(v, str, len);
}
static inline bool SetStrEscape(Value *v, const char *str) {
return zj_SetStrEscape(v, str);
}
static inline bool SetStrLenEscape(Value *v, const char *str, Size len) {
return zj_SetStrLenEscape(v, str, len);
}
static inline bool SetKeyFast(Value *v, const char *key) {
return zj_SetKeyFast(v, key);
}
static inline bool SetKeyLenFast(Value *v, const char *key, Size len) {
return zj_SetKeyLenFast(v, key, len);
}
static inline bool SetKey(Value *v, const char *key) {
return zj_SetKey(v, key);
}
static inline bool SetKeyLen(Value *v, const char *key, Size len) {
return zj_SetKeyLen(v, key, len);
}
static inline bool SetKeyEscape(Value *v, const char *key) {
return zj_SetKeyEscape(v, key);
}
static inline bool SetKeyLenEscape(Value *v, const char *key, Size len) {
return zj_SetKeyLenEscape(v, key, len);
}
static inline void SetArray(Value *v) { zj_SetArray(v); }
static inline void SetObj(Value *v) { zj_SetObj(v); }
static inline bool SetFast(Value *v, Value *vv) { return zj_SetFast(v, vv); }
static inline bool Set(Value *v, const Value *vv) { return zj_Set(v, vv); }
static inline bool ObjAddFast(Value *v, Value *vv) {
return zj_ObjAddFast(v, vv);
}
static inline bool ObjAdd(Value *v, const Value *vv) {
return zj_ObjAdd(v, vv);
}
static inline bool ArrayAddFast(Value *v, Value *vv) {
return zj_ArrayAddFast(v, vv);
}
static inline bool ArrayAdd(Value *v, const Value *vv) {
return zj_ArrayAdd(v, vv);
}
static inline bool ArrayDel(Value *v, Size index) {
return zj_ArrayDel(v, index);
}
static inline bool ObjDel(Value *v, const char *key) {
return zj_ObjDel(v, key);
}
#endif
// 长命名的固字符串,详见《数据结构》
static const char *zj_strTrue = "true";
static const char *zj_strFalse = "false";
static const char *zj_strNull = "null";
static const zj_JSONType zj_jsontype_bool = zj_JSONTypeBool;
static const zj_JSONType zj_jsontype_array = zj_JSONTypeArray;
static const zj_JSONType zj_jsontype_object = zj_JSONTypeObject;
static const zj_JSONType zj_jsontype_string = zj_JSONTypeString;
static const zj_JSONType zj_jsontype_null = zj_JSONTypeNull;
static const zj_JSONType zj_jsontype_number = zj_JSONTypeNumber;
// 内存拷贝函数
static inline void zj_memCopy(const char *src, zj_Size len, char *des) {
memcpy(des, src, len);
}
// 字符串长度计算函数
static inline zj_Size zj_strLen(const char *str) {
return (zj_Size)strlen(str);
}
static inline int zj_strToInt(const char *str, zj_Size len) {
char s[128] = {0};
memcpy(s, str, len);
return atoi(s);
}
static inline long zj_strToLong(const char *str, zj_Size len) {
char s[128] = {0};
memcpy(s, str, len);
return atol(str);
}
static inline long long zj_strToLongLong(const char *str, zj_Size len) {
char s[128] = {0};
memcpy(s, str, len);
return atoll(str);
}
static inline double zj_strToDouble(const char *str, zj_Size len) {
char s[128] = {0};
memcpy(s, str, len);
return atof(str);
}
static inline zj_Size zj_intToStr(int n, char *buff) {
// int 最大长度不超过12
return snprintf(buff, 12, "%d", n);
}
static inline zj_Size zj_longToStr(long n, char *buff) {
// long 最大长度不超过24
return snprintf(buff, 24, "%ld", n);
}
static inline zj_Size zj_longLongToStr(long long n, char *buff) {
// long long 最大长度不超过24
return snprintf(buff, 24, "%lld", n);
}
static inline zj_Size zj_doubleToStr(double n, char *buff) {
// double最大精度是17,使用sprintf转出字符串,%.17g,最大长度不超过32
return snprintf(buff, 32, "%.17g", n);
}
// 字符串比较,a必须以0结束,len为b的长度。
static inline bool zj_strIsEqual(const char *a, const char *b, zj_Size len) {
zj_Size i;
for (i = 0; zj_LIKELY(i < len); ++i) {
if (zj_LIKELY(a[i] != b[i])) {
return false;
}
}
// a字符串必须结束才能算相等
if (zj_LIKELY(a[i] == 0))
return true;
return false;
}
// 字符串比较,len为b的长度。
static inline bool zj_strIsEqualLen(const char *a, zj_Size a_len, const char *b,
zj_Size b_len) {
if (zj_LIKELY(a_len != b_len)) {
return false;
}
zj_Size i;
for (i = 0; zj_LIKELY(i < a_len); ++i) {
if (zj_LIKELY(a[i] != b[i])) {
return false;
}
}
return true;
}
// 内存分配器节点
struct zj_aNode {
// 数据地址
char *Data;
// 数据大小
zj_Size SizeOf;
// 使用到的位置
zj_Size Pos;
// 下一个节点
struct zj_aNode *Next;
};
// 内存分配器
// 内存分配器为由内存分配器节点组成的链表,Root为根节点,End总是指向最后一个节点
struct zj_Allocator {
#if zj_CPLUSPLUS == 1
public:
zj_Allocator() {
void *ptr = zj_new(sizeof(struct zj_aNode) + zj_AllocatorInitMemSize);
Root = (struct zj_aNode *)ptr;
End = Root;
Root->SizeOf = zj_AllocatorInitMemSize;
Root->Data = (char *)ptr + sizeof(struct zj_aNode);
Root->Pos = 0;
Root->Next = 0;
}
~zj_Allocator() {
struct zj_aNode *next = Root;
while (zj_LIKELY(next != 0)) {
struct zj_aNode *nn = next->Next;
zj_free((void *)next);
next = nn;
}
}
#endif
// 根节点
struct zj_aNode *Root;
// 最后一个节点
struct zj_aNode *End;
};
// 函数说明详见《API》
static inline zj_Allocator *zj_NewAllocator() {
// 分配大块内存
void *ptr = zj_new(sizeof(zj_Allocator) + sizeof(struct zj_aNode) +
zj_AllocatorInitMemSize);
zj_Allocator *alloc = (zj_Allocator *)ptr;
alloc->Root = (struct zj_aNode *)((char *)ptr + sizeof(zj_Allocator));
alloc->End = alloc->Root;
alloc->Root->SizeOf = zj_AllocatorInitMemSize;
alloc->Root->Data =
(char *)ptr + sizeof(zj_Allocator) + sizeof(struct zj_aNode);
alloc->Root->Pos = 0;
alloc->Root->Next = 0;
return alloc;
}
// 函数说明详见《API》
static inline void zj_ReleaseAllocator(zj_Allocator *alloc) {
// 遍历整个链表,每次释放一块内存
struct zj_aNode *next = alloc->Root->Next;
while (zj_LIKELY(next != 0)) {
struct zj_aNode *nn = next->Next;
zj_free((void *)next);
next = nn;
}
// 最后释放第一块内存
zj_free((void *)alloc);
}
// 追加一个大小为 init_size 的节点。
static inline void zj_allocatorAppendChild(zj_Allocator *alloc,
zj_Size init_size) {
// 每次分配一大块内存,避免多次分配
void *ptr = zj_new(sizeof(struct zj_aNode) + init_size);
struct zj_aNode *node = (struct zj_aNode *)ptr;
node->SizeOf = init_size;
node->Data = (char *)ptr + sizeof(struct zj_aNode);
node->Pos = 0;
node->Next = 0;
// 在ANode组成的链表最后加一个ANode
alloc->End->Next = node;
alloc->End = node;
return;
}
// 分配大小为size的内存
static inline char *zj_allocatorAlloc(zj_Allocator *alloc, zj_Size size) {
struct zj_aNode *cur_node = alloc->End;
zj_Size s = cur_node->SizeOf;
if (zj_UNLIKELY(cur_node->Pos + size > s)) {
s *= zj_Delta;
// 通过循环计算最终需要的空间大小
// 这里应该有更好的方法,就是直接通过计算所得
while (zj_UNLIKELY(size > s))
s *= zj_Delta; // 每次分配内存的大小是上次的zj_Delta倍
zj_allocatorAppendChild(alloc, s);
cur_node = alloc->End;
}
char *ret = cur_node->Data + cur_node->Pos;
cur_node->Pos += size;
return ret;
}
// 字符串
// 字符串由内存分配器分配内存,当追加操作导致内存不够时,直接分配 zj_Delta
// 倍内存,并把旧内存拷贝到新内存中
struct zj_string {
// 数据
char *Data;
// 位置
zj_Size Pos;
// 使用内存大小
zj_Size SizeOf;
// 分配器
zj_Allocator *A;
};
static struct zj_string *zj_stringCache = 0;
// 新建一个字符串
static inline struct zj_string *zj_newString(zj_Allocator *alloc,
zj_Size init_size) {
struct zj_string *str = (struct zj_string *)zj_allocatorAlloc(
alloc, sizeof(struct zj_string) + init_size);
str->SizeOf = init_size;
str->Data = (char *)str + sizeof(struct zj_string);
str->Pos = 0;
str->A = alloc;
return str;
}
// 清空一个字符串
static inline void zj_reset(struct zj_string *str) { str->Pos = 0; }
// 追加字符串
static inline void zj_appendStr(struct zj_string *str, const char *s,
zj_Size size) {
zj_Size src_s = str->SizeOf;
if (zj_UNLIKELY(str->Pos + size > src_s)) {
src_s *= zj_Delta;
while (zj_UNLIKELY(str->Pos + size > src_s))
src_s *= zj_Delta;
const char *src_d = str->Data;
str->Data = (char *)zj_allocatorAlloc(str->A, src_s);
str->SizeOf = src_s;
zj_memCopy(src_d, str->Pos, str->Data);
}
zj_memCopy(s, size, str->Data + str->Pos);
str->Pos += size;
}
// 追加字符
static inline void zj_appendChar(struct zj_string *str, const char c) {
zj_Size src_s = str->SizeOf;
if (zj_UNLIKELY(str->Pos + 1 > src_s)) {
src_s *= zj_Delta;
const char *src_d = str->Data;
str->Data = (char *)zj_allocatorAlloc(str->A, src_s);
str->SizeOf = src_s;
zj_memCopy(src_d, str->Pos, str->Data);
}
*(str->Data + str->Pos) = c;
str->Pos += 1;
}
// 追加结束符
static inline void zj_appendEnd(struct zj_string *str) {
zj_appendChar(str, 0);
}
// 获得字符串
static inline const char *zj_str(struct zj_string *str) { return str->Data; }
// zzzJSON把文本转化成内存中的一棵树,zj_Node为该数的节点,每个节点对应一个值
struct zj_node {
// 节点代表的值的类型
zj_JSONType Type;
// 节点代表的值的关键字
const char *Key;
// 节点代表的值的关键字长度
zj_Size KeyLen;
union {
// 如果节点代表的值的类型为数组或者对象,则表示数组或者对象的第一个值对应的节点
struct zj_node *Node;
// 如果节点代表的值的类型为字符串,数字,布尔值,则对应其字符串
const char *Str;
} Value;
// 节点对应的值包含值的个数,如果类型非对象或者数组,则为0
zj_Size Len;
// 下一个节点
struct zj_node *Next;
// 上一个节点
struct zj_node *Prev;
// 父节点
struct zj_node *Father;
// 最后一个节点
struct zj_node *End;
};
// zzzJSON的基本单位:值,包含一个节点和一个内存分配器
struct zj_Value {
#if zj_CPLUSPLUS == 1
private:
zj_Value() {}
public:
static Value *NewValue(Allocator *alloc) { return zj_NewValue(alloc); }
zj_Allocator *Allocator() { return A; }
inline bool ParseFast(const char *s) { return zj_ParseFast(this, s); }
inline bool ParseLen(const char *s, Size len) {
return zj_ParseLen(this, s, len);
}
inline bool Parse(const char *s) { return zj_Parse(this, s); }
inline const char *Stringify() { return zj_Stringify(this); }
inline const char *GetStrFast(Size *len) { return zj_GetStrFast(this, len); }
inline const char *GetUnEscapeStr() { return zj_GetUnEscapeStr(this); }
inline const char *GetStr() { return zj_GetStr(this); }
inline const char *GetNumFast(zj_Size *len) {
return zj_GetNumFast(this, len);
}
inline const char *GetNumStr() { return zj_GetNumStr(this); }
inline const double *GetNum() { return zj_GetNum(this); }
inline const double *GetDouble() { return zj_GetDouble(this); }
inline const int *GetInt() { return zj_GetInt(this); }
inline const long *GetLong() { return zj_GetLong(this); }
inline const long long *GetLongLong() { return zj_GetLongLong(this); }
inline const bool *GetBool() { return zj_GetBool(this); }
inline bool IsNull() { return zj_IsNull(this); }
inline const char *GetKey() { return zj_GetKey(this); }
inline const char *GetUnEscapeKey() { return zj_GetUnEscapeKey(this); }
inline const char *GetKeyFast(Size *len) { return zj_GetKeyFast(this, len); }
inline Value *ObjGet(const char *key) { return zj_ObjGet(this, key); }
inline Value *ObjGetLen(const char *key, Size len) {
return zj_ObjGetLen(this, key, len);
}
inline const JSONType *Type() { return (const JSONType *)zj_Type(this); }
inline Size SizeOf() { return zj_SizeOf(this); }
inline Value *ArrayGet(Size index) { return zj_ArrayGet(this, index); }
inline Value *Begin() { return zj_Begin(this); }
inline Value *Next() { return zj_Next(this); }
inline Value *Copy() { return zj_Copy(this); }
inline bool Move() { return zj_Move(this); }
inline void SetNull() { zj_SetNull(this); }
inline void SetBool(bool b) { zj_SetBool(this, b); }
inline bool SetNumStrFast(const char *num) {
return zj_SetNumStrFast(this, num);
}
inline bool SetNumStrLenFast(const char *num, Size len) {
return zj_SetNumStrLenFast(this, num, len);
}
inline bool SetNumStr(const char *num) { return zj_SetNumStr(this, num); }
inline bool SetNumStrLen(const char *num, Size len) {
return zj_SetNumStrLen(this, num, len);
}
inline bool SetNum(const double d) { return zj_SetNum(this, d); }
inline bool SetDouble(const double d) { return zj_SetDouble(this, d); }
inline bool SetInt(const int d) { return zj_SetInt(this, d); }
inline bool SetLong(const long d) { return zj_SetLong(this, d); }
inline bool SetLongLong(const long long d) { return zj_SetLongLong(this, d); }
inline bool SetStrFast(const char *str) { return zj_SetStrFast(this, str); }
inline bool SetStrLenFast(const char *str, Size len) {
return zj_SetStrLenFast(this, str, len);
}
inline bool SetStr(const char *str) { return zj_SetStr(this, str); }
inline bool SetStrLen(const char *str, Size len) {
return zj_SetStrLen(this, str, len);
}
inline bool SetStrEscape(const char *str) {
return zj_SetStrEscape(this, str);
}
inline bool SetStrLenEscape(const char *str, Size len) {
return zj_SetStrLenEscape(this, str, len);
}
inline bool SetKeyFast(const char *key) { return zj_SetKeyFast(this, key); }
inline bool SetKeyLenFast(const char *key, Size len) {
return zj_SetKeyLenFast(this, key, len);
}
inline bool SetKey(const char *key) { return zj_SetKey(this, key); }
inline bool SetKeyLen(const char *key, Size len) {
return zj_SetKeyLen(this, key, len);
}
inline bool SetKeyEscape(const char *key) {
return zj_SetKeyEscape(this, key);
}
inline bool SetKeyLenEscape(const char *key, Size len) {
return zj_SetKeyLenEscape(this, key, len);
}
inline void SetArray() { zj_SetArray(this); }
inline void SetObj() { zj_SetObj(this); }
inline bool SetFast(Value *vv) { return zj_SetFast(this, vv); }
inline bool Set(const Value *vv) { return zj_Set(this, vv); }
inline bool ObjAddFast(Value *vv) { return zj_ObjAddFast(this, vv); }
inline bool ObjAdd(const Value *vv) { return zj_ObjAdd(this, vv); }
inline bool ArrayAddFast(Value *vv) { return zj_ArrayAddFast(this, vv); }
inline bool ArrayAdd(const Value *vv) { return zj_ArrayAdd(this, vv); }
inline bool ArrayDel(Size index) { return zj_ArrayDel(this, index); }
inline bool ObjDel(const char *key) { return zj_ObjDel(this, key); }
#endif
struct zj_node *N;
zj_Allocator *A;
};
// 函数说明详见《API》
static inline zj_Value *zj_NewValue(zj_Allocator *alloc) {
zj_Value *v = (zj_Value *)zj_allocatorAlloc(alloc, sizeof(zj_Value));
v->A = alloc;
v->N = 0;
return v;
}
// 创建一个值,并赋值
static inline zj_Value *zj_innerNewValue(zj_Allocator *alloc,
struct zj_node *n) {
zj_Value *v = (zj_Value *)zj_allocatorAlloc(alloc, sizeof(zj_Value));
v->A = alloc;
v->N = n;
return v;
}
// 跳过空格、tab、换行符
static inline bool zj_skin(const char c) {
if (zj_UNLIKELY(zj_UNLIKELY(c == ' ') || zj_UNLIKELY(c == '\t') ||
zj_UNLIKELY(c == '\n') || zj_UNLIKELY(c == '\r'))) {
return true;
}
return false;
}
// 下一个有效字符
static inline char zj_peek(const char *s, zj_Size *index) {
while (zj_UNLIKELY(zj_skin(s[*index])))
++(*index);
return s[(*index)++];
}
// 消费一个字符
static inline bool zj_consume(const char c, const char *s, zj_Size *index) {
if (s[*index] == c) {
++(*index);
return true;
}
return false;
}
// 预期消费一个字符成功
static inline bool zj_likelyConsume(const char c, const char *s,
zj_Size *index) {
if (zj_LIKELY(s[*index] == c)) {
++(*index);
return true;
}
return false;
}
// 预期消费一个字符失败
static inline bool zj_unLikelyConsume(const char c, const char *s,
zj_Size *index) {
if (zj_UNLIKELY(s[*index] == c)) {
++(*index);
return true;
}
return false;
}
// 预期消费下一个有效字符成功
static inline bool zj_likelyPeekAndConsume(const char c, const char *s,
zj_Size *index) {
while (zj_UNLIKELY(zj_skin(s[*index])))
++(*index);
if (zj_LIKELY(s[*index] == c)) {
++(*index);
return true;
}
return false;
}
// 预期消费下一个有效字符失败
static inline bool zj_unLikelyPeekAndConsume(const char c, const char *s,
zj_Size *index) {
while (zj_UNLIKELY(zj_skin(s[*index])))
++(*index);
if (zj_UNLIKELY(s[*index] == c)) {
++(*index);
return true;
}
return false;
}
// 消费False
static inline bool zj_consumeFalse(const char *s, zj_Size *index) {
if (zj_LIKELY(*((uint32_t *)("alse")) == *((uint32_t *)(s + *index)))) {
*index += 4;
return true;
}
return false;
}
// 消费True
static inline bool zj_consumeTrue(const char *s, zj_Size *index) {
if (zj_LIKELY(*((uint32_t *)zj_strTrue) == *((uint32_t *)(s + *index - 1)))) {
*index += 3;
return true;
}
return false;
}
// 消费Null
static inline bool zj_consumeNull(const char *s, zj_Size *index) {
if (zj_LIKELY(*((uint32_t *)zj_strNull) == *((uint32_t *)(s + *index - 1)))) {
*index += 3;
return true;
}
return false;
}
// 十六进制对应的十进制数字
static inline zj_Size zj_hexCodePoint(const char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
else if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
return 16;
}
// 专为 zj_GetUnEscapeStr 使用
static inline zj_Size zj_hexCodePointForUnEscape(const char c) {
if (c >= '0' && c <= '9')
return c - '0';
else if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return c - 'a' + 10;
}
// 消费一个十六进制字符
static inline bool zj_consumeHexOne(const char *s, zj_Size *index,
zj_Size *cp) {
zj_Size tcp = zj_hexCodePoint(s[*index]);
if (zj_LIKELY(tcp < 16)) {
*cp = *cp << 4;
*cp += tcp;
++(*index);
return true;
}
return false;
}
// 专为 zj_GetUnEscapeStr 使用
static inline void zj_consumeHexOneForUnEscape(const char *s, zj_Size *index,
zj_Size *cp) {
*cp = *cp << 4;
*cp += zj_hexCodePointForUnEscape(s[*index]);
++(*index);
return;
}
// 消费4个十六进制字符
static inline bool zj_consumeHex(const char *s, zj_Size *index, zj_Size *cp) {
if (zj_LIKELY(zj_LIKELY(zj_consumeHexOne(s, index, cp)) &&
zj_LIKELY(zj_consumeHexOne(s, index, cp)) &&
zj_LIKELY(zj_consumeHexOne(s, index, cp)) &&
zj_LIKELY(zj_consumeHexOne(s, index, cp)))) {
return true;
}
return false;
}
// 专为 zj_GetUnEscapeStr 使用
static inline void zj_consumeHexForUnEscape(const char *s, zj_Size *index,
zj_Size *cp) {
zj_consumeHexOneForUnEscape(s, index, cp);
zj_consumeHexOneForUnEscape(s, index, cp);
zj_consumeHexOneForUnEscape(s, index, cp);
zj_consumeHexOneForUnEscape(s, index, cp);
return;
}
// 专为 zj_GetUnEscapeStr 使用,追加一个字符
static inline void zj_add(char *s, zj_Size *index, char c) {
s[(*index)++] = c;
}
static inline void zj_addLen(char *s, zj_Size *index, const char *str,
zj_Size len) {
zj_memCopy(str, len, s + (*index));
*index += len;
}
// 专为 zj_GetUnEscapeStr 使用,追加一个UTF8字符
static inline void zj_addUTF8(char *s, zj_Size *index, zj_Size codepoint) {
// UTF8的规则,具体请参考 UNICODE 相关文档
if (codepoint <= 0x7F) {
zj_add(s, index, (char)(codepoint & 0xFF));
} else if (codepoint <= 0x7FF) {
zj_add(s, index, (char)(0xC0 | ((codepoint >> 6) & 0xFF)));
zj_add(s, index, (char)(0x80 | ((codepoint & 0x3F))));
} else if (codepoint <= 0xFFFF) {
zj_add(s, index, (char)(0xE0 | ((codepoint >> 12) & 0xFF)));
zj_add(s, index, (char)(0x80 | ((codepoint >> 6) & 0x3F)));
zj_add(s, index, (char)(0x80 | (codepoint & 0x3F)));
} else {
zj_add(s, index, (char)(0xF0 | ((codepoint >> 18) & 0xFF)));
zj_add(s, index, (char)(0x80 | ((codepoint >> 12) & 0x3F)));
zj_add(s, index, (char)(0x80 | ((codepoint >> 6) & 0x3F)));
zj_add(s, index, (char)(0x80 | (codepoint & 0x3F)));
}
}
// 专为 zj_GetUnEscapeStr 使用,加一个结束符号
static inline void zj_addEnd(char *s, zj_Size *index) { zj_add(s, index, 0); }
static inline void zj_unEscapeStr(const char *str, zj_Size len, char *s) {
zj_Size s_index = 0;
zj_Size index;
char c;
for (index = 0; index < len;) {
c = str[index];
// 如果是一个合法的JSON字符串,那么\后面一定有字符,因此,一定不会越界
if (zj_UNLIKELY(c == '\\')) {
c = str[index + 1];
switch (c) {
case '"': {
zj_add(s, &s_index, '\"');
index += 2;
break;
}
case '\\': {
zj_add(s, &s_index, '\\');
index += 2;
break;
}
case 'b': {
zj_add(s, &s_index, '\b');
index += 2;
break;
}
case 'f': {
zj_add(s, &s_index, '\f');
index += 2;
break;
}
case 'n': {
zj_add(s, &s_index, '\n');
index += 2;
break;
}
case 'r': {
zj_add(s, &s_index, '\r');
index += 2;
break;
}
case 't': {
zj_add(s, &s_index, '\t');
index += 2;
break;