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.89 by root, Sat Jul 19 04:21:32 2008 UTC vs.
Revision 1.105 by root, Tue Jan 19 01:07:27 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 <= 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
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)
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
262static NV
263json_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
184typedef struct 283typedef struct
194INLINE void 293INLINE void
195need (enc_t *enc, STRLEN len) 294need (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
1124fail: 1222fail:
1125 return 0; 1223 return 0;
1126} 1224}
1127 1225
1405fail: 1503fail:
1406 return 0; 1504 return 0;
1407} 1505}
1408 1506
1409static SV * 1507static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1508decode_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.
1423 * But it doesn't fix it, so try another workaround, call SvPV_nolen 1526 * But it doesn't fix it, so try another workaround, call SvPV_nolen
1424 * and hope for the best. 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.
1425 */ 1531 */
1532 {
1426#ifdef DEBUGGING 1533#ifdef DEBUGGING
1427 SvPV_nolen (string); 1534 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1535#else
1536 STRLEN offset = SvCUR (string);
1428#endif 1537#endif
1429 1538
1430 if (SvCUR (string) > json->max_size && json->max_size) 1539 if (offset > json->max_size && json->max_size)
1431 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",
1432 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1541 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1542 }
1433 1543
1434 if (json->flags & F_UTF8) 1544 if (json->flags & F_UTF8)
1435 sv_utf8_downgrade (string, 0); 1545 sv_utf8_downgrade (string, 0);
1436 else 1546 else
1437 sv_utf8_upgrade (string); 1547 sv_utf8_upgrade (string);
1449 1559
1450 *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
1451 1561
1452 decode_ws (&dec); 1562 decode_ws (&dec);
1453 sv = decode_sv (&dec); 1563 sv = decode_sv (&dec);
1564
1565 if (offset_return)
1566 *offset_return = dec.cur;
1454 1567
1455 if (!(offset_return || !sv)) 1568 if (!(offset_return || !sv))
1456 { 1569 {
1457 // check for trailing garbage 1570 // check for trailing garbage
1458 decode_ws (&dec); 1571 decode_ws (&dec);
1461 { 1574 {
1462 dec.err = "garbage after JSON object"; 1575 dec.err = "garbage after JSON object";
1463 SvREFCNT_dec (sv); 1576 SvREFCNT_dec (sv);
1464 sv = 0; 1577 sv = 0;
1465 } 1578 }
1466 }
1467
1468 if (offset_return || !sv)
1469 {
1470 offset = dec.json.flags & F_UTF8
1471 ? dec.cur - SvPVX (string)
1472 : utf8_distance (dec.cur, SvPVX (string));
1473
1474 if (offset_return)
1475 *offset_return = offset;
1476 } 1579 }
1477 1580
1478 if (!sv) 1581 if (!sv)
1479 { 1582 {
1480 SV *uni = sv_newmortal (); 1583 SV *uni = sv_newmortal ();
1486 SAVEVPTR (PL_curcop); 1589 SAVEVPTR (PL_curcop);
1487 PL_curcop = &cop; 1590 PL_curcop = &cop;
1488 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);
1489 LEAVE; 1592 LEAVE;
1490 1593
1491 croak ("%s, at character offset %d [\"%s\"]", 1594 croak ("%s, at character offset %d (before \"%s\")",
1492 dec.err, 1595 dec.err,
1493 (int)offset, 1596 ptr_to_index (string, dec.cur),
1494 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1597 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1495 } 1598 }
1496 1599
1497 sv = sv_2mortal (sv); 1600 sv = sv_2mortal (sv);
1498 1601
1507 1610
1508static void 1611static void
1509incr_parse (JSON *self) 1612incr_parse (JSON *self)
1510{ 1613{
1511 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...
1512 1618
1513 for (;;) 1619 for (;;)
1514 { 1620 {
1515 //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
1516 switch (self->incr_mode) 1622 switch (self->incr_mode)
1517 { 1623 {
1518 // only used for intiial whitespace skipping 1624 // only used for initial whitespace skipping
1519 case INCR_M_WS: 1625 case INCR_M_WS:
1520 for (;;) 1626 for (;;)
1521 { 1627 {
1522 if (*p > 0x20) 1628 if (*p > 0x20)
1523 { 1629 {
1630 if (*p == '#')
1631 {
1632 self->incr_mode = INCR_M_C0;
1633 goto incr_m_c;
1634 }
1635 else
1636 {
1524 self->incr_mode = INCR_M_JSON; 1637 self->incr_mode = INCR_M_JSON;
1525 goto incr_m_json; 1638 goto incr_m_json;
1639 }
1526 } 1640 }
1527 else if (!*p) 1641 else if (!*p)
1528 goto interrupt; 1642 goto interrupt;
1529 1643
1530 ++p; 1644 ++p;
1536 goto interrupt; 1650 goto interrupt;
1537 1651
1538 ++p; 1652 ++p;
1539 self->incr_mode = INCR_M_STR; 1653 self->incr_mode = INCR_M_STR;
1540 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;
1541 1674
1542 // inside a string 1675 // inside a string
1543 case INCR_M_STR: 1676 case INCR_M_STR:
1544 incr_m_str: 1677 incr_m_str:
1545 for (;;) 1678 for (;;)
1604 1737
1605 case ']': 1738 case ']':
1606 case '}': 1739 case '}':
1607 if (--self->incr_nest <= 0) 1740 if (--self->incr_nest <= 0)
1608 goto interrupt; 1741 goto interrupt;
1742 break;
1743
1744 case '#':
1745 self->incr_mode = INCR_M_C1;
1746 goto incr_m_c;
1609 } 1747 }
1610 } 1748 }
1611 } 1749 }
1612 1750
1613 modechange: 1751 modechange:
1614 ; 1752 ;
1615 } 1753 }
1616 1754
1617interrupt: 1755interrupt:
1618 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
1619 //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
1620} 1759}
1621 1760
1622///////////////////////////////////////////////////////////////////////////// 1761/////////////////////////////////////////////////////////////////////////////
1623// XS interface functions 1762// XS interface functions
1638 json_stash = gv_stashpv ("JSON::XS" , 1); 1777 json_stash = gv_stashpv ("JSON::XS" , 1);
1639 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1778 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1640 1779
1641 json_true = get_bool ("JSON::XS::true"); 1780 json_true = get_bool ("JSON::XS::true");
1642 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 */
1643} 1784}
1644 1785
1645PROTOTYPES: DISABLE 1786PROTOTYPES: DISABLE
1646 1787
1647void CLONE (...) 1788void CLONE (...)
1767 XPUSHs (decode_json (jsonstr, self, 0)); 1908 XPUSHs (decode_json (jsonstr, self, 0));
1768 1909
1769void decode_prefix (JSON *self, SV *jsonstr) 1910void decode_prefix (JSON *self, SV *jsonstr)
1770 PPCODE: 1911 PPCODE:
1771{ 1912{
1772 STRLEN offset; 1913 char *offset;
1773 EXTEND (SP, 2); 1914 EXTEND (SP, 2);
1774 PUSHs (decode_json (jsonstr, self, &offset)); 1915 PUSHs (decode_json (jsonstr, self, &offset));
1775 PUSHs (sv_2mortal (newSVuv (offset))); 1916 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1776} 1917}
1777 1918
1778void incr_parse (JSON *self, SV *jsonstr = 0) 1919void incr_parse (JSON *self, SV *jsonstr = 0)
1779 PPCODE: 1920 PPCODE:
1780{ 1921{
1782 self->incr_text = newSVpvn ("", 0); 1923 self->incr_text = newSVpvn ("", 0);
1783 1924
1784 // append data, if any 1925 // append data, if any
1785 if (jsonstr) 1926 if (jsonstr)
1786 { 1927 {
1787 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1928 if (SvUTF8 (jsonstr))
1788 { 1929 {
1930 if (!SvUTF8 (self->incr_text))
1931 {
1789 /* utf-8-ness differs, need to upgrade */ 1932 /* utf-8-ness differs, need to upgrade */
1790 sv_utf8_upgrade (self->incr_text); 1933 sv_utf8_upgrade (self->incr_text);
1791 1934
1792 if (self->incr_pos) 1935 if (self->incr_pos)
1793 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)
1794 - (U8 *)SvPVX (self->incr_text); 1937 - (U8 *)SvPVX (self->incr_text);
1938 }
1795 } 1939 }
1940 else if (SvUTF8 (self->incr_text))
1941 sv_utf8_upgrade (jsonstr);
1796 1942
1797 { 1943 {
1798 STRLEN len; 1944 STRLEN len;
1799 const char *str = SvPV (jsonstr, len); 1945 const char *str = SvPV (jsonstr, len);
1800 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
1801 Move (str, SvEND (self->incr_text), len, char); 1951 Move (str, SvEND (self->incr_text), len, char);
1802 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1952 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1803 *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
1804 } 1954 }
1805 } 1955 }
1806 1956
1807 if (GIMME_V != G_VOID) 1957 if (GIMME_V != G_VOID)
1808 do 1958 do
1809 { 1959 {
1810 STRLEN offset; 1960 char *offset;
1811 1961
1812 if (!INCR_DONE (self)) 1962 if (!INCR_DONE (self))
1813 { 1963 {
1814 incr_parse (self); 1964 incr_parse (self);
1815 1965
1821 break; 1971 break;
1822 } 1972 }
1823 1973
1824 XPUSHs (decode_json (self->incr_text, self, &offset)); 1974 XPUSHs (decode_json (self->incr_text, self, &offset));
1825 1975
1826 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1827 self->incr_pos -= offset; 1976 self->incr_pos -= offset - SvPVX (self->incr_text);
1828 self->incr_nest = 0; 1977 self->incr_nest = 0;
1829 self->incr_mode = 0; 1978 self->incr_mode = 0;
1979
1980 sv_chop (self->incr_text, offset);
1830 } 1981 }
1831 while (GIMME_V == G_ARRAY); 1982 while (GIMME_V == G_ARRAY);
1832} 1983}
1833 1984
1834SV *incr_text (JSON *self) 1985SV *incr_text (JSON *self)
1895 json_init (&json); 2046 json_init (&json);
1896 json.flags |= ix; 2047 json.flags |= ix;
1897 XPUSHs (decode_json (jsonstr, &json, 0)); 2048 XPUSHs (decode_json (jsonstr, &json, 0));
1898} 2049}
1899 2050
1900

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines