/
usual.c
1228 lines (1042 loc) · 34.4 KB
/
usual.c
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) 2021, Peter Ohler, All rights reserved.
#include "cache.h"
#include "oj.h"
#include "parser.h"
// The Usual delegate builds Ruby objects during parsing. It makes use of
// three stacks. The first is the value stack. This is where parsed values are
// placed. With the value stack the bulk creation and setting can be used
// which is significantly faster than setting Array (15x) or Hash (3x)
// elements one at a time.
//
// The second stack is the collection stack. Each element on the collection
// stack marks the start of a Hash, Array, or Object.
//
// The third stack is the key stack which is used for Hash and Object
// members. The key stack elements store the keys that could be used for
// either a Hash or Object. Since the decision on whether the parent is a Hash
// or Object can not be made until the end of the JSON object the keys remain
// as strings until just before setting the Hash or Object members.
//
// The approach taken with the usual delegate is to configure the delegate for
// the parser up front so that the various options are not checked during
// parsing and thus avoiding conditionals as much as reasonably possible in
// the more time sensitive parsing. Configuration is simply setting the
// function pointers to point to the function to be used for the selected
// option.
#define DEBUG 0
// Used to mark the start of each Hash, Array, or Object. The members point at
// positions of the start in the value stack and if not an Array into the key
// stack.
typedef struct _col {
long vi; // value stack index
long ki; // key stack index if an hash else -1 for an array
} * Col;
typedef union _key {
struct {
int16_t len;
char buf[22];
};
struct {
int16_t xlen; // should be the same as len
char * key;
};
} * Key;
#define MISS_AUTO 'A'
#define MISS_RAISE 'R'
#define MISS_IGNORE 'I'
typedef struct _delegate {
VALUE *vhead;
VALUE *vtail;
VALUE *vend;
Col chead;
Col ctail;
Col cend;
Key khead;
Key ktail;
Key kend;
VALUE (*get_key)(ojParser p, Key kp);
struct _cache *key_cache; // same as str_cache or sym_cache
struct _cache *str_cache;
struct _cache *sym_cache;
struct _cache *class_cache;
struct _cache *attr_cache;
VALUE array_class;
VALUE hash_class;
char * create_id;
uint8_t create_id_len;
uint8_t cache_str;
uint8_t miss_class;
bool cache_keys;
bool ignore_json_create;
} * Delegate;
static ID to_f_id = 0;
static ID ltlt_id = 0;
static ID hset_id = 0;
static char *str_dup(const char *s, size_t len) {
char *d = ALLOC_N(char, len + 1);
memcpy(d, s, len);
d[len] = '\0';
return d;
}
static VALUE form_str(const char *str, size_t len) {
return rb_str_freeze(rb_utf8_str_new(str, len));
}
static VALUE form_sym(const char *str, size_t len) {
// return ID2SYM(rb_intern3(str, len, oj_utf8_encoding));
return rb_str_intern(rb_utf8_str_new(str, len));
}
static VALUE form_attr(const char *str, size_t len) {
char buf[256];
if (sizeof(buf) - 2 <= len) {
char *b = ALLOC_N(char, len + 2);
ID id;
*b = '@';
memcpy(b + 1, str, len);
b[len + 1] = '\0';
id = rb_intern3(buf, len + 1, oj_utf8_encoding);
xfree(b);
return id;
}
*buf = '@';
memcpy(buf + 1, str, len);
buf[len + 1] = '\0';
return (VALUE)rb_intern3(buf, len + 1, oj_utf8_encoding);
}
static VALUE resolve_classname(VALUE mod, const char *classname, bool auto_define) {
VALUE clas;
ID ci = rb_intern(classname);
if (rb_const_defined_at(mod, ci)) {
clas = rb_const_get_at(mod, ci);
} else if (auto_define) {
clas = rb_define_class_under(mod, classname, oj_bag_class);
} else {
clas = Qundef;
}
return clas;
}
static VALUE resolve_classpath(const char *name, size_t len, bool auto_define) {
char class_name[1024];
VALUE clas;
char * end = class_name + sizeof(class_name) - 1;
char * s;
const char *n = name;
clas = rb_cObject;
for (s = class_name; 0 < len; n++, len--) {
if (':' == *n) {
*s = '\0';
n++;
len--;
if (':' != *n) {
return Qundef;
}
if (Qundef == (clas = resolve_classname(clas, class_name, auto_define))) {
return Qundef;
}
s = class_name;
} else if (end <= s) {
return Qundef;
} else {
*s++ = *n;
}
}
*s = '\0';
return resolve_classname(clas, class_name, auto_define);
}
static VALUE form_class(const char *str, size_t len) {
return resolve_classpath(str, len, false);
}
static VALUE form_class_auto(const char *str, size_t len) {
return resolve_classpath(str, len, true);
}
static void assure_cstack(Delegate d) {
if (d->cend <= d->ctail + 1) {
size_t cap = d->cend - d->chead;
long pos = d->ctail - d->chead;
cap *= 2;
REALLOC_N(d->chead, struct _col, cap);
d->ctail = d->chead + pos;
d->cend = d->chead + cap;
}
}
static void push(ojParser p, VALUE v) {
Delegate d = (Delegate)p->ctx;
if (d->vend <= d->vtail) {
size_t cap = d->vend - d->vhead;
long pos = d->vtail - d->vhead;
cap *= 2;
REALLOC_N(d->vhead, VALUE, cap);
d->vtail = d->vhead + pos;
d->vend = d->vhead + cap;
}
*d->vtail = v;
d->vtail++;
}
static VALUE cache_key(ojParser p, Key kp) {
Delegate d = (Delegate)p->ctx;
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
return cache_intern(d->key_cache, kp->buf, kp->len);
}
return cache_intern(d->key_cache, kp->key, kp->len);
}
static VALUE str_key(ojParser p, Key kp) {
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
return rb_str_freeze(rb_utf8_str_new(kp->buf, kp->len));
}
return rb_str_freeze(rb_utf8_str_new(kp->key, kp->len));
}
static VALUE sym_key(ojParser p, Key kp) {
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
return rb_str_freeze(rb_str_intern(rb_utf8_str_new(kp->buf, kp->len)));
}
return rb_str_freeze(rb_str_intern(rb_utf8_str_new(kp->key, kp->len)));
}
static ID get_attr_id(ojParser p, Key kp) {
Delegate d = (Delegate)p->ctx;
if ((size_t)kp->len < sizeof(kp->buf) - 1) {
return (ID)cache_intern(d->attr_cache, kp->buf, kp->len);
}
return (ID)cache_intern(d->attr_cache, kp->key, kp->len);
}
static void push_key(ojParser p) {
Delegate d = (Delegate)p->ctx;
size_t klen = buf_len(&p->key);
const char *key = buf_str(&p->key);
if (d->kend <= d->ktail) {
size_t cap = d->kend - d->khead;
long pos = d->ktail - d->khead;
cap *= 2;
REALLOC_N(d->khead, union _key, cap);
d->ktail = d->khead + pos;
d->kend = d->khead + cap;
}
d->ktail->len = klen;
if (klen <= sizeof(d->ktail->buf) + 1) {
memcpy(d->ktail->buf, key, klen);
d->ktail->buf[klen] = '\0';
} else {
d->ktail->key = str_dup(key, klen);
}
d->ktail++;
}
static void push2(ojParser p, VALUE v) {
Delegate d = (Delegate)p->ctx;
if (d->vend <= d->vtail + 1) {
size_t cap = d->vend - d->vhead;
long pos = d->vtail - d->vhead;
cap *= 2;
REALLOC_N(d->vhead, VALUE, cap);
d->vtail = d->vhead + pos;
d->vend = d->vhead + cap;
}
*d->vtail = Qundef; // key place holder
d->vtail++;
*d->vtail = v;
d->vtail++;
}
static void open_object(ojParser p) {
Delegate d = (Delegate)p->ctx;
assure_cstack(d);
d->ctail->vi = d->vtail - d->vhead;
d->ctail->ki = d->ktail - d->khead;
d->ctail++;
push(p, Qundef);
}
static void open_object_key(ojParser p) {
Delegate d = (Delegate)p->ctx;
push_key(p);
assure_cstack(d);
d->ctail->vi = d->vtail - d->vhead + 1;
d->ctail->ki = d->ktail - d->khead;
d->ctail++;
push2(p, Qundef);
}
static void open_array(ojParser p) {
Delegate d = (Delegate)p->ctx;
assure_cstack(d);
d->ctail->vi = d->vtail - d->vhead;
d->ctail->ki = -1;
d->ctail++;
push(p, Qundef);
}
static void open_array_key(ojParser p) {
Delegate d = (Delegate)p->ctx;
push_key(p);
assure_cstack(d);
d->ctail->vi = d->vtail - d->vhead + 1;
d->ctail->ki = -1;
d->ctail++;
push2(p, Qundef);
}
static void close_object(ojParser p) {
VALUE *vp;
Delegate d = (Delegate)p->ctx;
d->ctail--;
Col c = d->ctail;
Key kp = d->khead + c->ki;
VALUE * head = d->vhead + c->vi + 1;
volatile VALUE obj = rb_hash_new();
#if HAVE_RB_HASH_BULK_INSERT
for (vp = head; kp < d->ktail; kp++, vp += 2) {
*vp = d->get_key(p, kp);
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
rb_hash_bulk_insert(d->vtail - head, head, obj);
#else
for (vp = head; kp < d->ktail; kp++, vp += 2) {
rb_hash_aset(obj, d->get_key(p, kp), *(vp + 1));
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
#endif
d->ktail = d->khead + c->ki;
d->vtail = head;
head--;
*head = obj;
}
static void close_object_class(ojParser p) {
VALUE *vp;
Delegate d = (Delegate)p->ctx;
d->ctail--;
Col c = d->ctail;
Key kp = d->khead + c->ki;
VALUE * head = d->vhead + c->vi + 1;
volatile VALUE obj = rb_class_new_instance(0, NULL, d->hash_class);
for (vp = head; kp < d->ktail; kp++, vp += 2) {
rb_funcall(obj, hset_id, 2, d->get_key(p, kp), *(vp + 1));
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
d->ktail = d->khead + c->ki;
d->vtail = head;
head--;
*head = obj;
}
static void close_object_create(ojParser p) {
VALUE *vp;
Delegate d = (Delegate)p->ctx;
d->ctail--;
Col c = d->ctail;
Key kp = d->khead + c->ki;
VALUE * head = d->vhead + c->vi;
volatile VALUE obj;
if (Qundef == *head) {
head++;
if (Qnil == d->hash_class) {
obj = rb_hash_new();
#if HAVE_RB_HASH_BULK_INSERT
for (vp = head; kp < d->ktail; kp++, vp += 2) {
*vp = d->get_key(p, kp);
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
rb_hash_bulk_insert(d->vtail - head, head, obj);
#else
for (vp = head; kp < d->ktail; kp++, vp += 2) {
rb_hash_aset(obj, d->get_key(p, kp), *(vp + 1));
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
#endif
} else {
obj = rb_class_new_instance(0, NULL, d->hash_class);
for (vp = head; kp < d->ktail; kp++, vp += 2) {
rb_funcall(obj, hset_id, 2, d->get_key(p, kp), *(vp + 1));
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
}
} else {
VALUE clas = *head;
head++;
if (!d->ignore_json_create && rb_respond_to(clas, oj_json_create_id)) {
volatile VALUE arg = rb_hash_new();
#if HAVE_RB_HASH_BULK_INSERT
for (vp = head; kp < d->ktail; kp++, vp += 2) {
*vp = d->get_key(p, kp);
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
rb_hash_bulk_insert(d->vtail - head, head, arg);
#else
for (vp = head; kp < d->ktail; kp++, vp += 2) {
rb_hash_aset(arg, d->get_key(p, kp), *(vp + 1));
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
#endif
obj = rb_funcall(clas, oj_json_create_id, 1, arg);
} else {
obj = rb_class_new_instance(0, NULL, clas);
for (vp = head; kp < d->ktail; kp++, vp += 2) {
rb_ivar_set(obj, get_attr_id(p, kp), *(vp + 1));
if (sizeof(kp->buf) - 1 < (size_t)kp->len) {
xfree(kp->key);
}
}
}
}
d->ktail = d->khead + c->ki;
d->vtail = head;
head--;
*head = obj;
}
static void close_array(ojParser p) {
Delegate d = (Delegate)p->ctx;
d->ctail--;
VALUE * head = d->vhead + d->ctail->vi + 1;
volatile VALUE a = rb_ary_new_from_values(d->vtail - head, head);
d->vtail = head;
head--;
*head = a;
}
static void close_array_class(ojParser p) {
VALUE *vp;
Delegate d = (Delegate)p->ctx;
d->ctail--;
VALUE * head = d->vhead + d->ctail->vi + 1;
volatile VALUE a = rb_class_new_instance(0, NULL, d->array_class);
for (vp = head; vp < d->vtail; vp++) {
rb_funcall(a, ltlt_id, 1, *vp);
}
d->vtail = head;
head--;
*head = a;
}
static void noop(ojParser p) {
}
static void add_null(ojParser p) {
push(p, Qnil);
}
static void add_null_key(ojParser p) {
push_key(p);
push2(p, Qnil);
}
static void add_true(ojParser p) {
push(p, Qtrue);
}
static void add_true_key(ojParser p) {
push_key(p);
push2(p, Qtrue);
}
static void add_false(ojParser p) {
push(p, Qfalse);
}
static void add_false_key(ojParser p) {
push_key(p);
push2(p, Qfalse);
}
static void add_int(ojParser p) {
push(p, LONG2NUM(p->num.fixnum));
}
static void add_int_key(ojParser p) {
push_key(p);
push2(p, LONG2NUM(p->num.fixnum));
}
static void add_float(ojParser p) {
push(p, rb_float_new(p->num.dub));
}
static void add_float_key(ojParser p) {
push_key(p);
push2(p, rb_float_new(p->num.dub));
}
static void add_float_as_big(ojParser p) {
char buf[64];
// fails on ubuntu
// snprintf(buf, sizeof(buf), "%Lg", p->num.dub);
sprintf(buf, "%Lg", p->num.dub);
push(p, rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new2(buf)));
}
static void add_float_as_big_key(ojParser p) {
char buf[64];
snprintf(buf, sizeof(buf), "%Lg", p->num.dub);
push_key(p);
push2(p, rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new2(buf)));
}
static void add_big(ojParser p) {
push(p, rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))));
}
static void add_big_key(ojParser p) {
push_key(p);
push2(p, rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf))));
}
static void add_big_as_float(ojParser p) {
volatile VALUE big = rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf)));
push(p, rb_funcall(big, to_f_id, 0));
}
static void add_big_as_float_key(ojParser p) {
volatile VALUE big = rb_funcall(rb_cObject, oj_bigdecimal_id, 1, rb_str_new(buf_str(&p->buf), buf_len(&p->buf)));
push_key(p);
push2(p, rb_funcall(big, to_f_id, 0));
}
static void add_big_as_ruby(ojParser p) {
push(p, rb_funcall(rb_str_new(buf_str(&p->buf), buf_len(&p->buf)), to_f_id, 0));
}
static void add_big_as_ruby_key(ojParser p) {
push_key(p);
push2(p, rb_funcall(rb_str_new(buf_str(&p->buf), buf_len(&p->buf)), to_f_id, 0));
}
static void add_str(ojParser p) {
Delegate d = (Delegate)p->ctx;
volatile VALUE rstr;
const char * str = buf_str(&p->buf);
size_t len = buf_len(&p->buf);
if (len < d->cache_str) {
rstr = cache_intern(d->str_cache, str, len);
} else {
rstr = rb_utf8_str_new(str, len);
}
push(p, rstr);
}
static void add_str_key(ojParser p) {
Delegate d = (Delegate)p->ctx;
volatile VALUE rstr;
const char * str = buf_str(&p->buf);
size_t len = buf_len(&p->buf);
if (len < d->cache_str) {
rstr = cache_intern(d->str_cache, str, len);
} else {
rstr = rb_utf8_str_new(str, len);
}
push_key(p);
push2(p, rstr);
}
static void add_str_key_create(ojParser p) {
Delegate d = (Delegate)p->ctx;
volatile VALUE rstr;
const char * str = buf_str(&p->buf);
size_t len = buf_len(&p->buf);
const char * key = buf_str(&p->key);
size_t klen = buf_len(&p->key);
if (klen == (size_t)d->create_id_len && 0 == strncmp(d->create_id, key, klen)) {
Col c = d->ctail - 1;
VALUE clas;
if (NULL != d->class_cache) {
clas = cache_intern(d->class_cache, str, len);
} else {
clas = resolve_classpath(str, len, MISS_AUTO == d->miss_class);
}
if (Qundef != clas) {
*(d->vhead + c->vi) = clas;
return;
}
if (MISS_RAISE == d->miss_class) {
rb_raise(rb_eLoadError, "%s is not define", str);
}
}
if (len < d->cache_str) {
rstr = cache_intern(d->str_cache, str, len);
} else {
rstr = rb_utf8_str_new(str, len);
}
push_key(p);
push2(p, rstr);
}
static VALUE result(ojParser p) {
Delegate d = (Delegate)p->ctx;
if (d->vhead < d->vtail) {
return *d->vhead;
}
return Qnil;
}
static void start(ojParser p) {
Delegate d = (Delegate)p->ctx;
d->vtail = d->vhead;
d->ctail = d->chead;
d->ktail = d->khead;
}
static void dfree(ojParser p) {
Delegate d = (Delegate)p->ctx;
cache_free(d->str_cache);
cache_free(d->attr_cache);
if (NULL != d->sym_cache) {
cache_free(d->sym_cache);
}
if (NULL != d->class_cache) {
cache_free(d->class_cache);
}
xfree(d->vhead);
xfree(d->chead);
xfree(d->khead);
xfree(d->create_id);
xfree(p->ctx);
p->ctx = NULL;
}
static void mark(ojParser p) {
if (NULL == p->ctx) {
return;
}
Delegate d = (Delegate)p->ctx;
VALUE *vp;
if (NULL == d) {
return;
}
cache_mark(d->str_cache);
if (NULL != d->sym_cache) {
cache_mark(d->sym_cache);
}
if (NULL != d->class_cache) {
cache_mark(d->class_cache);
}
for (vp = d->vhead; vp < d->vtail; vp++) {
if (Qundef != *vp) {
rb_gc_mark(*vp);
}
}
}
///// options /////////////////////////////////////////////////////////////////
// Each option is handled by a separate function and then added to an assoc
// list (struct opt}. The list is then iterated over until there is a name
// match. This is done primarily to keep each option separate and easier to
// understand instead of placing all in one large function.
struct opt {
const char *name;
VALUE (*func)(ojParser p, VALUE value);
};
static VALUE opt_array_class(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
return d->array_class;
}
static VALUE opt_array_class_set(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
if (Qnil == value) {
p->funcs[TOP_FUN].close_array = close_array;
p->funcs[ARRAY_FUN].close_array = close_array;
p->funcs[OBJECT_FUN].close_array = close_array;
} else {
rb_check_type(value, T_CLASS);
if (!rb_method_boundp(value, ltlt_id, 1)) {
rb_raise(rb_eArgError, "An array class must implement the << method.");
}
p->funcs[TOP_FUN].close_array = close_array_class;
p->funcs[ARRAY_FUN].close_array = close_array_class;
p->funcs[OBJECT_FUN].close_array = close_array_class;
}
d->array_class = value;
return d->array_class;
}
static VALUE opt_cache_keys(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
return d->cache_keys ? Qtrue : Qfalse;
}
static VALUE opt_cache_keys_set(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
if (Qtrue == value) {
d->cache_keys = true;
d->get_key = cache_key;
if (NULL == d->sym_cache) {
d->key_cache = d->str_cache;
} else {
d->key_cache = d->sym_cache;
}
} else {
d->cache_keys = false;
if (NULL == d->sym_cache) {
d->get_key = str_key;
} else {
d->get_key = sym_key;
}
}
return d->cache_keys ? Qtrue : Qfalse;
}
static VALUE opt_cache_strings(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
return INT2NUM((int)d->cache_str);
}
static VALUE opt_cache_strings_set(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
int limit = NUM2INT(value);
if (CACHE_MAX_KEY < limit) {
limit = CACHE_MAX_KEY;
} else if (limit < 0) {
limit = 0;
}
d->cache_str = limit;
return INT2NUM((int)d->cache_str);
}
static VALUE opt_capacity(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
return ULONG2NUM(d->vend - d->vhead);
}
static VALUE opt_capacity_set(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
long cap = NUM2LONG(value);
if (d->vend - d->vhead < cap) {
long pos = d->vtail - d->vhead;
REALLOC_N(d->vhead, VALUE, cap);
d->vtail = d->vhead + pos;
d->vend = d->vhead + cap;
}
if (d->kend - d->khead < cap) {
long pos = d->ktail - d->khead;
REALLOC_N(d->khead, union _key, cap);
d->ktail = d->khead + pos;
d->kend = d->khead + cap;
}
return ULONG2NUM(d->vend - d->vhead);
}
static VALUE opt_class_cache(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
return (NULL != d->class_cache) ? Qtrue : Qfalse;
}
static VALUE opt_class_cache_set(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
if (Qtrue == value) {
if (NULL == d->class_cache) {
if (MISS_AUTO == d->miss_class) {
d->class_cache = cache_create(0, form_class_auto, true);
} else {
d->class_cache = cache_create(0, form_class, false);
}
}
} else if (NULL != d->class_cache) {
cache_free(d->class_cache);
d->class_cache = NULL;
}
return (NULL != d->class_cache) ? Qtrue : Qfalse;
}
static VALUE opt_create_id(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
if (NULL == d->create_id) {
return Qnil;
}
return rb_utf8_str_new(d->create_id, d->create_id_len);
}
static VALUE opt_create_id_set(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
if (Qnil == value) {
d->create_id = NULL;
d->create_id_len = 0;
p->funcs[OBJECT_FUN].add_str = add_str_key;
if (Qnil == d->hash_class) {
p->funcs[TOP_FUN].close_object = close_object;
p->funcs[ARRAY_FUN].close_object = close_object;
p->funcs[OBJECT_FUN].close_object = close_object;
} else {
p->funcs[TOP_FUN].close_object = close_object_class;
p->funcs[ARRAY_FUN].close_object = close_object_class;
p->funcs[OBJECT_FUN].close_object = close_object_class;
}
} else {
rb_check_type(value, T_STRING);
size_t len = RSTRING_LEN(value);
if (1 << sizeof(d->create_id_len) <= len) {
rb_raise(rb_eArgError, "The create_id values is limited to %d bytes.", 1 << sizeof(d->create_id_len));
}
d->create_id_len = (uint8_t)len;
d->create_id = str_dup(RSTRING_PTR(value), len);
p->funcs[OBJECT_FUN].add_str = add_str_key_create;
p->funcs[TOP_FUN].close_object = close_object_create;
p->funcs[ARRAY_FUN].close_object = close_object_create;
p->funcs[OBJECT_FUN].close_object = close_object_create;
}
return opt_create_id(p, value);
}
static VALUE opt_decimal(ojParser p, VALUE value) {
if (add_float_as_big == p->funcs[TOP_FUN].add_float) {
return ID2SYM(rb_intern("bigdecimal"));
}
if (add_big == p->funcs[TOP_FUN].add_big) {
return ID2SYM(rb_intern("auto"));
}
if (add_big_as_float == p->funcs[TOP_FUN].add_big) {
return ID2SYM(rb_intern("float"));
}
if (add_big_as_ruby == p->funcs[TOP_FUN].add_big) {
return ID2SYM(rb_intern("ruby"));
}
return Qnil;
}
static VALUE opt_decimal_set(ojParser p, VALUE value) {
const char * mode;
volatile VALUE s;
switch (rb_type(value)) {
case T_STRING: mode = RSTRING_PTR(value); break;
case T_SYMBOL:
s = rb_sym2str(value);
mode = RSTRING_PTR(s);
break;
default:
rb_raise(rb_eTypeError,
"the decimal options must be a Symbol or String, not %s.",
rb_class2name(rb_obj_class(value)));
break;
}
if (0 == strcmp("auto", mode)) {
p->funcs[TOP_FUN].add_big = add_big;
p->funcs[ARRAY_FUN].add_big = add_big;
p->funcs[OBJECT_FUN].add_big = add_big_key;
p->funcs[TOP_FUN].add_float = add_float;
p->funcs[ARRAY_FUN].add_float = add_float;
p->funcs[OBJECT_FUN].add_float = add_float_key;
return opt_decimal(p, Qnil);
}
if (0 == strcmp("bigdecimal", mode)) {
p->funcs[TOP_FUN].add_big = add_big;
p->funcs[ARRAY_FUN].add_big = add_big;
p->funcs[OBJECT_FUN].add_big = add_big_key;
p->funcs[TOP_FUN].add_float = add_float_as_big;
p->funcs[ARRAY_FUN].add_float = add_float_as_big;
p->funcs[OBJECT_FUN].add_float = add_float_as_big_key;
return opt_decimal(p, Qnil);
}
if (0 == strcmp("float", mode)) {
p->funcs[TOP_FUN].add_big = add_big_as_float;
p->funcs[ARRAY_FUN].add_big = add_big_as_float;
p->funcs[OBJECT_FUN].add_big = add_big_as_float_key;
p->funcs[TOP_FUN].add_float = add_float;
p->funcs[ARRAY_FUN].add_float = add_float;
p->funcs[OBJECT_FUN].add_float = add_float_key;
return opt_decimal(p, Qnil);
}
if (0 == strcmp("ruby", mode)) {
p->funcs[TOP_FUN].add_big = add_big_as_ruby;
p->funcs[ARRAY_FUN].add_big = add_big_as_ruby;
p->funcs[OBJECT_FUN].add_big = add_big_as_ruby_key;
p->funcs[TOP_FUN].add_float = add_float;
p->funcs[ARRAY_FUN].add_float = add_float;
p->funcs[OBJECT_FUN].add_float = add_float_key;
return opt_decimal(p, Qnil);
}
rb_raise(rb_eArgError, "%s is not a valid option for the decimal option.", mode);
return Qnil;
}
static VALUE opt_hash_class(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
return d->hash_class;
}
static VALUE opt_hash_class_set(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
if (Qnil != value) {
rb_check_type(value, T_CLASS);
if (!rb_method_boundp(value, hset_id, 1)) {
rb_raise(rb_eArgError, "A hash class must implement the []= method.");
}
}
d->hash_class = value;
if (NULL == d->create_id) {
if (Qnil == value) {
p->funcs[TOP_FUN].close_object = close_object;
p->funcs[ARRAY_FUN].close_object = close_object;
p->funcs[OBJECT_FUN].close_object = close_object;
} else {
p->funcs[TOP_FUN].close_object = close_object_class;
p->funcs[ARRAY_FUN].close_object = close_object_class;
p->funcs[OBJECT_FUN].close_object = close_object_class;
}
}
return d->hash_class;
}
static VALUE opt_ignore_json_create(ojParser p, VALUE value) {
Delegate d = (Delegate)p->ctx;
return d->ignore_json_create ? Qtrue : Qfalse;
}