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.97 by root, Fri Jul 3 09:38:30 2009 UTC vs.
Revision 1.105 by root, Tue Jan 19 01:07:27 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 <= 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
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 // this relies greatly on the quality of the pow ()
255 // implementation of the platform, but a good
256 // implementation is hard to beat.
257 if (postdp) *expo -= eaccum;
258 *accum += uaccum * Perl_pow (10., *expo);
259 *expo += eaccum;
260}
261
262static NV
263json_atof (const char *s)
264{
265 NV accum = 0.;
266 int expo = 0;
267 int neg = 0;
268
269 if (*s == '-')
270 {
271 ++s;
272 neg = 1;
273 }
274
275 json_atof_scan1 (s, &accum, &expo, 0);
276
277 return neg ? -accum : accum;
278}
189///////////////////////////////////////////////////////////////////////////// 279/////////////////////////////////////////////////////////////////////////////
190// encoder 280// encoder
191 281
192// structure used for encoding JSON 282// structure used for encoding JSON
193typedef struct 283typedef struct
471 561
472 // for canonical output we have to sort by keys first 562 // for canonical output we have to sort by keys first
473 // actually, this is mostly due to the stupid so-called 563 // actually, this is mostly due to the stupid so-called
474 // security workaround added somewhere in 5.8.x 564 // security workaround added somewhere in 5.8.x
475 // that randomises hash orderings 565 // that randomises hash orderings
476 if (enc->json.flags & F_CANONICAL) 566 if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv))
477 { 567 {
478 int count = hv_iterinit (hv); 568 int count = hv_iterinit (hv);
479 569
480 if (SvMAGICAL (hv)) 570 if (SvMAGICAL (hv))
481 { 571 {
759 : enc.json.flags & F_LATIN1 ? 0x000100UL 849 : enc.json.flags & F_LATIN1 ? 0x000100UL
760 : 0x110000UL; 850 : 0x110000UL;
761 851
762 SvPOK_only (enc.sv); 852 SvPOK_only (enc.sv);
763 encode_sv (&enc, scalar); 853 encode_sv (&enc, scalar);
854 encode_nl (&enc);
764 855
765 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 856 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
766 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings 857 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
767 858
768 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) 859 if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8)))
948 else if (expect_true (ch >= 0x20 && ch < 0x80)) 1039 else if (expect_true (ch >= 0x20 && ch < 0x80))
949 *cur++ = ch; 1040 *cur++ = ch;
950 else if (ch >= 0x80) 1041 else if (ch >= 0x80)
951 { 1042 {
952 STRLEN clen; 1043 STRLEN clen;
953 UV uch;
954 1044
955 --dec_cur; 1045 --dec_cur;
956 1046
957 uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); 1047 decode_utf8 (dec_cur, dec->end - dec_cur, &clen);
958 if (clen == (STRLEN)-1) 1048 if (clen == (STRLEN)-1)
959 ERR ("malformed UTF-8 character in JSON string"); 1049 ERR ("malformed UTF-8 character in JSON string");
960 1050
961 do 1051 do
962 *cur++ = *dec_cur++; 1052 *cur++ = *dec_cur++;
1116 } 1206 }
1117 1207
1118 len -= *start == '-' ? 1 : 0; 1208 len -= *start == '-' ? 1 : 0;
1119 1209
1120 // does not fit into IV or UV, try NV 1210 // does not fit into IV or UV, try NV
1121 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) 1211 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 1212 // fits into NV without loss of precision
1127 return newSVnv (Atof (start)); 1213 return newSVnv (json_atof (start));
1128 1214
1129 // everything else fails, convert it to a string 1215 // everything else fails, convert it to a string
1130 return newSVpvn (start, dec->cur - start); 1216 return newSVpvn (start, dec->cur - start);
1131 } 1217 }
1132 1218
1133 // loss of precision here 1219 // loss of precision here
1134 return newSVnv (Atof (start)); 1220 return newSVnv (json_atof (start));
1135 1221
1136fail: 1222fail:
1137 return 0; 1223 return 0;
1138} 1224}
1139 1225
1525static void 1611static void
1526incr_parse (JSON *self) 1612incr_parse (JSON *self)
1527{ 1613{
1528 const char *p = SvPVX (self->incr_text) + self->incr_pos; 1614 const char *p = SvPVX (self->incr_text) + self->incr_pos;
1529 1615
1616 // the state machine here is a bit convoluted and could be simplified a lot
1617 // but this would make it slower, so...
1618
1530 for (;;) 1619 for (;;)
1531 { 1620 {
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 1621 //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) 1622 switch (self->incr_mode)
1534 { 1623 {
1535 // only used for intiial whitespace skipping 1624 // only used for initial whitespace skipping
1536 case INCR_M_WS: 1625 case INCR_M_WS:
1537 for (;;) 1626 for (;;)
1538 { 1627 {
1539 if (*p > 0x20) 1628 if (*p > 0x20)
1540 { 1629 {
1630 if (*p == '#')
1631 {
1632 self->incr_mode = INCR_M_C0;
1633 goto incr_m_c;
1634 }
1635 else
1636 {
1541 self->incr_mode = INCR_M_JSON; 1637 self->incr_mode = INCR_M_JSON;
1542 goto incr_m_json; 1638 goto incr_m_json;
1639 }
1543 } 1640 }
1544 else if (!*p) 1641 else if (!*p)
1545 goto interrupt; 1642 goto interrupt;
1546 1643
1547 ++p; 1644 ++p;
1553 goto interrupt; 1650 goto interrupt;
1554 1651
1555 ++p; 1652 ++p;
1556 self->incr_mode = INCR_M_STR; 1653 self->incr_mode = INCR_M_STR;
1557 goto incr_m_str; 1654 goto incr_m_str;
1655
1656 // inside #-style comments
1657 case INCR_M_C0:
1658 case INCR_M_C1:
1659 incr_m_c:
1660 for (;;)
1661 {
1662 if (*p == '\n')
1663 {
1664 self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON;
1665 break;
1666 }
1667 else if (!*p)
1668 goto interrupt;
1669
1670 ++p;
1671 }
1672
1673 break;
1558 1674
1559 // inside a string 1675 // inside a string
1560 case INCR_M_STR: 1676 case INCR_M_STR:
1561 incr_m_str: 1677 incr_m_str:
1562 for (;;) 1678 for (;;)
1621 1737
1622 case ']': 1738 case ']':
1623 case '}': 1739 case '}':
1624 if (--self->incr_nest <= 0) 1740 if (--self->incr_nest <= 0)
1625 goto interrupt; 1741 goto interrupt;
1742 break;
1743
1744 case '#':
1745 self->incr_mode = INCR_M_C1;
1746 goto incr_m_c;
1626 } 1747 }
1627 } 1748 }
1628 } 1749 }
1629 1750
1630 modechange: 1751 modechange:
1631 ; 1752 ;
1632 } 1753 }
1633 1754
1634interrupt: 1755interrupt:
1635 self->incr_pos = p - SvPVX (self->incr_text); 1756 self->incr_pos = p - SvPVX (self->incr_text);
1757 //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 1758 //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D
1637} 1759}
1638 1760
1639///////////////////////////////////////////////////////////////////////////// 1761/////////////////////////////////////////////////////////////////////////////
1640// XS interface functions 1762// XS interface functions
1655 json_stash = gv_stashpv ("JSON::XS" , 1); 1777 json_stash = gv_stashpv ("JSON::XS" , 1);
1656 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); 1778 json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1);
1657 1779
1658 json_true = get_bool ("JSON::XS::true"); 1780 json_true = get_bool ("JSON::XS::true");
1659 json_false = get_bool ("JSON::XS::false"); 1781 json_false = get_bool ("JSON::XS::false");
1782
1783 CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */
1660} 1784}
1661 1785
1662PROTOTYPES: DISABLE 1786PROTOTYPES: DISABLE
1663 1787
1664void CLONE (...) 1788void CLONE (...)
1922 json_init (&json); 2046 json_init (&json);
1923 json.flags |= ix; 2047 json.flags |= ix;
1924 XPUSHs (decode_json (jsonstr, &json, 0)); 2048 XPUSHs (decode_json (jsonstr, &json, 0));
1925} 2049}
1926 2050
1927

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines