… | |
… | |
17 | // guarantees, 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 | |
77 | enum { |
78 | enum { |
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 | |
… | |
… | |
184 | return SvUTF8 (sv) |
187 | return SvUTF8 (sv) |
185 | ? utf8_distance (offset, SvPVX (sv)) |
188 | ? utf8_distance (offset, SvPVX (sv)) |
186 | : offset - SvPVX (sv); |
189 | : offset - SvPVX (sv); |
187 | } |
190 | } |
188 | |
191 | |
|
|
192 | ///////////////////////////////////////////////////////////////////////////// |
|
|
193 | // fp hell |
|
|
194 | |
|
|
195 | // scan a group of digits, and a trailing exponent |
|
|
196 | static void |
|
|
197 | json_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 | |
|
|
259 | static NV |
|
|
260 | json_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 | } |
189 | ///////////////////////////////////////////////////////////////////////////// |
276 | ///////////////////////////////////////////////////////////////////////////// |
190 | // encoder |
277 | // encoder |
191 | |
278 | |
192 | // structure used for encoding JSON |
279 | // structure used for encoding JSON |
193 | typedef struct |
280 | typedef struct |
… | |
… | |
759 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
846 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
760 | : 0x110000UL; |
847 | : 0x110000UL; |
761 | |
848 | |
762 | SvPOK_only (enc.sv); |
849 | SvPOK_only (enc.sv); |
763 | encode_sv (&enc, scalar); |
850 | encode_sv (&enc, scalar); |
|
|
851 | encode_nl (&enc); |
764 | |
852 | |
765 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
853 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
766 | *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 |
767 | |
855 | |
768 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
856 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
948 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
1036 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
949 | *cur++ = ch; |
1037 | *cur++ = ch; |
950 | else if (ch >= 0x80) |
1038 | else if (ch >= 0x80) |
951 | { |
1039 | { |
952 | STRLEN clen; |
1040 | STRLEN clen; |
953 | UV uch; |
|
|
954 | |
1041 | |
955 | --dec_cur; |
1042 | --dec_cur; |
956 | |
1043 | |
957 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
1044 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
958 | if (clen == (STRLEN)-1) |
1045 | if (clen == (STRLEN)-1) |
959 | ERR ("malformed UTF-8 character in JSON string"); |
1046 | ERR ("malformed UTF-8 character in JSON string"); |
960 | |
1047 | |
961 | do |
1048 | do |
962 | *cur++ = *dec_cur++; |
1049 | *cur++ = *dec_cur++; |
… | |
… | |
1116 | } |
1203 | } |
1117 | |
1204 | |
1118 | len -= *start == '-' ? 1 : 0; |
1205 | len -= *start == '-' ? 1 : 0; |
1119 | |
1206 | |
1120 | // does not fit into IV or UV, try NV |
1207 | // does not fit into IV or UV, try NV |
1121 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
1208 | if (len <= NV_DIG) |
1122 | #if defined (LDBL_DIG) |
|
|
1123 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
1124 | #endif |
|
|
1125 | ) |
|
|
1126 | // fits into NV without loss of precision |
1209 | // fits into NV without loss of precision |
1127 | return newSVnv (Atof (start)); |
1210 | return newSVnv (json_atof (start)); |
1128 | |
1211 | |
1129 | // everything else fails, convert it to a string |
1212 | // everything else fails, convert it to a string |
1130 | return newSVpvn (start, dec->cur - start); |
1213 | return newSVpvn (start, dec->cur - start); |
1131 | } |
1214 | } |
1132 | |
1215 | |
1133 | // loss of precision here |
1216 | // loss of precision here |
1134 | return newSVnv (Atof (start)); |
1217 | return newSVnv (json_atof (start)); |
1135 | |
1218 | |
1136 | fail: |
1219 | fail: |
1137 | return 0; |
1220 | return 0; |
1138 | } |
1221 | } |
1139 | |
1222 | |
… | |
… | |
1525 | static void |
1608 | static void |
1526 | incr_parse (JSON *self) |
1609 | incr_parse (JSON *self) |
1527 | { |
1610 | { |
1528 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1611 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1529 | |
1612 | |
|
|
1613 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1614 | // but this would make it slower, so... |
|
|
1615 | |
1530 | for (;;) |
1616 | for (;;) |
1531 | { |
1617 | { |
1532 | //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 |
1533 | switch (self->incr_mode) |
1619 | switch (self->incr_mode) |
1534 | { |
1620 | { |
1535 | // only used for intiial whitespace skipping |
1621 | // only used for initial whitespace skipping |
1536 | case INCR_M_WS: |
1622 | case INCR_M_WS: |
1537 | for (;;) |
1623 | for (;;) |
1538 | { |
1624 | { |
1539 | if (*p > 0x20) |
1625 | if (*p > 0x20) |
1540 | { |
1626 | { |
|
|
1627 | if (*p == '#') |
|
|
1628 | { |
|
|
1629 | self->incr_mode = INCR_M_C0; |
|
|
1630 | goto incr_m_c; |
|
|
1631 | } |
|
|
1632 | else |
|
|
1633 | { |
1541 | self->incr_mode = INCR_M_JSON; |
1634 | self->incr_mode = INCR_M_JSON; |
1542 | goto incr_m_json; |
1635 | goto incr_m_json; |
|
|
1636 | } |
1543 | } |
1637 | } |
1544 | else if (!*p) |
1638 | else if (!*p) |
1545 | goto interrupt; |
1639 | goto interrupt; |
1546 | |
1640 | |
1547 | ++p; |
1641 | ++p; |
… | |
… | |
1553 | goto interrupt; |
1647 | goto interrupt; |
1554 | |
1648 | |
1555 | ++p; |
1649 | ++p; |
1556 | self->incr_mode = INCR_M_STR; |
1650 | self->incr_mode = INCR_M_STR; |
1557 | 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; |
1558 | |
1671 | |
1559 | // inside a string |
1672 | // inside a string |
1560 | case INCR_M_STR: |
1673 | case INCR_M_STR: |
1561 | incr_m_str: |
1674 | incr_m_str: |
1562 | for (;;) |
1675 | for (;;) |
… | |
… | |
1621 | |
1734 | |
1622 | case ']': |
1735 | case ']': |
1623 | case '}': |
1736 | case '}': |
1624 | if (--self->incr_nest <= 0) |
1737 | if (--self->incr_nest <= 0) |
1625 | goto interrupt; |
1738 | goto interrupt; |
|
|
1739 | break; |
|
|
1740 | |
|
|
1741 | case '#': |
|
|
1742 | self->incr_mode = INCR_M_C1; |
|
|
1743 | goto incr_m_c; |
1626 | } |
1744 | } |
1627 | } |
1745 | } |
1628 | } |
1746 | } |
1629 | |
1747 | |
1630 | modechange: |
1748 | modechange: |
1631 | ; |
1749 | ; |
1632 | } |
1750 | } |
1633 | |
1751 | |
1634 | interrupt: |
1752 | interrupt: |
1635 | 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 |
1636 | //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 |
1637 | } |
1756 | } |
1638 | |
1757 | |
1639 | ///////////////////////////////////////////////////////////////////////////// |
1758 | ///////////////////////////////////////////////////////////////////////////// |
1640 | // XS interface functions |
1759 | // XS interface functions |
… | |
… | |
1924 | json_init (&json); |
2043 | json_init (&json); |
1925 | json.flags |= ix; |
2044 | json.flags |= ix; |
1926 | XPUSHs (decode_json (jsonstr, &json, 0)); |
2045 | XPUSHs (decode_json (jsonstr, &json, 0)); |
1927 | } |
2046 | } |
1928 | |
2047 | |
1929 | |
|
|