… | |
… | |
7 | #include "stdlib.h" |
7 | #include "stdlib.h" |
8 | #include "stdio.h" |
8 | #include "stdio.h" |
9 | |
9 | |
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 |
|
|
13 | |
|
|
14 | // some old perls do not have this, try to make it work, no |
|
|
15 | // guarentees, though. |
|
|
16 | #ifndef UTF8_MAXBYTES |
|
|
17 | # define UTF8_MAXBYTES 13 |
12 | #endif |
18 | #endif |
13 | |
19 | |
14 | #define F_ASCII 0x00000001UL |
20 | #define F_ASCII 0x00000001UL |
15 | #define F_LATIN1 0x00000002UL |
21 | #define F_LATIN1 0x00000002UL |
16 | #define F_UTF8 0x00000004UL |
22 | #define F_UTF8 0x00000004UL |
… | |
… | |
178 | STRLEN clen; |
184 | STRLEN clen; |
179 | UV uch; |
185 | UV uch; |
180 | |
186 | |
181 | if (is_utf8) |
187 | if (is_utf8) |
182 | { |
188 | { |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
189 | uch = decode_utf8 (str, end - str, &clen); |
185 | if (clen == (STRLEN)-1) |
190 | if (clen == (STRLEN)-1) |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
191 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
187 | } |
192 | } |
188 | else |
193 | else |
… | |
… | |
631 | decode_4hex (dec_t *dec) |
636 | decode_4hex (dec_t *dec) |
632 | { |
637 | { |
633 | signed char d1, d2, d3, d4; |
638 | signed char d1, d2, d3, d4; |
634 | unsigned char *cur = (unsigned char *)dec->cur; |
639 | unsigned char *cur = (unsigned char *)dec->cur; |
635 | |
640 | |
636 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
641 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("exactly four hexadecimal digits expected"); |
637 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("four hexadecimal digits expected"); |
642 | d2 = decode_hexdigit [cur [1]]; if (expect_false (d2 < 0)) ERR ("exactly four hexadecimal digits expected"); |
638 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("four hexadecimal digits expected"); |
643 | d3 = decode_hexdigit [cur [2]]; if (expect_false (d3 < 0)) ERR ("exactly four hexadecimal digits expected"); |
639 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("four hexadecimal digits expected"); |
644 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
640 | |
645 | |
641 | dec->cur += 4; |
646 | dec->cur += 4; |
642 | |
647 | |
643 | return ((UV)d1) << 12 |
648 | return ((UV)d1) << 12 |
644 | | ((UV)d2) << 8 |
649 | | ((UV)d2) << 8 |
… | |
… | |
867 | { |
872 | { |
868 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
873 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
869 | if (*start == '-') |
874 | if (*start == '-') |
870 | switch (dec->cur - start) |
875 | switch (dec->cur - start) |
871 | { |
876 | { |
872 | case 2: return newSViv (-( start [1] - '0' )); |
877 | case 2: return newSViv (-( start [1] - '0' * 1)); |
873 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
878 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
874 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
879 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
875 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
880 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
876 | } |
881 | } |
877 | else |
882 | else |
878 | switch (dec->cur - start) |
883 | switch (dec->cur - start) |
879 | { |
884 | { |
880 | case 1: return newSViv ( start [0] - '0' ); |
885 | case 1: return newSViv ( start [0] - '0' * 1); |
881 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
886 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
882 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
887 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
883 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
888 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
884 | } |
889 | } |
885 | |
890 | |
… | |
… | |
892 | if (uv < (UV)IV_MIN) |
897 | if (uv < (UV)IV_MIN) |
893 | return newSViv (-(IV)uv); |
898 | return newSViv (-(IV)uv); |
894 | } |
899 | } |
895 | else |
900 | else |
896 | return newSVuv (uv); |
901 | return newSVuv (uv); |
|
|
902 | |
|
|
903 | // here would likely be the place for bigint support |
897 | } |
904 | } |
898 | } |
905 | } |
899 | |
906 | |
|
|
907 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
900 | return newSVnv (Atof (start)); |
908 | return newSVnv (Atof (start)); |
901 | |
909 | |
902 | fail: |
910 | fail: |
903 | return 0; |
911 | return 0; |
904 | } |
912 | } |
… | |
… | |
1005 | |
1013 | |
1006 | static SV * |
1014 | static SV * |
1007 | decode_sv (dec_t *dec) |
1015 | decode_sv (dec_t *dec) |
1008 | { |
1016 | { |
1009 | decode_ws (dec); |
1017 | decode_ws (dec); |
|
|
1018 | |
|
|
1019 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1020 | // to parse anything. |
1010 | switch (*dec->cur) |
1021 | switch (*dec->cur) |
1011 | { |
1022 | { |
1012 | case '"': ++dec->cur; return decode_str (dec); |
1023 | case '"': ++dec->cur; return decode_str (dec); |
1013 | case '[': ++dec->cur; return decode_av (dec); |
1024 | case '[': ++dec->cur; return decode_av (dec); |
1014 | case '{': ++dec->cur; return decode_hv (dec); |
1025 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1143 | MODULE = JSON::XS PACKAGE = JSON::XS |
1154 | MODULE = JSON::XS PACKAGE = JSON::XS |
1144 | |
1155 | |
1145 | BOOT: |
1156 | BOOT: |
1146 | { |
1157 | { |
1147 | int i; |
1158 | int i; |
1148 | |
|
|
1149 | memset (decode_hexdigit, 0xff, 256); |
|
|
1150 | |
1159 | |
1151 | for (i = 0; i < 256; ++i) |
1160 | for (i = 0; i < 256; ++i) |
1152 | decode_hexdigit [i] = |
1161 | decode_hexdigit [i] = |
1153 | i >= '0' && i <= '9' ? i - '0' |
1162 | i >= '0' && i <= '9' ? i - '0' |
1154 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1163 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |