… | |
… | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
12 | #endif |
13 | |
13 | |
14 | #define F_ASCII 0x00000001UL |
14 | #define F_ASCII 0x00000001UL |
|
|
15 | #define F_LATIN1 0x00000002UL |
15 | #define F_UTF8 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
16 | #define F_INDENT 0x00000004UL |
17 | #define F_INDENT 0x00000008UL |
17 | #define F_CANONICAL 0x00000008UL |
18 | #define F_CANONICAL 0x00000010UL |
18 | #define F_SPACE_BEFORE 0x00000010UL |
19 | #define F_SPACE_BEFORE 0x00000020UL |
19 | #define F_SPACE_AFTER 0x00000020UL |
20 | #define F_SPACE_AFTER 0x00000040UL |
20 | #define F_ALLOW_NONREF 0x00000080UL |
21 | #define F_ALLOW_NONREF 0x00000100UL |
21 | #define F_SHRINK 0x00000100UL |
22 | #define F_SHRINK 0x00000200UL |
22 | #define F_MAXDEPTH 0xf8000000UL |
23 | #define F_MAXDEPTH 0xf8000000UL |
23 | #define S_MAXDEPTH 27 |
24 | #define S_MAXDEPTH 27 |
24 | |
25 | |
25 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
26 | #define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) |
26 | |
27 | |
… | |
… | |
180 | } |
181 | } |
181 | |
182 | |
182 | if (uch > 0x10FFFFUL) |
183 | if (uch > 0x10FFFFUL) |
183 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
184 | croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); |
184 | |
185 | |
185 | if (uch < 0x80 || enc->flags & F_ASCII) |
186 | if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF)) |
186 | { |
187 | { |
187 | if (uch > 0xFFFFUL) |
188 | if (uch > 0xFFFFUL) |
188 | { |
189 | { |
189 | need (enc, len += 11); |
190 | need (enc, len += 11); |
190 | sprintf (enc->cur, "\\u%04x\\u%04x", |
191 | sprintf (enc->cur, "\\u%04x\\u%04x", |
… | |
… | |
204 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
205 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
205 | } |
206 | } |
206 | |
207 | |
207 | str += clen; |
208 | str += clen; |
208 | } |
209 | } |
|
|
210 | else if (enc->flags & F_LATIN1) |
|
|
211 | { |
|
|
212 | *enc->cur++ = uch; |
|
|
213 | str += clen; |
|
|
214 | } |
209 | else if (is_utf8) |
215 | else if (is_utf8) |
210 | { |
216 | { |
211 | need (enc, len += clen); |
217 | need (enc, len += clen); |
212 | do |
218 | do |
213 | { |
219 | { |
… | |
… | |
514 | enc.maxdepth = DEC_DEPTH (flags); |
520 | enc.maxdepth = DEC_DEPTH (flags); |
515 | |
521 | |
516 | SvPOK_only (enc.sv); |
522 | SvPOK_only (enc.sv); |
517 | encode_sv (&enc, scalar); |
523 | encode_sv (&enc, scalar); |
518 | |
524 | |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
|
|
520 | SvUTF8_on (enc.sv); |
|
|
521 | |
|
|
522 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
525 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
523 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
526 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
|
|
527 | |
|
|
528 | if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
|
|
529 | SvUTF8_on (enc.sv); |
524 | |
530 | |
525 | if (enc.flags & F_SHRINK) |
531 | if (enc.flags & F_SHRINK) |
526 | shrink (enc.sv); |
532 | shrink (enc.sv); |
527 | |
533 | |
528 | return enc.sv; |
534 | return enc.sv; |
… | |
… | |
1064 | RETVAL |
1070 | RETVAL |
1065 | |
1071 | |
1066 | SV *ascii (SV *self, int enable = 1) |
1072 | SV *ascii (SV *self, int enable = 1) |
1067 | ALIAS: |
1073 | ALIAS: |
1068 | ascii = F_ASCII |
1074 | ascii = F_ASCII |
|
|
1075 | latin1 = F_LATIN1 |
1069 | utf8 = F_UTF8 |
1076 | utf8 = F_UTF8 |
1070 | indent = F_INDENT |
1077 | indent = F_INDENT |
1071 | canonical = F_CANONICAL |
1078 | canonical = F_CANONICAL |
1072 | space_before = F_SPACE_BEFORE |
1079 | space_before = F_SPACE_BEFORE |
1073 | space_after = F_SPACE_AFTER |
1080 | space_after = F_SPACE_AFTER |