… | |
… | |
20 | |
20 | |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
21 | #define INIT_SIZE 32 // initial scalar size to be allocated |
22 | #define INDENT_STEP 3 // spaces per indentation level |
22 | #define INDENT_STEP 3 // spaces per indentation level |
23 | |
23 | |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
24 | #define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character |
25 | #define SHORT_STRING_LEN 256 // special-case strings of up to this size |
25 | #define SHORT_STRING_LEN 512 // special-case strings of up to this size |
26 | |
26 | |
27 | #define SB do { |
27 | #define SB do { |
28 | #define SE } while (0) |
28 | #define SE } while (0) |
29 | |
29 | |
30 | static HV *json_stash; // JSON::XS:: |
30 | static HV *json_stash; // JSON::XS:: |
… | |
… | |
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 |
|
|
63 | static UV |
|
|
64 | decode_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 | { |