oj 3.13.11 → 3.13.14

Sign up to get free protection for your applications and to get access to all the features.
data/ext/oj/dump_compat.c CHANGED
@@ -32,21 +32,17 @@ dump_obj_classname(const char *classname, int depth, Out out) {
32
32
  *out->cur++ = '{';
33
33
  fill_indent(out, d2);
34
34
  *out->cur++ = '"';
35
- memcpy(out->cur, out->opts->create_id, out->opts->create_id_len);
36
- out->cur += out->opts->create_id_len;
35
+ APPEND_CHARS(out->cur, out->opts->create_id, out->opts->create_id_len);
37
36
  *out->cur++ = '"';
38
37
  if (0 < out->opts->dump_opts.before_size) {
39
- strcpy(out->cur, out->opts->dump_opts.before_sep);
40
- out->cur += out->opts->dump_opts.before_size;
38
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
41
39
  }
42
40
  *out->cur++ = ':';
43
41
  if (0 < out->opts->dump_opts.after_size) {
44
- strcpy(out->cur, out->opts->dump_opts.after_sep);
45
- out->cur += out->opts->dump_opts.after_size;
42
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
46
43
  }
47
44
  *out->cur++ = '"';
48
- memcpy(out->cur, classname, len);
49
- out->cur += len;
45
+ APPEND_CHARS(out->cur, classname, len);
50
46
  *out->cur++ = '"';
51
47
  }
52
48
 
@@ -71,15 +67,13 @@ dump_values_array(VALUE *values, int depth, Out out) {
71
67
  assure_size(out, size);
72
68
  if (out->opts->dump_opts.use) {
73
69
  if (0 < out->opts->dump_opts.array_size) {
74
- strcpy(out->cur, out->opts->dump_opts.array_nl);
75
- out->cur += out->opts->dump_opts.array_size;
70
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
76
71
  }
77
72
  if (0 < out->opts->dump_opts.indent_size) {
78
73
  int i;
79
74
 
80
75
  for (i = d2; 0 < i; i--) {
81
- strcpy(out->cur, out->opts->dump_opts.indent_str);
82
- out->cur += out->opts->dump_opts.indent_size;
76
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
83
77
  }
84
78
  }
85
79
  } else {
@@ -93,15 +87,13 @@ dump_values_array(VALUE *values, int depth, Out out) {
93
87
  assure_size(out, size);
94
88
  if (out->opts->dump_opts.use) {
95
89
  if (0 < out->opts->dump_opts.array_size) {
96
- strcpy(out->cur, out->opts->dump_opts.array_nl);
97
- out->cur += out->opts->dump_opts.array_size;
90
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
98
91
  }
99
92
  if (0 < out->opts->dump_opts.indent_size) {
100
93
  int i;
101
94
 
102
95
  for (i = depth; 0 < i; i--) {
103
- strcpy(out->cur, out->opts->dump_opts.indent_str);
104
- out->cur += out->opts->dump_opts.indent_size;
96
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
105
97
  }
106
98
  }
107
99
  } else {
@@ -133,8 +125,7 @@ dump_to_json(VALUE obj, Out out) {
133
125
  len = (int)RSTRING_LEN(rs);
134
126
 
135
127
  assure_size(out, len + 1);
136
- memcpy(out->cur, s, len);
137
- out->cur += len;
128
+ APPEND_CHARS(out->cur, s, len);
138
129
  *out->cur = '\0';
139
130
  }
140
131
 
@@ -164,19 +155,17 @@ dump_array(VALUE a, int depth, Out out, bool as_ok) {
164
155
  } else {
165
156
  size = d2 * out->indent + 2;
166
157
  }
158
+ assure_size(out, size * cnt);
167
159
  cnt--;
168
160
  for (i = 0; i <= cnt; i++) {
169
- assure_size(out, size);
170
161
  if (out->opts->dump_opts.use) {
171
162
  if (0 < out->opts->dump_opts.array_size) {
172
- strcpy(out->cur, out->opts->dump_opts.array_nl);
173
- out->cur += out->opts->dump_opts.array_size;
163
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
174
164
  }
175
165
  if (0 < out->opts->dump_opts.indent_size) {
176
166
  int i;
177
167
  for (i = d2; 0 < i; i--) {
178
- strcpy(out->cur, out->opts->dump_opts.indent_str);
179
- out->cur += out->opts->dump_opts.indent_size;
168
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
180
169
  }
181
170
  }
182
171
  } else {
@@ -191,15 +180,13 @@ dump_array(VALUE a, int depth, Out out, bool as_ok) {
191
180
  size = out->opts->dump_opts.array_size + out->opts->dump_opts.indent_size * depth + 1;
192
181
  assure_size(out, size);
193
182
  if (0 < out->opts->dump_opts.array_size) {
194
- strcpy(out->cur, out->opts->dump_opts.array_nl);
195
- out->cur += out->opts->dump_opts.array_size;
183
+ APPEND_CHARS(out->cur, out->opts->dump_opts.array_nl, out->opts->dump_opts.array_size);
196
184
  }
197
185
  if (0 < out->opts->dump_opts.indent_size) {
198
186
  int i;
199
187
 
200
188
  for (i = depth; 0 < i; i--) {
201
- strcpy(out->cur, out->opts->dump_opts.indent_str);
202
- out->cur += out->opts->dump_opts.indent_size;
189
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
203
190
  }
204
191
  }
205
192
  } else {
@@ -336,33 +323,25 @@ exception_alt(VALUE obj, int depth, Out out) {
336
323
  assure_size(out, size + sep_len + 6);
337
324
  *out->cur++ = ',';
338
325
  fill_indent(out, d3);
339
- *out->cur++ = '"';
340
- *out->cur++ = 'm';
341
- *out->cur++ = '"';
326
+ APPEND_CHARS(out->cur, "\"m\"", 3);
342
327
  if (0 < out->opts->dump_opts.before_size) {
343
- strcpy(out->cur, out->opts->dump_opts.before_sep);
344
- out->cur += out->opts->dump_opts.before_size;
328
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
345
329
  }
346
330
  *out->cur++ = ':';
347
331
  if (0 < out->opts->dump_opts.after_size) {
348
- strcpy(out->cur, out->opts->dump_opts.after_sep);
349
- out->cur += out->opts->dump_opts.after_size;
332
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
350
333
  }
351
334
  oj_dump_str(rb_funcall(obj, message_id, 0), 0, out, false);
352
335
  assure_size(out, size + sep_len + 6);
353
336
  *out->cur++ = ',';
354
337
  fill_indent(out, d3);
355
- *out->cur++ = '"';
356
- *out->cur++ = 'b';
357
- *out->cur++ = '"';
338
+ APPEND_CHARS(out->cur, "\"b\"", 3);
358
339
  if (0 < out->opts->dump_opts.before_size) {
359
- strcpy(out->cur, out->opts->dump_opts.before_sep);
360
- out->cur += out->opts->dump_opts.before_size;
340
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
361
341
  }
362
342
  *out->cur++ = ':';
363
343
  if (0 < out->opts->dump_opts.after_size) {
364
- strcpy(out->cur, out->opts->dump_opts.after_sep);
365
- out->cur += out->opts->dump_opts.after_size;
344
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
366
345
  }
367
346
  dump_array(rb_funcall(obj, backtrace_id, 0), depth, out, false);
368
347
  fill_indent(out, depth);
@@ -398,17 +377,13 @@ range_alt(VALUE obj, int depth, Out out) {
398
377
  assure_size(out, size + sep_len + 6);
399
378
  *out->cur++ = ',';
400
379
  fill_indent(out, d3);
401
- *out->cur++ = '"';
402
- *out->cur++ = 'a';
403
- *out->cur++ = '"';
380
+ APPEND_CHARS(out->cur, "\"a\"", 3);
404
381
  if (0 < out->opts->dump_opts.before_size) {
405
- strcpy(out->cur, out->opts->dump_opts.before_sep);
406
- out->cur += out->opts->dump_opts.before_size;
382
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
407
383
  }
408
384
  *out->cur++ = ':';
409
385
  if (0 < out->opts->dump_opts.after_size) {
410
- strcpy(out->cur, out->opts->dump_opts.after_sep);
411
- out->cur += out->opts->dump_opts.after_size;
386
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
412
387
  }
413
388
  args[0] = rb_funcall(obj, oj_begin_id, 0);
414
389
  args[1] = rb_funcall(obj, oj_end_id, 0);
@@ -476,12 +451,12 @@ time_alt(VALUE obj, int depth, Out out) {
476
451
  sec = (long long)ts.tv_sec;
477
452
  nsec = ts.tv_nsec;
478
453
  } else {
479
- sec = rb_num2ll(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
480
- nsec = rb_num2ll(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
454
+ sec = NUM2LL(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
455
+ nsec = NUM2LL(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
481
456
  }
482
457
  #else
483
- sec = rb_num2ll(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
484
- nsec = rb_num2ll(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
458
+ sec = NUM2LL(rb_funcall2(obj, oj_tv_sec_id, 0, 0));
459
+ nsec = NUM2LL(rb_funcall2(obj, oj_tv_nsec_id, 0, 0));
485
460
  #endif
486
461
 
487
462
  attrs[0].num = sec;
@@ -613,18 +588,21 @@ dump_float(VALUE obj, int depth, Out out, bool as_ok) {
613
588
  } else if (OJ_INFINITY == d) {
614
589
  if (WordNan == out->opts->dump_opts.nan_dump) {
615
590
  strcpy(buf, "Infinity");
591
+ cnt = 8;
616
592
  } else {
617
593
  raise_json_err("Infinity not allowed in JSON.", "GeneratorError");
618
594
  }
619
595
  } else if (-OJ_INFINITY == d) {
620
596
  if (WordNan == out->opts->dump_opts.nan_dump) {
621
597
  strcpy(buf, "-Infinity");
598
+ cnt = 9;
622
599
  } else {
623
600
  raise_json_err("-Infinity not allowed in JSON.", "GeneratorError");
624
601
  }
625
602
  } else if (isnan(d)) {
626
603
  if (WordNan == out->opts->dump_opts.nan_dump) {
627
604
  strcpy(buf, "NaN");
605
+ cnt = 3;
628
606
  } else {
629
607
  raise_json_err("NaN not allowed in JSON.", "GeneratorError");
630
608
  }
@@ -639,9 +617,7 @@ dump_float(VALUE obj, int depth, Out out, bool as_ok) {
639
617
  cnt = (int)RSTRING_LEN(rstr);
640
618
  }
641
619
  assure_size(out, cnt);
642
- for (b = buf; '\0' != *b; b++) {
643
- *out->cur++ = *b;
644
- }
620
+ APPEND_CHARS(out->cur, buf, cnt);
645
621
  *out->cur = '\0';
646
622
  }
647
623
 
@@ -659,14 +635,12 @@ hash_cb(VALUE key, VALUE value, VALUE ov) {
659
635
  } else {
660
636
  assure_size(out, depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1);
661
637
  if (0 < out->opts->dump_opts.hash_size) {
662
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
663
- out->cur += out->opts->dump_opts.hash_size;
638
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
664
639
  }
665
640
  if (0 < out->opts->dump_opts.indent_size) {
666
641
  int i;
667
642
  for (i = depth; 0 < i; i--) {
668
- strcpy(out->cur, out->opts->dump_opts.indent_str);
669
- out->cur += out->opts->dump_opts.indent_size;
643
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
670
644
  }
671
645
  }
672
646
  }
@@ -687,13 +661,11 @@ hash_cb(VALUE key, VALUE value, VALUE ov) {
687
661
  } else {
688
662
  assure_size(out, out->opts->dump_opts.before_size + out->opts->dump_opts.after_size + 2);
689
663
  if (0 < out->opts->dump_opts.before_size) {
690
- strcpy(out->cur, out->opts->dump_opts.before_sep);
691
- out->cur += out->opts->dump_opts.before_size;
664
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
692
665
  }
693
666
  *out->cur++ = ':';
694
667
  if (0 < out->opts->dump_opts.after_size) {
695
- strcpy(out->cur, out->opts->dump_opts.after_sep);
696
- out->cur += out->opts->dump_opts.after_size;
668
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
697
669
  }
698
670
  }
699
671
  oj_dump_compat_val(value, depth, out, true);
@@ -719,8 +691,7 @@ dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
719
691
  cnt = (int)RHASH_SIZE(obj);
720
692
  assure_size(out, 2);
721
693
  if (0 == cnt) {
722
- *out->cur++ = '{';
723
- *out->cur++ = '}';
694
+ APPEND_CHARS(out->cur, "{}", 2);
724
695
  } else {
725
696
  *out->cur++ = '{';
726
697
  out->depth = depth + 1;
@@ -734,15 +705,13 @@ dump_hash(VALUE obj, int depth, Out out, bool as_ok) {
734
705
  } else {
735
706
  assure_size(out, depth * out->opts->dump_opts.indent_size + out->opts->dump_opts.hash_size + 1);
736
707
  if (0 < out->opts->dump_opts.hash_size) {
737
- strcpy(out->cur, out->opts->dump_opts.hash_nl);
738
- out->cur += out->opts->dump_opts.hash_size;
708
+ APPEND_CHARS(out->cur, out->opts->dump_opts.hash_nl, out->opts->dump_opts.hash_size);
739
709
  }
740
710
  if (0 < out->opts->dump_opts.indent_size) {
741
711
  int i;
742
712
 
743
713
  for (i = depth; 0 < i; i--) {
744
- strcpy(out->cur, out->opts->dump_opts.indent_str);
745
- out->cur += out->opts->dump_opts.indent_size;
714
+ APPEND_CHARS(out->cur, out->opts->dump_opts.indent_str, out->opts->dump_opts.indent_size);
746
715
  }
747
716
  }
748
717
  }
@@ -786,8 +755,7 @@ dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
786
755
  *out->cur++ = '"';
787
756
  oj_dump_compat_val(rb_funcall(obj, oj_begin_id, 0), 0, out, false);
788
757
  assure_size(out, 3);
789
- *out->cur++ = '.';
790
- *out->cur++ = '.';
758
+ APPEND_CHARS(out->cur, "..", 2);
791
759
  if (Qtrue == rb_funcall(obj, oj_exclude_end_id, 0)) {
792
760
  *out->cur++ = '.';
793
761
  }
@@ -833,17 +801,13 @@ dump_struct(VALUE obj, int depth, Out out, bool as_ok) {
833
801
  assure_size(out, size + sep_len + 6);
834
802
  *out->cur++ = ',';
835
803
  fill_indent(out, d3);
836
- *out->cur++ = '"';
837
- *out->cur++ = 'v';
838
- *out->cur++ = '"';
804
+ APPEND_CHARS(out->cur, "\"v\"", 3);
839
805
  if (0 < out->opts->dump_opts.before_size) {
840
- strcpy(out->cur, out->opts->dump_opts.before_sep);
841
- out->cur += out->opts->dump_opts.before_size;
806
+ APPEND_CHARS(out->cur, out->opts->dump_opts.before_sep, out->opts->dump_opts.before_size);
842
807
  }
843
808
  *out->cur++ = ':';
844
809
  if (0 < out->opts->dump_opts.after_size) {
845
- strcpy(out->cur, out->opts->dump_opts.after_sep);
846
- out->cur += out->opts->dump_opts.after_size;
810
+ APPEND_CHARS(out->cur, out->opts->dump_opts.after_sep, out->opts->dump_opts.after_size);
847
811
  }
848
812
  for (i = 0; i < cnt; i++) {
849
813
  #ifdef RSTRUCT_LEN
@@ -886,8 +850,7 @@ dump_bignum(VALUE obj, int depth, Out out, bool as_ok) {
886
850
  } else {
887
851
  assure_size(out, cnt);
888
852
  }
889
- memcpy(out->cur, RSTRING_PTR(rs), cnt);
890
- out->cur += cnt;
853
+ APPEND_CHARS(out->cur, RSTRING_PTR(rs), cnt);
891
854
  if (dump_as_string) {
892
855
  *out->cur++ = '"';
893
856
  }
data/ext/oj/dump_leaf.c CHANGED
@@ -8,36 +8,9 @@
8
8
 
9
9
  static void dump_leaf(Leaf leaf, int depth, Out out);
10
10
 
11
- static void grow(Out out, size_t len) {
12
- size_t size = out->end - out->buf;
13
- long pos = out->cur - out->buf;
14
- char * buf;
15
-
16
- size *= 2;
17
- if (size <= len * 2 + pos) {
18
- size += len;
19
- }
20
- if (out->allocated) {
21
- buf = REALLOC_N(out->buf, char, (size + BUFFER_EXTRA));
22
- } else {
23
- buf = ALLOC_N(char, (size + BUFFER_EXTRA));
24
- out->allocated = true;
25
- memcpy(buf, out->buf, out->end - out->buf + BUFFER_EXTRA);
26
- }
27
- if (0 == buf) {
28
- rb_raise(rb_eNoMemError, "Failed to create string. [%d:%s]\n", ENOSPC, strerror(ENOSPC));
29
- }
30
- out->buf = buf;
31
- out->end = buf + size;
32
- out->cur = out->buf + pos;
33
- }
34
-
35
11
  inline static void dump_chars(const char *s, size_t size, Out out) {
36
- if (out->end - out->cur <= (long)size) {
37
- grow(out, size);
38
- }
39
- memcpy(out->cur, s, size);
40
- out->cur += size;
12
+ assure_size(out, size);
13
+ APPEND_CHARS(out->cur, s, size);
41
14
  *out->cur = '\0';
42
15
  }
43
16
 
@@ -81,9 +54,7 @@ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
81
54
  int d2 = depth + 1;
82
55
 
83
56
  size = 2;
84
- if (out->end - out->cur <= (long)size) {
85
- grow(out, size);
86
- }
57
+ assure_size(out, size);
87
58
  *out->cur++ = '[';
88
59
  if (0 == leaf->elements) {
89
60
  *out->cur++ = ']';
@@ -93,9 +64,7 @@ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
93
64
 
94
65
  size = d2 * out->indent + 2;
95
66
  do {
96
- if (out->end - out->cur <= (long)size) {
97
- grow(out, size);
98
- }
67
+ assure_size(out, size);
99
68
  fill_indent(out, d2);
100
69
  dump_leaf(e, d2, out);
101
70
  if (e->next != first) {
@@ -104,9 +73,7 @@ static void dump_leaf_array(Leaf leaf, int depth, Out out) {
104
73
  e = e->next;
105
74
  } while (e != first);
106
75
  size = depth * out->indent + 1;
107
- if (out->end - out->cur <= (long)size) {
108
- grow(out, size);
109
- }
76
+ assure_size(out, size);
110
77
  fill_indent(out, depth);
111
78
  *out->cur++ = ']';
112
79
  }
@@ -118,9 +85,7 @@ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
118
85
  int d2 = depth + 1;
119
86
 
120
87
  size = 2;
121
- if (out->end - out->cur <= (long)size) {
122
- grow(out, size);
123
- }
88
+ assure_size(out, size);
124
89
  *out->cur++ = '{';
125
90
  if (0 == leaf->elements) {
126
91
  *out->cur++ = '}';
@@ -130,9 +95,7 @@ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
130
95
 
131
96
  size = d2 * out->indent + 2;
132
97
  do {
133
- if (out->end - out->cur <= (long)size) {
134
- grow(out, size);
135
- }
98
+ assure_size(out, size);
136
99
  fill_indent(out, d2);
137
100
  oj_dump_cstr(e->key, strlen(e->key), 0, 0, out);
138
101
  *out->cur++ = ':';
@@ -143,9 +106,7 @@ static void dump_leaf_hash(Leaf leaf, int depth, Out out) {
143
106
  e = e->next;
144
107
  } while (e != first);
145
108
  size = depth * out->indent + 1;
146
- if (out->end - out->cur <= (long)size) {
147
- grow(out, size);
148
- }
109
+ assure_size(out, size);
149
110
  fill_indent(out, depth);
150
111
  *out->cur++ = '}';
151
112
  }
@@ -168,10 +129,7 @@ static void dump_leaf(Leaf leaf, int depth, Out out) {
168
129
 
169
130
  void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
170
131
  if (0 == out->buf) {
171
- out->buf = ALLOC_N(char, 4096);
172
- out->end = out->buf + 4095 -
173
- BUFFER_EXTRA; // 1 less than end plus extra for possible errors
174
- out->allocated = true;
132
+ oj_out_init(out);
175
133
  }
176
134
  out->cur = out->buf;
177
135
  out->circ_cnt = 0;
@@ -182,14 +140,12 @@ void oj_dump_leaf_to_json(Leaf leaf, Options copts, Out out) {
182
140
  }
183
141
 
184
142
  void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
185
- char buf[4096];
186
143
  struct _out out;
187
144
  size_t size;
188
145
  FILE * f;
189
146
 
190
- out.buf = buf;
191
- out.end = buf + sizeof(buf) - BUFFER_EXTRA;
192
- out.allocated = false;
147
+ oj_out_init(&out);
148
+
193
149
  out.omit_nil = copts->dump_opts.omit_nil;
194
150
  oj_dump_leaf_to_json(leaf, copts, &out);
195
151
  size = out.cur - out.buf;
@@ -201,8 +157,8 @@ void oj_write_leaf_to_file(Leaf leaf, const char *path, Options copts) {
201
157
 
202
158
  rb_raise(rb_eIOError, "Write failed. [%d:%s]\n", err, strerror(err));
203
159
  }
204
- if (out.allocated) {
205
- xfree(out.buf);
206
- }
160
+
161
+ oj_out_free(&out);
162
+
207
163
  fclose(f);
208
164
  }