oj 2.18.3 → 3.13.14

Sign up to get free protection for your applications and to get access to all the features.
Files changed (182) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +1324 -0
  3. data/README.md +51 -204
  4. data/RELEASE_NOTES.md +61 -0
  5. data/ext/oj/buf.h +49 -72
  6. data/ext/oj/cache.c +326 -0
  7. data/ext/oj/cache.h +21 -0
  8. data/ext/oj/cache8.c +61 -64
  9. data/ext/oj/cache8.h +12 -39
  10. data/ext/oj/circarray.c +37 -68
  11. data/ext/oj/circarray.h +16 -42
  12. data/ext/oj/code.c +221 -0
  13. data/ext/oj/code.h +40 -0
  14. data/ext/oj/compat.c +231 -107
  15. data/ext/oj/custom.c +1125 -0
  16. data/ext/oj/debug.c +132 -0
  17. data/ext/oj/dump.c +935 -2513
  18. data/ext/oj/dump.h +108 -0
  19. data/ext/oj/dump_compat.c +936 -0
  20. data/ext/oj/dump_leaf.c +164 -0
  21. data/ext/oj/dump_object.c +761 -0
  22. data/ext/oj/dump_strict.c +410 -0
  23. data/ext/oj/encode.h +7 -42
  24. data/ext/oj/encoder.c +43 -0
  25. data/ext/oj/err.c +40 -54
  26. data/ext/oj/err.h +52 -46
  27. data/ext/oj/extconf.rb +21 -30
  28. data/ext/oj/fast.c +1097 -1080
  29. data/ext/oj/intern.c +301 -0
  30. data/ext/oj/intern.h +26 -0
  31. data/ext/oj/mimic_json.c +893 -0
  32. data/ext/oj/object.c +549 -620
  33. data/ext/oj/odd.c +155 -167
  34. data/ext/oj/odd.h +37 -63
  35. data/ext/oj/oj.c +1661 -2063
  36. data/ext/oj/oj.h +341 -270
  37. data/ext/oj/parse.c +974 -737
  38. data/ext/oj/parse.h +105 -97
  39. data/ext/oj/parser.c +1526 -0
  40. data/ext/oj/parser.h +90 -0
  41. data/ext/oj/rails.c +1504 -0
  42. data/ext/oj/rails.h +18 -0
  43. data/ext/oj/reader.c +141 -163
  44. data/ext/oj/reader.h +75 -113
  45. data/ext/oj/resolve.c +45 -93
  46. data/ext/oj/resolve.h +7 -34
  47. data/ext/oj/rxclass.c +143 -0
  48. data/ext/oj/rxclass.h +26 -0
  49. data/ext/oj/saj.c +447 -511
  50. data/ext/oj/saj2.c +348 -0
  51. data/ext/oj/scp.c +91 -138
  52. data/ext/oj/sparse.c +793 -644
  53. data/ext/oj/stream_writer.c +331 -0
  54. data/ext/oj/strict.c +145 -109
  55. data/ext/oj/string_writer.c +493 -0
  56. data/ext/oj/trace.c +72 -0
  57. data/ext/oj/trace.h +28 -0
  58. data/ext/oj/usual.c +1254 -0
  59. data/ext/oj/util.c +136 -0
  60. data/ext/oj/util.h +20 -0
  61. data/ext/oj/val_stack.c +62 -70
  62. data/ext/oj/val_stack.h +95 -129
  63. data/ext/oj/validate.c +51 -0
  64. data/ext/oj/wab.c +622 -0
  65. data/lib/oj/bag.rb +1 -0
  66. data/lib/oj/easy_hash.rb +17 -8
  67. data/lib/oj/error.rb +10 -11
  68. data/lib/oj/json.rb +176 -0
  69. data/lib/oj/mimic.rb +158 -19
  70. data/lib/oj/state.rb +132 -0
  71. data/lib/oj/version.rb +2 -2
  72. data/lib/oj.rb +1 -31
  73. data/pages/Advanced.md +22 -0
  74. data/pages/Compatibility.md +25 -0
  75. data/pages/Custom.md +23 -0
  76. data/pages/Encoding.md +65 -0
  77. data/pages/JsonGem.md +94 -0
  78. data/pages/Modes.md +161 -0
  79. data/pages/Options.md +327 -0
  80. data/pages/Parser.md +309 -0
  81. data/pages/Rails.md +167 -0
  82. data/pages/Security.md +20 -0
  83. data/pages/WAB.md +13 -0
  84. data/test/activerecord/result_test.rb +32 -0
  85. data/test/activesupport4/decoding_test.rb +108 -0
  86. data/test/activesupport4/encoding_test.rb +531 -0
  87. data/test/activesupport4/test_helper.rb +41 -0
  88. data/test/activesupport5/abstract_unit.rb +45 -0
  89. data/test/activesupport5/decoding_test.rb +133 -0
  90. data/test/activesupport5/encoding_test.rb +500 -0
  91. data/test/activesupport5/encoding_test_cases.rb +98 -0
  92. data/test/activesupport5/test_helper.rb +72 -0
  93. data/test/activesupport5/time_zone_test_helpers.rb +39 -0
  94. data/test/activesupport6/abstract_unit.rb +44 -0
  95. data/test/activesupport6/decoding_test.rb +133 -0
  96. data/test/activesupport6/encoding_test.rb +507 -0
  97. data/test/activesupport6/encoding_test_cases.rb +98 -0
  98. data/test/activesupport6/test_common.rb +17 -0
  99. data/test/activesupport6/test_helper.rb +163 -0
  100. data/test/activesupport6/time_zone_test_helpers.rb +39 -0
  101. data/test/activesupport7/abstract_unit.rb +49 -0
  102. data/test/activesupport7/decoding_test.rb +125 -0
  103. data/test/activesupport7/encoding_test.rb +486 -0
  104. data/test/activesupport7/encoding_test_cases.rb +104 -0
  105. data/test/activesupport7/time_zone_test_helpers.rb +47 -0
  106. data/test/bar.rb +9 -0
  107. data/test/baz.rb +16 -0
  108. data/test/bug.rb +11 -46
  109. data/test/foo.rb +69 -16
  110. data/test/helper.rb +10 -1
  111. data/test/isolated/shared.rb +12 -8
  112. data/test/isolated/test_mimic_rails_after.rb +3 -3
  113. data/test/isolated/test_mimic_rails_before.rb +3 -3
  114. data/test/json_gem/json_addition_test.rb +216 -0
  115. data/test/json_gem/json_common_interface_test.rb +153 -0
  116. data/test/json_gem/json_encoding_test.rb +107 -0
  117. data/test/json_gem/json_ext_parser_test.rb +20 -0
  118. data/test/json_gem/json_fixtures_test.rb +35 -0
  119. data/test/json_gem/json_generator_test.rb +397 -0
  120. data/test/json_gem/json_generic_object_test.rb +90 -0
  121. data/test/json_gem/json_parser_test.rb +470 -0
  122. data/test/json_gem/json_string_matching_test.rb +42 -0
  123. data/test/json_gem/test_helper.rb +26 -0
  124. data/test/mem.rb +33 -0
  125. data/test/perf.rb +1 -1
  126. data/test/perf_compat.rb +30 -28
  127. data/test/perf_dump.rb +50 -0
  128. data/test/perf_object.rb +1 -1
  129. data/test/perf_once.rb +58 -0
  130. data/test/perf_parser.rb +189 -0
  131. data/test/perf_scp.rb +11 -10
  132. data/test/perf_strict.rb +30 -19
  133. data/test/perf_wab.rb +131 -0
  134. data/test/prec.rb +23 -0
  135. data/test/sample.rb +0 -1
  136. data/test/sample_json.rb +1 -1
  137. data/test/test_compat.rb +219 -102
  138. data/test/test_custom.rb +533 -0
  139. data/test/test_fast.rb +107 -35
  140. data/test/test_file.rb +19 -25
  141. data/test/test_generate.rb +21 -0
  142. data/test/test_hash.rb +11 -1
  143. data/test/test_integer_range.rb +72 -0
  144. data/test/test_null.rb +376 -0
  145. data/test/test_object.rb +357 -70
  146. data/test/test_parser.rb +27 -0
  147. data/test/test_parser_saj.rb +245 -0
  148. data/test/test_parser_usual.rb +217 -0
  149. data/test/test_rails.rb +35 -0
  150. data/test/test_saj.rb +1 -1
  151. data/test/test_scp.rb +39 -2
  152. data/test/test_strict.rb +186 -7
  153. data/test/test_various.rb +160 -774
  154. data/test/test_wab.rb +307 -0
  155. data/test/test_writer.rb +90 -2
  156. data/test/tests.rb +24 -0
  157. data/test/tests_mimic.rb +14 -0
  158. data/test/tests_mimic_addition.rb +7 -0
  159. data/test/zoo.rb +13 -0
  160. metadata +194 -56
  161. data/ext/oj/hash.c +0 -163
  162. data/ext/oj/hash.h +0 -46
  163. data/ext/oj/hash_test.c +0 -512
  164. data/test/activesupport_datetime_test.rb +0 -23
  165. data/test/bug2.rb +0 -10
  166. data/test/bug3.rb +0 -46
  167. data/test/bug_fast.rb +0 -32
  168. data/test/bug_load.rb +0 -24
  169. data/test/crash.rb +0 -111
  170. data/test/curl/curl_oj.rb +0 -46
  171. data/test/curl/get_oj.rb +0 -24
  172. data/test/curl/just_curl.rb +0 -31
  173. data/test/curl/just_oj.rb +0 -51
  174. data/test/example.rb +0 -11
  175. data/test/io.rb +0 -48
  176. data/test/isolated/test_mimic_rails_datetime.rb +0 -27
  177. data/test/mod.rb +0 -16
  178. data/test/rails.rb +0 -50
  179. data/test/russian.rb +0 -18
  180. data/test/struct.rb +0 -29
  181. data/test/test_serializer.rb +0 -59
  182. data/test/write_timebars.rb +0 -31
data/ext/oj/parser.c ADDED
@@ -0,0 +1,1526 @@
1
+ // Copyright (c) 2020, 2021, Peter Ohler, All rights reserved.
2
+
3
+ #include "parser.h"
4
+
5
+ #include <fcntl.h>
6
+
7
+ #include "oj.h"
8
+
9
+ #define DEBUG 0
10
+
11
+ #define USE_THREAD_LIMIT 0
12
+ // #define USE_THREAD_LIMIT 100000
13
+ #define MAX_EXP 4932
14
+ // max in the pow_map which is the limit for double
15
+ #define MAX_POW 308
16
+
17
+ #define MIN_SLEEP (1000000000LL / (double)CLOCKS_PER_SEC)
18
+ // 9,223,372,036,854,775,807
19
+ #define BIG_LIMIT LLONG_MAX / 10
20
+ #define FRAC_LIMIT 10000000000000000ULL
21
+
22
+ // Give better performance with indented JSON but worse with unindented.
23
+ //#define SPACE_JUMP
24
+
25
+ enum {
26
+ SKIP_CHAR = 'a',
27
+ SKIP_NEWLINE = 'b',
28
+ VAL_NULL = 'c',
29
+ VAL_TRUE = 'd',
30
+ VAL_FALSE = 'e',
31
+ VAL_NEG = 'f',
32
+ VAL0 = 'g',
33
+ VAL_DIGIT = 'h',
34
+ VAL_QUOTE = 'i',
35
+ OPEN_ARRAY = 'k',
36
+ OPEN_OBJECT = 'l',
37
+ CLOSE_ARRAY = 'm',
38
+ CLOSE_OBJECT = 'n',
39
+ AFTER_COMMA = 'o',
40
+ KEY_QUOTE = 'p',
41
+ COLON_COLON = 'q',
42
+ NUM_SPC = 'r',
43
+ NUM_NEWLINE = 's',
44
+ NUM_DOT = 't',
45
+ NUM_COMMA = 'u',
46
+ NUM_FRAC = 'v',
47
+ FRAC_E = 'w',
48
+ EXP_SIGN = 'x',
49
+ EXP_DIGIT = 'y',
50
+ STR_QUOTE = 'z',
51
+ NEG_DIGIT = '-',
52
+ STR_SLASH = 'A',
53
+ ESC_OK = 'B',
54
+ BIG_DIGIT = 'C',
55
+ BIG_DOT = 'D',
56
+ U_OK = 'E',
57
+ TOKEN_OK = 'F',
58
+ NUM_CLOSE_OBJECT = 'G',
59
+ NUM_CLOSE_ARRAY = 'H',
60
+ BIG_FRAC = 'I',
61
+ BIG_E = 'J',
62
+ BIG_EXP_SIGN = 'K',
63
+ BIG_EXP = 'L',
64
+ UTF1 = 'M', // expect 1 more follow byte
65
+ NUM_DIGIT = 'N',
66
+ NUM_ZERO = 'O',
67
+ UTF2 = 'P', // expect 2 more follow byte
68
+ UTF3 = 'Q', // expect 3 more follow byte
69
+ STR_OK = 'R',
70
+ UTFX = 'S', // following bytes
71
+ ESC_U = 'U',
72
+ CHAR_ERR = '.',
73
+ DONE = 'X',
74
+ };
75
+
76
+ /*
77
+ 0123456789abcdef0123456789abcdef */
78
+ static const char value_map[257] = "\
79
+ X........ab..a..................\
80
+ a.i..........f..ghhhhhhhhh......\
81
+ ...........................k.m..\
82
+ ......e.......c.....d......l.n..\
83
+ ................................\
84
+ ................................\
85
+ ................................\
86
+ ................................v";
87
+
88
+ static const char null_map[257] = "\
89
+ ................................\
90
+ ............o...................\
91
+ ................................\
92
+ ............F........F..........\
93
+ ................................\
94
+ ................................\
95
+ ................................\
96
+ ................................N";
97
+
98
+ static const char true_map[257] = "\
99
+ ................................\
100
+ ............o...................\
101
+ ................................\
102
+ .....F............F..F..........\
103
+ ................................\
104
+ ................................\
105
+ ................................\
106
+ ................................T";
107
+
108
+ static const char false_map[257] = "\
109
+ ................................\
110
+ ............o...................\
111
+ ................................\
112
+ .F...F......F......F............\
113
+ ................................\
114
+ ................................\
115
+ ................................\
116
+ ................................F";
117
+
118
+ static const char comma_map[257] = "\
119
+ .........ab..a..................\
120
+ a.i..........f..ghhhhhhhhh......\
121
+ ...........................k....\
122
+ ......e.......c.....d......l....\
123
+ ................................\
124
+ ................................\
125
+ ................................\
126
+ ................................,";
127
+
128
+ static const char after_map[257] = "\
129
+ X........ab..a..................\
130
+ a...........o...................\
131
+ .............................m..\
132
+ .............................n..\
133
+ ................................\
134
+ ................................\
135
+ ................................\
136
+ ................................a";
137
+
138
+ static const char key1_map[257] = "\
139
+ .........ab..a..................\
140
+ a.p.............................\
141
+ ................................\
142
+ .............................n..\
143
+ ................................\
144
+ ................................\
145
+ ................................\
146
+ ................................K";
147
+
148
+ static const char key_map[257] = "\
149
+ .........ab..a..................\
150
+ a.p.............................\
151
+ ................................\
152
+ ................................\
153
+ ................................\
154
+ ................................\
155
+ ................................\
156
+ ................................k";
157
+
158
+ static const char colon_map[257] = "\
159
+ .........ab..a..................\
160
+ a.........................q.....\
161
+ ................................\
162
+ ................................\
163
+ ................................\
164
+ ................................\
165
+ ................................\
166
+ ................................:";
167
+
168
+ static const char neg_map[257] = "\
169
+ ................................\
170
+ ................O---------......\
171
+ ................................\
172
+ ................................\
173
+ ................................\
174
+ ................................\
175
+ ................................\
176
+ ................................-";
177
+
178
+ static const char zero_map[257] = "\
179
+ .........rs..r..................\
180
+ r...........u.t.................\
181
+ .............................H..\
182
+ .............................G..\
183
+ ................................\
184
+ ................................\
185
+ ................................\
186
+ ................................0";
187
+
188
+ static const char digit_map[257] = "\
189
+ .........rs..r..................\
190
+ r...........u.t.NNNNNNNNNN......\
191
+ .....w.......................H..\
192
+ .....w.......................G..\
193
+ ................................\
194
+ ................................\
195
+ ................................\
196
+ ................................d";
197
+
198
+ static const char dot_map[257] = "\
199
+ ................................\
200
+ ................vvvvvvvvvv......\
201
+ ................................\
202
+ ................................\
203
+ ................................\
204
+ ................................\
205
+ ................................\
206
+ .................................";
207
+
208
+ static const char frac_map[257] = "\
209
+ .........rs..r..................\
210
+ r...........u...vvvvvvvvvv......\
211
+ .....w.......................H..\
212
+ .....w.......................G..\
213
+ ................................\
214
+ ................................\
215
+ ................................\
216
+ ................................f";
217
+
218
+ static const char exp_sign_map[257] = "\
219
+ ................................\
220
+ ...........x.x..yyyyyyyyyy......\
221
+ ................................\
222
+ ................................\
223
+ ................................\
224
+ ................................\
225
+ ................................\
226
+ ................................x";
227
+
228
+ static const char exp_zero_map[257] = "\
229
+ ................................\
230
+ ................yyyyyyyyyy......\
231
+ ................................\
232
+ ................................\
233
+ ................................\
234
+ ................................\
235
+ ................................\
236
+ ................................z";
237
+
238
+ static const char exp_map[257] = "\
239
+ .........rs..r..................\
240
+ r...........u...yyyyyyyyyy......\
241
+ .............................H..\
242
+ .............................G..\
243
+ ................................\
244
+ ................................\
245
+ ................................\
246
+ ................................X";
247
+
248
+ static const char big_digit_map[257] = "\
249
+ .........rs..r..................\
250
+ r...........u.D.CCCCCCCCCC......\
251
+ .....J.......................H..\
252
+ .....J.......................G..\
253
+ ................................\
254
+ ................................\
255
+ ................................\
256
+ ................................D";
257
+
258
+ static const char big_dot_map[257] = "\
259
+ ................................\
260
+ ................IIIIIIIIII......\
261
+ ................................\
262
+ ................................\
263
+ ................................\
264
+ ................................\
265
+ ................................\
266
+ ................................o";
267
+
268
+ static const char big_frac_map[257] = "\
269
+ .........rs..r..................\
270
+ r...........u...IIIIIIIIII......\
271
+ .....J.......................H..\
272
+ .....J.......................G..\
273
+ ................................\
274
+ ................................\
275
+ ................................\
276
+ ................................g";
277
+
278
+ static const char big_exp_sign_map[257] = "\
279
+ ................................\
280
+ ...........K.K..LLLLLLLLLL......\
281
+ ................................\
282
+ ................................\
283
+ ................................\
284
+ ................................\
285
+ ................................\
286
+ ................................B";
287
+
288
+ static const char big_exp_zero_map[257] = "\
289
+ ................................\
290
+ ................LLLLLLLLLL......\
291
+ ................................\
292
+ ................................\
293
+ ................................\
294
+ ................................\
295
+ ................................\
296
+ ................................Z";
297
+
298
+ static const char big_exp_map[257] = "\
299
+ .........rs..r..................\
300
+ r...........u...LLLLLLLLLL......\
301
+ .............................H..\
302
+ .............................G..\
303
+ ................................\
304
+ ................................\
305
+ ................................\
306
+ ................................Y";
307
+
308
+ static const char string_map[257] = "\
309
+ ................................\
310
+ RRzRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\
311
+ RRRRRRRRRRRRRRRRRRRRRRRRRRRRARRR\
312
+ RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR\
313
+ ................................\
314
+ ................................\
315
+ MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM\
316
+ PPPPPPPPPPPPPPPPQQQQQQQQ........s";
317
+
318
+ static const char esc_map[257] = "\
319
+ ................................\
320
+ ..B............B................\
321
+ ............................B...\
322
+ ..B...B.......B...B.BU..........\
323
+ ................................\
324
+ ................................\
325
+ ................................\
326
+ ................................~";
327
+
328
+ static const char esc_byte_map[257] = "\
329
+ ................................\
330
+ ..\"............/................\
331
+ ............................\\...\
332
+ ..\b...\f.......\n...\r.\t..........\
333
+ ................................\
334
+ ................................\
335
+ ................................\
336
+ ................................b";
337
+
338
+ static const char u_map[257] = "\
339
+ ................................\
340
+ ................EEEEEEEEEE......\
341
+ .EEEEEE.........................\
342
+ .EEEEEE.........................\
343
+ ................................\
344
+ ................................\
345
+ ................................\
346
+ ................................u";
347
+
348
+ static const char utf_map[257] = "\
349
+ ................................\
350
+ ................................\
351
+ ................................\
352
+ ................................\
353
+ SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\
354
+ SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSS\
355
+ ................................\
356
+ ................................8";
357
+
358
+ static const char space_map[257] = "\
359
+ .........ab..a..................\
360
+ a...............................\
361
+ ................................\
362
+ ................................\
363
+ ................................\
364
+ ................................\
365
+ ................................\
366
+ ................................S";
367
+
368
+ static const char trail_map[257] = "\
369
+ .........ab..a..................\
370
+ a...............................\
371
+ ................................\
372
+ ................................\
373
+ ................................\
374
+ ................................\
375
+ ................................\
376
+ ................................R";
377
+
378
+ static const byte hex_map[256] = "\
379
+ ................................\
380
+ ................\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09......\
381
+ .\x0a\x0b\x0c\x0d\x0e\x0f.........................\
382
+ .\x0a\x0b\x0c\x0d\x0e\x0f.........................\
383
+ ................................\
384
+ ................................\
385
+ ................................\
386
+ ................................";
387
+
388
+ static long double pow_map[309] = {
389
+ 1.0L, 1.0e1L, 1.0e2L, 1.0e3L, 1.0e4L, 1.0e5L, 1.0e6L, 1.0e7L, 1.0e8L, 1.0e9L, 1.0e10L,
390
+ 1.0e11L, 1.0e12L, 1.0e13L, 1.0e14L, 1.0e15L, 1.0e16L, 1.0e17L, 1.0e18L, 1.0e19L, 1.0e20L, 1.0e21L,
391
+ 1.0e22L, 1.0e23L, 1.0e24L, 1.0e25L, 1.0e26L, 1.0e27L, 1.0e28L, 1.0e29L, 1.0e30L, 1.0e31L, 1.0e32L,
392
+ 1.0e33L, 1.0e34L, 1.0e35L, 1.0e36L, 1.0e37L, 1.0e38L, 1.0e39L, 1.0e40L, 1.0e41L, 1.0e42L, 1.0e43L,
393
+ 1.0e44L, 1.0e45L, 1.0e46L, 1.0e47L, 1.0e48L, 1.0e49L, 1.0e50L, 1.0e51L, 1.0e52L, 1.0e53L, 1.0e54L,
394
+ 1.0e55L, 1.0e56L, 1.0e57L, 1.0e58L, 1.0e59L, 1.0e60L, 1.0e61L, 1.0e62L, 1.0e63L, 1.0e64L, 1.0e65L,
395
+ 1.0e66L, 1.0e67L, 1.0e68L, 1.0e69L, 1.0e70L, 1.0e71L, 1.0e72L, 1.0e73L, 1.0e74L, 1.0e75L, 1.0e76L,
396
+ 1.0e77L, 1.0e78L, 1.0e79L, 1.0e80L, 1.0e81L, 1.0e82L, 1.0e83L, 1.0e84L, 1.0e85L, 1.0e86L, 1.0e87L,
397
+ 1.0e88L, 1.0e89L, 1.0e90L, 1.0e91L, 1.0e92L, 1.0e93L, 1.0e94L, 1.0e95L, 1.0e96L, 1.0e97L, 1.0e98L,
398
+ 1.0e99L, 1.0e100L, 1.0e101L, 1.0e102L, 1.0e103L, 1.0e104L, 1.0e105L, 1.0e106L, 1.0e107L, 1.0e108L, 1.0e109L,
399
+ 1.0e110L, 1.0e111L, 1.0e112L, 1.0e113L, 1.0e114L, 1.0e115L, 1.0e116L, 1.0e117L, 1.0e118L, 1.0e119L, 1.0e120L,
400
+ 1.0e121L, 1.0e122L, 1.0e123L, 1.0e124L, 1.0e125L, 1.0e126L, 1.0e127L, 1.0e128L, 1.0e129L, 1.0e130L, 1.0e131L,
401
+ 1.0e132L, 1.0e133L, 1.0e134L, 1.0e135L, 1.0e136L, 1.0e137L, 1.0e138L, 1.0e139L, 1.0e140L, 1.0e141L, 1.0e142L,
402
+ 1.0e143L, 1.0e144L, 1.0e145L, 1.0e146L, 1.0e147L, 1.0e148L, 1.0e149L, 1.0e150L, 1.0e151L, 1.0e152L, 1.0e153L,
403
+ 1.0e154L, 1.0e155L, 1.0e156L, 1.0e157L, 1.0e158L, 1.0e159L, 1.0e160L, 1.0e161L, 1.0e162L, 1.0e163L, 1.0e164L,
404
+ 1.0e165L, 1.0e166L, 1.0e167L, 1.0e168L, 1.0e169L, 1.0e170L, 1.0e171L, 1.0e172L, 1.0e173L, 1.0e174L, 1.0e175L,
405
+ 1.0e176L, 1.0e177L, 1.0e178L, 1.0e179L, 1.0e180L, 1.0e181L, 1.0e182L, 1.0e183L, 1.0e184L, 1.0e185L, 1.0e186L,
406
+ 1.0e187L, 1.0e188L, 1.0e189L, 1.0e190L, 1.0e191L, 1.0e192L, 1.0e193L, 1.0e194L, 1.0e195L, 1.0e196L, 1.0e197L,
407
+ 1.0e198L, 1.0e199L, 1.0e200L, 1.0e201L, 1.0e202L, 1.0e203L, 1.0e204L, 1.0e205L, 1.0e206L, 1.0e207L, 1.0e208L,
408
+ 1.0e209L, 1.0e210L, 1.0e211L, 1.0e212L, 1.0e213L, 1.0e214L, 1.0e215L, 1.0e216L, 1.0e217L, 1.0e218L, 1.0e219L,
409
+ 1.0e220L, 1.0e221L, 1.0e222L, 1.0e223L, 1.0e224L, 1.0e225L, 1.0e226L, 1.0e227L, 1.0e228L, 1.0e229L, 1.0e230L,
410
+ 1.0e231L, 1.0e232L, 1.0e233L, 1.0e234L, 1.0e235L, 1.0e236L, 1.0e237L, 1.0e238L, 1.0e239L, 1.0e240L, 1.0e241L,
411
+ 1.0e242L, 1.0e243L, 1.0e244L, 1.0e245L, 1.0e246L, 1.0e247L, 1.0e248L, 1.0e249L, 1.0e250L, 1.0e251L, 1.0e252L,
412
+ 1.0e253L, 1.0e254L, 1.0e255L, 1.0e256L, 1.0e257L, 1.0e258L, 1.0e259L, 1.0e260L, 1.0e261L, 1.0e262L, 1.0e263L,
413
+ 1.0e264L, 1.0e265L, 1.0e266L, 1.0e267L, 1.0e268L, 1.0e269L, 1.0e270L, 1.0e271L, 1.0e272L, 1.0e273L, 1.0e274L,
414
+ 1.0e275L, 1.0e276L, 1.0e277L, 1.0e278L, 1.0e279L, 1.0e280L, 1.0e281L, 1.0e282L, 1.0e283L, 1.0e284L, 1.0e285L,
415
+ 1.0e286L, 1.0e287L, 1.0e288L, 1.0e289L, 1.0e290L, 1.0e291L, 1.0e292L, 1.0e293L, 1.0e294L, 1.0e295L, 1.0e296L,
416
+ 1.0e297L, 1.0e298L, 1.0e299L, 1.0e300L, 1.0e301L, 1.0e302L, 1.0e303L, 1.0e304L, 1.0e305L, 1.0e306L, 1.0e307L,
417
+ 1.0e308L};
418
+
419
+ static VALUE parser_class;
420
+
421
+ // Works with extended unicode as well. \Uffffffff if support is desired in
422
+ // the future.
423
+ static size_t unicodeToUtf8(uint32_t code, byte *buf) {
424
+ byte *start = buf;
425
+
426
+ if (0x0000007F >= code) {
427
+ *buf++ = (byte)code;
428
+ } else if (0x000007FF >= code) {
429
+ *buf++ = 0xC0 | (code >> 6);
430
+ *buf++ = 0x80 | (0x3F & code);
431
+ } else if (0x0000FFFF >= code) {
432
+ *buf++ = 0xE0 | (code >> 12);
433
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
434
+ *buf++ = 0x80 | (0x3F & code);
435
+ } else if (0x001FFFFF >= code) {
436
+ *buf++ = 0xF0 | (code >> 18);
437
+ *buf++ = 0x80 | ((code >> 12) & 0x3F);
438
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
439
+ *buf++ = 0x80 | (0x3F & code);
440
+ } else if (0x03FFFFFF >= code) {
441
+ *buf++ = 0xF8 | (code >> 24);
442
+ *buf++ = 0x80 | ((code >> 18) & 0x3F);
443
+ *buf++ = 0x80 | ((code >> 12) & 0x3F);
444
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
445
+ *buf++ = 0x80 | (0x3F & code);
446
+ } else if (0x7FFFFFFF >= code) {
447
+ *buf++ = 0xFC | (code >> 30);
448
+ *buf++ = 0x80 | ((code >> 24) & 0x3F);
449
+ *buf++ = 0x80 | ((code >> 18) & 0x3F);
450
+ *buf++ = 0x80 | ((code >> 12) & 0x3F);
451
+ *buf++ = 0x80 | ((code >> 6) & 0x3F);
452
+ *buf++ = 0x80 | (0x3F & code);
453
+ }
454
+ return buf - start;
455
+ }
456
+
457
+ static void parser_reset(ojParser p) {
458
+ p->reader = 0;
459
+ memset(&p->num, 0, sizeof(p->num));
460
+ buf_reset(&p->key);
461
+ buf_reset(&p->buf);
462
+ p->map = value_map;
463
+ p->next_map = NULL;
464
+ p->depth = 0;
465
+ }
466
+
467
+ static void parse_error(ojParser p, const char *fmt, ...) {
468
+ va_list ap;
469
+ char buf[256];
470
+
471
+ va_start(ap, fmt);
472
+ vsnprintf(buf, sizeof(buf), fmt, ap);
473
+ va_end(ap);
474
+ rb_raise(oj_json_parser_error_class, "%s at %ld:%ld", buf, p->line, p->col);
475
+ }
476
+
477
+ static void byte_error(ojParser p, byte b) {
478
+ switch (p->map[256]) {
479
+ case 'N': // null_map
480
+ parse_error(p, "expected null");
481
+ break;
482
+ case 'T': // true_map
483
+ parse_error(p, "expected true");
484
+ break;
485
+ case 'F': // false_map
486
+ parse_error(p, "expected false");
487
+ break;
488
+ case 's': // string_map
489
+ parse_error(p, "invalid JSON character 0x%02x", b);
490
+ break;
491
+ default: parse_error(p, "unexpected character '%c' in '%c' mode", b, p->map[256]); break;
492
+ }
493
+ }
494
+
495
+ static void calc_num(ojParser p) {
496
+ switch (p->type) {
497
+ case OJ_INT:
498
+ if (p->num.neg) {
499
+ p->num.fixnum = -p->num.fixnum;
500
+ p->num.neg = false;
501
+ }
502
+ p->funcs[p->stack[p->depth]].add_int(p);
503
+ break;
504
+ case OJ_DECIMAL: {
505
+ long double d = (long double)p->num.fixnum;
506
+
507
+ if (p->num.neg) {
508
+ d = -d;
509
+ }
510
+ if (0 < p->num.shift) {
511
+ d /= pow_map[p->num.shift];
512
+ }
513
+ if (0 < p->num.exp) {
514
+ long double x;
515
+
516
+ if (MAX_POW < p->num.exp) {
517
+ x = powl(10.0L, (long double)p->num.exp);
518
+ } else {
519
+ x = pow_map[p->num.exp];
520
+ }
521
+ if (p->num.exp_neg) {
522
+ d /= x;
523
+ } else {
524
+ d *= x;
525
+ }
526
+ }
527
+ p->num.dub = d;
528
+ p->funcs[p->stack[p->depth]].add_float(p);
529
+ break;
530
+ }
531
+ case OJ_BIG: p->funcs[p->stack[p->depth]].add_big(p);
532
+ default:
533
+ // nothing to do
534
+ break;
535
+ }
536
+ }
537
+
538
+ static void big_change(ojParser p) {
539
+ char buf[32];
540
+ int64_t i = p->num.fixnum;
541
+ int len = 0;
542
+
543
+ buf[sizeof(buf) - 1] = '\0';
544
+ p->buf.tail = p->buf.head;
545
+ switch (p->type) {
546
+ case OJ_INT:
547
+ // If an int then it will fit in the num.raw so no need to check length;
548
+ for (len = sizeof(buf) - 1; 0 < i; len--, i /= 10) {
549
+ buf[len] = '0' + (i % 10);
550
+ }
551
+ if (p->num.neg) {
552
+ buf[len] = '-';
553
+ len--;
554
+ }
555
+ buf_append_string(&p->buf, buf + len + 1, sizeof(buf) - len - 1);
556
+ p->type = OJ_BIG;
557
+ break;
558
+ case OJ_DECIMAL: {
559
+ int shift = p->num.shift;
560
+
561
+ for (len = sizeof(buf) - 1; 0 < i; len--, i /= 10, shift--) {
562
+ if (0 == shift) {
563
+ buf[len] = '.';
564
+ len--;
565
+ }
566
+ buf[len] = '0' + (i % 10);
567
+ }
568
+ if (p->num.neg) {
569
+ buf[len] = '-';
570
+ len--;
571
+ }
572
+ buf_append_string(&p->buf, buf + len + 1, sizeof(buf) - len - 1);
573
+ if (0 < p->num.exp) {
574
+ int x = p->num.exp;
575
+ int d, div;
576
+ bool started = false;
577
+
578
+ buf_append(&p->buf, 'e');
579
+ if (0 < p->num.exp_neg) {
580
+ buf_append(&p->buf, '-');
581
+ }
582
+ for (div = 1000; 0 < div; div /= 10) {
583
+ d = x / div % 10;
584
+ if (started || 0 < d) {
585
+ buf_append(&p->buf, '0' + d);
586
+ }
587
+ }
588
+ }
589
+ p->type = OJ_BIG;
590
+ break;
591
+ }
592
+ default: break;
593
+ }
594
+ }
595
+
596
+ static void parse(ojParser p, const byte *json) {
597
+ const byte *start;
598
+ const byte *b = json;
599
+ int i;
600
+
601
+ #if DEBUG
602
+ printf("*** parse - mode: %c %s\n", p->map[256], (const char *)json);
603
+ #endif
604
+ for (; '\0' != *b; b++) {
605
+ #if DEBUG
606
+ printf("*** parse - mode: %c %02x %s => %c\n", p->map[256], *b, b, p->map[*b]);
607
+ #endif
608
+ switch (p->map[*b]) {
609
+ case SKIP_NEWLINE:
610
+ p->line++;
611
+ p->col = b - json;
612
+ b++;
613
+ #ifdef SPACE_JUMP
614
+ // for (uint32_t *sj = (uint32_t*)b; 0x20202020 == *sj; sj++) { b += 4; }
615
+ for (uint16_t *sj = (uint16_t *)b; 0x2020 == *sj; sj++) {
616
+ b += 2;
617
+ }
618
+ #endif
619
+ for (; SKIP_CHAR == space_map[*b]; b++) {
620
+ }
621
+ b--;
622
+ break;
623
+ case COLON_COLON: p->map = value_map; break;
624
+ case SKIP_CHAR: break;
625
+ case KEY_QUOTE:
626
+ b++;
627
+ p->key.tail = p->key.head;
628
+ start = b;
629
+ for (; STR_OK == string_map[*b]; b++) {
630
+ }
631
+ buf_append_string(&p->key, (const char *)start, b - start);
632
+ if ('"' == *b) {
633
+ p->map = colon_map;
634
+ break;
635
+ }
636
+ b--;
637
+ p->map = string_map;
638
+ p->next_map = colon_map;
639
+ break;
640
+ case AFTER_COMMA:
641
+ if (0 < p->depth && OBJECT_FUN == p->stack[p->depth]) {
642
+ p->map = key_map;
643
+ } else {
644
+ p->map = comma_map;
645
+ }
646
+ break;
647
+ case VAL_QUOTE:
648
+ b++;
649
+ start = b;
650
+ p->buf.tail = p->buf.head;
651
+ for (; STR_OK == string_map[*b]; b++) {
652
+ }
653
+ buf_append_string(&p->buf, (const char *)start, b - start);
654
+ if ('"' == *b) {
655
+ p->funcs[p->stack[p->depth]].add_str(p);
656
+ p->map = (0 == p->depth) ? value_map : after_map;
657
+ break;
658
+ }
659
+ b--;
660
+ p->map = string_map;
661
+ p->next_map = (0 == p->depth) ? value_map : after_map;
662
+ break;
663
+ case OPEN_OBJECT:
664
+ p->funcs[p->stack[p->depth]].open_object(p);
665
+ p->depth++;
666
+ p->stack[p->depth] = OBJECT_FUN;
667
+ p->map = key1_map;
668
+ break;
669
+ case NUM_CLOSE_OBJECT:
670
+ calc_num(p);
671
+ // flow through
672
+ case CLOSE_OBJECT:
673
+ p->map = (1 == p->depth) ? value_map : after_map;
674
+ if (p->depth <= 0 || OBJECT_FUN != p->stack[p->depth]) {
675
+ p->col = b - json - p->col + 1;
676
+ parse_error(p, "unexpected object close");
677
+ return;
678
+ }
679
+ p->depth--;
680
+ p->funcs[p->stack[p->depth]].close_object(p);
681
+ break;
682
+ case OPEN_ARRAY:
683
+ p->funcs[p->stack[p->depth]].open_array(p);
684
+ p->depth++;
685
+ p->stack[p->depth] = ARRAY_FUN;
686
+ p->map = value_map;
687
+ break;
688
+ case NUM_CLOSE_ARRAY:
689
+ calc_num(p);
690
+ // flow through
691
+ case CLOSE_ARRAY:
692
+ p->map = (1 == p->depth) ? value_map : after_map;
693
+ if (p->depth <= 0 || ARRAY_FUN != p->stack[p->depth]) {
694
+ p->col = b - json - p->col + 1;
695
+ parse_error(p, "unexpected array close");
696
+ return;
697
+ }
698
+ p->depth--;
699
+ p->funcs[p->stack[p->depth]].close_array(p);
700
+ break;
701
+ case NUM_COMMA:
702
+ calc_num(p);
703
+ if (0 < p->depth && OBJECT_FUN == p->stack[p->depth]) {
704
+ p->map = key_map;
705
+ } else {
706
+ p->map = comma_map;
707
+ }
708
+ break;
709
+ case VAL0:
710
+ p->type = OJ_INT;
711
+ p->num.fixnum = 0;
712
+ p->num.neg = false;
713
+ p->num.shift = 0;
714
+ p->num.len = 0;
715
+ p->num.exp = 0;
716
+ p->num.exp_neg = false;
717
+ p->map = zero_map;
718
+ break;
719
+ case VAL_NEG:
720
+ p->type = OJ_INT;
721
+ p->num.fixnum = 0;
722
+ p->num.neg = true;
723
+ p->num.shift = 0;
724
+ p->num.len = 0;
725
+ p->num.exp = 0;
726
+ p->num.exp_neg = false;
727
+ p->map = neg_map;
728
+ break;
729
+ ;
730
+ case VAL_DIGIT:
731
+ p->type = OJ_INT;
732
+ p->num.fixnum = 0;
733
+ p->num.neg = false;
734
+ p->num.shift = 0;
735
+ p->num.exp = 0;
736
+ p->num.exp_neg = false;
737
+ p->num.len = 0;
738
+ p->map = digit_map;
739
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
740
+ uint64_t x = (uint64_t)p->num.fixnum * 10 + (uint64_t)(*b - '0');
741
+
742
+ // Tried just checking for an int less than zero but that
743
+ // fails when optimization is on for some reason with the
744
+ // clang compiler so us a bit mask instead.
745
+ if (x < BIG_LIMIT) {
746
+ p->num.fixnum = (int64_t)x;
747
+ } else {
748
+ big_change(p);
749
+ p->map = big_digit_map;
750
+ break;
751
+ }
752
+ }
753
+ b--;
754
+ break;
755
+ case NUM_DIGIT:
756
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
757
+ uint64_t x = p->num.fixnum * 10 + (uint64_t)(*b - '0');
758
+
759
+ if (x < BIG_LIMIT) {
760
+ p->num.fixnum = (int64_t)x;
761
+ } else {
762
+ big_change(p);
763
+ p->map = big_digit_map;
764
+ break;
765
+ }
766
+ }
767
+ b--;
768
+ break;
769
+ case NUM_DOT:
770
+ p->type = OJ_DECIMAL;
771
+ p->map = dot_map;
772
+ break;
773
+ case NUM_FRAC:
774
+ p->map = frac_map;
775
+ for (; NUM_FRAC == frac_map[*b]; b++) {
776
+ uint64_t x = p->num.fixnum * 10 + (uint64_t)(*b - '0');
777
+
778
+ if (x < FRAC_LIMIT) {
779
+ p->num.fixnum = (int64_t)x;
780
+ p->num.shift++;
781
+ } else {
782
+ big_change(p);
783
+ p->map = big_frac_map;
784
+ break;
785
+ }
786
+ }
787
+ b--;
788
+ break;
789
+ case FRAC_E:
790
+ p->type = OJ_DECIMAL;
791
+ p->map = exp_sign_map;
792
+ break;
793
+ case NUM_ZERO: p->map = zero_map; break;
794
+ case NEG_DIGIT:
795
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
796
+ uint64_t x = p->num.fixnum * 10 + (uint64_t)(*b - '0');
797
+
798
+ if (x < BIG_LIMIT) {
799
+ p->num.fixnum = (int64_t)x;
800
+ } else {
801
+ big_change(p);
802
+ p->map = big_digit_map;
803
+ break;
804
+ }
805
+ }
806
+ b--;
807
+ p->map = digit_map;
808
+ break;
809
+ case EXP_SIGN:
810
+ p->num.exp_neg = ('-' == *b);
811
+ p->map = exp_zero_map;
812
+ break;
813
+ case EXP_DIGIT:
814
+ p->map = exp_map;
815
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
816
+ int16_t x = p->num.exp * 10 + (int16_t)(*b - '0');
817
+
818
+ if (x <= MAX_EXP) {
819
+ p->num.exp = x;
820
+ } else {
821
+ big_change(p);
822
+ p->map = big_exp_map;
823
+ break;
824
+ }
825
+ }
826
+ b--;
827
+ break;
828
+ case BIG_DIGIT:
829
+ start = b;
830
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
831
+ }
832
+ buf_append_string(&p->buf, (const char *)start, b - start);
833
+ b--;
834
+ break;
835
+ case BIG_DOT:
836
+ buf_append(&p->buf, '.');
837
+ p->map = big_dot_map;
838
+ break;
839
+ case BIG_FRAC:
840
+ p->map = big_frac_map;
841
+ start = b;
842
+ for (; NUM_FRAC == frac_map[*b]; b++) {
843
+ }
844
+ buf_append_string(&p->buf, (const char *)start, b - start);
845
+ b--;
846
+ break;
847
+ case BIG_E:
848
+ buf_append(&p->buf, *b);
849
+ p->map = big_exp_sign_map;
850
+ break;
851
+ case BIG_EXP_SIGN:
852
+ buf_append(&p->buf, *b);
853
+ p->map = big_exp_zero_map;
854
+ break;
855
+ case BIG_EXP:
856
+ start = b;
857
+ for (; NUM_DIGIT == digit_map[*b]; b++) {
858
+ }
859
+ buf_append_string(&p->buf, (const char *)start, b - start);
860
+ b--;
861
+ p->map = big_exp_map;
862
+ break;
863
+ case NUM_SPC: calc_num(p); break;
864
+ case NUM_NEWLINE: calc_num(p); b++;
865
+ #ifdef SPACE_JUMP
866
+ // for (uint32_t *sj = (uint32_t*)b; 0x20202020 == *sj; sj++) { b += 4; }
867
+ for (uint16_t *sj = (uint16_t *)b; 0x2020 == *sj; sj++) {
868
+ b += 2;
869
+ }
870
+ #endif
871
+ for (; SKIP_CHAR == space_map[*b]; b++) {
872
+ }
873
+ b--;
874
+ break;
875
+ case STR_OK:
876
+ start = b;
877
+ for (; STR_OK == string_map[*b]; b++) {
878
+ }
879
+ if (':' == p->next_map[256]) {
880
+ buf_append_string(&p->key, (const char *)start, b - start);
881
+ } else {
882
+ buf_append_string(&p->buf, (const char *)start, b - start);
883
+ }
884
+ if ('"' == *b) {
885
+ p->funcs[p->stack[p->depth]].add_str(p);
886
+ p->map = p->next_map;
887
+ break;
888
+ }
889
+ b--;
890
+ break;
891
+ case STR_SLASH: p->map = esc_map; break;
892
+ case STR_QUOTE:
893
+ p->funcs[p->stack[p->depth]].add_str(p);
894
+ p->map = p->next_map;
895
+ break;
896
+ case ESC_U:
897
+ p->map = u_map;
898
+ p->ri = 0;
899
+ p->ucode = 0;
900
+ break;
901
+ case U_OK:
902
+ p->ri++;
903
+ p->ucode = p->ucode << 4 | (uint32_t)hex_map[*b];
904
+ if (4 <= p->ri) {
905
+ byte utf8[8];
906
+ size_t ulen = unicodeToUtf8(p->ucode, utf8);
907
+
908
+ if (0 < ulen) {
909
+ if (':' == p->next_map[256]) {
910
+ buf_append_string(&p->key, (const char *)utf8, ulen);
911
+ } else {
912
+ buf_append_string(&p->buf, (const char *)utf8, ulen);
913
+ }
914
+ } else {
915
+ parse_error(p, "invalid unicode");
916
+ return;
917
+ }
918
+ p->map = string_map;
919
+ }
920
+ break;
921
+ case ESC_OK:
922
+ if (':' == p->next_map[256]) {
923
+ buf_append(&p->key, esc_byte_map[*b]);
924
+ } else {
925
+ buf_append(&p->buf, esc_byte_map[*b]);
926
+ }
927
+ p->map = string_map;
928
+ break;
929
+ case UTF1:
930
+ p->ri = 1;
931
+ p->map = utf_map;
932
+ if (':' == p->next_map[256]) {
933
+ buf_append(&p->key, *b);
934
+ } else {
935
+ buf_append(&p->buf, *b);
936
+ }
937
+ break;
938
+ case UTF2:
939
+ p->ri = 2;
940
+ p->map = utf_map;
941
+ if (':' == p->next_map[256]) {
942
+ buf_append(&p->key, *b);
943
+ } else {
944
+ buf_append(&p->buf, *b);
945
+ }
946
+ break;
947
+ case UTF3:
948
+ p->ri = 3;
949
+ p->map = utf_map;
950
+ if (':' == p->next_map[256]) {
951
+ buf_append(&p->key, *b);
952
+ } else {
953
+ buf_append(&p->buf, *b);
954
+ }
955
+ break;
956
+ case UTFX:
957
+ p->ri--;
958
+ if (':' == p->next_map[256]) {
959
+ buf_append(&p->key, *b);
960
+ } else {
961
+ buf_append(&p->buf, *b);
962
+ }
963
+ if (p->ri <= 0) {
964
+ p->map = string_map;
965
+ }
966
+ break;
967
+ case VAL_NULL:
968
+ if ('u' == b[1] && 'l' == b[2] && 'l' == b[3]) {
969
+ b += 3;
970
+ p->funcs[p->stack[p->depth]].add_null(p);
971
+ p->map = (0 == p->depth) ? value_map : after_map;
972
+ break;
973
+ }
974
+ p->ri = 0;
975
+ *p->token = *b++;
976
+ for (i = 1; i < 4; i++) {
977
+ if ('\0' == *b) {
978
+ p->ri = i;
979
+ break;
980
+ } else {
981
+ p->token[i] = *b++;
982
+ }
983
+ }
984
+ if (0 < p->ri) {
985
+ p->map = null_map;
986
+ b--;
987
+ break;
988
+ }
989
+ p->col = b - json - p->col;
990
+ parse_error(p, "expected null");
991
+ return;
992
+ case VAL_TRUE:
993
+ if ('r' == b[1] && 'u' == b[2] && 'e' == b[3]) {
994
+ b += 3;
995
+ p->funcs[p->stack[p->depth]].add_true(p);
996
+ p->map = (0 == p->depth) ? value_map : after_map;
997
+ break;
998
+ }
999
+ p->ri = 0;
1000
+ *p->token = *b++;
1001
+ for (i = 1; i < 4; i++) {
1002
+ if ('\0' == *b) {
1003
+ p->ri = i;
1004
+ break;
1005
+ } else {
1006
+ p->token[i] = *b++;
1007
+ }
1008
+ }
1009
+ if (0 < p->ri) {
1010
+ p->map = true_map;
1011
+ b--;
1012
+ break;
1013
+ }
1014
+ p->col = b - json - p->col;
1015
+ parse_error(p, "expected true");
1016
+ return;
1017
+ case VAL_FALSE:
1018
+ if ('a' == b[1] && 'l' == b[2] && 's' == b[3] && 'e' == b[4]) {
1019
+ b += 4;
1020
+ p->funcs[p->stack[p->depth]].add_false(p);
1021
+ p->map = (0 == p->depth) ? value_map : after_map;
1022
+ break;
1023
+ }
1024
+ p->ri = 0;
1025
+ *p->token = *b++;
1026
+ for (i = 1; i < 5; i++) {
1027
+ if ('\0' == *b) {
1028
+ p->ri = i;
1029
+ break;
1030
+ } else {
1031
+ p->token[i] = *b++;
1032
+ }
1033
+ }
1034
+ if (0 < p->ri) {
1035
+ p->map = false_map;
1036
+ b--;
1037
+ break;
1038
+ }
1039
+ p->col = b - json - p->col;
1040
+ parse_error(p, "expected false");
1041
+ return;
1042
+ case TOKEN_OK:
1043
+ p->token[p->ri] = *b;
1044
+ p->ri++;
1045
+ switch (p->map[256]) {
1046
+ case 'N':
1047
+ if (4 == p->ri) {
1048
+ if (0 != strncmp("null", p->token, 4)) {
1049
+ p->col = b - json - p->col;
1050
+ parse_error(p, "expected null");
1051
+ return;
1052
+ }
1053
+ p->funcs[p->stack[p->depth]].add_null(p);
1054
+ p->map = (0 == p->depth) ? value_map : after_map;
1055
+ }
1056
+ break;
1057
+ case 'F':
1058
+ if (5 == p->ri) {
1059
+ if (0 != strncmp("false", p->token, 5)) {
1060
+ p->col = b - json - p->col;
1061
+ parse_error(p, "expected false");
1062
+ return;
1063
+ }
1064
+ p->funcs[p->stack[p->depth]].add_false(p);
1065
+ p->map = (0 == p->depth) ? value_map : after_map;
1066
+ }
1067
+ break;
1068
+ case 'T':
1069
+ if (4 == p->ri) {
1070
+ if (0 != strncmp("true", p->token, 4)) {
1071
+ p->col = b - json - p->col;
1072
+ parse_error(p, "expected true");
1073
+ return;
1074
+ }
1075
+ p->funcs[p->stack[p->depth]].add_true(p);
1076
+ p->map = (0 == p->depth) ? value_map : after_map;
1077
+ }
1078
+ break;
1079
+ default:
1080
+ p->col = b - json - p->col;
1081
+ parse_error(p, "parse error");
1082
+ return;
1083
+ }
1084
+ break;
1085
+ case CHAR_ERR: byte_error(p, *b); return;
1086
+ default: break;
1087
+ }
1088
+ if (0 == p->depth && 'v' == p->map[256] && p->just_one) {
1089
+ p->map = trail_map;
1090
+ }
1091
+ }
1092
+ if (0 == p->depth) {
1093
+ switch (p->map[256]) {
1094
+ case '0':
1095
+ case 'd':
1096
+ case 'f':
1097
+ case 'z':
1098
+ case 'X':
1099
+ case 'D':
1100
+ case 'g':
1101
+ case 'B':
1102
+ case 'Y': calc_num(p); break;
1103
+ }
1104
+ }
1105
+ return;
1106
+ }
1107
+
1108
+ static void parser_free(void *ptr) {
1109
+ ojParser p;
1110
+
1111
+ if (0 == ptr) {
1112
+ return;
1113
+ }
1114
+ p = (ojParser)ptr;
1115
+ buf_cleanup(&p->key);
1116
+ buf_cleanup(&p->buf);
1117
+ p->free(p);
1118
+ xfree(ptr);
1119
+ }
1120
+
1121
+ static void parser_mark(void *ptr) {
1122
+ if (NULL != ptr) {
1123
+ ojParser p = (ojParser)ptr;
1124
+
1125
+ if (0 != p->reader) {
1126
+ rb_gc_mark(p->reader);
1127
+ }
1128
+ p->mark(p);
1129
+ }
1130
+ }
1131
+
1132
+ extern void oj_set_parser_validator(ojParser p);
1133
+ extern void oj_set_parser_saj(ojParser p);
1134
+ extern void oj_set_parser_usual(ojParser p);
1135
+ extern void oj_set_parser_debug(ojParser p);
1136
+
1137
+ static int opt_cb(VALUE rkey, VALUE value, VALUE ptr) {
1138
+ ojParser p = (ojParser)ptr;
1139
+ const char *key = NULL;
1140
+ char set_key[64];
1141
+ long klen;
1142
+
1143
+ switch (rb_type(rkey)) {
1144
+ case RUBY_T_SYMBOL:
1145
+ rkey = rb_sym2str(rkey);
1146
+ // fall through
1147
+ case RUBY_T_STRING:
1148
+ key = rb_string_value_ptr(&rkey);
1149
+ klen = RSTRING_LEN(rkey);
1150
+ break;
1151
+ default: rb_raise(rb_eArgError, "option keys must be a symbol or string");
1152
+ }
1153
+ if ((long)sizeof(set_key) - 1 <= klen) {
1154
+ return ST_CONTINUE;
1155
+ }
1156
+ memcpy(set_key, key, klen);
1157
+ set_key[klen] = '=';
1158
+ set_key[klen + 1] = '\0';
1159
+ p->option(p, set_key, value);
1160
+
1161
+ return ST_CONTINUE;
1162
+ }
1163
+
1164
+ /* Document-method: new
1165
+ * call-seq: new(mode=nil)
1166
+ *
1167
+ * Creates a new Parser with the specified mode. If no mode is provided
1168
+ * validation is assumed. Optional arguments can be provided that match the
1169
+ * mode. For example with the :usual mode the call might look like
1170
+ * Oj::Parser.new(:usual, cache_keys: true).
1171
+ */
1172
+ static VALUE parser_new(int argc, VALUE *argv, VALUE self) {
1173
+ ojParser p = ALLOC(struct _ojParser);
1174
+
1175
+ #if HAVE_RB_EXT_RACTOR_SAFE
1176
+ // This doesn't seem to do anything.
1177
+ rb_ext_ractor_safe(true);
1178
+ #endif
1179
+ memset(p, 0, sizeof(struct _ojParser));
1180
+ buf_init(&p->key);
1181
+ buf_init(&p->buf);
1182
+ p->map = value_map;
1183
+
1184
+ if (argc < 1) {
1185
+ oj_set_parser_validator(p);
1186
+ } else {
1187
+ VALUE mode = argv[0];
1188
+
1189
+ if (Qnil == mode) {
1190
+ oj_set_parser_validator(p);
1191
+ } else {
1192
+ const char *ms = NULL;
1193
+
1194
+ switch (rb_type(mode)) {
1195
+ case RUBY_T_SYMBOL:
1196
+ mode = rb_sym2str(mode);
1197
+ // fall through
1198
+ case RUBY_T_STRING: ms = RSTRING_PTR(mode); break;
1199
+ default: rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
1200
+ }
1201
+ if (0 == strcmp("usual", ms) || 0 == strcmp("standard", ms) || 0 == strcmp("strict", ms) ||
1202
+ 0 == strcmp("compat", ms)) {
1203
+ oj_set_parser_usual(p);
1204
+ } else if (0 == strcmp("object", ms)) {
1205
+ // TBD
1206
+ } else if (0 == strcmp("saj", ms)) {
1207
+ oj_set_parser_saj(p);
1208
+ } else if (0 == strcmp("validate", ms)) {
1209
+ oj_set_parser_validator(p);
1210
+ } else if (0 == strcmp("debug", ms)) {
1211
+ oj_set_parser_debug(p);
1212
+ } else {
1213
+ rb_raise(rb_eArgError, "mode must be :validate, :usual, :saj, or :object");
1214
+ }
1215
+ }
1216
+ if (1 < argc) {
1217
+ VALUE ropts = argv[1];
1218
+
1219
+ Check_Type(ropts, T_HASH);
1220
+ rb_hash_foreach(ropts, opt_cb, (VALUE)p);
1221
+ }
1222
+ }
1223
+ return Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1224
+ }
1225
+
1226
+ /* Document-method: method_missing(value)
1227
+ * call-seq: method_missing(value)
1228
+ *
1229
+ * Methods not handled by the parser are passed to the delegate. The methods
1230
+ * supported by delegate are:
1231
+ *
1232
+ * - *:validate*
1233
+ * - no options
1234
+ *
1235
+ * - *:saj*
1236
+ * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1237
+ * - _cache_keys_ returns the value of the _cache_keys_ flag.
1238
+ * - _cache_strings=_ sets the value of the _cache_strings_ to an positive integer less than 35. Strings shorter than
1239
+ * that length are cached.
1240
+ * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1241
+ * - _handler=_ sets the SAJ handler
1242
+ * - _handler_ returns the SAJ handler
1243
+ *
1244
+ * - *:usual*
1245
+ * - _cache_keys=_ sets the value of the _cache_keys_ flag.
1246
+ * - _cache_keys_ returns the value of the _cache_keys_ flag.
1247
+ * - _cache_strings=_ sets the value of the _cache_strings_ to a positive integer less than 35. Strings shorter than
1248
+ * that length are cached.
1249
+ * - _cache_strings_ returns the value of the _cache_strings_ integer value.
1250
+ * - _cache_expunge=_ sets the value of the _cache_expunge_ where 0 never expunges, 1 expunges slowly, 2 expunges
1251
+ * faster, and 3 or higher expunges agressively.
1252
+ * - _cache_expunge_ returns the value of the _cache_expunge_ integer value.
1253
+ * - _capacity=_ sets the capacity of the parser. The parser grows automatically but can be updated directly with this
1254
+ * call.
1255
+ * - _capacity_ returns the current capacity of the parser's internal stack.
1256
+ * - _create_id_ returns the value _create_id_ or _nil_ if there is no _create_id_.
1257
+ * - _create_id=_ sets the value _create_id_ or if _nil_ unsets it. Parsed JSON objects that include the specified
1258
+ * element use the element value as the name of the class to create an object from instead of a Hash.
1259
+ * - _decimal=_ sets the approach to how decimals are parser. If _:auto_ then the decimals with significant digits are
1260
+ * 16 or less are Floats and long ones are BigDecimal. _:ruby_ uses a call to Ruby to convert a string to a Float.
1261
+ * _:float_ always generates a Float. _:bigdecimal_ always results in a BigDecimal.
1262
+ * - _decimal_ returns the value of the decimal conversion option which can be :auto (default), :ruby, :float, or
1263
+ * :bigdecimal.
1264
+ * - _ignore_json_create_ returns the value of the _ignore_json_create_ flag.
1265
+ * - _ignore_json_create=_ sets the value of the _ignore_json_create_ flag. When set the class json_create method is
1266
+ * ignored on parsing in favor of creating an instance and populating directly.
1267
+ * - _missing_class_ return the value of the _missing_class_ indicator.
1268
+ * - _missing_class=_ sets the value of the _missing_class_ flag. Valid values are _:auto_ which creates any missing
1269
+ * classes on parse, :ignore which ignores and continues as a Hash (default), and :raise which raises an exception if
1270
+ * the class is not found.
1271
+ * - _omit_null=_ sets the _omit_null_ flag. If true then null values in a map or object are omitted from the
1272
+ * resulting Hash or Object.
1273
+ * - _omit_null_ returns the value of the _omit_null_ flag.
1274
+ * - _symbol_keys=_ sets the flag that indicates Hash keys should be parsed to Symbols versus Strings.
1275
+ * - _symbol_keys_ returns the value of the _symbol_keys_ flag.
1276
+ */
1277
+ static VALUE parser_missing(int argc, VALUE *argv, VALUE self) {
1278
+ ojParser p = (ojParser)DATA_PTR(self);
1279
+ const char *key = NULL;
1280
+ volatile VALUE rkey = *argv;
1281
+ volatile VALUE rv = Qnil;
1282
+
1283
+ #if HAVE_RB_EXT_RACTOR_SAFE
1284
+ // This doesn't seem to do anything.
1285
+ rb_ext_ractor_safe(true);
1286
+ #endif
1287
+ switch (rb_type(rkey)) {
1288
+ case RUBY_T_SYMBOL:
1289
+ rkey = rb_sym2str(rkey);
1290
+ // fall through
1291
+ case RUBY_T_STRING: key = rb_string_value_ptr(&rkey); break;
1292
+ default: rb_raise(rb_eArgError, "option method must be a symbol or string");
1293
+ }
1294
+ if (1 < argc) {
1295
+ rv = argv[1];
1296
+ }
1297
+ return p->option(p, key, rv);
1298
+ }
1299
+
1300
+ /* Document-method: parse(json)
1301
+ * call-seq: parse(json)
1302
+ *
1303
+ * Parse a JSON string.
1304
+ *
1305
+ * Returns the result according to the delegate of the parser.
1306
+ */
1307
+ static VALUE parser_parse(VALUE self, VALUE json) {
1308
+ ojParser p = (ojParser)DATA_PTR(self);
1309
+
1310
+ Check_Type(json, T_STRING);
1311
+ parser_reset(p);
1312
+ p->start(p);
1313
+ parse(p, (const byte *)rb_string_value_ptr(&json));
1314
+
1315
+ return p->result(p);
1316
+ }
1317
+
1318
+ static VALUE load_rescue(VALUE self, VALUE x) {
1319
+ // Normal EOF. No action needed other than to stop loading.
1320
+ return Qfalse;
1321
+ }
1322
+
1323
+ static VALUE load(VALUE self) {
1324
+ ojParser p = (ojParser)DATA_PTR(self);
1325
+ volatile VALUE rbuf = rb_str_new2("");
1326
+
1327
+ p->start(p);
1328
+ while (true) {
1329
+ rb_funcall(p->reader, oj_readpartial_id, 2, INT2NUM(16385), rbuf);
1330
+ if (0 < RSTRING_LEN(rbuf)) {
1331
+ parse(p, (byte *)StringValuePtr(rbuf));
1332
+ }
1333
+ }
1334
+ return Qtrue;
1335
+ }
1336
+
1337
+ /* Document-method: load(reader)
1338
+ * call-seq: load(reader)
1339
+ *
1340
+ * Parse a JSON stream.
1341
+ *
1342
+ * Returns the result according to the delegate of the parser.
1343
+ */
1344
+ static VALUE parser_load(VALUE self, VALUE reader) {
1345
+ ojParser p = (ojParser)DATA_PTR(self);
1346
+
1347
+ parser_reset(p);
1348
+ p->reader = reader;
1349
+ rb_rescue2(load, self, load_rescue, Qnil, rb_eEOFError, 0);
1350
+
1351
+ return p->result(p);
1352
+ }
1353
+
1354
+ /* Document-method: file(filename)
1355
+ * call-seq: file(filename)
1356
+ *
1357
+ * Parse a JSON file.
1358
+ *
1359
+ * Returns the result according to the delegate of the parser.
1360
+ */
1361
+ static VALUE parser_file(VALUE self, VALUE filename) {
1362
+ ojParser p = (ojParser)DATA_PTR(self);
1363
+ const char *path;
1364
+ int fd;
1365
+
1366
+ Check_Type(filename, T_STRING);
1367
+ path = rb_string_value_ptr(&filename);
1368
+
1369
+ parser_reset(p);
1370
+ p->start(p);
1371
+
1372
+ if (0 > (fd = open(path, O_RDONLY))) {
1373
+ rb_raise(rb_eIOError, "error opening %s", path);
1374
+ }
1375
+ #if USE_THREAD_LIMIT
1376
+ struct stat info;
1377
+ // st_size will be 0 if not a file
1378
+ if (0 == fstat(fd, &info) && USE_THREAD_LIMIT < info.st_size) {
1379
+ // Use threaded version.
1380
+ // TBD only if has pthreads
1381
+ // TBD parse_large(p, fd);
1382
+ return p->result(p);
1383
+ }
1384
+ #endif
1385
+ byte buf[16385];
1386
+ size_t size = sizeof(buf) - 1;
1387
+ size_t rsize;
1388
+
1389
+ while (true) {
1390
+ if (0 < (rsize = read(fd, buf, size))) {
1391
+ buf[rsize] = '\0';
1392
+ parse(p, buf);
1393
+ }
1394
+ if (rsize <= 0) {
1395
+ if (0 != rsize) {
1396
+ rb_raise(rb_eIOError, "error reading from %s", path);
1397
+ }
1398
+ break;
1399
+ }
1400
+ }
1401
+ return p->result(p);
1402
+ }
1403
+
1404
+ /* Document-method: just_one
1405
+ * call-seq: just_one
1406
+ *
1407
+ * Returns the current state of the just_one [_Boolean_] option.
1408
+ */
1409
+ static VALUE parser_just_one(VALUE self) {
1410
+ ojParser p = (ojParser)DATA_PTR(self);
1411
+
1412
+ return p->just_one ? Qtrue : Qfalse;
1413
+ }
1414
+
1415
+ /* Document-method: just_one=
1416
+ * call-seq: just_one=(value)
1417
+ *
1418
+ * Sets the *just_one* option which limits the parsing of a string or or
1419
+ * stream to a single JSON element.
1420
+ *
1421
+ * Returns the current state of the just_one [_Boolean_] option.
1422
+ */
1423
+ static VALUE parser_just_one_set(VALUE self, VALUE v) {
1424
+ ojParser p = (ojParser)DATA_PTR(self);
1425
+
1426
+ p->just_one = (Qtrue == v);
1427
+
1428
+ return p->just_one ? Qtrue : Qfalse;
1429
+ }
1430
+
1431
+ static VALUE usual_parser = Qundef;
1432
+
1433
+ /* Document-method: usual
1434
+ * call-seq: usual
1435
+ *
1436
+ * Returns the default usual parser. Note the default usual parser can not be
1437
+ * used concurrently in more than one thread.
1438
+ */
1439
+ static VALUE parser_usual(VALUE self) {
1440
+ if (Qundef == usual_parser) {
1441
+ ojParser p = ALLOC(struct _ojParser);
1442
+
1443
+ memset(p, 0, sizeof(struct _ojParser));
1444
+ buf_init(&p->key);
1445
+ buf_init(&p->buf);
1446
+ p->map = value_map;
1447
+ oj_set_parser_usual(p);
1448
+ usual_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1449
+ rb_gc_register_address(&usual_parser);
1450
+ }
1451
+ return usual_parser;
1452
+ }
1453
+
1454
+ static VALUE saj_parser = Qundef;
1455
+
1456
+ /* Document-method: saj
1457
+ * call-seq: saj
1458
+ *
1459
+ * Returns the default saj parser. Note the default SAJ parser can not be used
1460
+ * concurrently in more than one thread.
1461
+ */
1462
+ static VALUE parser_saj(VALUE self) {
1463
+ if (Qundef == saj_parser) {
1464
+ ojParser p = ALLOC(struct _ojParser);
1465
+
1466
+ memset(p, 0, sizeof(struct _ojParser));
1467
+ buf_init(&p->key);
1468
+ buf_init(&p->buf);
1469
+ p->map = value_map;
1470
+ oj_set_parser_saj(p);
1471
+ saj_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1472
+ rb_gc_register_address(&saj_parser);
1473
+ }
1474
+ return saj_parser;
1475
+ }
1476
+
1477
+ static VALUE validate_parser = Qundef;
1478
+
1479
+ /* Document-method: validate
1480
+ * call-seq: validate
1481
+ *
1482
+ * Returns the default validate parser.
1483
+ */
1484
+ static VALUE parser_validate(VALUE self) {
1485
+ if (Qundef == validate_parser) {
1486
+ ojParser p = ALLOC(struct _ojParser);
1487
+
1488
+ memset(p, 0, sizeof(struct _ojParser));
1489
+ buf_init(&p->key);
1490
+ buf_init(&p->buf);
1491
+ p->map = value_map;
1492
+ oj_set_parser_validator(p);
1493
+ validate_parser = Data_Wrap_Struct(parser_class, parser_mark, parser_free, p);
1494
+ rb_gc_register_address(&validate_parser);
1495
+ }
1496
+ return validate_parser;
1497
+ }
1498
+
1499
+ /* Document-class: Oj::Parser
1500
+ *
1501
+ * A reusable parser that makes use of named delegates to determine the
1502
+ * handling of parsed data. Delegates are available for validation, a callback
1503
+ * parser (SAJ), and a usual delegate that builds Ruby objects as parsing
1504
+ * proceeds.
1505
+ *
1506
+ * This parser is considerably faster than the older Oj.parse call and
1507
+ * isolates options to just the parser so that other parts of the code are not
1508
+ * forced to use the same options.
1509
+ */
1510
+ void oj_parser_init(void) {
1511
+ parser_class = rb_define_class_under(Oj, "Parser", rb_cObject);
1512
+ rb_gc_register_address(&parser_class);
1513
+ rb_undef_alloc_func(parser_class);
1514
+
1515
+ rb_define_module_function(parser_class, "new", parser_new, -1);
1516
+ rb_define_method(parser_class, "parse", parser_parse, 1);
1517
+ rb_define_method(parser_class, "load", parser_load, 1);
1518
+ rb_define_method(parser_class, "file", parser_file, 1);
1519
+ rb_define_method(parser_class, "just_one", parser_just_one, 0);
1520
+ rb_define_method(parser_class, "just_one=", parser_just_one_set, 1);
1521
+ rb_define_method(parser_class, "method_missing", parser_missing, -1);
1522
+
1523
+ rb_define_module_function(parser_class, "usual", parser_usual, 0);
1524
+ rb_define_module_function(parser_class, "saj", parser_saj, 0);
1525
+ rb_define_module_function(parser_class, "validate", parser_validate, 0);
1526
+ }