… | |
… | |
178 | STRLEN clen; |
178 | STRLEN clen; |
179 | UV uch; |
179 | UV uch; |
180 | |
180 | |
181 | if (is_utf8) |
181 | if (is_utf8) |
182 | { |
182 | { |
183 | //uch = utf8n_to_uvuni (str, end - str, &clen, UTF8_CHECK_ONLY); |
|
|
184 | uch = decode_utf8 (str, end - str, &clen); |
183 | uch = decode_utf8 (str, end - str, &clen); |
185 | if (clen == (STRLEN)-1) |
184 | if (clen == (STRLEN)-1) |
186 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
185 | croak ("malformed or illegal unicode character in string [%.11s], cannot convert to JSON", str); |
187 | } |
186 | } |
188 | else |
187 | else |
… | |
… | |
497 | encode_str (enc, str, len, SvUTF8 (sv)); |
496 | encode_str (enc, str, len, SvUTF8 (sv)); |
498 | encode_ch (enc, '"'); |
497 | encode_ch (enc, '"'); |
499 | } |
498 | } |
500 | else if (SvNOKp (sv)) |
499 | else if (SvNOKp (sv)) |
501 | { |
500 | { |
|
|
501 | // trust that perl will do the right thing w.r.t. JSON syntax. |
502 | need (enc, NV_DIG + 32); |
502 | need (enc, NV_DIG + 32); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
503 | Gconvert (SvNVX (sv), NV_DIG, 0, enc->cur); |
504 | enc->cur += strlen (enc->cur); |
504 | enc->cur += strlen (enc->cur); |
505 | } |
505 | } |
506 | else if (SvIOKp (sv)) |
506 | else if (SvIOKp (sv)) |
507 | { |
507 | { |
508 | // we assume we can always read an IV as a UV |
508 | // we assume we can always read an IV as a UV |
509 | if (SvUV (sv) & ~(UV)0x7fff) |
509 | if (SvUV (sv) & ~(UV)0x7fff) |
510 | { |
510 | { |
|
|
511 | // large integer, use the (rather slow) snprintf way. |
511 | need (enc, sizeof (UV) * 3); |
512 | need (enc, sizeof (UV) * 3); |
512 | enc->cur += |
513 | enc->cur += |
513 | SvIsUV(sv) |
514 | SvIsUV(sv) |
514 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
515 | ? snprintf (enc->cur, sizeof (UV) * 3, "%"UVuf, (UV)SvUVX (sv)) |
515 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
516 | : snprintf (enc->cur, sizeof (UV) * 3, "%"IVdf, (IV)SvIVX (sv)); |
… | |
… | |
518 | { |
519 | { |
519 | // optimise the "small number case" |
520 | // optimise the "small number case" |
520 | // code will likely be branchless and use only a single multiplication |
521 | // code will likely be branchless and use only a single multiplication |
521 | I32 i = SvIV (sv); |
522 | I32 i = SvIV (sv); |
522 | U32 u; |
523 | U32 u; |
|
|
524 | char digit, nz = 0; |
523 | |
525 | |
524 | need (enc, 6); |
526 | need (enc, 6); |
525 | |
527 | |
526 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
528 | *enc->cur = '-'; enc->cur += i < 0 ? 1 : 0; |
527 | u = i < 0 ? -i : i; |
529 | u = i < 0 ? -i : i; |
528 | |
530 | |
529 | // convert to 4.28 fixed-point representation |
531 | // convert to 4.28 fixed-point representation |
530 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
532 | u = u * ((0xfffffff + 10000) / 10000); // 10**5, 5 fractional digits |
531 | |
533 | |
532 | char digit, nz = 0; |
534 | // now output digit by digit, each time masking out the integer part |
533 | |
535 | // and multiplying by 5 while moving the decimal point one to the right, |
|
|
536 | // resulting in a net multiplication by 10. |
|
|
537 | // we always write the digit to memory but conditionally increment |
|
|
538 | // 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; |
539 | 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; |
540 | 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; |
541 | 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; |
542 | 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; |
543 | digit = u >> 24; *enc->cur = digit + '0'; enc->cur += 1; // correctly generate '0' |
539 | } |
544 | } |
540 | } |
545 | } |
541 | else if (SvROK (sv)) |
546 | else if (SvROK (sv)) |
542 | encode_rv (enc, SvRV (sv)); |
547 | encode_rv (enc, SvRV (sv)); |
543 | else if (!SvOK (sv)) |
548 | else if (!SvOK (sv)) |
… | |
… | |
625 | decode_4hex (dec_t *dec) |
630 | decode_4hex (dec_t *dec) |
626 | { |
631 | { |
627 | signed char d1, d2, d3, d4; |
632 | signed char d1, d2, d3, d4; |
628 | unsigned char *cur = (unsigned char *)dec->cur; |
633 | unsigned char *cur = (unsigned char *)dec->cur; |
629 | |
634 | |
630 | d1 = decode_hexdigit [cur [0]]; if (expect_false (d1 < 0)) ERR ("four hexadecimal digits expected"); |
635 | 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"); |
636 | 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"); |
637 | 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"); |
638 | d4 = decode_hexdigit [cur [3]]; if (expect_false (d4 < 0)) ERR ("exactly four hexadecimal digits expected"); |
634 | |
639 | |
635 | dec->cur += 4; |
640 | dec->cur += 4; |
636 | |
641 | |
637 | return ((UV)d1) << 12 |
642 | return ((UV)d1) << 12 |
638 | | ((UV)d2) << 8 |
643 | | ((UV)d2) << 8 |
… | |
… | |
861 | { |
866 | { |
862 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
867 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
863 | if (*start == '-') |
868 | if (*start == '-') |
864 | switch (dec->cur - start) |
869 | switch (dec->cur - start) |
865 | { |
870 | { |
866 | case 2: return newSViv (-( start [1] - '0' )); |
871 | case 2: return newSViv (-( start [1] - '0' * 1)); |
867 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
872 | 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)); |
873 | 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)); |
874 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
870 | } |
875 | } |
871 | else |
876 | else |
872 | switch (dec->cur - start) |
877 | switch (dec->cur - start) |
873 | { |
878 | { |
874 | case 1: return newSViv ( start [0] - '0' ); |
879 | case 1: return newSViv ( start [0] - '0' * 1); |
875 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
880 | 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); |
881 | 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); |
882 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
878 | } |
883 | } |
879 | |
884 | |
… | |
… | |
886 | if (uv < (UV)IV_MIN) |
891 | if (uv < (UV)IV_MIN) |
887 | return newSViv (-(IV)uv); |
892 | return newSViv (-(IV)uv); |
888 | } |
893 | } |
889 | else |
894 | else |
890 | return newSVuv (uv); |
895 | return newSVuv (uv); |
|
|
896 | |
|
|
897 | // here would likely be the place for bigint support |
891 | } |
898 | } |
892 | } |
899 | } |
893 | |
900 | |
|
|
901 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
894 | return newSVnv (Atof (start)); |
902 | return newSVnv (Atof (start)); |
895 | |
903 | |
896 | fail: |
904 | fail: |
897 | return 0; |
905 | return 0; |
898 | } |
906 | } |
… | |
… | |
999 | |
1007 | |
1000 | static SV * |
1008 | static SV * |
1001 | decode_sv (dec_t *dec) |
1009 | decode_sv (dec_t *dec) |
1002 | { |
1010 | { |
1003 | decode_ws (dec); |
1011 | decode_ws (dec); |
|
|
1012 | |
|
|
1013 | // the beauty of JSON: you need exactly one character lookahead |
|
|
1014 | // to parse anything. |
1004 | switch (*dec->cur) |
1015 | switch (*dec->cur) |
1005 | { |
1016 | { |
1006 | case '"': ++dec->cur; return decode_str (dec); |
1017 | case '"': ++dec->cur; return decode_str (dec); |
1007 | case '[': ++dec->cur; return decode_av (dec); |
1018 | case '[': ++dec->cur; return decode_av (dec); |
1008 | case '{': ++dec->cur; return decode_hv (dec); |
1019 | case '{': ++dec->cur; return decode_hv (dec); |
… | |
… | |
1137 | MODULE = JSON::XS PACKAGE = JSON::XS |
1148 | MODULE = JSON::XS PACKAGE = JSON::XS |
1138 | |
1149 | |
1139 | BOOT: |
1150 | BOOT: |
1140 | { |
1151 | { |
1141 | int i; |
1152 | int i; |
1142 | |
|
|
1143 | memset (decode_hexdigit, 0xff, 256); |
|
|
1144 | |
1153 | |
1145 | for (i = 0; i < 256; ++i) |
1154 | for (i = 0; i < 256; ++i) |
1146 | decode_hexdigit [i] = |
1155 | decode_hexdigit [i] = |
1147 | i >= '0' && i <= '9' ? i - '0' |
1156 | i >= '0' && i <= '9' ? i - '0' |
1148 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |
1157 | : i >= 'a' && i <= 'f' ? i - 'a' + 10 |