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.104 by root, Tue Jan 19 00:31:13 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)
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 if (postdp) *expo -= eaccum;
255 *accum += uaccum * pow (10., *expo);
256 *expo += eaccum;
257}
258
259static NV
260json_atof (const char *s)
261{
262 NV accum = 0.;
263 int expo = 0;
264 int neg = 0;
265
266 if (*s == '-')
267 {
268 ++s;
269 neg = 1;
270 }
271
272 json_atof_scan1 (s, &accum, &expo, 0);
273
274 return neg ? -accum : accum;
275}
180///////////////////////////////////////////////////////////////////////////// 276/////////////////////////////////////////////////////////////////////////////
181// encoder 277// encoder
182 278
183// structure used for encoding JSON 279// structure used for encoding JSON
184typedef struct 280typedef struct
194INLINE void 290INLINE void
195need (enc_t *enc, STRLEN len) 291need (enc_t *enc, STRLEN len)
196{ 292{
197 if (expect_false (enc->cur + len >= enc->end)) 293 if (expect_false (enc->cur + len >= enc->end))
198 { 294 {
199 STRLEN cur = enc->cur - SvPVX (enc->sv); 295 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 296 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 297 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 298 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 299 }
204} 300}
205 301
280 (int)((uch - 0x10000) % 0x400 + 0xDC00)); 376 (int)((uch - 0x10000) % 0x400 + 0xDC00));
281 enc->cur += 12; 377 enc->cur += 12;
282 } 378 }
283 else 379 else
284 { 380 {
285 static char hexdigit [16] = "0123456789abcdef";
286 need (enc, len += 5); 381 need (enc, len += 5);
287 *enc->cur++ = '\\'; 382 *enc->cur++ = '\\';
288 *enc->cur++ = 'u'; 383 *enc->cur++ = 'u';
289 *enc->cur++ = hexdigit [ uch >> 12 ]; 384 *enc->cur++ = PL_hexdigit [ uch >> 12 ];
290 *enc->cur++ = hexdigit [(uch >> 8) & 15]; 385 *enc->cur++ = PL_hexdigit [(uch >> 8) & 15];
291 *enc->cur++ = hexdigit [(uch >> 4) & 15]; 386 *enc->cur++ = PL_hexdigit [(uch >> 4) & 15];
292 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 387 *enc->cur++ = PL_hexdigit [(uch >> 0) & 15];
293 } 388 }
294 389
295 str += clen; 390 str += clen;
296 } 391 }
297 else if (enc->json.flags & F_LATIN1) 392 else if (enc->json.flags & F_LATIN1)
461 556
462 encode_ch (enc, '{'); 557 encode_ch (enc, '{');
463 558
464 // for canonical output we have to sort by keys first 559 // for canonical output we have to sort by keys first
465 // actually, this is mostly due to the stupid so-called 560 // actually, this is mostly due to the stupid so-called
466 // security workaround added somewhere in 5.8.x. 561 // security workaround added somewhere in 5.8.x
467 // that randomises hash orderings 562 // that randomises hash orderings
468 if (enc->json.flags & F_CANONICAL) 563 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
469 { 564 {
470 int count = hv_iterinit (hv); 565 int count = hv_iterinit (hv);
471 566
472 if (SvMAGICAL (hv)) 567 if (SvMAGICAL (hv))
473 { 568 {
751 : enc.json.flags & F_LATIN1 ? 0x000100UL 846 : enc.json.flags & F_LATIN1 ? 0x000100UL
752 : 0x110000UL; 847 : 0x110000UL;
753 848
754 SvPOK_only (enc.sv); 849 SvPOK_only (enc.sv);
755 encode_sv (&enc, scalar); 850 encode_sv (&enc, scalar);
851 encode_nl (&enc);
756 852
757 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 853 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
758 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 854 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
759 855
760 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 856 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
940 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1036 else if (expect_true (ch >= 0x20 && ch < 0x80))
941 *cur++ = ch; 1037 *cur++ = ch;
942 else if (ch >= 0x80) 1038 else if (ch >= 0x80)
943 { 1039 {
944 STRLEN clen; 1040 STRLEN clen;
945 UV uch;
946 1041
947 --dec_cur; 1042 --dec_cur;
948 1043
949 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1044 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
950 if (clen == (STRLEN)-1) 1045 if (clen == (STRLEN)-1)
951 ERR ("malformed UTF-8 character in JSON string"); 1046 ERR ("malformed UTF-8 character in JSON string");
952 1047
953 do 1048 do
954 *cur++ = *dec_cur++; 1049 *cur++ = *dec_cur++;
971 { 1066 {
972 STRLEN len = cur - buf; 1067 STRLEN len = cur - buf;
973 1068
974 if (sv) 1069 if (sv)
975 { 1070 {
976 SvGROW (sv, SvCUR (sv) + len + 1); 1071 STRLEN cur = SvCUR (sv);
1072
1073 if (SvLEN (sv) <= cur + len)
1074 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1075
977 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 1076 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
978 SvCUR_set (sv, SvCUR (sv) + len); 1077 SvCUR_set (sv, SvCUR (sv) + len);
979 } 1078 }
980 else 1079 else
981 sv = newSVpvn (buf, len); 1080 sv = newSVpvn (buf, len);
1072 1171
1073 // special case the rather common 1..5-digit-int case 1172 // special case the rather common 1..5-digit-int case
1074 if (*start == '-') 1173 if (*start == '-')
1075 switch (len) 1174 switch (len)
1076 { 1175 {
1077 case 2: return newSViv (-( start [1] - '0' * 1)); 1176 case 2: return newSViv (-(IV)( start [1] - '0' * 1));
1078 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 1177 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)); 1178 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)); 1179 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)); 1180 case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111));
1082 } 1181 }
1083 else 1182 else
1084 switch (len) 1183 switch (len)
1085 { 1184 {
1086 case 1: return newSViv ( start [0] - '0' * 1); 1185 case 1: return newSViv ( start [0] - '0' * 1);
1087 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 1186 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); 1187 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); 1188 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); 1189 case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111);
1091 } 1190 }
1092 1191
1093 { 1192 {
1094 UV uv; 1193 UV uv;
1095 int numtype = grok_number (start, len, &uv); 1194 int numtype = grok_number (start, len, &uv);
1104 } 1203 }
1105 1204
1106 len -= *start == '-' ? 1 : 0; 1205 len -= *start == '-' ? 1 : 0;
1107 1206
1108 // does not fit into IV or UV, try NV 1207 // does not fit into IV or UV, try NV
1109 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1208 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 1209 // fits into NV without loss of precision
1115 return newSVnv (Atof (start)); 1210 return newSVnv (json_atof (start));
1116 1211
1117 // everything else fails, convert it to a string 1212 // everything else fails, convert it to a string
1118 return newSVpvn (start, dec->cur - start); 1213 return newSVpvn (start, dec->cur - start);
1119 } 1214 }
1120 1215
1121 // loss of precision here 1216 // loss of precision here
1122 return newSVnv (Atof (start)); 1217 return newSVnv (json_atof (start));
1123 1218
1124fail: 1219fail:
1125 return 0; 1220 return 0;
1126} 1221}
1127 1222
1405fail: 1500fail:
1406 return 0; 1501 return 0;
1407} 1502}
1408 1503
1409static SV * 1504static SV *
1410decode_json (SV *string, JSON *json, STRLEN *offset_return) 1505decode_json (SV *string, JSON *json, char **offset_return)
1411{ 1506{
1412 dec_t dec; 1507 dec_t dec;
1413 STRLEN offset;
1414 SV *sv; 1508 SV *sv;
1415 1509
1510 /* work around bugs in 5.10 where manipulating magic values
1511 * will perl ignore the magic in subsequent accesses
1512 */
1416 SvGETMAGIC (string); 1513 /*SvGETMAGIC (string);*/
1514 if (SvMAGICAL (string))
1515 string = sv_2mortal (newSVsv (string));
1516
1417 SvUPGRADE (string, SVt_PV); 1517 SvUPGRADE (string, SVt_PV);
1418 1518
1419 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1519 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1420 * assertion with -DDEBUGGING, although SvCUR is documented to 1520 * assertion with -DDEBUGGING, although SvCUR is documented to
1421 * return the xpv_cur field which certainly exists after upgrading. 1521 * return the xpv_cur field which certainly exists after upgrading.
1422 * according to nicholas clark, calling SvPOK fixes this. 1522 * according to nicholas clark, calling SvPOK fixes this.
1523 * But it doesn't fix it, so try another workaround, call SvPV_nolen
1524 * and hope for the best.
1525 * Damnit, SvPV_nolen still trips over yet another assertion. This
1526 * assertion business is seriously broken, try yet another workaround
1527 * for the broken -DDEBUGGING.
1423 */ 1528 */
1424 SvPOK (string); 1529 {
1530#ifdef DEBUGGING
1531 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1532#else
1533 STRLEN offset = SvCUR (string);
1534#endif
1425 1535
1426 if (SvCUR (string) > json->max_size && json->max_size) 1536 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", 1537 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); 1538 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1539 }
1429 1540
1430 if (json->flags & F_UTF8) 1541 if (json->flags & F_UTF8)
1431 sv_utf8_downgrade (string, 0); 1542 sv_utf8_downgrade (string, 0);
1432 else 1543 else
1433 sv_utf8_upgrade (string); 1544 sv_utf8_upgrade (string);
1445 1556
1446 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1557 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1447 1558
1448 decode_ws (&dec); 1559 decode_ws (&dec);
1449 sv = decode_sv (&dec); 1560 sv = decode_sv (&dec);
1561
1562 if (offset_return)
1563 *offset_return = dec.cur;
1450 1564
1451 if (!(offset_return || !sv)) 1565 if (!(offset_return || !sv))
1452 { 1566 {
1453 // check for trailing garbage 1567 // check for trailing garbage
1454 decode_ws (&dec); 1568 decode_ws (&dec);
1457 { 1571 {
1458 dec.err = "garbage after JSON object"; 1572 dec.err = "garbage after JSON object";
1459 SvREFCNT_dec (sv); 1573 SvREFCNT_dec (sv);
1460 sv = 0; 1574 sv = 0;
1461 } 1575 }
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 } 1576 }
1473 1577
1474 if (!sv) 1578 if (!sv)
1475 { 1579 {
1476 SV *uni = sv_newmortal (); 1580 SV *uni = sv_newmortal ();
1482 SAVEVPTR (PL_curcop); 1586 SAVEVPTR (PL_curcop);
1483 PL_curcop = &cop; 1587 PL_curcop = &cop;
1484 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1588 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1485 LEAVE; 1589 LEAVE;
1486 1590
1487 croak ("%s, at character offset %d [\"%s\"]", 1591 croak ("%s, at character offset %d (before \"%s\")",
1488 dec.err, 1592 dec.err,
1489 (int)offset, 1593 ptr_to_index (string, dec.cur),
1490 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1594 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1491 } 1595 }
1492 1596
1493 sv = sv_2mortal (sv); 1597 sv = sv_2mortal (sv);
1494 1598
1503 1607
1504static void 1608static void
1505incr_parse (JSON *self) 1609incr_parse (JSON *self)
1506{ 1610{
1507 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1611 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1612
1613 // the state machine here is a bit convoluted and could be simplified a lot
1614 // but this would make it slower, so...
1508 1615
1509 for (;;) 1616 for (;;)
1510 { 1617 {
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 1618 //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) 1619 switch (self->incr_mode)
1513 { 1620 {
1514 // only used for intiial whitespace skipping 1621 // only used for initial whitespace skipping
1515 case INCR_M_WS: 1622 case INCR_M_WS:
1516 for (;;) 1623 for (;;)
1517 { 1624 {
1518 if (*p > 0x20) 1625 if (*p > 0x20)
1519 { 1626 {
1627 if (*p == '#')
1628 {
1629 self->incr_mode = INCR_M_C0;
1630 goto incr_m_c;
1631 }
1632 else
1633 {
1520 self->incr_mode = INCR_M_JSON; 1634 self->incr_mode = INCR_M_JSON;
1521 goto incr_m_json; 1635 goto incr_m_json;
1636 }
1522 } 1637 }
1523 else if (!*p) 1638 else if (!*p)
1524 goto interrupt; 1639 goto interrupt;
1525 1640
1526 ++p; 1641 ++p;
1532 goto interrupt; 1647 goto interrupt;
1533 1648
1534 ++p; 1649 ++p;
1535 self->incr_mode = INCR_M_STR; 1650 self->incr_mode = INCR_M_STR;
1536 goto incr_m_str; 1651 goto incr_m_str;
1652
1653 // inside #-style comments
1654 case INCR_M_C0:
1655 case INCR_M_C1:
1656 incr_m_c:
1657 for (;;)
1658 {
1659 if (*p == '\n')
1660 {
1661 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1662 break;
1663 }
1664 else if (!*p)
1665 goto interrupt;
1666
1667 ++p;
1668 }
1669
1670 break;
1537 1671
1538 // inside a string 1672 // inside a string
1539 case INCR_M_STR: 1673 case INCR_M_STR:
1540 incr_m_str: 1674 incr_m_str:
1541 for (;;) 1675 for (;;)
1598 croak (ERR_NESTING_EXCEEDED); 1732 croak (ERR_NESTING_EXCEEDED);
1599 break; 1733 break;
1600 1734
1601 case ']': 1735 case ']':
1602 case '}': 1736 case '}':
1603 if (!--self->incr_nest) 1737 if (--self->incr_nest <= 0)
1604 goto interrupt; 1738 goto interrupt;
1739 break;
1740
1741 case '#':
1742 self->incr_mode = INCR_M_C1;
1743 goto incr_m_c;
1605 } 1744 }
1606 } 1745 }
1607 } 1746 }
1608 1747
1609 modechange: 1748 modechange:
1610 ; 1749 ;
1611 } 1750 }
1612 1751
1613interrupt: 1752interrupt:
1614 self->incr_pos = p - SvPVX (self->incr_text); 1753 self->incr_pos = p - SvPVX (self->incr_text);
1754 //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 1755 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1616} 1756}
1617 1757
1618///////////////////////////////////////////////////////////////////////////// 1758/////////////////////////////////////////////////////////////////////////////
1619// XS interface functions 1759// XS interface functions
1634 json_stash = gv_stashpv ("JSON::XS" , 1); 1774 json_stash = gv_stashpv ("JSON::XS" , 1);
1635 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1775 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1636 1776
1637 json_true = get_bool ("JSON::XS::true"); 1777 json_true = get_bool ("JSON::XS::true");
1638 json_false = get_bool ("JSON::XS::false"); 1778 json_false = get_bool ("JSON::XS::false");
1779
1780 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1639} 1781}
1640 1782
1641PROTOTYPES: DISABLE 1783PROTOTYPES: DISABLE
1642 1784
1643void CLONE (...) 1785void CLONE (...)
1763 XPUSHs (decode_json (jsonstr, self, 0)); 1905 XPUSHs (decode_json (jsonstr, self, 0));
1764 1906
1765void decode_prefix (JSON *self, SV *jsonstr) 1907void decode_prefix (JSON *self, SV *jsonstr)
1766 PPCODE: 1908 PPCODE:
1767{ 1909{
1768 STRLEN offset; 1910 char *offset;
1769 EXTEND (SP, 2); 1911 EXTEND (SP, 2);
1770 PUSHs (decode_json (jsonstr, self, &offset)); 1912 PUSHs (decode_json (jsonstr, self, &offset));
1771 PUSHs (sv_2mortal (newSVuv (offset))); 1913 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1772} 1914}
1773 1915
1774void incr_parse (JSON *self, SV *jsonstr = 0) 1916void incr_parse (JSON *self, SV *jsonstr = 0)
1775 PPCODE: 1917 PPCODE:
1776{ 1918{
1778 self->incr_text = newSVpvn ("", 0); 1920 self->incr_text = newSVpvn ("", 0);
1779 1921
1780 // append data, if any 1922 // append data, if any
1781 if (jsonstr) 1923 if (jsonstr)
1782 { 1924 {
1783 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1925 if (SvUTF8 (jsonstr))
1784 { 1926 {
1927 if (!SvUTF8 (self->incr_text))
1928 {
1785 /* utf-8-ness differs, need to upgrade */ 1929 /* utf-8-ness differs, need to upgrade */
1786 sv_utf8_upgrade (self->incr_text); 1930 sv_utf8_upgrade (self->incr_text);
1787 1931
1788 if (self->incr_pos) 1932 if (self->incr_pos)
1789 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1933 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1790 - (U8 *)SvPVX (self->incr_text); 1934 - (U8 *)SvPVX (self->incr_text);
1935 }
1791 } 1936 }
1937 else if (SvUTF8 (self->incr_text))
1938 sv_utf8_upgrade (jsonstr);
1792 1939
1793 { 1940 {
1794 STRLEN len; 1941 STRLEN len;
1795 const char *str = SvPV (jsonstr, len); 1942 const char *str = SvPV (jsonstr, len);
1796 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1943 STRLEN cur = SvCUR (self->incr_text);
1944
1945 if (SvLEN (self->incr_text) <= cur + len)
1946 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1947
1797 Move (str, SvEND (self->incr_text), len, char); 1948 Move (str, SvEND (self->incr_text), len, char);
1798 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1949 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 1950 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1800 } 1951 }
1801 } 1952 }
1802 1953
1803 if (GIMME_V != G_VOID) 1954 if (GIMME_V != G_VOID)
1804 do 1955 do
1805 { 1956 {
1806 STRLEN offset; 1957 char *offset;
1807 1958
1808 if (!INCR_DONE (self)) 1959 if (!INCR_DONE (self))
1809 { 1960 {
1810 incr_parse (self); 1961 incr_parse (self);
1811 1962
1817 break; 1968 break;
1818 } 1969 }
1819 1970
1820 XPUSHs (decode_json (self->incr_text, self, &offset)); 1971 XPUSHs (decode_json (self->incr_text, self, &offset));
1821 1972
1822 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1823 self->incr_pos -= offset; 1973 self->incr_pos -= offset - SvPVX (self->incr_text);
1824 self->incr_nest = 0; 1974 self->incr_nest = 0;
1825 self->incr_mode = 0; 1975 self->incr_mode = 0;
1976
1977 sv_chop (self->incr_text, offset);
1826 } 1978 }
1827 while (GIMME_V == G_ARRAY); 1979 while (GIMME_V == G_ARRAY);
1828} 1980}
1829 1981
1830SV *incr_text (JSON *self) 1982SV *incr_text (JSON *self)
1891 json_init (&json); 2043 json_init (&json);
1892 json.flags |= ix; 2044 json.flags |= ix;
1893 XPUSHs (decode_json (jsonstr, &json, 0)); 2045 XPUSHs (decode_json (jsonstr, &json, 0));
1894} 2046}
1895 2047
1896

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines