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.95 by root, Sun Feb 22 06:55:28 2009 UTC vs.
Revision 1.96 by root, Sat May 30 06:26:05 2009 UTC

173 *s++ = 0x80 | ((ch >> 12) & 0x3f), 173 *s++ = 0x80 | ((ch >> 12) & 0x3f),
174 *s++ = 0x80 | ((ch >> 6) & 0x3f), 174 *s++ = 0x80 | ((ch >> 6) & 0x3f),
175 *s++ = 0x80 | ( ch & 0x3f); 175 *s++ = 0x80 | ( ch & 0x3f);
176 176
177 return s; 177 return s;
178}
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);
178} 187}
179 188
180///////////////////////////////////////////////////////////////////////////// 189/////////////////////////////////////////////////////////////////////////////
181// encoder 190// encoder
182 191
1408fail: 1417fail:
1409 return 0; 1418 return 0;
1410} 1419}
1411 1420
1412static SV * 1421static SV *
1413decode_json (SV *string, JSON *json, STRLEN *offset_return) 1422decode_json (SV *string, JSON *json, char **offset_return)
1414{ 1423{
1415 dec_t dec; 1424 dec_t dec;
1416 STRLEN offset;
1417 SV *sv; 1425 SV *sv;
1418 1426
1419 /* work around bugs in 5.10 where manipulating magic values 1427 /* work around bugs in 5.10 where manipulating magic values
1420 * will perl ignore the magic in subsequent accesses 1428 * will perl ignore the magic in subsequent accesses
1421 */ 1429 */
1433 * and hope for the best. 1441 * and hope for the best.
1434 * Damnit, SvPV_nolen still trips over yet another assertion. This 1442 * Damnit, SvPV_nolen still trips over yet another assertion. This
1435 * assertion business is seriously broken, try yet another workaround 1443 * assertion business is seriously broken, try yet another workaround
1436 * for the broken -DDEBUGGING. 1444 * for the broken -DDEBUGGING.
1437 */ 1445 */
1446 {
1438#ifdef DEBUGGING 1447#ifdef DEBUGGING
1439 offset = SvOK (string) ? sv_len (string) : 0; 1448 STRLEN offset = SvOK (string) ? sv_len (string) : 0;
1440#else 1449#else
1441 offset = SvCUR (string); 1450 STRLEN offset = SvCUR (string);
1442#endif 1451#endif
1443 1452
1444 if (offset > json->max_size && json->max_size) 1453 if (offset > json->max_size && json->max_size)
1445 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",
1446 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1455 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1456 }
1447 1457
1448 if (json->flags & F_UTF8) 1458 if (json->flags & F_UTF8)
1449 sv_utf8_downgrade (string, 0); 1459 sv_utf8_downgrade (string, 0);
1450 else 1460 else
1451 sv_utf8_upgrade (string); 1461 sv_utf8_upgrade (string);
1463 1473
1464 *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
1465 1475
1466 decode_ws (&dec); 1476 decode_ws (&dec);
1467 sv = decode_sv (&dec); 1477 sv = decode_sv (&dec);
1478
1479 if (offset_return)
1480 *offset_return = dec.cur;
1468 1481
1469 if (!(offset_return || !sv)) 1482 if (!(offset_return || !sv))
1470 { 1483 {
1471 // check for trailing garbage 1484 // check for trailing garbage
1472 decode_ws (&dec); 1485 decode_ws (&dec);
1475 { 1488 {
1476 dec.err = "garbage after JSON object"; 1489 dec.err = "garbage after JSON object";
1477 SvREFCNT_dec (sv); 1490 SvREFCNT_dec (sv);
1478 sv = 0; 1491 sv = 0;
1479 } 1492 }
1480 }
1481
1482 if (offset_return || !sv)
1483 {
1484 offset = dec.json.flags & F_UTF8
1485 ? dec.cur - SvPVX (string)
1486 : utf8_distance (dec.cur, SvPVX (string));
1487
1488 if (offset_return)
1489 *offset_return = offset;
1490 } 1493 }
1491 1494
1492 if (!sv) 1495 if (!sv)
1493 { 1496 {
1494 SV *uni = sv_newmortal (); 1497 SV *uni = sv_newmortal ();
1500 SAVEVPTR (PL_curcop); 1503 SAVEVPTR (PL_curcop);
1501 PL_curcop = &cop; 1504 PL_curcop = &cop;
1502 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);
1503 LEAVE; 1506 LEAVE;
1504 1507
1505 croak ("%s, at character offset %d [\"%s\"]", 1508 croak ("%s, at character offset %d (before \"%s\")",
1506 dec.err, 1509 dec.err,
1507 (int)offset, 1510 ptr_to_index (string, dec.cur),
1508 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1511 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1509 } 1512 }
1510 1513
1511 sv = sv_2mortal (sv); 1514 sv = sv_2mortal (sv);
1512 1515
1781 XPUSHs (decode_json (jsonstr, self, 0)); 1784 XPUSHs (decode_json (jsonstr, self, 0));
1782 1785
1783void decode_prefix (JSON *self, SV *jsonstr) 1786void decode_prefix (JSON *self, SV *jsonstr)
1784 PPCODE: 1787 PPCODE:
1785{ 1788{
1786 STRLEN offset; 1789 char *offset;
1787 EXTEND (SP, 2); 1790 EXTEND (SP, 2);
1788 PUSHs (decode_json (jsonstr, self, &offset)); 1791 PUSHs (decode_json (jsonstr, self, &offset));
1789 PUSHs (sv_2mortal (newSVuv (offset))); 1792 PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset))));
1790} 1793}
1791 1794
1792void incr_parse (JSON *self, SV *jsonstr = 0) 1795void incr_parse (JSON *self, SV *jsonstr = 0)
1793 PPCODE: 1796 PPCODE:
1794{ 1797{
1796 self->incr_text = newSVpvn ("", 0); 1799 self->incr_text = newSVpvn ("", 0);
1797 1800
1798 // append data, if any 1801 // append data, if any
1799 if (jsonstr) 1802 if (jsonstr)
1800 { 1803 {
1801 if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) 1804 if (SvUTF8 (jsonstr))
1802 { 1805 {
1806 if (!SvUTF8 (self->incr_text))
1807 {
1803 /* utf-8-ness differs, need to upgrade */ 1808 /* utf-8-ness differs, need to upgrade */
1804 sv_utf8_upgrade (self->incr_text); 1809 sv_utf8_upgrade (self->incr_text);
1805 1810
1806 if (self->incr_pos) 1811 if (self->incr_pos)
1807 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)
1808 - (U8 *)SvPVX (self->incr_text); 1813 - (U8 *)SvPVX (self->incr_text);
1814 }
1809 } 1815 }
1816 else if (SvUTF8 (self->incr_text))
1817 sv_utf8_upgrade (jsonstr);
1810 1818
1811 { 1819 {
1812 STRLEN len; 1820 STRLEN len;
1813 const char *str = SvPV (jsonstr, len); 1821 const char *str = SvPV (jsonstr, len);
1814 STRLEN cur = SvCUR (self->incr_text); 1822 STRLEN cur = SvCUR (self->incr_text);
1823 } 1831 }
1824 1832
1825 if (GIMME_V != G_VOID) 1833 if (GIMME_V != G_VOID)
1826 do 1834 do
1827 { 1835 {
1828 STRLEN offset; 1836 char *offset;
1829 1837
1830 if (!INCR_DONE (self)) 1838 if (!INCR_DONE (self))
1831 { 1839 {
1832 incr_parse (self); 1840 incr_parse (self);
1833 1841
1839 break; 1847 break;
1840 } 1848 }
1841 1849
1842 XPUSHs (decode_json (self->incr_text, self, &offset)); 1850 XPUSHs (decode_json (self->incr_text, self, &offset));
1843 1851
1844 sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset);
1845 self->incr_pos -= offset; 1852 self->incr_pos -= offset - SvPVX (self->incr_text);
1846 self->incr_nest = 0; 1853 self->incr_nest = 0;
1847 self->incr_mode = 0; 1854 self->incr_mode = 0;
1855
1856 sv_chop (self->incr_text, offset);
1848 } 1857 }
1849 while (GIMME_V == G_ARRAY); 1858 while (GIMME_V == G_ARRAY);
1850} 1859}
1851 1860
1852SV *incr_text (JSON *self) 1861SV *incr_text (JSON *self)

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines