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.90 by root, Sun Jul 20 17:55:19 2008 UTC vs.
Revision 1.106 by root, Tue Jan 19 01:36:34 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, 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
1405fail: 1510fail:
1406 return 0; 1511 return 0;
1407} 1512}
1408 1513
1409static SV * 1514static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1515decode_json (SV *string, JSON *json, char **offset_return)
1411{ 1516{
1412 dec_t dec; 1517 dec_t dec;
1413 STRLEN offset;
1414 SV *sv; 1518 SV *sv;
1415 1519
1520 /* work around bugs in 5.10 where manipulating magic values
1521 * will perl ignore the magic in subsequent accesses
1522 */
1416 SvGETMAGIC (string); 1523 /*SvGETMAGIC (string);*/
1524 if (SvMAGICAL (string))
1525 string = sv_2mortal (newSVsv (string));
1526
1417 SvUPGRADE (string, SVt_PV); 1527 SvUPGRADE (string, SVt_PV);
1418 1528
1419 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1529 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1420 * assertion with -DDEBUGGING, although SvCUR is documented to 1530 * assertion with -DDEBUGGING, although SvCUR is documented to
1421 * return the xpv_cur field which certainly exists after upgrading. 1531 * return the xpv_cur field which certainly exists after upgrading.
1424 * and hope for the best. 1534 * and hope for the best.
1425 * Damnit, SvPV_nolen still trips over yet another assertion. This 1535 * Damnit, SvPV_nolen still trips over yet another assertion. This
1426 * assertion business is seriously broken, try yet another workaround 1536 * assertion business is seriously broken, try yet another workaround
1427 * for the broken -DDEBUGGING. 1537 * for the broken -DDEBUGGING.
1428 */ 1538 */
1539 {
1429#ifdef DEBUGGING 1540#ifdef DEBUGGING
1430 offset = SvOK (string) ? sv_len (string) : 0; 1541 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1431#else 1542#else
1432 offset = SvCUR (string); 1543 STRLEN offset = SvCUR (string);
1433#endif 1544#endif
1434 1545
1435 if (offset > json->max_size && json->max_size) 1546 if (offset > json->max_size && json->max_size)
1436 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1547 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1437 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1548 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1549 }
1438 1550
1439 if (json->flags & F_UTF8) 1551 if (json->flags & F_UTF8)
1440 sv_utf8_downgrade (string, 0); 1552 sv_utf8_downgrade (string, 0);
1441 else 1553 else
1442 sv_utf8_upgrade (string); 1554 sv_utf8_upgrade (string);
1454 1566
1455 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1567 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1456 1568
1457 decode_ws (&dec); 1569 decode_ws (&dec);
1458 sv = decode_sv (&dec); 1570 sv = decode_sv (&dec);
1571
1572 if (offset_return)
1573 *offset_return = dec.cur;
1459 1574
1460 if (!(offset_return || !sv)) 1575 if (!(offset_return || !sv))
1461 { 1576 {
1462 // check for trailing garbage 1577 // check for trailing garbage
1463 decode_ws (&dec); 1578 decode_ws (&dec);
1466 { 1581 {
1467 dec.err = "garbage after JSON object"; 1582 dec.err = "garbage after JSON object";
1468 SvREFCNT_dec (sv); 1583 SvREFCNT_dec (sv);
1469 sv = 0; 1584 sv = 0;
1470 } 1585 }
1471 }
1472
1473 if (offset_return || !sv)
1474 {
1475 offset = dec.json.flags & F_UTF8
1476 ? dec.cur - SvPVX (string)
1477 : utf8_distance (dec.cur, SvPVX (string));
1478
1479 if (offset_return)
1480 *offset_return = offset;
1481 } 1586 }
1482 1587
1483 if (!sv) 1588 if (!sv)
1484 { 1589 {
1485 SV *uni = sv_newmortal (); 1590 SV *uni = sv_newmortal ();
1491 SAVEVPTR (PL_curcop); 1596 SAVEVPTR (PL_curcop);
1492 PL_curcop = &cop; 1597 PL_curcop = &cop;
1493 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1598 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1494 LEAVE; 1599 LEAVE;
1495 1600
1496 croak ("%s, at character offset %d [\"%s\"]", 1601 croak ("%s, at character offset %d (before \"%s\")",
1497 dec.err, 1602 dec.err,
1498 (int)offset, 1603 ptr_to_index (string, dec.cur),
1499 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1604 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1500 } 1605 }
1501 1606
1502 sv = sv_2mortal (sv); 1607 sv = sv_2mortal (sv);
1503 1608
1512 1617
1513static void 1618static void
1514incr_parse (JSON *self) 1619incr_parse (JSON *self)
1515{ 1620{
1516 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1621 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1622
1623 // the state machine here is a bit convoluted and could be simplified a lot
1624 // but this would make it slower, so...
1517 1625
1518 for (;;) 1626 for (;;)
1519 { 1627 {
1520 //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 1628 //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
1521 switch (self->incr_mode) 1629 switch (self->incr_mode)
1522 { 1630 {
1523 // only used for intiial whitespace skipping 1631 // only used for initial whitespace skipping
1524 case INCR_M_WS: 1632 case INCR_M_WS:
1525 for (;;) 1633 for (;;)
1526 { 1634 {
1527 if (*p > 0x20) 1635 if (*p > 0x20)
1528 { 1636 {
1637 if (*p == '#')
1638 {
1639 self->incr_mode = INCR_M_C0;
1640 goto incr_m_c;
1641 }
1642 else
1643 {
1529 self->incr_mode = INCR_M_JSON; 1644 self->incr_mode = INCR_M_JSON;
1530 goto incr_m_json; 1645 goto incr_m_json;
1646 }
1531 } 1647 }
1532 else if (!*p) 1648 else if (!*p)
1533 goto interrupt; 1649 goto interrupt;
1534 1650
1535 ++p; 1651 ++p;
1541 goto interrupt; 1657 goto interrupt;
1542 1658
1543 ++p; 1659 ++p;
1544 self->incr_mode = INCR_M_STR; 1660 self->incr_mode = INCR_M_STR;
1545 goto incr_m_str; 1661 goto incr_m_str;
1662
1663 // inside #-style comments
1664 case INCR_M_C0:
1665 case INCR_M_C1:
1666 incr_m_c:
1667 for (;;)
1668 {
1669 if (*p == '\n')
1670 {
1671 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1672 break;
1673 }
1674 else if (!*p)
1675 goto interrupt;
1676
1677 ++p;
1678 }
1679
1680 break;
1546 1681
1547 // inside a string 1682 // inside a string
1548 case INCR_M_STR: 1683 case INCR_M_STR:
1549 incr_m_str: 1684 incr_m_str:
1550 for (;;) 1685 for (;;)
1609 1744
1610 case ']': 1745 case ']':
1611 case '}': 1746 case '}':
1612 if (--self->incr_nest <= 0) 1747 if (--self->incr_nest <= 0)
1613 goto interrupt; 1748 goto interrupt;
1749 break;
1750
1751 case '#':
1752 self->incr_mode = INCR_M_C1;
1753 goto incr_m_c;
1614 } 1754 }
1615 } 1755 }
1616 } 1756 }
1617 1757
1618 modechange: 1758 modechange:
1619 ; 1759 ;
1620 } 1760 }
1621 1761
1622interrupt: 1762interrupt:
1623 self->incr_pos = p - SvPVX (self->incr_text); 1763 self->incr_pos = p - SvPVX (self->incr_text);
1764 //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D
1624 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D 1765 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1625} 1766}
1626 1767
1627///////////////////////////////////////////////////////////////////////////// 1768/////////////////////////////////////////////////////////////////////////////
1628// XS interface functions 1769// XS interface functions
1643 json_stash = gv_stashpv ("JSON::XS" , 1); 1784 json_stash = gv_stashpv ("JSON::XS" , 1);
1644 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1785 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1645 1786
1646 json_true = get_bool ("JSON::XS::true"); 1787 json_true = get_bool ("JSON::XS::true");
1647 json_false = get_bool ("JSON::XS::false"); 1788 json_false = get_bool ("JSON::XS::false");
1789
1790 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1648} 1791}
1649 1792
1650PROTOTYPES: DISABLE 1793PROTOTYPES: DISABLE
1651 1794
1652void CLONE (...) 1795void CLONE (...)
1772 XPUSHs (decode_json (jsonstr, self, 0)); 1915 XPUSHs (decode_json (jsonstr, self, 0));
1773 1916
1774void decode_prefix (JSON *self, SV *jsonstr) 1917void decode_prefix (JSON *self, SV *jsonstr)
1775 PPCODE: 1918 PPCODE:
1776{ 1919{
1777 STRLEN offset; 1920 char *offset;
1778 EXTEND (SP, 2); 1921 EXTEND (SP, 2);
1779 PUSHs (decode_json (jsonstr, self, &offset)); 1922 PUSHs (decode_json (jsonstr, self, &offset));
1780 PUSHs (sv_2mortal (newSVuv (offset))); 1923 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1781} 1924}
1782 1925
1783void incr_parse (JSON *self, SV *jsonstr = 0) 1926void incr_parse (JSON *self, SV *jsonstr = 0)
1784 PPCODE: 1927 PPCODE:
1785{ 1928{
1787 self->incr_text = newSVpvn ("", 0); 1930 self->incr_text = newSVpvn ("", 0);
1788 1931
1789 // append data, if any 1932 // append data, if any
1790 if (jsonstr) 1933 if (jsonstr)
1791 { 1934 {
1792 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1935 if (SvUTF8 (jsonstr))
1793 { 1936 {
1937 if (!SvUTF8 (self->incr_text))
1938 {
1794 /* utf-8-ness differs, need to upgrade */ 1939 /* utf-8-ness differs, need to upgrade */
1795 sv_utf8_upgrade (self->incr_text); 1940 sv_utf8_upgrade (self->incr_text);
1796 1941
1797 if (self->incr_pos) 1942 if (self->incr_pos)
1798 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1943 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1799 - (U8 *)SvPVX (self->incr_text); 1944 - (U8 *)SvPVX (self->incr_text);
1945 }
1800 } 1946 }
1947 else if (SvUTF8 (self->incr_text))
1948 sv_utf8_upgrade (jsonstr);
1801 1949
1802 { 1950 {
1803 STRLEN len; 1951 STRLEN len;
1804 const char *str = SvPV (jsonstr, len); 1952 const char *str = SvPV (jsonstr, len);
1805 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1953 STRLEN cur = SvCUR (self->incr_text);
1954
1955 if (SvLEN (self->incr_text) <= cur + len)
1956 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1957
1806 Move (str, SvEND (self->incr_text), len, char); 1958 Move (str, SvEND (self->incr_text), len, char);
1807 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1959 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1808 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1960 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1809 } 1961 }
1810 } 1962 }
1811 1963
1812 if (GIMME_V != G_VOID) 1964 if (GIMME_V != G_VOID)
1813 do 1965 do
1814 { 1966 {
1815 STRLEN offset; 1967 char *offset;
1816 1968
1817 if (!INCR_DONE (self)) 1969 if (!INCR_DONE (self))
1818 { 1970 {
1819 incr_parse (self); 1971 incr_parse (self);
1820 1972
1826 break; 1978 break;
1827 } 1979 }
1828 1980
1829 XPUSHs (decode_json (self->incr_text, self, &offset)); 1981 XPUSHs (decode_json (self->incr_text, self, &offset));
1830 1982
1831 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1832 self->incr_pos -= offset; 1983 self->incr_pos -= offset - SvPVX (self->incr_text);
1833 self->incr_nest = 0; 1984 self->incr_nest = 0;
1834 self->incr_mode = 0; 1985 self->incr_mode = 0;
1986
1987 sv_chop (self->incr_text, offset);
1835 } 1988 }
1836 while (GIMME_V == G_ARRAY); 1989 while (GIMME_V == G_ARRAY);
1837} 1990}
1838 1991
1839SV *incr_text (JSON *self) 1992SV *incr_text (JSON *self)
1900 json_init (&json); 2053 json_init (&json);
1901 json.flags |= ix; 2054 json.flags |= ix;
1902 XPUSHs (decode_json (jsonstr, &json, 0)); 2055 XPUSHs (decode_json (jsonstr, &json, 0));
1903} 2056}
1904 2057
1905

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines