… | |
… | |
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 |
… | |
… | |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
502 | encode_str (enc, str, len, SvUTF8 (sv)); |
498 | encode_ch (enc, '"'); |
503 | encode_ch (enc, '"'); |
499 | } |
504 | } |
500 | else if (SvNOKp (sv)) |
505 | else if (SvNOKp (sv)) |
501 | { |
506 | { |
|
|
507 | // trust that perl will do the right thing w.r.t. JSON syntax. |
502 | need (enc, NV_DIG + 32); |
508 | need (enc, NV_DIG + 32); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
509 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | enc->cur += strlen (enc->cur); |
510 | enc->cur += strlen (enc->cur); |
505 | } |
511 | } |
506 | else if (SvIOKp (sv)) |
512 | else if (SvIOKp (sv)) |
507 | { |
513 | { |
508 | // we assume we can always read an IV as a UV |
514 | // we assume we can always read an IV as a UV |
509 | if (SvUV (sv) & ~(UV)0x7fff) |
515 | if (SvUV (sv) & ~(UV)0x7fff) |
510 | { |
516 | { |
|
|
517 | // large integer, use the (rather slow) snprintf way. |
511 | need (enc, sizeof (UV) * 3); |
518 | need (enc, sizeof (UV) * 3); |
512 | enc->cur += |
519 | enc->cur += |
513 | SvIsUV(sv) |
520 | SvIsUV(sv) |
514 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
521 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
515 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
522 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
… | |
… | |
518 | { |
525 | { |
519 | // optimise the "small number case" |
526 | // optimise the "small number case" |
520 | // code will likely be branchless and use only a single multiplication |
527 | // code will likely be branchless and use only a single multiplication |
521 | I32 i = SvIV (sv); |
528 | I32 i = SvIV (sv); |
522 | U32 u; |
529 | U32 u; |
|
|
530 | char digit, nz = 0; |
523 | |
531 | |
524 | need (enc, 6); |
532 | need (enc, 6); |
525 | |
533 | |
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
534 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
527 | u = i < 0 ? -i : i; |
535 | u = i < 0 ? -i : i; |
528 | |
536 | |
529 | // convert to 4.28 fixed-point representation |
537 | // convert to 4.28 fixed-point representation |
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
538 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
531 | |
539 | |
532 | char digit, nz = 0; |
540 | // now output digit by digit, each time masking out the integer part |
533 | |
541 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
542 | // resulting in a net multiplication by 10. |
|
|
543 | // we always write the digit to memory but conditionally increment |
|
|
544 | // the pointer, to ease the usage of conditional move instructions. |
534 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
545 | digit = u >> 28; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0xfffffff) * 5; |
535 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
546 | digit = u >> 27; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x7ffffff) * 5; |
536 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
547 | digit = u >> 26; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x3ffffff) * 5; |
537 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
548 | digit = u >> 25; *enc->cur = digit + '0'; enc->cur += (nz = nz || digit); u = (u & 0x1ffffff) * 5; |
538 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; |
549 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
539 | } |
550 | } |
540 | } |
551 | } |
541 | else if (SvROK (sv)) |
552 | else if (SvROK (sv)) |
542 | encode_rv (enc, SvRV (sv)); |
553 | encode_rv (enc, SvRV (sv)); |
543 | else if (!SvOK (sv)) |
554 | else if (!SvOK (sv)) |
… | |
… | |
625 | decode_4hex (dec_t *dec) |
636 | decode_4hex (dec_t *dec) |
626 | { |
637 | { |
627 | signed char d1, d2, d3, d4; |
638 | signed char d1, d2, d3, d4; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
639 | unsigned char *cur = (unsigned char *)dec->cur; |
629 | |
640 | |
630 | 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"); |
631 | 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"); |
632 | 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"); |
633 | 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"); |
634 | |
645 | |
635 | dec->cur += 4; |
646 | dec->cur += 4; |
636 | |
647 | |
637 | return ((UV)d1) << 12 |
648 | return ((UV)d1) << 12 |
638 | | ((UV)d2) << 8 |
649 | | ((UV)d2) << 8 |
… | |
… | |
861 | { |
872 | { |
862 | // 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 |
863 | if (*start == '-') |
874 | if (*start == '-') |
864 | switch (dec->cur - start) |
875 | switch (dec->cur - start) |
865 | { |
876 | { |
866 | case 2: return newSViv (-( start [1] - '0' )); |
877 | case 2: return newSViv (-( start [1] - '0' * 1)); |
867 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
878 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
868 | 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)); |
869 | 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)); |
870 | } |
881 | } |
871 | else |
882 | else |
872 | switch (dec->cur - start) |
883 | switch (dec->cur - start) |
873 | { |
884 | { |
874 | case 1: return newSViv ( start [0] - '0' ); |
885 | case 1: return newSViv ( start [0] - '0' * 1); |
875 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
886 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
876 | 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); |
877 | 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); |
878 | } |
889 | } |
879 | |
890 | |
… | |
… | |
886 | if (uv < (UV)IV_MIN) |
897 | if (uv < (UV)IV_MIN) |
887 | return newSViv (-(IV)uv); |
898 | return newSViv (-(IV)uv); |
888 | } |
899 | } |
889 | else |
900 | else |
890 | return newSVuv (uv); |
901 | return newSVuv (uv); |
|
|
902 | |
|
|
903 | // here would likely be the place for bigint support |
891 | } |
904 | } |
892 | } |
905 | } |
893 | |
906 | |
|
|
907 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
894 | return newSVnv (Atof (start)); |
908 | return newSVnv (Atof (start)); |
895 | |
909 | |
896 | fail: |
910 | fail: |
897 | return 0; |
911 | return 0; |
898 | } |
912 | } |
… | |
… | |
999 | |
1013 | |
1000 | static SV * |
1014 | static SV * |
1001 | decode_sv (dec_t *dec) |
1015 | decode_sv (dec_t *dec) |
1002 | { |
1016 | { |
1003 | decode_ws (dec); |
1017 | decode_ws (dec); |
|
|
1018 | |
|
|
1019 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1020 | // to parse anything. |
1004 | switch (*dec->cur) |
1021 | switch (*dec->cur) |
1005 | { |
1022 | { |
1006 | case '"': ++dec->cur; return decode_str (dec); |
1023 | case '"': ++dec->cur; return decode_str (dec); |
1007 | case '[': ++dec->cur; return decode_av (dec); |
1024 | case '[': ++dec->cur; return decode_av (dec); |
1008 | case '{': ++dec->cur; return decode_hv (dec); |
1025 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1137 | MODULE = JSON::XS PACKAGE = JSON::XS |
1154 | MODULE = JSON::XS PACKAGE = JSON::XS |
1138 | |
1155 | |
1139 | BOOT: |
1156 | BOOT: |
1140 | { |
1157 | { |
1141 | int i; |
1158 | int i; |
1142 | |
|
|
1143 | memset (decode_hexdigit, 0xff, 256); |
|
|
1144 | |
1159 | |
1145 | for (i = 0; i < 256; ++i) |
1160 | for (i = 0; i < 256; ++i) |
1146 | decode_hexdigit [i] = |
1161 | decode_hexdigit [i] = |
1147 | i >= '0' && i <= '9' ? i - '0' |
1162 | i >= '0' && i <= '9' ? i - '0' |
1148 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1163 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |