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.12 by root, Sat Mar 24 22:10:08 2007 UTC vs.
Revision 1.13 by root, Sat Mar 24 22:55:16 2007 UTC

51 SvPV_shrink_to_cur (sv); 51 SvPV_shrink_to_cur (sv);
52#elif defined (SvPV_renew) 52#elif defined (SvPV_renew)
53 SvPV_renew (sv, SvCUR (sv) + 1); 53 SvPV_renew (sv, SvCUR (sv) + 1);
54#endif 54#endif
55 } 55 }
56}
57
58// decode an utf-8 character and return it, or (UV)-1 in
59// case of an error.
60// we special-case "safe" characters from U+80 .. U+7FF,
61// but use the very good perl function to parse anything else.
62// note that we never call this function for a ascii codepoints
63static UV
64decode_utf8 (unsigned char *s, STRLEN len, STRLEN *clen)
65{
66 if (s[0] > 0xdf || s[0] < 0xc2)
67 return utf8n_to_uvuni (s, len, clen, UTF8_CHECK_ONLY);
68 else if (len > 1 && s[1] >= 0x80 && s[1] <= 0xbf)
69 {
70 *clen = 2;
71 return ((s[0] & 0x1f) << 6) | (s[1] & 0x3f);
72 }
73 else
74 return (UV)-1;
56} 75}
57 76
58///////////////////////////////////////////////////////////////////////////// 77/////////////////////////////////////////////////////////////////////////////
59// encoder 78// encoder
60 79
133 STRLEN clen; 152 STRLEN clen;
134 UV uch; 153 UV uch;
135 154
136 if (is_utf8) 155 if (is_utf8)
137 { 156 {
138 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); 157 //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
158 uch = decode_utf8 (str, end - str, &clen);
139 if (clen == (STRLEN)-1) 159 if (clen == (STRLEN)-1)
140 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); 160 croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str);
141 } 161 }
142 else 162 else
143 { 163 {
619 else if (ch >= 0x80) 639 else if (ch >= 0x80)
620 { 640 {
621 --dec->cur; 641 --dec->cur;
622 642
623 STRLEN clen; 643 STRLEN clen;
624 UV uch = utf8n_to_uvuni (dec->cur, dec->end - dec->cur, &clen, UTF8_CHECK_ONLY); 644 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
625 if (clen == (STRLEN)-1) 645 if (clen == (STRLEN)-1)
626 ERR ("malformed UTF-8 character in JSON string"); 646 ERR ("malformed UTF-8 character in JSON string");
627 647
628 do 648 do
629 { 649 {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines