ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.87 by root, Tue Jun 3 06:43:45 2008 UTC vs.
Revision 1.107 by root, Wed Mar 17 01:45:43 2010 UTC

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
77enum { 78enum {
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 && (json)->incr_mode == INCR_M_JSON) 87#define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON)
85 88
86typedef struct { 89typedef struct {
87 U32 flags; 90 U32 flags;
88 U32 max_depth; 91 U32 max_depth;
89 STRLEN max_size; 92 STRLEN max_size;
92 HV *cb_sk_object; 95 HV *cb_sk_object;
93 96
94 // for the incremental parser 97 // for the incremental parser
95 SV *incr_text; // the source text so far 98 SV *incr_text; // the source text so far
96 STRLEN incr_pos; // the current offset into the text 99 STRLEN incr_pos; // the current offset into the text
97 unsigned char incr_nest; // {[]}-nesting level 100 int incr_nest; // {[]}-nesting level
98 unsigned char incr_mode; 101 unsigned char incr_mode;
99} JSON; 102} JSON;
100 103
101INLINE void 104INLINE void
102json_init (JSON *json) 105json_init (JSON *json)
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
184static STRLEN
185ptr_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
196static void
197json_atof_scan1 (const char *s, NV *accum, int *expo, int postdp, int maxdepth)
198{
199 UV uaccum = 0;
200 int eaccum = 0;
201
202 // if we recurse too deep, skip all remaining digits
203 // to avoid a stack overflow attack
204 if (expect_false (--maxdepth <= 0))
205 while (((U8)*s - '0') < 10)
206 ++s;
207
208 for (;;)
209 {
210 U8 dig = (U8)*s - '0';
211
212 if (expect_false (dig >= 10))
213 {
214 if (dig == (U8)((U8)'.' - (U8)'0'))
215 {
216 ++s;
217 json_atof_scan1 (s, accum, expo, 1, maxdepth);
218 }
219 else if ((dig | ' ') == 'e' - '0')
220 {
221 int exp2 = 0;
222 int neg = 0;
223
224 ++s;
225
226 if (*s == '-')
227 {
228 ++s;
229 neg = 1;
230 }
231 else if (*s == '+')
232 ++s;
233
234 while ((dig = (U8)*s - '0') < 10)
235 exp2 = exp2 * 10 + *s++ - '0';
236
237 *expo += neg ? -exp2 : exp2;
238 }
239
240 break;
241 }
242
243 ++s;
244
245 uaccum = uaccum * 10 + dig;
246 ++eaccum;
247
248 // if we have too many digits, then recurse for more
249 // we actually do this for rather few digits
250 if (uaccum >= (UV_MAX - 9) / 10)
251 {
252 if (postdp) *expo -= eaccum;
253 json_atof_scan1 (s, accum, expo, postdp, maxdepth);
254 if (postdp) *expo += eaccum;
255
256 break;
257 }
258 }
259
260 // this relies greatly on the quality of the pow ()
261 // implementation of the platform, but a good
262 // implementation is hard to beat.
263 if (postdp) *expo -= eaccum;
264 *accum += uaccum * Perl_pow (10., *expo);
265 *expo += eaccum;
266}
267
268static NV
269json_atof (const char *s)
270{
271 NV accum = 0.;
272 int expo = 0;
273 int neg = 0;
274
275 if (*s == '-')
276 {
277 ++s;
278 neg = 1;
279 }
280
281 // a recursion depth of ten gives us >>500 bits
282 json_atof_scan1 (s, &accum, &expo, 0, 10);
283
284 return neg ? -accum : accum;
285}
180///////////////////////////////////////////////////////////////////////////// 286/////////////////////////////////////////////////////////////////////////////
181// encoder 287// encoder
182 288
183// structure used for encoding JSON 289// structure used for encoding JSON
184typedef struct 290typedef struct
194INLINE void 300INLINE void
195need (enc_t *enc, STRLEN len) 301need (enc_t *enc, STRLEN len)
196{ 302{
197 if (expect_false (enc->cur + len >= enc->end)) 303 if (expect_false (enc->cur + len >= enc->end))
198 { 304 {
199 STRLEN cur = enc->cur - SvPVX (enc->sv); 305 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 306 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 307 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 308 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 309 }
204} 310}
205 311
280 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 386 (int)((uch - 0x10000) % 0x400 + 0xDC00));
281 enc->cur += 12; 387 enc->cur += 12;
282 } 388 }
283 else 389 else
284 { 390 {
285 static char hexdigit [16] = "0123456789abcdef";
286 need (enc, len += 5); 391 need (enc, len += 5);
287 *enc->cur++ = '\\'; 392 *enc->cur++ = '\\';
288 *enc->cur++ = 'u'; 393 *enc->cur++ = 'u';
289 *enc->cur++ = hexdigit [ uch >> 12 ]; 394 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
290 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 395 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
291 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 396 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
292 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 397 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
293 } 398 }
294 399
295 str += clen; 400 str += clen;
296 } 401 }
297 else if (enc->json.flags & F_LATIN1) 402 else if (enc->json.flags & F_LATIN1)
461 566
462 encode_ch (enc, '{'); 567 encode_ch (enc, '{');
463 568
464 // for canonical output we have to sort by keys first 569 // for canonical output we have to sort by keys first
465 // actually, this is mostly due to the stupid so-called 570 // actually, this is mostly due to the stupid so-called
466 // security workaround added somewhere in 5.8.x. 571 // security workaround added somewhere in 5.8.x
467 // that randomises hash orderings 572 // that randomises hash orderings
468 if (enc->json.flags & F_CANONICAL) 573 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
469 { 574 {
470 int count = hv_iterinit (hv); 575 int count = hv_iterinit (hv);
471 576
472 if (SvMAGICAL (hv)) 577 if (SvMAGICAL (hv))
473 { 578 {
751 : enc.json.flags & F_LATIN1 ? 0x000100UL 856 : enc.json.flags & F_LATIN1 ? 0x000100UL
752 : 0x110000UL; 857 : 0x110000UL;
753 858
754 SvPOK_only (enc.sv); 859 SvPOK_only (enc.sv);
755 encode_sv (&enc, scalar); 860 encode_sv (&enc, scalar);
861 encode_nl (&enc);
756 862
757 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 863 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
758 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 864 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
759 865
760 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 866 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
940 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1046 else if (expect_true (ch >= 0x20 && ch < 0x80))
941 *cur++ = ch; 1047 *cur++ = ch;
942 else if (ch >= 0x80) 1048 else if (ch >= 0x80)
943 { 1049 {
944 STRLEN clen; 1050 STRLEN clen;
945 UV uch;
946 1051
947 --dec_cur; 1052 --dec_cur;
948 1053
949 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1054 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
950 if (clen == (STRLEN)-1) 1055 if (clen == (STRLEN)-1)
951 ERR ("malformed UTF-8 character in JSON string"); 1056 ERR ("malformed UTF-8 character in JSON string");
952 1057
953 do 1058 do
954 *cur++ = *dec_cur++; 1059 *cur++ = *dec_cur++;
971 { 1076 {
972 STRLEN len = cur - buf; 1077 STRLEN len = cur - buf;
973 1078
974 if (sv) 1079 if (sv)
975 { 1080 {
976 SvGROW (sv, SvCUR (sv) + len + 1); 1081 STRLEN cur = SvCUR (sv);
1082
1083 if (SvLEN (sv) <= cur + len)
1084 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1085
977 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 1086 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
978 SvCUR_set (sv, SvCUR (sv) + len); 1087 SvCUR_set (sv, SvCUR (sv) + len);
979 } 1088 }
980 else 1089 else
981 sv = newSVpvn (buf, len); 1090 sv = newSVpvn (buf, len);
1072 1181
1073 // special case the rather common 1..5-digit-int case 1182 // special case the rather common 1..5-digit-int case
1074 if (*start == '-') 1183 if (*start == '-')
1075 switch (len) 1184 switch (len)
1076 { 1185 {
1077 case 2: return newSViv (-( start [1] - '0' * 1)); 1186 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1078 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1187 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)); 1188 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)); 1189 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)); 1190 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1082 } 1191 }
1083 else 1192 else
1084 switch (len) 1193 switch (len)
1085 { 1194 {
1086 case 1: return newSViv ( start [0] - '0' * 1); 1195 case 1: return newSViv ( start [0] - '0' * 1);
1087 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1196 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); 1197 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); 1198 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); 1199 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1091 } 1200 }
1092 1201
1093 { 1202 {
1094 UV uv; 1203 UV uv;
1095 int numtype = grok_number (start, len, &uv); 1204 int numtype = grok_number (start, len, &uv);
1104 } 1213 }
1105 1214
1106 len -= *start == '-' ? 1 : 0; 1215 len -= *start == '-' ? 1 : 0;
1107 1216
1108 // does not fit into IV or UV, try NV 1217 // does not fit into IV or UV, try NV
1109 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1218 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 1219 // fits into NV without loss of precision
1115 return newSVnv (Atof (start)); 1220 return newSVnv (json_atof (start));
1116 1221
1117 // everything else fails, convert it to a string 1222 // everything else fails, convert it to a string
1118 return newSVpvn (start, dec->cur - start); 1223 return newSVpvn (start, dec->cur - start);
1119 } 1224 }
1120 1225
1121 // loss of precision here 1226 // loss of precision here
1122 return newSVnv (Atof (start)); 1227 return newSVnv (json_atof (start));
1123 1228
1124fail: 1229fail:
1125 return 0; 1230 return 0;
1126} 1231}
1127 1232
1296 dSP; 1401 dSP;
1297 int count; 1402 int count;
1298 1403
1299 ENTER; SAVETMPS; PUSHMARK (SP); 1404 ENTER; SAVETMPS; PUSHMARK (SP);
1300 XPUSHs (HeVAL (he)); 1405 XPUSHs (HeVAL (he));
1406 sv_2mortal (sv);
1301 1407
1302 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN; 1408 PUTBACK; count = call_sv (HeVAL (cb), G_ARRAY); SPAGAIN;
1303 1409
1304 if (count == 1) 1410 if (count == 1)
1305 { 1411 {
1306 sv = newSVsv (POPs); 1412 sv = newSVsv (POPs);
1307 FREETMPS; LEAVE; 1413 FREETMPS; LEAVE;
1308 return sv; 1414 return sv;
1309 } 1415 }
1310 1416
1417 SvREFCNT_inc (sv);
1311 FREETMPS; LEAVE; 1418 FREETMPS; LEAVE;
1312 } 1419 }
1313 } 1420 }
1314 1421
1315 if (dec->json.cb_object) 1422 if (dec->json.cb_object)
1405fail: 1512fail:
1406 return 0; 1513 return 0;
1407} 1514}
1408 1515
1409static SV * 1516static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1517decode_json (SV *string, JSON *json, char **offset_return)
1411{ 1518{
1412 dec_t dec; 1519 dec_t dec;
1413 STRLEN offset;
1414 SV *sv; 1520 SV *sv;
1415 1521
1522 /* work around bugs in 5.10 where manipulating magic values
1523 * will perl ignore the magic in subsequent accesses
1524 */
1416 SvGETMAGIC (string); 1525 /*SvGETMAGIC (string);*/
1526 if (SvMAGICAL (string))
1527 string = sv_2mortal (newSVsv (string));
1528
1417 SvUPGRADE (string, SVt_PV); 1529 SvUPGRADE (string, SVt_PV);
1418 1530
1419 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1531 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1420 * assertion with -DDEBUGGING, although SvCUR is documented to 1532 * assertion with -DDEBUGGING, although SvCUR is documented to
1421 * return the xpv_cur field which certainly exists after upgrading. 1533 * return the xpv_cur field which certainly exists after upgrading.
1422 * according to nicholas clark, calling SvPOK fixes this. 1534 * according to nicholas clark, calling SvPOK fixes this.
1535 * But it doesn't fix it, so try another workaround, call SvPV_nolen
1536 * and hope for the best.
1537 * Damnit, SvPV_nolen still trips over yet another assertion. This
1538 * assertion business is seriously broken, try yet another workaround
1539 * for the broken -DDEBUGGING.
1423 */ 1540 */
1424 SvPOK (string); 1541 {
1542#ifdef DEBUGGING
1543 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1544#else
1545 STRLEN offset = SvCUR (string);
1546#endif
1425 1547
1426 if (SvCUR (string) > json->max_size && json->max_size) 1548 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", 1549 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); 1550 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1551 }
1429 1552
1430 if (json->flags & F_UTF8) 1553 if (json->flags & F_UTF8)
1431 sv_utf8_downgrade (string, 0); 1554 sv_utf8_downgrade (string, 0);
1432 else 1555 else
1433 sv_utf8_upgrade (string); 1556 sv_utf8_upgrade (string);
1445 1568
1446 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1569 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1447 1570
1448 decode_ws (&dec); 1571 decode_ws (&dec);
1449 sv = decode_sv (&dec); 1572 sv = decode_sv (&dec);
1573
1574 if (offset_return)
1575 *offset_return = dec.cur;
1450 1576
1451 if (!(offset_return || !sv)) 1577 if (!(offset_return || !sv))
1452 { 1578 {
1453 // check for trailing garbage 1579 // check for trailing garbage
1454 decode_ws (&dec); 1580 decode_ws (&dec);
1457 { 1583 {
1458 dec.err = "garbage after JSON object"; 1584 dec.err = "garbage after JSON object";
1459 SvREFCNT_dec (sv); 1585 SvREFCNT_dec (sv);
1460 sv = 0; 1586 sv = 0;
1461 } 1587 }
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 } 1588 }
1473 1589
1474 if (!sv) 1590 if (!sv)
1475 { 1591 {
1476 SV *uni = sv_newmortal (); 1592 SV *uni = sv_newmortal ();
1482 SAVEVPTR (PL_curcop); 1598 SAVEVPTR (PL_curcop);
1483 PL_curcop = &cop; 1599 PL_curcop = &cop;
1484 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1600 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1485 LEAVE; 1601 LEAVE;
1486 1602
1487 croak ("%s, at character offset %d [\"%s\"]", 1603 croak ("%s, at character offset %d (before \"%s\")",
1488 dec.err, 1604 dec.err,
1489 (int)offset, 1605 ptr_to_index (string, dec.cur),
1490 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1606 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1491 } 1607 }
1492 1608
1493 sv = sv_2mortal (sv); 1609 sv = sv_2mortal (sv);
1494 1610
1503 1619
1504static void 1620static void
1505incr_parse (JSON *self) 1621incr_parse (JSON *self)
1506{ 1622{
1507 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1623 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1624
1625 // the state machine here is a bit convoluted and could be simplified a lot
1626 // but this would make it slower, so...
1508 1627
1509 for (;;) 1628 for (;;)
1510 { 1629 {
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 1630 //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) 1631 switch (self->incr_mode)
1513 { 1632 {
1514 // only used for intiial whitespace skipping 1633 // only used for initial whitespace skipping
1515 case INCR_M_WS: 1634 case INCR_M_WS:
1516 for (;;) 1635 for (;;)
1517 { 1636 {
1518 if (*p > 0x20) 1637 if (*p > 0x20)
1519 { 1638 {
1639 if (*p == '#')
1640 {
1641 self->incr_mode = INCR_M_C0;
1642 goto incr_m_c;
1643 }
1644 else
1645 {
1520 self->incr_mode = INCR_M_JSON; 1646 self->incr_mode = INCR_M_JSON;
1521 goto incr_m_json; 1647 goto incr_m_json;
1648 }
1522 } 1649 }
1523 else if (!*p) 1650 else if (!*p)
1524 goto interrupt; 1651 goto interrupt;
1525 1652
1526 ++p; 1653 ++p;
1532 goto interrupt; 1659 goto interrupt;
1533 1660
1534 ++p; 1661 ++p;
1535 self->incr_mode = INCR_M_STR; 1662 self->incr_mode = INCR_M_STR;
1536 goto incr_m_str; 1663 goto incr_m_str;
1664
1665 // inside #-style comments
1666 case INCR_M_C0:
1667 case INCR_M_C1:
1668 incr_m_c:
1669 for (;;)
1670 {
1671 if (*p == '\n')
1672 {
1673 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1674 break;
1675 }
1676 else if (!*p)
1677 goto interrupt;
1678
1679 ++p;
1680 }
1681
1682 break;
1537 1683
1538 // inside a string 1684 // inside a string
1539 case INCR_M_STR: 1685 case INCR_M_STR:
1540 incr_m_str: 1686 incr_m_str:
1541 for (;;) 1687 for (;;)
1598 croak (ERR_NESTING_EXCEEDED); 1744 croak (ERR_NESTING_EXCEEDED);
1599 break; 1745 break;
1600 1746
1601 case ']': 1747 case ']':
1602 case '}': 1748 case '}':
1603 if (!--self->incr_nest) 1749 if (--self->incr_nest <= 0)
1604 goto interrupt; 1750 goto interrupt;
1751 break;
1752
1753 case '#':
1754 self->incr_mode = INCR_M_C1;
1755 goto incr_m_c;
1605 } 1756 }
1606 } 1757 }
1607 } 1758 }
1608 1759
1609 modechange: 1760 modechange:
1610 ; 1761 ;
1611 } 1762 }
1612 1763
1613interrupt: 1764interrupt:
1614 self->incr_pos = p - SvPVX (self->incr_text); 1765 self->incr_pos = p - SvPVX (self->incr_text);
1766 //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 1767 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1616} 1768}
1617 1769
1618///////////////////////////////////////////////////////////////////////////// 1770/////////////////////////////////////////////////////////////////////////////
1619// XS interface functions 1771// XS interface functions
1634 json_stash = gv_stashpv ("JSON::XS" , 1); 1786 json_stash = gv_stashpv ("JSON::XS" , 1);
1635 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1787 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1636 1788
1637 json_true = get_bool ("JSON::XS::true"); 1789 json_true = get_bool ("JSON::XS::true");
1638 json_false = get_bool ("JSON::XS::false"); 1790 json_false = get_bool ("JSON::XS::false");
1791
1792 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1639} 1793}
1640 1794
1641PROTOTYPES: DISABLE 1795PROTOTYPES: DISABLE
1642 1796
1643void CLONE (...) 1797void CLONE (...)
1763 XPUSHs (decode_json (jsonstr, self, 0)); 1917 XPUSHs (decode_json (jsonstr, self, 0));
1764 1918
1765void decode_prefix (JSON *self, SV *jsonstr) 1919void decode_prefix (JSON *self, SV *jsonstr)
1766 PPCODE: 1920 PPCODE:
1767{ 1921{
1768 STRLEN offset; 1922 char *offset;
1769 EXTEND (SP, 2); 1923 EXTEND (SP, 2);
1770 PUSHs (decode_json (jsonstr, self, &offset)); 1924 PUSHs (decode_json (jsonstr, self, &offset));
1771 PUSHs (sv_2mortal (newSVuv (offset))); 1925 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1772} 1926}
1773 1927
1774void incr_parse (JSON *self, SV *jsonstr = 0) 1928void incr_parse (JSON *self, SV *jsonstr = 0)
1775 PPCODE: 1929 PPCODE:
1776{ 1930{
1778 self->incr_text = newSVpvn ("", 0); 1932 self->incr_text = newSVpvn ("", 0);
1779 1933
1780 // append data, if any 1934 // append data, if any
1781 if (jsonstr) 1935 if (jsonstr)
1782 { 1936 {
1783 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1937 if (SvUTF8 (jsonstr))
1784 { 1938 {
1939 if (!SvUTF8 (self->incr_text))
1940 {
1785 /* utf-8-ness differs, need to upgrade */ 1941 /* utf-8-ness differs, need to upgrade */
1786 sv_utf8_upgrade (self->incr_text); 1942 sv_utf8_upgrade (self->incr_text);
1787 1943
1788 if (self->incr_pos) 1944 if (self->incr_pos)
1789 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1945 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1790 - (U8 *)SvPVX (self->incr_text); 1946 - (U8 *)SvPVX (self->incr_text);
1947 }
1791 } 1948 }
1949 else if (SvUTF8 (self->incr_text))
1950 sv_utf8_upgrade (jsonstr);
1792 1951
1793 { 1952 {
1794 STRLEN len; 1953 STRLEN len;
1795 const char *str = SvPV (jsonstr, len); 1954 const char *str = SvPV (jsonstr, len);
1796 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1955 STRLEN cur = SvCUR (self->incr_text);
1956
1957 if (SvLEN (self->incr_text) <= cur + len)
1958 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1959
1797 Move (str, SvEND (self->incr_text), len, char); 1960 Move (str, SvEND (self->incr_text), len, char);
1798 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1961 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 1962 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1800 } 1963 }
1801 } 1964 }
1802 1965
1803 if (GIMME_V != G_VOID) 1966 if (GIMME_V != G_VOID)
1804 do 1967 do
1805 { 1968 {
1806 STRLEN offset; 1969 char *offset;
1807 1970
1808 if (!INCR_DONE (self)) 1971 if (!INCR_DONE (self))
1809 { 1972 {
1810 incr_parse (self); 1973 incr_parse (self);
1811 1974
1817 break; 1980 break;
1818 } 1981 }
1819 1982
1820 XPUSHs (decode_json (self->incr_text, self, &offset)); 1983 XPUSHs (decode_json (self->incr_text, self, &offset));
1821 1984
1822 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1823 self->incr_pos -= offset; 1985 self->incr_pos -= offset - SvPVX (self->incr_text);
1824 self->incr_nest = 0; 1986 self->incr_nest = 0;
1825 self->incr_mode = 0; 1987 self->incr_mode = 0;
1988
1989 sv_chop (self->incr_text, offset);
1826 } 1990 }
1827 while (GIMME_V == G_ARRAY); 1991 while (GIMME_V == G_ARRAY);
1828} 1992}
1829 1993
1830SV *incr_text (JSON *self) 1994SV *incr_text (JSON *self)
1891 json_init (&json); 2055 json_init (&json);
1892 json.flags |= ix; 2056 json.flags |= ix;
1893 XPUSHs (decode_json (jsonstr, &json, 0)); 2057 XPUSHs (decode_json (jsonstr, &json, 0));
1894} 2058}
1895 2059
1896

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines