… | |
… | |
4 | |
4 | |
5 | #include <assert.h> |
5 | #include <assert.h> |
6 | #include <string.h> |
6 | #include <string.h> |
7 | #include <stdlib.h> |
7 | #include <stdlib.h> |
8 | #include <stdio.h> |
8 | #include <stdio.h> |
|
|
9 | #include <limits.h> |
9 | #include <float.h> |
10 | #include <float.h> |
10 | |
11 | |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | #endif |
14 | #endif |
… | |
… | |
15 | // 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 |
16 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
18 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
19 | #endif |
20 | #endif |
|
|
21 | |
|
|
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
20 | |
23 | |
21 | #define F_ASCII 0x00000001UL |
24 | #define F_ASCII 0x00000001UL |
22 | #define F_LATIN1 0x00000002UL |
25 | #define F_LATIN1 0x00000002UL |
23 | #define F_UTF8 0x00000004UL |
26 | #define F_UTF8 0x00000004UL |
24 | #define F_INDENT 0x00000008UL |
27 | #define F_INDENT 0x00000008UL |
… | |
… | |
75 | #endif |
78 | #endif |
76 | |
79 | |
77 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
80 | static HV *json_stash, *json_boolean_stash; // JSON::XS:: |
78 | static SV *json_true, *json_false; |
81 | static SV *json_true, *json_false; |
79 | |
82 | |
|
|
83 | enum { |
|
|
84 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
|
|
85 | INCR_M_STR, // inside string |
|
|
86 | INCR_M_BS, // inside backslash |
|
|
87 | INCR_M_JSON // outside anything, count nesting |
|
|
88 | }; |
|
|
89 | |
|
|
90 | #define INCR_DONE(json) (!(json)->incr_nest && (json)->incr_mode == INCR_M_JSON) |
|
|
91 | |
80 | typedef struct { |
92 | typedef struct { |
81 | U32 flags; |
93 | U32 flags; |
82 | SV *cb_object; |
94 | SV *cb_object; |
83 | HV *cb_sk_object; |
95 | HV *cb_sk_object; |
|
|
96 | |
|
|
97 | // for the incremental parser |
|
|
98 | SV *incr_text; // the source text so far |
|
|
99 | STRLEN incr_pos; // the current offset into the text |
|
|
100 | int incr_nest; // {[]}-nesting level |
|
|
101 | int incr_mode; |
84 | } JSON; |
102 | } JSON; |
85 | |
103 | |
86 | ///////////////////////////////////////////////////////////////////////////// |
104 | ///////////////////////////////////////////////////////////////////////////// |
87 | // utility functions |
105 | // utility functions |
88 | |
106 | |
… | |
… | |
420 | |
438 | |
421 | static void |
439 | static void |
422 | encode_hv (enc_t *enc, HV *hv) |
440 | encode_hv (enc_t *enc, HV *hv) |
423 | { |
441 | { |
424 | HE *he; |
442 | HE *he; |
425 | int count; |
|
|
426 | |
443 | |
427 | if (enc->indent >= enc->maxdepth) |
444 | if (enc->indent >= enc->maxdepth) |
428 | croak ("data structure too deep (hit recursion limit)"); |
445 | croak ("data structure too deep (hit recursion limit)"); |
429 | |
446 | |
430 | encode_ch (enc, '{'); |
447 | encode_ch (enc, '{'); |
… | |
… | |
648 | else if (SvIOKp (sv)) |
665 | else if (SvIOKp (sv)) |
649 | { |
666 | { |
650 | // we assume we can always read an IV as a UV and vice versa |
667 | // we assume we can always read an IV as a UV and vice versa |
651 | // we assume two's complement |
668 | // we assume two's complement |
652 | // we assume no aliasing issues in the union |
669 | // we assume no aliasing issues in the union |
653 | if (SvIsUV (sv) ? SvUVX (sv) > 59000 |
670 | if (SvIsUV (sv) ? SvUVX (sv) <= 59000 |
654 | : SvIVX (sv) > 59000 || SvIVX (sv) < -59000) |
671 | : SvIVX (sv) <= 59000 && SvIVX (sv) >= -59000) |
655 | { |
|
|
656 | // large integer, use the (rather slow) snprintf way. |
|
|
657 | need (enc, sizeof (UV) * 5 / 2 + 1); // CHAR_BIT is at least 8 |
|
|
658 | enc->cur += |
|
|
659 | SvIsUV(sv) |
|
|
660 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
|
|
661 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
|
|
662 | } |
|
|
663 | else |
|
|
664 | { |
672 | { |
665 | // optimise the "small number case" |
673 | // optimise the "small number case" |
666 | // code will likely be branchless and use only a single multiplication |
674 | // code will likely be branchless and use only a single multiplication |
667 | // works for numbers up to 59074 |
675 | // works for numbers up to 59074 |
668 | I32 i = SvIVX (sv); |
676 | I32 i = SvIVX (sv); |
… | |
… | |
679 | |
687 | |
680 | // now output digit by digit, each time masking out the integer part |
688 | // now output digit by digit, each time masking out the integer part |
681 | // and multiplying by 5 while moving the decimal point one to the right, |
689 | // and multiplying by 5 while moving the decimal point one to the right, |
682 | // resulting in a net multiplication by 10. |
690 | // resulting in a net multiplication by 10. |
683 | // we always write the digit to memory but conditionally increment |
691 | // we always write the digit to memory but conditionally increment |
684 | // the pointer, to ease the usage of conditional move instructions. |
692 | // the pointer, to enable the use of conditional move instructions. |
685 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
693 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffffUL) * 5; |
686 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
694 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffffUL) * 5; |
687 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
695 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffffUL) * 5; |
688 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
696 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffffUL) * 5; |
689 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
697 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
|
|
698 | } |
|
|
699 | else |
|
|
700 | { |
|
|
701 | // large integer, use the (rather slow) snprintf way. |
|
|
702 | need (enc, IVUV_MAXCHARS); |
|
|
703 | enc->cur += |
|
|
704 | SvIsUV(sv) |
|
|
705 | ? snprintf (enc->cur, IVUV_MAXCHARS, "%"UVuf, (UV)SvUVX (sv)) |
|
|
706 | : snprintf (enc->cur, IVUV_MAXCHARS, "%"IVdf, (IV)SvIVX (sv)); |
690 | } |
707 | } |
691 | } |
708 | } |
692 | else if (SvROK (sv)) |
709 | else if (SvROK (sv)) |
693 | encode_rv (enc, SvRV (sv)); |
710 | encode_rv (enc, SvRV (sv)); |
694 | else if (!SvOK (sv)) |
711 | else if (!SvOK (sv)) |
… | |
… | |
1168 | char *p = dec->cur; |
1185 | char *p = dec->cur; |
1169 | char *e = p + 24; // only try up to 24 bytes |
1186 | char *e = p + 24; // only try up to 24 bytes |
1170 | |
1187 | |
1171 | for (;;) |
1188 | for (;;) |
1172 | { |
1189 | { |
1173 | // the >= 0x80 is true on most architectures |
1190 | // the >= 0x80 is false on most architectures |
1174 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1191 | if (p == e || *p < 0x20 || *p >= 0x80 || *p == '\\') |
1175 | { |
1192 | { |
1176 | // slow path, back up and use decode_str |
1193 | // slow path, back up and use decode_str |
1177 | SV *key = decode_str (dec); |
1194 | SV *key = decode_str (dec); |
1178 | if (!key) |
1195 | if (!key) |
… | |
… | |
1370 | fail: |
1387 | fail: |
1371 | return 0; |
1388 | return 0; |
1372 | } |
1389 | } |
1373 | |
1390 | |
1374 | static SV * |
1391 | static SV * |
1375 | decode_json (SV *string, JSON *json, UV *offset_return) |
1392 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1376 | { |
1393 | { |
1377 | dec_t dec; |
1394 | dec_t dec; |
1378 | UV offset; |
1395 | STRLEN offset; |
1379 | SV *sv; |
1396 | SV *sv; |
1380 | |
1397 | |
1381 | SvGETMAGIC (string); |
1398 | SvGETMAGIC (string); |
1382 | SvUPGRADE (string, SVt_PV); |
1399 | SvUPGRADE (string, SVt_PV); |
1383 | |
1400 | |
… | |
… | |
1453 | |
1470 | |
1454 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1471 | if (!(dec.json.flags & F_ALLOW_NONREF) && !SvROK (sv)) |
1455 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1472 | croak ("JSON text must be an object or array (but found number, string, true, false or null, use allow_nonref to allow this)"); |
1456 | |
1473 | |
1457 | return sv; |
1474 | return sv; |
|
|
1475 | } |
|
|
1476 | |
|
|
1477 | ///////////////////////////////////////////////////////////////////////////// |
|
|
1478 | // incremental parser |
|
|
1479 | |
|
|
1480 | static void |
|
|
1481 | incr_parse (JSON *self) |
|
|
1482 | { |
|
|
1483 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1484 | |
|
|
1485 | for (;;) |
|
|
1486 | { |
|
|
1487 | //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 |
|
|
1488 | switch (self->incr_mode) |
|
|
1489 | { |
|
|
1490 | // only used for intiial whitespace skipping |
|
|
1491 | case INCR_M_WS: |
|
|
1492 | for (;;) |
|
|
1493 | { |
|
|
1494 | if (*p > 0x20) |
|
|
1495 | { |
|
|
1496 | self->incr_mode = INCR_M_JSON; |
|
|
1497 | goto incr_m_json; |
|
|
1498 | } |
|
|
1499 | else if (!*p) |
|
|
1500 | goto interrupt; |
|
|
1501 | |
|
|
1502 | ++p; |
|
|
1503 | } |
|
|
1504 | |
|
|
1505 | // skip a single char inside a string (for \\-processing) |
|
|
1506 | case INCR_M_BS: |
|
|
1507 | if (!*p) |
|
|
1508 | goto interrupt; |
|
|
1509 | |
|
|
1510 | ++p; |
|
|
1511 | self->incr_mode = INCR_M_STR; |
|
|
1512 | goto incr_m_str; |
|
|
1513 | |
|
|
1514 | // inside a string |
|
|
1515 | case INCR_M_STR: |
|
|
1516 | incr_m_str: |
|
|
1517 | for (;;) |
|
|
1518 | { |
|
|
1519 | if (*p == '"') |
|
|
1520 | { |
|
|
1521 | ++p; |
|
|
1522 | self->incr_mode = INCR_M_JSON; |
|
|
1523 | |
|
|
1524 | if (!self->incr_nest) |
|
|
1525 | goto interrupt; |
|
|
1526 | |
|
|
1527 | goto incr_m_json; |
|
|
1528 | } |
|
|
1529 | else if (*p == '\\') |
|
|
1530 | { |
|
|
1531 | ++p; // "virtually" consumes character after \ |
|
|
1532 | |
|
|
1533 | if (!*p) // if at end of string we have to switch modes |
|
|
1534 | { |
|
|
1535 | self->incr_mode = INCR_M_BS; |
|
|
1536 | goto interrupt; |
|
|
1537 | } |
|
|
1538 | } |
|
|
1539 | else if (!*p) |
|
|
1540 | goto interrupt; |
|
|
1541 | |
|
|
1542 | ++p; |
|
|
1543 | } |
|
|
1544 | |
|
|
1545 | // after initial ws, outside string |
|
|
1546 | case INCR_M_JSON: |
|
|
1547 | incr_m_json: |
|
|
1548 | for (;;) |
|
|
1549 | { |
|
|
1550 | switch (*p++) |
|
|
1551 | { |
|
|
1552 | case 0: |
|
|
1553 | --p; |
|
|
1554 | goto interrupt; |
|
|
1555 | |
|
|
1556 | case 0x09: |
|
|
1557 | case 0x0a: |
|
|
1558 | case 0x0d: |
|
|
1559 | case 0x20: |
|
|
1560 | if (!self->incr_nest) |
|
|
1561 | { |
|
|
1562 | --p; // do not eat the whitespace, let the next round do it |
|
|
1563 | goto interrupt; |
|
|
1564 | } |
|
|
1565 | break; |
|
|
1566 | |
|
|
1567 | case '"': |
|
|
1568 | self->incr_mode = INCR_M_STR; |
|
|
1569 | goto incr_m_str; |
|
|
1570 | |
|
|
1571 | case '[': |
|
|
1572 | case '{': |
|
|
1573 | ++self->incr_nest; |
|
|
1574 | break; |
|
|
1575 | |
|
|
1576 | case ']': |
|
|
1577 | case '}': |
|
|
1578 | if (!--self->incr_nest) |
|
|
1579 | goto interrupt; |
|
|
1580 | } |
|
|
1581 | } |
|
|
1582 | } |
|
|
1583 | |
|
|
1584 | modechange: |
|
|
1585 | ; |
|
|
1586 | } |
|
|
1587 | |
|
|
1588 | interrupt: |
|
|
1589 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1590 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1458 | } |
1591 | } |
1459 | |
1592 | |
1460 | ///////////////////////////////////////////////////////////////////////////// |
1593 | ///////////////////////////////////////////////////////////////////////////// |
1461 | // XS interface functions |
1594 | // XS interface functions |
1462 | |
1595 | |
… | |
… | |
1625 | XPUSHs (decode_json (jsonstr, self, 0)); |
1758 | XPUSHs (decode_json (jsonstr, self, 0)); |
1626 | |
1759 | |
1627 | void decode_prefix (JSON *self, SV *jsonstr) |
1760 | void decode_prefix (JSON *self, SV *jsonstr) |
1628 | PPCODE: |
1761 | PPCODE: |
1629 | { |
1762 | { |
1630 | UV offset; |
1763 | STRLEN offset; |
1631 | EXTEND (SP, 2); |
1764 | EXTEND (SP, 2); |
1632 | PUSHs (decode_json (jsonstr, self, &offset)); |
1765 | PUSHs (decode_json (jsonstr, self, &offset)); |
1633 | PUSHs (sv_2mortal (newSVuv (offset))); |
1766 | PUSHs (sv_2mortal (newSVuv (offset))); |
1634 | } |
1767 | } |
|
|
1768 | |
|
|
1769 | void incr_parse (JSON *self, SV *jsonstr = 0) |
|
|
1770 | PPCODE: |
|
|
1771 | { |
|
|
1772 | if (!self->incr_text) |
|
|
1773 | self->incr_text = newSVpvn ("", 0); |
|
|
1774 | |
|
|
1775 | // append data, if any |
|
|
1776 | if (jsonstr) |
|
|
1777 | { |
|
|
1778 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
|
|
1779 | { |
|
|
1780 | /* utf-8-ness differs, need to upgrade */ |
|
|
1781 | sv_utf8_upgrade (self->incr_text); |
|
|
1782 | |
|
|
1783 | if (self->incr_pos) |
|
|
1784 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
|
|
1785 | - (U8 *)SvPVX (self->incr_text); |
|
|
1786 | } |
|
|
1787 | |
|
|
1788 | { |
|
|
1789 | STRLEN len; |
|
|
1790 | const char *str = SvPV (jsonstr, len); |
|
|
1791 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
|
|
1792 | Move (str, SvEND (self->incr_text), len, char); |
|
|
1793 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
|
|
1794 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
|
|
1795 | } |
|
|
1796 | } |
|
|
1797 | |
|
|
1798 | if (GIMME_V != G_VOID) |
|
|
1799 | do |
|
|
1800 | { |
|
|
1801 | STRLEN offset; |
|
|
1802 | |
|
|
1803 | incr_parse (self); |
|
|
1804 | |
|
|
1805 | if (!INCR_DONE (self)) |
|
|
1806 | break; |
|
|
1807 | |
|
|
1808 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
|
|
1809 | |
|
|
1810 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1811 | self->incr_pos -= offset; |
|
|
1812 | self->incr_nest = 0; |
|
|
1813 | self->incr_mode = 0; |
|
|
1814 | } |
|
|
1815 | while (GIMME_V == G_ARRAY); |
|
|
1816 | } |
|
|
1817 | |
|
|
1818 | SV *incr_text (JSON *self) |
|
|
1819 | ATTRS: lvalue |
|
|
1820 | CODE: |
|
|
1821 | { |
|
|
1822 | if (self->incr_pos) |
|
|
1823 | croak ("incr_text can only be called after a successful incr_parse call in scalar context %d", self->incr_pos);//D |
|
|
1824 | |
|
|
1825 | RETVAL = self->incr_text ? SvREFCNT_inc (self->incr_text) : &PL_sv_undef; |
|
|
1826 | } |
|
|
1827 | OUTPUT: |
|
|
1828 | RETVAL |
1635 | |
1829 | |
1636 | void DESTROY (JSON *self) |
1830 | void DESTROY (JSON *self) |
1637 | CODE: |
1831 | CODE: |
1638 | SvREFCNT_dec (self->cb_sk_object); |
1832 | SvREFCNT_dec (self->cb_sk_object); |
1639 | SvREFCNT_dec (self->cb_object); |
1833 | SvREFCNT_dec (self->cb_object); |
|
|
1834 | SvREFCNT_dec (self->incr_text); |
1640 | |
1835 | |
1641 | PROTOTYPES: ENABLE |
1836 | PROTOTYPES: ENABLE |
1642 | |
1837 | |
1643 | void encode_json (SV *scalar) |
1838 | void encode_json (SV *scalar) |
|
|
1839 | ALIAS: |
|
|
1840 | to_json_ = 0 |
|
|
1841 | encode_json = F_UTF8 |
1644 | PPCODE: |
1842 | PPCODE: |
1645 | { |
1843 | { |
1646 | JSON json = { F_DEFAULT | F_UTF8 }; |
1844 | JSON json = { F_DEFAULT | ix }; |
1647 | XPUSHs (encode_json (scalar, &json)); |
1845 | XPUSHs (encode_json (scalar, &json)); |
1648 | } |
1846 | } |
1649 | |
1847 | |
1650 | void decode_json (SV *jsonstr) |
1848 | void decode_json (SV *jsonstr) |
|
|
1849 | ALIAS: |
|
|
1850 | from_json_ = 0 |
|
|
1851 | decode_json = F_UTF8 |
1651 | PPCODE: |
1852 | PPCODE: |
1652 | { |
1853 | { |
1653 | JSON json = { F_DEFAULT | F_UTF8 }; |
1854 | JSON json = { F_DEFAULT | ix }; |
1654 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1855 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1655 | } |
1856 | } |
1656 | |
1857 | |
|
|
1858 | |