1 | #include "EXTERN.h" |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
2 | #include "perl.h" |
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" |
8 | #include <stdio.h> |
|
|
9 | #include <float.h> |
9 | |
10 | |
10 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
11 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | # define snprintf _snprintf // C compilers have this in stdio.h |
12 | #endif |
13 | #endif |
13 | |
14 | |
… | |
… | |
937 | is_nv = 1; |
938 | is_nv = 1; |
938 | } |
939 | } |
939 | |
940 | |
940 | if (!is_nv) |
941 | if (!is_nv) |
941 | { |
942 | { |
|
|
943 | int len = dec->cur - start; |
|
|
944 | |
942 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
945 | // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so |
943 | if (*start == '-') |
946 | if (*start == '-') |
944 | switch (dec->cur - start) |
947 | switch (len) |
945 | { |
948 | { |
946 | case 2: return newSViv (-( start [1] - '0' * 1)); |
949 | case 2: return newSViv (-( start [1] - '0' * 1)); |
947 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
950 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
948 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
951 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
949 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
952 | case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
950 | } |
953 | } |
951 | else |
954 | else |
952 | switch (dec->cur - start) |
955 | switch (len) |
953 | { |
956 | { |
954 | case 1: return newSViv ( start [0] - '0' * 1); |
957 | case 1: return newSViv ( start [0] - '0' * 1); |
955 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
958 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
956 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
959 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
957 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
960 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
958 | } |
961 | } |
959 | |
962 | |
960 | { |
963 | { |
961 | UV uv; |
964 | UV uv; |
962 | int numtype = grok_number (start, dec->cur - start, &uv); |
965 | int numtype = grok_number (start, len, &uv); |
963 | if (numtype & IS_NUMBER_IN_UV) |
966 | if (numtype & IS_NUMBER_IN_UV) |
964 | if (numtype & IS_NUMBER_NEG) |
967 | if (numtype & IS_NUMBER_NEG) |
965 | { |
968 | { |
966 | if (uv < (UV)IV_MIN) |
969 | if (uv < (UV)IV_MIN) |
967 | return newSViv (-(IV)uv); |
970 | return newSViv (-(IV)uv); |
968 | } |
971 | } |
969 | else |
972 | else |
970 | return newSVuv (uv); |
973 | return newSVuv (uv); |
971 | |
|
|
972 | // here would likely be the place for bigint support |
|
|
973 | } |
974 | } |
974 | } |
|
|
975 | |
975 | |
976 | // if we ever support bigint or bigfloat, this is the place for bigfloat |
976 | len -= *start == '-' ? 1 : 0; |
|
|
977 | |
|
|
978 | // does not fit into IV or UV, try NV |
|
|
979 | if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len) |
|
|
980 | #if defined (LDBL_DIG) |
|
|
981 | || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len) |
|
|
982 | #endif |
|
|
983 | ) |
|
|
984 | // fits into NV without loss of precision |
|
|
985 | return newSVnv (Atof (start)); |
|
|
986 | |
|
|
987 | // everything else fails, convert it to a string |
|
|
988 | return newSVpvn (start, dec->cur - start); |
|
|
989 | } |
|
|
990 | |
|
|
991 | // loss of precision here |
977 | return newSVnv (Atof (start)); |
992 | return newSVnv (Atof (start)); |
978 | |
993 | |
979 | fail: |
994 | fail: |
980 | return 0; |
995 | return 0; |
981 | } |
996 | } |