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.107 by root, Wed Mar 17 01:45:43 2010 UTC vs.
Revision 1.113 by root, Thu Aug 11 21:37:41 2011 UTC

42#define INIT_SIZE 32 // initial scalar size to be allocated 42#define INIT_SIZE 32 // initial scalar size to be allocated
43#define INDENT_STEP 3 // spaces per indentation level 43#define INDENT_STEP 3 // spaces per indentation level
44 44
45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size 45#define SHORT_STRING_LEN 16384 // special-case strings of up to this size
46 46
47#define DECODE_WANTS_OCTETS(json) ((json)->flags & F_UTF8)
48
47#define SB do { 49#define SB do {
48#define SE } while (0) 50#define SE } while (0)
49 51
50#if __GNUC__ >= 3 52#if __GNUC__ >= 3
51# define expect(expr,value) __builtin_expect ((expr), (value)) 53# define expect(expr,value) __builtin_expect ((expr), (value))
258 } 260 }
259 261
260 // this relies greatly on the quality of the pow () 262 // this relies greatly on the quality of the pow ()
261 // implementation of the platform, but a good 263 // implementation of the platform, but a good
262 // implementation is hard to beat. 264 // implementation is hard to beat.
265 // (IEEE 754 conformant ones are required to be exact)
263 if (postdp) *expo -= eaccum; 266 if (postdp) *expo -= eaccum;
264 *accum += uaccum * Perl_pow (10., *expo); 267 *accum += uaccum * Perl_pow (10., *expo);
265 *expo += eaccum; 268 *expo += eaccum;
266} 269}
267 270
834 encode_rv (enc, SvRV (sv)); 837 encode_rv (enc, SvRV (sv));
835 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN) 838 else if (!SvOK (sv) || enc->json.flags & F_ALLOW_UNKNOWN)
836 encode_str (enc, "null", 4, 0); 839 encode_str (enc, "null", 4, 0);
837 else 840 else
838 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this", 841 croak ("encountered perl type (%s,0x%x) that JSON cannot handle, you might want to report this",
839 SvPV_nolen (sv), SvFLAGS (sv)); 842 SvPV_nolen (sv), (unsigned int)SvFLAGS (sv));
840} 843}
841 844
842static SV * 845static SV *
843encode_json (SV *scalar, JSON *json) 846encode_json (SV *scalar, JSON *json)
844{ 847{
1518{ 1521{
1519 dec_t dec; 1522 dec_t dec;
1520 SV *sv; 1523 SV *sv;
1521 1524
1522 /* work around bugs in 5.10 where manipulating magic values 1525 /* work around bugs in 5.10 where manipulating magic values
1523 * will perl ignore the magic in subsequent accesses 1526 * will perl ignore the magic in subsequent accesses.
1527 * also make a copy of non-PV values, to get them into a clean
1528 * state (SvPV should do that, but it's buggy, see below).
1524 */ 1529 */
1525 /*SvGETMAGIC (string);*/ 1530 /*SvGETMAGIC (string);*/
1526 if (SvMAGICAL (string)) 1531 if (SvMAGICAL (string) || !SvPOK (string))
1527 string = sv_2mortal (newSVsv (string)); 1532 string = sv_2mortal (newSVsv (string));
1528 1533
1529 SvUPGRADE (string, SVt_PV); 1534 SvUPGRADE (string, SVt_PV);
1530 1535
1531 /* work around a bug in perl 5.10, which causes SvCUR to fail an 1536 /* work around a bug in perl 5.10, which causes SvCUR to fail an
1548 if (offset > json->max_size && json->max_size) 1553 if (offset > json->max_size && json->max_size)
1549 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1554 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1550 (unsigned long)SvCUR (string), (unsigned long)json->max_size); 1555 (unsigned long)SvCUR (string), (unsigned long)json->max_size);
1551 } 1556 }
1552 1557
1553 if (json->flags & F_UTF8) 1558 if (DECODE_WANTS_OCTETS (json))
1554 sv_utf8_downgrade (string, 0); 1559 sv_utf8_downgrade (string, 0);
1555 else 1560 else
1556 sv_utf8_upgrade (string); 1561 sv_utf8_upgrade (string);
1557 1562
1558 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1563 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
1600 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); 1605 pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ);
1601 LEAVE; 1606 LEAVE;
1602 1607
1603 croak ("%s, at character offset %d (before \"%s\")", 1608 croak ("%s, at character offset %d (before \"%s\")",
1604 dec.err, 1609 dec.err,
1605 ptr_to_index (string, dec.cur), 1610 (int)ptr_to_index (string, dec.cur),
1606 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); 1611 dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)");
1607 } 1612 }
1608 1613
1609 sv = sv_2mortal (sv); 1614 sv = sv_2mortal (sv);
1610 1615
1929 PPCODE: 1934 PPCODE:
1930{ 1935{
1931 if (!self->incr_text) 1936 if (!self->incr_text)
1932 self->incr_text = newSVpvn ("", 0); 1937 self->incr_text = newSVpvn ("", 0);
1933 1938
1939 /* if utf8-ness doesn't match the decoder, need to upgrade/downgrade */
1940 if (!DECODE_WANTS_OCTETS (self) == !SvUTF8 (self->incr_text))
1941 if (DECODE_WANTS_OCTETS (self))
1942 {
1943 if (self->incr_pos)
1944 self->incr_pos = utf8_length ((U8 *)SvPVX (self->incr_text),
1945 (U8 *)SvPVX (self->incr_text) + self->incr_pos);
1946
1947 sv_utf8_downgrade (self->incr_text, 0);
1948 }
1949 else
1950 {
1951 sv_utf8_upgrade (self->incr_text);
1952
1953 if (self->incr_pos)
1954 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1955 - (U8 *)SvPVX (self->incr_text);
1956 }
1957
1934 // append data, if any 1958 // append data, if any
1935 if (jsonstr) 1959 if (jsonstr)
1936 { 1960 {
1961 /* make sure both strings have same encoding */
1962 if (SvUTF8 (jsonstr) != SvUTF8 (self->incr_text))
1937 if (SvUTF8 (jsonstr)) 1963 if (SvUTF8 (jsonstr))
1964 sv_utf8_downgrade (jsonstr, 0);
1938 { 1965 else
1939 if (!SvUTF8 (self->incr_text))
1940 {
1941 /* utf-8-ness differs, need to upgrade */
1942 sv_utf8_upgrade (self->incr_text);
1943
1944 if (self->incr_pos)
1945 self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos)
1946 - (U8 *)SvPVX (self->incr_text);
1947 }
1948 }
1949 else if (SvUTF8 (self->incr_text))
1950 sv_utf8_upgrade (jsonstr); 1966 sv_utf8_upgrade (jsonstr);
1951 1967
1968 /* and then just blindly append */
1952 { 1969 {
1953 STRLEN len; 1970 STRLEN len;
1954 const char *str = SvPV (jsonstr, len); 1971 const char *str = SvPV (jsonstr, len);
1955 STRLEN cur = SvCUR (self->incr_text); 1972 STRLEN cur = SvCUR (self->incr_text);
1956 1973
1975 if (self->incr_pos > self->max_size && self->max_size) 1992 if (self->incr_pos > self->max_size && self->max_size)
1976 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", 1993 croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu",
1977 (unsigned long)self->incr_pos, (unsigned long)self->max_size); 1994 (unsigned long)self->incr_pos, (unsigned long)self->max_size);
1978 1995
1979 if (!INCR_DONE (self)) 1996 if (!INCR_DONE (self))
1997 {
1998 // as an optimisation, do not accumulate white space in the incr buffer
1999 if (self->incr_mode == INCR_M_WS && self->incr_pos)
2000 {
2001 self->incr_pos = 0;
2002 SvCUR_set (self->incr_text, 0);
2003 }
2004
1980 break; 2005 break;
2006 }
1981 } 2007 }
1982 2008
1983 XPUSHs (decode_json (self->incr_text, self, &offset)); 2009 XPUSHs (decode_json (self->incr_text, self, &offset));
1984 2010
1985 self->incr_pos -= offset - SvPVX (self->incr_text); 2011 self->incr_pos -= offset - SvPVX (self->incr_text);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines