… | |
… | |
3 | #include "XSUB.h" |
3 | #include "XSUB.h" |
4 | |
4 | |
5 | #include "assert.h" |
5 | #include "assert.h" |
6 | #include "string.h" |
6 | #include "string.h" |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
|
|
8 | #include "stdio.h" |
|
|
9 | |
|
|
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
|
|
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
|
|
12 | #endif |
8 | |
13 | |
9 | #define F_ASCII 0x00000001UL |
14 | #define F_ASCII 0x00000001UL |
10 | #define F_UTF8 0x00000002UL |
15 | #define F_UTF8 0x00000002UL |
11 | #define F_INDENT 0x00000004UL |
16 | #define F_INDENT 0x00000004UL |
12 | #define F_CANONICAL 0x00000008UL |
17 | #define F_CANONICAL 0x00000008UL |
… | |
… | |
26 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
31 | #define F_DEFAULT (9UL << S_MAXDEPTH) |
27 | |
32 | |
28 | #define INIT_SIZE 32 // initial scalar size to be allocated |
33 | #define INIT_SIZE 32 // initial scalar size to be allocated |
29 | #define INDENT_STEP 3 // spaces per indentation level |
34 | #define INDENT_STEP 3 // spaces per indentation level |
30 | |
35 | |
31 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
|
|
32 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
36 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
33 | |
37 | |
34 | #define SB do { |
38 | #define SB do { |
35 | #define SE } while (0) |
39 | #define SE } while (0) |
36 | |
40 | |
… | |
… | |
104 | if (enc->cur + len >= enc->end) |
108 | if (enc->cur + len >= enc->end) |
105 | { |
109 | { |
106 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
110 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
107 | SvGROW (enc->sv, cur + len + 1); |
111 | SvGROW (enc->sv, cur + len + 1); |
108 | enc->cur = SvPVX (enc->sv) + cur; |
112 | enc->cur = SvPVX (enc->sv) + cur; |
109 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); |
113 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
110 | } |
114 | } |
111 | } |
115 | } |
112 | |
116 | |
113 | static void |
117 | static void |
114 | encode_ch (enc_t *enc, char ch) |
118 | encode_ch (enc_t *enc, char ch) |
… | |
… | |
211 | } |
215 | } |
212 | while (--clen); |
216 | while (--clen); |
213 | } |
217 | } |
214 | else |
218 | else |
215 | { |
219 | { |
216 | need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed |
220 | need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed |
217 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
221 | enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); |
218 | ++str; |
222 | ++str; |
219 | } |
223 | } |
220 | } |
224 | } |
221 | } |
225 | } |
… | |
… | |
514 | |
518 | |
515 | if (!(flags & (F_ASCII | F_UTF8))) |
519 | if (!(flags & (F_ASCII | F_UTF8))) |
516 | SvUTF8_on (enc.sv); |
520 | SvUTF8_on (enc.sv); |
517 | |
521 | |
518 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
522 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
|
|
523 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
519 | |
524 | |
520 | if (enc.flags & F_SHRINK) |
525 | if (enc.flags & F_SHRINK) |
521 | shrink (enc.sv); |
526 | shrink (enc.sv); |
522 | |
527 | |
523 | return enc.sv; |
528 | return enc.sv; |
… | |
… | |
595 | SV *sv = 0; |
600 | SV *sv = 0; |
596 | int utf8 = 0; |
601 | int utf8 = 0; |
597 | |
602 | |
598 | do |
603 | do |
599 | { |
604 | { |
600 | char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; |
605 | char buf [SHORT_STRING_LEN + UTF8_MAXBYTES]; |
601 | char *cur = buf; |
606 | char *cur = buf; |
602 | |
607 | |
603 | do |
608 | do |
604 | { |
609 | { |
605 | unsigned char ch = *(unsigned char *)dec->cur++; |
610 | unsigned char ch = *(unsigned char *)dec->cur++; |
… | |
… | |
977 | decode_json (SV *string, U32 flags) |
982 | decode_json (SV *string, U32 flags) |
978 | { |
983 | { |
979 | dec_t dec; |
984 | dec_t dec; |
980 | SV *sv; |
985 | SV *sv; |
981 | |
986 | |
|
|
987 | SvGETMAGIC (string); |
982 | SvUPGRADE (string, SVt_PV); |
988 | SvUPGRADE (string, SVt_PV); |
983 | |
989 | |
984 | if (flags & F_UTF8) |
990 | if (flags & F_UTF8) |
985 | sv_utf8_downgrade (string, 0); |
991 | sv_utf8_downgrade (string, 0); |
986 | else |
992 | else |