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.55 by root, Mon Jul 23 22:57:40 2007 UTC vs.
Revision 1.57 by root, Mon Aug 13 16:05:42 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
56# define inline static 57# define inline static
57#endif 58#endif
58 59
59#define expect_false(expr) expect ((expr) != 0, 0) 60#define expect_false(expr) expect ((expr) != 0, 0)
60#define expect_true(expr) expect ((expr) != 0, 1) 61#define expect_true(expr) expect ((expr) != 0, 1)
62
63#ifdef USE_ITHREADS
64# define JSON_SLOW 1
65#else
66# define JSON_SLOW 1
67#endif
61 68
62static HV *json_stash, *json_boolean_stash; // JSON::XS:: 69static HV *json_stash, *json_boolean_stash; // JSON::XS::
63static SV *json_true, *json_false; 70static SV *json_true, *json_false;
64 71
65typedef struct { 72typedef struct {
477 SvGETMAGIC (sv); 484 SvGETMAGIC (sv);
478 svt = SvTYPE (sv); 485 svt = SvTYPE (sv);
479 486
480 if (expect_false (SvOBJECT (sv))) 487 if (expect_false (SvOBJECT (sv)))
481 { 488 {
489 HV *stash = !JSON_SLOW || json_boolean_stash
490 ? json_boolean_stash
491 : gv_stashpv ("JSON::XS::Boolean", 1);
492
482 if (SvSTASH (sv) == json_boolean_stash) 493 if (SvSTASH (sv) == stash)
483 { 494 {
484 if (SvIV (sv)) 495 if (SvIV (sv))
485 encode_str (enc, "true", 4, 0); 496 encode_str (enc, "true", 4, 0);
486 else 497 else
487 encode_str (enc, "false", 5, 0); 498 encode_str (enc, "false", 5, 0);
937 is_nv = 1; 948 is_nv = 1;
938 } 949 }
939 950
940 if (!is_nv) 951 if (!is_nv)
941 { 952 {
953 int len = dec->cur - start;
954
942 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 955 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
943 if (*start == '-') 956 if (*start == '-')
944 switch (dec->cur - start) 957 switch (len)
945 { 958 {
946 case 2: return newSViv (-( start [1] - '0' * 1)); 959 case 2: return newSViv (-( start [1] - '0' * 1));
947 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 960 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)); 961 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)); 962 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
950 } 963 }
951 else 964 else
952 switch (dec->cur - start) 965 switch (len)
953 { 966 {
954 case 1: return newSViv ( start [0] - '0' * 1); 967 case 1: return newSViv ( start [0] - '0' * 1);
955 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 968 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); 969 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); 970 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
958 } 971 }
959 972
960 { 973 {
961 UV uv; 974 UV uv;
962 int numtype = grok_number (start, dec->cur - start, &uv); 975 int numtype = grok_number (start, len, &uv);
963 if (numtype & IS_NUMBER_IN_UV) 976 if (numtype & IS_NUMBER_IN_UV)
964 if (numtype & IS_NUMBER_NEG) 977 if (numtype & IS_NUMBER_NEG)
965 { 978 {
966 if (uv < (UV)IV_MIN) 979 if (uv < (UV)IV_MIN)
967 return newSViv (-(IV)uv); 980 return newSViv (-(IV)uv);
968 } 981 }
969 else 982 else
970 return newSVuv (uv); 983 return newSVuv (uv);
971
972 // here would likely be the place for bigint support
973 } 984 }
974 }
975 985
976 // if we ever support bigint or bigfloat, this is the place for bigfloat 986 len -= *start == '-' ? 1 : 0;
987
988 // does not fit into IV or UV, try NV
989 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
990 #if defined (LDBL_DIG)
991 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
992 #endif
993 )
994 // fits into NV without loss of precision
995 return newSVnv (Atof (start));
996
997 // everything else fails, convert it to a string
998 return newSVpvn (start, dec->cur - start);
999 }
1000
1001 // loss of precision here
977 return newSVnv (Atof (start)); 1002 return newSVnv (Atof (start));
978 1003
979fail: 1004fail:
980 return 0; 1005 return 0;
981} 1006}
1346 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1371 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1347} 1372}
1348 1373
1349PROTOTYPES: DISABLE 1374PROTOTYPES: DISABLE
1350 1375
1376void CLONE (...)
1377 CODE:
1378 json_stash = 0;
1379 json_boolean_stash = 0;
1380
1351void new (char *klass) 1381void new (char *klass)
1352 PPCODE: 1382 PPCODE:
1353{ 1383{
1384 HV *stash = !JSON_SLOW || json_stash
1385 ? json_stash
1386 : gv_stashpv ("JSON::XS", 1);
1354 SV *pv = NEWSV (0, sizeof (JSON)); 1387 SV *pv = NEWSV (0, sizeof (JSON));
1355 SvPOK_only (pv); 1388 SvPOK_only (pv);
1356 Zero (SvPVX (pv), 1, JSON); 1389 Zero (SvPVX (pv), 1, JSON);
1357 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1390 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1358 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1391 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash)));
1359} 1392}
1360 1393
1361void ascii (JSON *self, int enable = 1) 1394void ascii (JSON *self, int enable = 1)
1362 ALIAS: 1395 ALIAS:
1363 ascii = F_ASCII 1396 ascii = F_ASCII

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines