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.94 by root, Thu Nov 20 03:59:53 2008 UTC vs.
Revision 1.96 by root, Sat May 30 06:26:05 2009 UTC

175 *s++ = 0x80 | ( ch & 0x3f); 175 *s++ = 0x80 | ( ch & 0x3f);
176 176
177 return s; 177 return s;
178} 178}
179 179
180// convert offset pointer to character index, sv must be string
181static STRLEN
182ptr_to_index (SV *sv, char *offset)
183{
184 return SvUTF8 (sv)
185 ? utf8_distance (offset, SvPVX (sv))
186 : offset - SvPVX (sv);
187}
188
180///////////////////////////////////////////////////////////////////////////// 189/////////////////////////////////////////////////////////////////////////////
181// encoder 190// encoder
182 191
183// structure used for encoding JSON 192// structure used for encoding JSON
184typedef struct 193typedef struct
195need (enc_t *enc, STRLEN len) 204need (enc_t *enc, STRLEN len)
196{ 205{
197 if (expect_false (enc->cur + len >= enc->end)) 206 if (expect_false (enc->cur + len >= enc->end))
198 { 207 {
199 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); 208 STRLEN cur = enc->cur - (char *)SvPVX (enc->sv);
200 SvGROW (enc->sv, cur + len + 1); 209 SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
201 enc->cur = SvPVX (enc->sv) + cur; 210 enc->cur = SvPVX (enc->sv) + cur;
202 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; 211 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
203 } 212 }
204} 213}
205 214
970 { 979 {
971 STRLEN len = cur - buf; 980 STRLEN len = cur - buf;
972 981
973 if (sv) 982 if (sv)
974 { 983 {
975 SvGROW (sv, SvCUR (sv) + len + 1); 984 STRLEN cur = SvCUR (sv);
985
986 if (SvLEN (sv) <= cur + len)
987 SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
988
976 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 989 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
977 SvCUR_set (sv, SvCUR (sv) + len); 990 SvCUR_set (sv, SvCUR (sv) + len);
978 } 991 }
979 else 992 else
980 sv = newSVpvn (buf, len); 993 sv = newSVpvn (buf, len);
1404fail: 1417fail:
1405 return 0; 1418 return 0;
1406} 1419}
1407 1420
1408static SV * 1421static SV *
1409decode_json (SV *string, JSON *json, STRLEN *offset_return) 1422decode_json (SV *string, JSON *json, char **offset_return)
1410{ 1423{
1411 dec_t dec; 1424 dec_t dec;
1412 STRLEN offset;
1413 SV *sv; 1425 SV *sv;
1414 1426
1415 /* work around bugs in 5.10 where manipulating magic values 1427 /* work around bugs in 5.10 where manipulating magic values
1416 * will perl ignore the magic in subsequent accesses 1428 * will perl ignore the magic in subsequent accesses
1417 */ 1429 */
1429 * and hope for the best. 1441 * and hope for the best.
1430 * Damnit, SvPV_nolen still trips over yet another assertion. This 1442 * Damnit, SvPV_nolen still trips over yet another assertion. This
1431 * assertion business is seriously broken, try yet another workaround 1443 * assertion business is seriously broken, try yet another workaround
1432 * for the broken -DDEBUGGING. 1444 * for the broken -DDEBUGGING.
1433 */ 1445 */
1446 {
1434#ifdef DEBUGGING 1447#ifdef DEBUGGING
1435 offset = SvOK (string) ? sv_len (string) : 0; 1448 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1436#else 1449#else
1437 offset = SvCUR (string); 1450 STRLEN offset = SvCUR (string);
1438#endif 1451#endif
1439 1452
1440 if (offset > json->max_size && json->max_size) 1453 if (offset > json->max_size && json->max_size)
1441 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1454 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1442 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 }
1443 1457
1444 if (json->flags & F_UTF8) 1458 if (json->flags & F_UTF8)
1445 sv_utf8_downgrade (string, 0); 1459 sv_utf8_downgrade (string, 0);
1446 else 1460 else
1447 sv_utf8_upgrade (string); 1461 sv_utf8_upgrade (string);
1459 1473
1460 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1474 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1461 1475
1462 decode_ws (&dec); 1476 decode_ws (&dec);
1463 sv = decode_sv (&dec); 1477 sv = decode_sv (&dec);
1478
1479 if (offset_return)
1480 *offset_return = dec.cur;
1464 1481
1465 if (!(offset_return || !sv)) 1482 if (!(offset_return || !sv))
1466 { 1483 {
1467 // check for trailing garbage 1484 // check for trailing garbage
1468 decode_ws (&dec); 1485 decode_ws (&dec);
1471 { 1488 {
1472 dec.err = "garbage after JSON object"; 1489 dec.err = "garbage after JSON object";
1473 SvREFCNT_dec (sv); 1490 SvREFCNT_dec (sv);
1474 sv = 0; 1491 sv = 0;
1475 } 1492 }
1476 }
1477
1478 if (offset_return || !sv)
1479 {
1480 offset = dec.json.flags & F_UTF8
1481 ? dec.cur - SvPVX (string)
1482 : utf8_distance (dec.cur, SvPVX (string));
1483
1484 if (offset_return)
1485 *offset_return = offset;
1486 } 1493 }
1487 1494
1488 if (!sv) 1495 if (!sv)
1489 { 1496 {
1490 SV *uni = sv_newmortal (); 1497 SV *uni = sv_newmortal ();
1496 SAVEVPTR (PL_curcop); 1503 SAVEVPTR (PL_curcop);
1497 PL_curcop = &cop; 1504 PL_curcop = &cop;
1498 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1505 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1499 LEAVE; 1506 LEAVE;
1500 1507
1501 croak ("%s, at character offset %d [\"%s\"]", 1508 croak ("%s, at character offset %d (before \"%s\")",
1502 dec.err, 1509 dec.err,
1503 (int)offset, 1510 ptr_to_index (string, dec.cur),
1504 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1511 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1505 } 1512 }
1506 1513
1507 sv = sv_2mortal (sv); 1514 sv = sv_2mortal (sv);
1508 1515
1777 XPUSHs (decode_json (jsonstr, self, 0)); 1784 XPUSHs (decode_json (jsonstr, self, 0));
1778 1785
1779void decode_prefix (JSON *self, SV *jsonstr) 1786void decode_prefix (JSON *self, SV *jsonstr)
1780 PPCODE: 1787 PPCODE:
1781{ 1788{
1782 STRLEN offset; 1789 char *offset;
1783 EXTEND (SP, 2); 1790 EXTEND (SP, 2);
1784 PUSHs (decode_json (jsonstr, self, &offset)); 1791 PUSHs (decode_json (jsonstr, self, &offset));
1785 PUSHs (sv_2mortal (newSVuv (offset))); 1792 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1786} 1793}
1787 1794
1788void incr_parse (JSON *self, SV *jsonstr = 0) 1795void incr_parse (JSON *self, SV *jsonstr = 0)
1789 PPCODE: 1796 PPCODE:
1790{ 1797{
1792 self->incr_text = newSVpvn ("", 0); 1799 self->incr_text = newSVpvn ("", 0);
1793 1800
1794 // append data, if any 1801 // append data, if any
1795 if (jsonstr) 1802 if (jsonstr)
1796 { 1803 {
1797 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1804 if (SvUTF8 (jsonstr))
1798 { 1805 {
1806 if (!SvUTF8 (self->incr_text))
1807 {
1799 /* utf-8-ness differs, need to upgrade */ 1808 /* utf-8-ness differs, need to upgrade */
1800 sv_utf8_upgrade (self->incr_text); 1809 sv_utf8_upgrade (self->incr_text);
1801 1810
1802 if (self->incr_pos) 1811 if (self->incr_pos)
1803 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) 1812 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1804 - (U8 *)SvPVX (self->incr_text); 1813 - (U8 *)SvPVX (self->incr_text);
1814 }
1805 } 1815 }
1816 else if (SvUTF8 (self->incr_text))
1817 sv_utf8_upgrade (jsonstr);
1806 1818
1807 { 1819 {
1808 STRLEN len; 1820 STRLEN len;
1809 const char *str = SvPV (jsonstr, len); 1821 const char *str = SvPV (jsonstr, len);
1810 SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); 1822 STRLEN cur = SvCUR (self->incr_text);
1823
1824 if (SvLEN (self->incr_text) <= cur + len)
1825 SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1);
1826
1811 Move (str, SvEND (self->incr_text), len, char); 1827 Move (str, SvEND (self->incr_text), len, char);
1812 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); 1828 SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len);
1813 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there 1829 *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there
1814 } 1830 }
1815 } 1831 }
1816 1832
1817 if (GIMME_V != G_VOID) 1833 if (GIMME_V != G_VOID)
1818 do 1834 do
1819 { 1835 {
1820 STRLEN offset; 1836 char *offset;
1821 1837
1822 if (!INCR_DONE (self)) 1838 if (!INCR_DONE (self))
1823 { 1839 {
1824 incr_parse (self); 1840 incr_parse (self);
1825 1841
1831 break; 1847 break;
1832 } 1848 }
1833 1849
1834 XPUSHs (decode_json (self->incr_text, self, &offset)); 1850 XPUSHs (decode_json (self->incr_text, self, &offset));
1835 1851
1836 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1837 self->incr_pos -= offset; 1852 self->incr_pos -= offset - SvPVX (self->incr_text);
1838 self->incr_nest = 0; 1853 self->incr_nest = 0;
1839 self->incr_mode = 0; 1854 self->incr_mode = 0;
1855
1856 sv_chop (self->incr_text, offset);
1840 } 1857 }
1841 while (GIMME_V == G_ARRAY); 1858 while (GIMME_V == G_ARRAY);
1842} 1859}
1843 1860
1844SV *incr_text (JSON *self) 1861SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines