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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines