ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/JSON-XS/XS.xs
(Generate patch)

Comparing JSON-XS/XS.xs (file contents):
Revision 1.54 by root, Tue Jul 10 16:22:31 2007 UTC vs.
Revision 1.56 by root, Thu Jul 26 11:33:35 2007 UTC

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
306 encode_ch (enc, '['); encode_nl (enc); 307 encode_ch (enc, '['); encode_nl (enc);
307 ++enc->indent; 308 ++enc->indent;
308 309
309 for (i = 0; i <= len; ++i) 310 for (i = 0; i <= len; ++i)
310 { 311 {
312 SV **svp = av_fetch (av, i, 0);
313
311 encode_indent (enc); 314 encode_indent (enc);
312 encode_sv (enc, *av_fetch (av, i, 0)); 315
316 if (svp)
317 encode_sv (enc, *svp);
318 else
319 encode_str (enc, "null", 4, 0);
313 320
314 if (i < len) 321 if (i < len)
315 encode_comma (enc); 322 encode_comma (enc);
316 } 323 }
317 324
931 is_nv = 1; 938 is_nv = 1;
932 } 939 }
933 940
934 if (!is_nv) 941 if (!is_nv)
935 { 942 {
943 int len = dec->cur - start;
944
936 // 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
937 if (*start == '-') 946 if (*start == '-')
938 switch (dec->cur - start) 947 switch (len)
939 { 948 {
940 case 2: return newSViv (-( start [1] - '0' * 1)); 949 case 2: return newSViv (-( start [1] - '0' * 1));
941 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 950 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11));
942 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));
943 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));
944 } 953 }
945 else 954 else
946 switch (dec->cur - start) 955 switch (len)
947 { 956 {
948 case 1: return newSViv ( start [0] - '0' * 1); 957 case 1: return newSViv ( start [0] - '0' * 1);
949 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 958 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11);
950 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);
951 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);
952 } 961 }
953 962
954 { 963 {
955 UV uv; 964 UV uv;
956 int numtype = grok_number (start, dec->cur - start, &uv); 965 int numtype = grok_number (start, len, &uv);
957 if (numtype & IS_NUMBER_IN_UV) 966 if (numtype & IS_NUMBER_IN_UV)
958 if (numtype & IS_NUMBER_NEG) 967 if (numtype & IS_NUMBER_NEG)
959 { 968 {
960 if (uv < (UV)IV_MIN) 969 if (uv < (UV)IV_MIN)
961 return newSViv (-(IV)uv); 970 return newSViv (-(IV)uv);
962 } 971 }
963 else 972 else
964 return newSVuv (uv); 973 return newSVuv (uv);
965
966 // here would likely be the place for bigint support
967 } 974 }
968 }
969 975
970 // 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
971 return newSVnv (Atof (start)); 992 return newSVnv (Atof (start));
972 993
973fail: 994fail:
974 return 0; 995 return 0;
975} 996}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines