… | |
… | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
14 | #endif |
15 | |
15 | |
16 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
20 | #endif |
20 | #endif |
21 | |
21 | |
|
|
22 | // three extra for rounding, sign, and end of string |
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
23 | |
24 | |
24 | #define F_ASCII 0x00000001UL |
25 | #define F_ASCII 0x00000001UL |
25 | #define F_LATIN1 0x00000002UL |
26 | #define F_LATIN1 0x00000002UL |
26 | #define F_UTF8 0x00000004UL |
27 | #define F_UTF8 0x00000004UL |
27 | #define F_INDENT 0x00000008UL |
28 | #define F_INDENT 0x00000008UL |
… | |
… | |
76 | |
77 | |
77 | enum { |
78 | enum { |
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_STR, // inside string |
80 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
81 | INCR_M_BS, // inside backslash |
|
|
82 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
83 | INCR_M_C1, // inside comment in other places |
81 | INCR_M_JSON // outside anything, count nesting |
84 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
85 | }; |
83 | |
86 | |
84 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
87 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
88 | |
… | |
… | |
175 | *s++ = 0x80 | ( ch & 0x3f); |
178 | *s++ = 0x80 | ( ch & 0x3f); |
176 | |
179 | |
177 | return s; |
180 | return s; |
178 | } |
181 | } |
179 | |
182 | |
|
|
183 | // convert offset pointer to character index, sv must be string |
|
|
184 | static STRLEN |
|
|
185 | ptr_to_index (SV *sv, char *offset) |
|
|
186 | { |
|
|
187 | return SvUTF8 (sv) |
|
|
188 | ? utf8_distance (offset, SvPVX (sv)) |
|
|
189 | : offset - SvPVX (sv); |
|
|
190 | } |
|
|
191 | |
|
|
192 | ///////////////////////////////////////////////////////////////////////////// |
|
|
193 | // fp hell |
|
|
194 | |
|
|
195 | // scan a group of digits, and a trailing exponent |
|
|
196 | static void |
|
|
197 | json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp) |
|
|
198 | { |
|
|
199 | UV uaccum = 0; |
|
|
200 | int eaccum = 0; |
|
|
201 | |
|
|
202 | for (;;) |
|
|
203 | { |
|
|
204 | U8 dig = (U8)*s - '0'; |
|
|
205 | |
|
|
206 | if (expect_false (dig >= 10)) |
|
|
207 | { |
|
|
208 | if (dig == (U8)((U8)'.' - (U8)'0')) |
|
|
209 | { |
|
|
210 | ++s; |
|
|
211 | json_atof_scan1 (s, accum, expo, 1); |
|
|
212 | } |
|
|
213 | else if ((dig | ' ') == 'e' - '0') |
|
|
214 | { |
|
|
215 | int exp2 = 0; |
|
|
216 | int neg = 0; |
|
|
217 | |
|
|
218 | ++s; |
|
|
219 | |
|
|
220 | if (*s == '-') |
|
|
221 | { |
|
|
222 | ++s; |
|
|
223 | neg = 1; |
|
|
224 | } |
|
|
225 | else if (*s == '+') |
|
|
226 | ++s; |
|
|
227 | |
|
|
228 | while ((dig = (U8)*s - '0') < 10) |
|
|
229 | exp2 = exp2 * 10 + *s++ - '0'; |
|
|
230 | |
|
|
231 | *expo += neg ? -exp2 : exp2; |
|
|
232 | } |
|
|
233 | |
|
|
234 | break; |
|
|
235 | } |
|
|
236 | |
|
|
237 | ++s; |
|
|
238 | |
|
|
239 | uaccum = uaccum * 10 + dig; |
|
|
240 | ++eaccum; |
|
|
241 | |
|
|
242 | // if we have too many digits, then recurse for more |
|
|
243 | // we actually do this for rather few digits |
|
|
244 | if (uaccum >= (UV_MAX - 9) / 10) |
|
|
245 | { |
|
|
246 | if (postdp) *expo -= eaccum; |
|
|
247 | json_atof_scan1 (s, accum, expo, postdp); |
|
|
248 | if (postdp) *expo += eaccum; |
|
|
249 | |
|
|
250 | break; |
|
|
251 | } |
|
|
252 | } |
|
|
253 | |
|
|
254 | // this relies greatly on the quality of the pow () |
|
|
255 | // implementation of the platform, but a good |
|
|
256 | // implementation is hard to beat. |
|
|
257 | if (postdp) *expo -= eaccum; |
|
|
258 | *accum += uaccum * Perl_pow (10., *expo); |
|
|
259 | *expo += eaccum; |
|
|
260 | } |
|
|
261 | |
|
|
262 | static NV |
|
|
263 | json_atof (const char *s) |
|
|
264 | { |
|
|
265 | NV accum = 0.; |
|
|
266 | int expo = 0; |
|
|
267 | int neg = 0; |
|
|
268 | |
|
|
269 | if (*s == '-') |
|
|
270 | { |
|
|
271 | ++s; |
|
|
272 | neg = 1; |
|
|
273 | } |
|
|
274 | |
|
|
275 | json_atof_scan1 (s, &accum, &expo, 0); |
|
|
276 | |
|
|
277 | return neg ? -accum : accum; |
|
|
278 | } |
180 | ///////////////////////////////////////////////////////////////////////////// |
279 | ///////////////////////////////////////////////////////////////////////////// |
181 | // encoder |
280 | // encoder |
182 | |
281 | |
183 | // structure used for encoding JSON |
282 | // structure used for encoding JSON |
184 | typedef struct |
283 | typedef struct |
… | |
… | |
194 | INLINE void |
293 | INLINE void |
195 | need (enc_t *enc, STRLEN len) |
294 | need (enc_t *enc, STRLEN len) |
196 | { |
295 | { |
197 | if (expect_false (enc->cur + len >= enc->end)) |
296 | if (expect_false (enc->cur + len >= enc->end)) |
198 | { |
297 | { |
199 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
298 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
200 | SvGROW (enc->sv, cur + len + 1); |
299 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
201 | enc->cur = SvPVX (enc->sv) + cur; |
300 | enc->cur = SvPVX (enc->sv) + cur; |
202 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
301 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
203 | } |
302 | } |
204 | } |
303 | } |
205 | |
304 | |
… | |
… | |
280 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
379 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
281 | enc->cur += 12; |
380 | enc->cur += 12; |
282 | } |
381 | } |
283 | else |
382 | else |
284 | { |
383 | { |
285 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
286 | need (enc, len += 5); |
384 | need (enc, len += 5); |
287 | *enc->cur++ = '\\'; |
385 | *enc->cur++ = '\\'; |
288 | *enc->cur++ = 'u'; |
386 | *enc->cur++ = 'u'; |
289 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
387 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
290 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
388 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
291 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
389 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
292 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
390 | *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
293 | } |
391 | } |
294 | |
392 | |
295 | str += clen; |
393 | str += clen; |
296 | } |
394 | } |
297 | else if (enc->json.flags & F_LATIN1) |
395 | else if (enc->json.flags & F_LATIN1) |
… | |
… | |
461 | |
559 | |
462 | encode_ch (enc, '{'); |
560 | encode_ch (enc, '{'); |
463 | |
561 | |
464 | // for canonical output we have to sort by keys first |
562 | // for canonical output we have to sort by keys first |
465 | // actually, this is mostly due to the stupid so-called |
563 | // actually, this is mostly due to the stupid so-called |
466 | // security workaround added somewhere in 5.8.x. |
564 | // security workaround added somewhere in 5.8.x |
467 | // that randomises hash orderings |
565 | // that randomises hash orderings |
468 | if (enc->json.flags & F_CANONICAL) |
566 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
469 | { |
567 | { |
470 | int count = hv_iterinit (hv); |
568 | int count = hv_iterinit (hv); |
471 | |
569 | |
472 | if (SvMAGICAL (hv)) |
570 | if (SvMAGICAL (hv)) |
473 | { |
571 | { |
… | |
… | |
751 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
849 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
752 | : 0x110000UL; |
850 | : 0x110000UL; |
753 | |
851 | |
754 | SvPOK_only (enc.sv); |
852 | SvPOK_only (enc.sv); |
755 | encode_sv (&enc, scalar); |
853 | encode_sv (&enc, scalar); |
|
|
854 | encode_nl (&enc); |
756 | |
855 | |
757 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
856 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
758 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
857 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
759 | |
858 | |
760 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
859 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
940 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1039 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
941 | *cur++ = ch; |
1040 | *cur++ = ch; |
942 | else if (ch >= 0x80) |
1041 | else if (ch >= 0x80) |
943 | { |
1042 | { |
944 | STRLEN clen; |
1043 | STRLEN clen; |
945 | UV uch; |
|
|
946 | |
1044 | |
947 | --dec_cur; |
1045 | --dec_cur; |
948 | |
1046 | |
949 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1047 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
950 | if (clen == (STRLEN)-1) |
1048 | if (clen == (STRLEN)-1) |
951 | ERR ("malformed UTF-8 character in JSON string"); |
1049 | ERR ("malformed UTF-8 character in JSON string"); |
952 | |
1050 | |
953 | do |
1051 | do |
954 | *cur++ = *dec_cur++; |
1052 | *cur++ = *dec_cur++; |
… | |
… | |
971 | { |
1069 | { |
972 | STRLEN len = cur - buf; |
1070 | STRLEN len = cur - buf; |
973 | |
1071 | |
974 | if (sv) |
1072 | if (sv) |
975 | { |
1073 | { |
976 | SvGROW (sv, SvCUR (sv) + len + 1); |
1074 | STRLEN cur = SvCUR (sv); |
|
|
1075 | |
|
|
1076 | if (SvLEN (sv) <= cur + len) |
|
|
1077 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1078 | |
977 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
1079 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
978 | SvCUR_set (sv, SvCUR (sv) + len); |
1080 | SvCUR_set (sv, SvCUR (sv) + len); |
979 | } |
1081 | } |
980 | else |
1082 | else |
981 | sv = newSVpvn (buf, len); |
1083 | sv = newSVpvn (buf, len); |
… | |
… | |
1072 | |
1174 | |
1073 | // special case the rather common 1..5-digit-int case |
1175 | // special case the rather common 1..5-digit-int case |
1074 | if (*start == '-') |
1176 | if (*start == '-') |
1075 | switch (len) |
1177 | switch (len) |
1076 | { |
1178 | { |
1077 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1179 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1078 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1180 | case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
1079 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1181 | case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1080 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1182 | case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1081 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1183 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1082 | } |
1184 | } |
1083 | else |
1185 | else |
1084 | switch (len) |
1186 | switch (len) |
1085 | { |
1187 | { |
1086 | case 1: return newSViv ( start [0] - '0' * 1); |
1188 | case 1: return newSViv ( start [0] - '0' * 1); |
1087 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1189 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1088 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1190 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1089 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1191 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1090 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1192 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1091 | } |
1193 | } |
1092 | |
1194 | |
1093 | { |
1195 | { |
1094 | UV uv; |
1196 | UV uv; |
1095 | int numtype = grok_number (start, len, &uv); |
1197 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1104 | } |
1206 | } |
1105 | |
1207 | |
1106 | len -= *start == '-' ? 1 : 0; |
1208 | len -= *start == '-' ? 1 : 0; |
1107 | |
1209 | |
1108 | // does not fit into IV or UV, try NV |
1210 | // does not fit into IV or UV, try NV |
1109 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1211 | if (len <= NV_DIG) |
1110 | #if defined (LDBL_DIG) |
|
|
1111 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1112 | #endif |
|
|
1113 | ) |
|
|
1114 | // fits into NV without loss of precision |
1212 | // fits into NV without loss of precision |
1115 | return newSVnv (Atof (start)); |
1213 | return newSVnv (json_atof (start)); |
1116 | |
1214 | |
1117 | // everything else fails, convert it to a string |
1215 | // everything else fails, convert it to a string |
1118 | return newSVpvn (start, dec->cur - start); |
1216 | return newSVpvn (start, dec->cur - start); |
1119 | } |
1217 | } |
1120 | |
1218 | |
1121 | // loss of precision here |
1219 | // loss of precision here |
1122 | return newSVnv (Atof (start)); |
1220 | return newSVnv (json_atof (start)); |
1123 | |
1221 | |
1124 | fail: |
1222 | fail: |
1125 | return 0; |
1223 | return 0; |
1126 | } |
1224 | } |
1127 | |
1225 | |
… | |
… | |
1405 | fail: |
1503 | fail: |
1406 | return 0; |
1504 | return 0; |
1407 | } |
1505 | } |
1408 | |
1506 | |
1409 | static SV * |
1507 | static SV * |
1410 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1508 | decode_json (SV *string, JSON *json, char **offset_return) |
1411 | { |
1509 | { |
1412 | dec_t dec; |
1510 | dec_t dec; |
1413 | STRLEN offset; |
|
|
1414 | SV *sv; |
1511 | SV *sv; |
1415 | |
1512 | |
|
|
1513 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1514 | * will perl ignore the magic in subsequent accesses |
|
|
1515 | */ |
1416 | SvGETMAGIC (string); |
1516 | /*SvGETMAGIC (string);*/ |
|
|
1517 | if (SvMAGICAL (string)) |
|
|
1518 | string = sv_2mortal (newSVsv (string)); |
|
|
1519 | |
1417 | SvUPGRADE (string, SVt_PV); |
1520 | SvUPGRADE (string, SVt_PV); |
1418 | |
1521 | |
1419 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1522 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1420 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1523 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1421 | * return the xpv_cur field which certainly exists after upgrading. |
1524 | * return the xpv_cur field which certainly exists after upgrading. |
1422 | * according to nicholas clark, calling SvPOK fixes this. |
1525 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1526 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1527 | * and hope for the best. |
|
|
1528 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
1529 | * assertion business is seriously broken, try yet another workaround |
|
|
1530 | * for the broken -DDEBUGGING. |
1423 | */ |
1531 | */ |
1424 | SvPOK (string); |
1532 | { |
|
|
1533 | #ifdef DEBUGGING |
|
|
1534 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
1535 | #else |
|
|
1536 | STRLEN offset = SvCUR (string); |
|
|
1537 | #endif |
1425 | |
1538 | |
1426 | if (SvCUR (string) > json->max_size && json->max_size) |
1539 | if (offset > json->max_size && json->max_size) |
1427 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1540 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1428 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1541 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1542 | } |
1429 | |
1543 | |
1430 | if (json->flags & F_UTF8) |
1544 | if (json->flags & F_UTF8) |
1431 | sv_utf8_downgrade (string, 0); |
1545 | sv_utf8_downgrade (string, 0); |
1432 | else |
1546 | else |
1433 | sv_utf8_upgrade (string); |
1547 | sv_utf8_upgrade (string); |
… | |
… | |
1445 | |
1559 | |
1446 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1560 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1447 | |
1561 | |
1448 | decode_ws (&dec); |
1562 | decode_ws (&dec); |
1449 | sv = decode_sv (&dec); |
1563 | sv = decode_sv (&dec); |
|
|
1564 | |
|
|
1565 | if (offset_return) |
|
|
1566 | *offset_return = dec.cur; |
1450 | |
1567 | |
1451 | if (!(offset_return || !sv)) |
1568 | if (!(offset_return || !sv)) |
1452 | { |
1569 | { |
1453 | // check for trailing garbage |
1570 | // check for trailing garbage |
1454 | decode_ws (&dec); |
1571 | decode_ws (&dec); |
… | |
… | |
1457 | { |
1574 | { |
1458 | dec.err = "garbage after JSON object"; |
1575 | dec.err = "garbage after JSON object"; |
1459 | SvREFCNT_dec (sv); |
1576 | SvREFCNT_dec (sv); |
1460 | sv = 0; |
1577 | sv = 0; |
1461 | } |
1578 | } |
1462 | } |
|
|
1463 | |
|
|
1464 | if (offset_return || !sv) |
|
|
1465 | { |
|
|
1466 | offset = dec.json.flags & F_UTF8 |
|
|
1467 | ? dec.cur - SvPVX (string) |
|
|
1468 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1469 | |
|
|
1470 | if (offset_return) |
|
|
1471 | *offset_return = offset; |
|
|
1472 | } |
1579 | } |
1473 | |
1580 | |
1474 | if (!sv) |
1581 | if (!sv) |
1475 | { |
1582 | { |
1476 | SV *uni = sv_newmortal (); |
1583 | SV *uni = sv_newmortal (); |
… | |
… | |
1482 | SAVEVPTR (PL_curcop); |
1589 | SAVEVPTR (PL_curcop); |
1483 | PL_curcop = &cop; |
1590 | PL_curcop = &cop; |
1484 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1591 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1485 | LEAVE; |
1592 | LEAVE; |
1486 | |
1593 | |
1487 | croak ("%s, at character offset %d [\"%s\"]", |
1594 | croak ("%s, at character offset %d (before \"%s\")", |
1488 | dec.err, |
1595 | dec.err, |
1489 | (int)offset, |
1596 | ptr_to_index (string, dec.cur), |
1490 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1597 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1491 | } |
1598 | } |
1492 | |
1599 | |
1493 | sv = sv_2mortal (sv); |
1600 | sv = sv_2mortal (sv); |
1494 | |
1601 | |
… | |
… | |
1503 | |
1610 | |
1504 | static void |
1611 | static void |
1505 | incr_parse (JSON *self) |
1612 | incr_parse (JSON *self) |
1506 | { |
1613 | { |
1507 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1614 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1615 | |
|
|
1616 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1617 | // but this would make it slower, so... |
1508 | |
1618 | |
1509 | for (;;) |
1619 | for (;;) |
1510 | { |
1620 | { |
1511 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1621 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1512 | switch (self->incr_mode) |
1622 | switch (self->incr_mode) |
1513 | { |
1623 | { |
1514 | // only used for intiial whitespace skipping |
1624 | // only used for initial whitespace skipping |
1515 | case INCR_M_WS: |
1625 | case INCR_M_WS: |
1516 | for (;;) |
1626 | for (;;) |
1517 | { |
1627 | { |
1518 | if (*p > 0x20) |
1628 | if (*p > 0x20) |
1519 | { |
1629 | { |
|
|
1630 | if (*p == '#') |
|
|
1631 | { |
|
|
1632 | self->incr_mode = INCR_M_C0; |
|
|
1633 | goto incr_m_c; |
|
|
1634 | } |
|
|
1635 | else |
|
|
1636 | { |
1520 | self->incr_mode = INCR_M_JSON; |
1637 | self->incr_mode = INCR_M_JSON; |
1521 | goto incr_m_json; |
1638 | goto incr_m_json; |
|
|
1639 | } |
1522 | } |
1640 | } |
1523 | else if (!*p) |
1641 | else if (!*p) |
1524 | goto interrupt; |
1642 | goto interrupt; |
1525 | |
1643 | |
1526 | ++p; |
1644 | ++p; |
… | |
… | |
1532 | goto interrupt; |
1650 | goto interrupt; |
1533 | |
1651 | |
1534 | ++p; |
1652 | ++p; |
1535 | self->incr_mode = INCR_M_STR; |
1653 | self->incr_mode = INCR_M_STR; |
1536 | goto incr_m_str; |
1654 | goto incr_m_str; |
|
|
1655 | |
|
|
1656 | // inside #-style comments |
|
|
1657 | case INCR_M_C0: |
|
|
1658 | case INCR_M_C1: |
|
|
1659 | incr_m_c: |
|
|
1660 | for (;;) |
|
|
1661 | { |
|
|
1662 | if (*p == '\n') |
|
|
1663 | { |
|
|
1664 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1665 | break; |
|
|
1666 | } |
|
|
1667 | else if (!*p) |
|
|
1668 | goto interrupt; |
|
|
1669 | |
|
|
1670 | ++p; |
|
|
1671 | } |
|
|
1672 | |
|
|
1673 | break; |
1537 | |
1674 | |
1538 | // inside a string |
1675 | // inside a string |
1539 | case INCR_M_STR: |
1676 | case INCR_M_STR: |
1540 | incr_m_str: |
1677 | incr_m_str: |
1541 | for (;;) |
1678 | for (;;) |
… | |
… | |
1600 | |
1737 | |
1601 | case ']': |
1738 | case ']': |
1602 | case '}': |
1739 | case '}': |
1603 | if (--self->incr_nest <= 0) |
1740 | if (--self->incr_nest <= 0) |
1604 | goto interrupt; |
1741 | goto interrupt; |
|
|
1742 | break; |
|
|
1743 | |
|
|
1744 | case '#': |
|
|
1745 | self->incr_mode = INCR_M_C1; |
|
|
1746 | goto incr_m_c; |
1605 | } |
1747 | } |
1606 | } |
1748 | } |
1607 | } |
1749 | } |
1608 | |
1750 | |
1609 | modechange: |
1751 | modechange: |
1610 | ; |
1752 | ; |
1611 | } |
1753 | } |
1612 | |
1754 | |
1613 | interrupt: |
1755 | interrupt: |
1614 | self->incr_pos = p - SvPVX (self->incr_text); |
1756 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1757 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1615 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1758 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1616 | } |
1759 | } |
1617 | |
1760 | |
1618 | ///////////////////////////////////////////////////////////////////////////// |
1761 | ///////////////////////////////////////////////////////////////////////////// |
1619 | // XS interface functions |
1762 | // XS interface functions |
… | |
… | |
1634 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1777 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1635 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1778 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1636 | |
1779 | |
1637 | json_true = get_bool ("JSON::XS::true"); |
1780 | json_true = get_bool ("JSON::XS::true"); |
1638 | json_false = get_bool ("JSON::XS::false"); |
1781 | json_false = get_bool ("JSON::XS::false"); |
|
|
1782 | |
|
|
1783 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1639 | } |
1784 | } |
1640 | |
1785 | |
1641 | PROTOTYPES: DISABLE |
1786 | PROTOTYPES: DISABLE |
1642 | |
1787 | |
1643 | void CLONE (...) |
1788 | void CLONE (...) |
… | |
… | |
1763 | XPUSHs (decode_json (jsonstr, self, 0)); |
1908 | XPUSHs (decode_json (jsonstr, self, 0)); |
1764 | |
1909 | |
1765 | void decode_prefix (JSON *self, SV *jsonstr) |
1910 | void decode_prefix (JSON *self, SV *jsonstr) |
1766 | PPCODE: |
1911 | PPCODE: |
1767 | { |
1912 | { |
1768 | STRLEN offset; |
1913 | char *offset; |
1769 | EXTEND (SP, 2); |
1914 | EXTEND (SP, 2); |
1770 | PUSHs (decode_json (jsonstr, self, &offset)); |
1915 | PUSHs (decode_json (jsonstr, self, &offset)); |
1771 | PUSHs (sv_2mortal (newSVuv (offset))); |
1916 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1772 | } |
1917 | } |
1773 | |
1918 | |
1774 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1919 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1775 | PPCODE: |
1920 | PPCODE: |
1776 | { |
1921 | { |
… | |
… | |
1778 | self->incr_text = newSVpvn ("", 0); |
1923 | self->incr_text = newSVpvn ("", 0); |
1779 | |
1924 | |
1780 | // append data, if any |
1925 | // append data, if any |
1781 | if (jsonstr) |
1926 | if (jsonstr) |
1782 | { |
1927 | { |
1783 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1928 | if (SvUTF8 (jsonstr)) |
1784 | { |
1929 | { |
|
|
1930 | if (!SvUTF8 (self->incr_text)) |
|
|
1931 | { |
1785 | /* utf-8-ness differs, need to upgrade */ |
1932 | /* utf-8-ness differs, need to upgrade */ |
1786 | sv_utf8_upgrade (self->incr_text); |
1933 | sv_utf8_upgrade (self->incr_text); |
1787 | |
1934 | |
1788 | if (self->incr_pos) |
1935 | if (self->incr_pos) |
1789 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1936 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1790 | - (U8 *)SvPVX (self->incr_text); |
1937 | - (U8 *)SvPVX (self->incr_text); |
|
|
1938 | } |
1791 | } |
1939 | } |
|
|
1940 | else if (SvUTF8 (self->incr_text)) |
|
|
1941 | sv_utf8_upgrade (jsonstr); |
1792 | |
1942 | |
1793 | { |
1943 | { |
1794 | STRLEN len; |
1944 | STRLEN len; |
1795 | const char *str = SvPV (jsonstr, len); |
1945 | const char *str = SvPV (jsonstr, len); |
1796 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
1946 | STRLEN cur = SvCUR (self->incr_text); |
|
|
1947 | |
|
|
1948 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
1949 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1950 | |
1797 | Move (str, SvEND (self->incr_text), len, char); |
1951 | Move (str, SvEND (self->incr_text), len, char); |
1798 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1952 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1799 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1953 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1800 | } |
1954 | } |
1801 | } |
1955 | } |
1802 | |
1956 | |
1803 | if (GIMME_V != G_VOID) |
1957 | if (GIMME_V != G_VOID) |
1804 | do |
1958 | do |
1805 | { |
1959 | { |
1806 | STRLEN offset; |
1960 | char *offset; |
1807 | |
1961 | |
1808 | if (!INCR_DONE (self)) |
1962 | if (!INCR_DONE (self)) |
1809 | { |
1963 | { |
1810 | incr_parse (self); |
1964 | incr_parse (self); |
1811 | |
1965 | |
… | |
… | |
1817 | break; |
1971 | break; |
1818 | } |
1972 | } |
1819 | |
1973 | |
1820 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1974 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1821 | |
1975 | |
1822 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1823 | self->incr_pos -= offset; |
1976 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1824 | self->incr_nest = 0; |
1977 | self->incr_nest = 0; |
1825 | self->incr_mode = 0; |
1978 | self->incr_mode = 0; |
|
|
1979 | |
|
|
1980 | sv_chop (self->incr_text, offset); |
1826 | } |
1981 | } |
1827 | while (GIMME_V == G_ARRAY); |
1982 | while (GIMME_V == G_ARRAY); |
1828 | } |
1983 | } |
1829 | |
1984 | |
1830 | SV *incr_text (JSON *self) |
1985 | SV *incr_text (JSON *self) |
… | |
… | |
1891 | json_init (&json); |
2046 | json_init (&json); |
1892 | json.flags |= ix; |
2047 | json.flags |= ix; |
1893 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2048 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1894 | } |
2049 | } |
1895 | |
2050 | |
1896 | |
|
|