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.5 by root, Thu Mar 22 23:24:18 2007 UTC vs.
Revision 1.6 by root, Fri Mar 23 15:10:55 2007 UTC

12#define F_CANONICAL 0x00000008 12#define F_CANONICAL 0x00000008
13#define F_SPACE_BEFORE 0x00000010 13#define F_SPACE_BEFORE 0x00000010
14#define F_SPACE_AFTER 0x00000020 14#define F_SPACE_AFTER 0x00000020
15#define F_JSON_RPC 0x00000040 15#define F_JSON_RPC 0x00000040
16#define F_ALLOW_NONREF 0x00000080 16#define F_ALLOW_NONREF 0x00000080
17#define F_SHRINK 0x00000100
17 18
18#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER 19#define F_PRETTY F_INDENT | F_SPACE_BEFORE | F_SPACE_AFTER
19#define F_DEFAULT 0 20#define F_DEFAULT 0
20 21
21#define INIT_SIZE 32 // initial scalar size to be allocated 22#define INIT_SIZE 32 // initial scalar size to be allocated
85 86
86 while (str < end) 87 while (str < end)
87 { 88 {
88 unsigned char ch = *(unsigned char *)str; 89 unsigned char ch = *(unsigned char *)str;
89 90
90 if (ch == '"') 91 if (ch >= 0x20 && ch < 0x80) // most common case
91 { 92 {
93 if (ch == '"') // but with slow exceptions
94 {
92 need (enc, len += 1); 95 need (enc, len += 1);
93 *enc->cur++ = '\\'; 96 *enc->cur++ = '\\';
94 *enc->cur++ = '"'; 97 *enc->cur++ = '"';
95 ++str;
96 } 98 }
97 else if (ch == '\\') 99 else if (ch == '\\')
98 { 100 {
99 need (enc, len += 1); 101 need (enc, len += 1);
100 *enc->cur++ = '\\'; 102 *enc->cur++ = '\\';
101 *enc->cur++ = '\\'; 103 *enc->cur++ = '\\';
102 ++str;
103 } 104 }
104 else if (ch >= 0x20 && ch < 0x80) // most common case 105 else
105 {
106 *enc->cur++ = ch; 106 *enc->cur++ = ch;
107 ++str; 107
108 }
109 else if (ch == '\015')
110 {
111 need (enc, len += 1);
112 *enc->cur++ = '\\';
113 *enc->cur++ = 'r';
114 ++str;
115 }
116 else if (ch == '\012')
117 {
118 need (enc, len += 1);
119 *enc->cur++ = '\\';
120 *enc->cur++ = 'n';
121 ++str; 108 ++str;
122 } 109 }
123 else 110 else
124 { 111 {
125 STRLEN clen; 112 switch (ch)
126 UV uch;
127
128 if (is_utf8)
129 { 113 {
130 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); 114 case '\010': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'b'; ++str; break;
131 if (clen == (STRLEN)-1) 115 case '\011': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 't'; ++str; break;
132 croak ("malformed UTF-8 character in string, cannot convert to JSON"); 116 case '\012': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'n'; ++str; break;
133 } 117 case '\014': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'f'; ++str; break;
134 else 118 case '\015': need (enc, len += 1); *enc->cur++ = '\\'; *enc->cur++ = 'r'; ++str; break;
135 {
136 uch = ch;
137 clen = 1;
138 }
139 119
140 if (uch < 0x80 || enc->flags & F_ASCII) 120 default:
141 {
142 if (uch > 0xFFFFUL)
143 { 121 {
122 STRLEN clen;
123 UV uch;
124
125 if (is_utf8)
126 {
127 uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY);
128 if (clen == (STRLEN)-1)
129 croak ("malformed UTF-8 character in string, cannot convert to JSON");
130 }
131 else
132 {
133 uch = ch;
134 clen = 1;
135 }
136
137 if (uch < 0x80 || enc->flags & F_ASCII)
138 {
139 if (uch > 0xFFFFUL)
140 {
144 need (enc, len += 11); 141 need (enc, len += 11);
145 sprintf (enc->cur, "\\u%04x\\u%04x", 142 sprintf (enc->cur, "\\u%04x\\u%04x",
146 (uch - 0x10000) / 0x400 + 0xD800, 143 (uch - 0x10000) / 0x400 + 0xD800,
147 (uch - 0x10000) % 0x400 + 0xDC00); 144 (uch - 0x10000) % 0x400 + 0xDC00);
148 enc->cur += 12; 145 enc->cur += 12;
146 }
147 else
148 {
149 static char hexdigit [16] = "0123456789abcdef";
150 need (enc, len += 5);
151 *enc->cur++ = '\\';
152 *enc->cur++ = 'u';
153 *enc->cur++ = hexdigit [ uch >> 12 ];
154 *enc->cur++ = hexdigit [(uch >> 8) & 15];
155 *enc->cur++ = hexdigit [(uch >> 4) & 15];
156 *enc->cur++ = hexdigit [(uch >> 0) & 15];
157 }
158
159 str += clen;
160 }
161 else if (is_utf8)
162 {
163 need (enc, len += clen);
164 do
165 {
166 *enc->cur++ = *str++;
167 }
168 while (--clen);
169 }
170 else
171 {
172 need (enc, 10); // never more than 11 bytes needed
173 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
174 ++str;
175 }
149 } 176 }
150 else
151 {
152 static char hexdigit [16] = "0123456789abcdef";
153 need (enc, len += 5);
154 *enc->cur++ = '\\';
155 *enc->cur++ = 'u';
156 *enc->cur++ = hexdigit [ uch >> 12 ];
157 *enc->cur++ = hexdigit [(uch >> 8) & 15];
158 *enc->cur++ = hexdigit [(uch >> 4) & 15];
159 *enc->cur++ = hexdigit [(uch >> 0) & 15];
160 }
161
162 str += clen;
163 }
164 else if (is_utf8)
165 {
166 need (enc, len += clen);
167 do
168 {
169 *enc->cur++ = *str++;
170 }
171 while (--clen);
172 }
173 else
174 {
175 need (enc, 10); // never more than 11 bytes needed
176 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
177 ++str;
178 } 177 }
179 } 178 }
180 179
181 --len; 180 --len;
182 } 181 }
425 424
426 if (!(flags & (F_ASCII | F_UTF8))) 425 if (!(flags & (F_ASCII | F_UTF8)))
427 SvUTF8_on (enc.sv); 426 SvUTF8_on (enc.sv);
428 427
429 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); 428 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
429
430#ifdef SvPV_shrink_to_cur
431 if (enc.flags & F_SHRINK)
432 SvPV_shrink_to_cur (enc.sv);
433#endif
430 return enc.sv; 434 return enc.sv;
431} 435}
432 436
433///////////////////////////////////////////////////////////////////////////// 437/////////////////////////////////////////////////////////////////////////////
434 438
599 *SvEND (sv) = 0; 603 *SvEND (sv) = 0;
600 604
601 if (utf8) 605 if (utf8)
602 SvUTF8_on (sv); 606 SvUTF8_on (sv);
603 607
608#ifdef SvPV_shrink_to_cur
609 if (dec->flags & F_SHRINK)
610 SvPV_shrink_to_cur (sv);
611#endif
612
604 return sv; 613 return sv;
605 614
606fail: 615fail:
607 SvREFCNT_dec (sv); 616 SvREFCNT_dec (sv);
608 return 0; 617 return 0;
910 CODE: 919 CODE:
911 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash); 920 RETVAL = sv_bless (newRV_noinc (newSVuv (F_DEFAULT)), json_stash);
912 OUTPUT: 921 OUTPUT:
913 RETVAL 922 RETVAL
914 923
915SV *ascii (SV *self, int enable) 924SV *ascii (SV *self, int enable = 1)
916 ALIAS: 925 ALIAS:
917 ascii = F_ASCII 926 ascii = F_ASCII
918 utf8 = F_UTF8 927 utf8 = F_UTF8
919 indent = F_INDENT 928 indent = F_INDENT
920 canonical = F_CANONICAL 929 canonical = F_CANONICAL
921 space_before = F_SPACE_BEFORE 930 space_before = F_SPACE_BEFORE
922 space_after = F_SPACE_AFTER 931 space_after = F_SPACE_AFTER
923 json_rpc = F_JSON_RPC 932 json_rpc = F_JSON_RPC
924 pretty = F_PRETTY 933 pretty = F_PRETTY
925 allow_nonref = F_ALLOW_NONREF 934 allow_nonref = F_ALLOW_NONREF
935 shrink = F_SHRINK
926 CODE: 936 CODE:
927{ 937{
928 UV *uv = SvJSON (self); 938 UV *uv = SvJSON (self);
929 if (enable) 939 if (enable)
930 *uv |= ix; 940 *uv |= ix;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines