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.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 {
306 encode_ch (enc, '['); encode_nl (enc); 313 encode_ch (enc, '['); encode_nl (enc);
307 ++enc->indent; 314 ++enc->indent;
308 315
309 for (i = 0; i <= len; ++i) 316 for (i = 0; i <= len; ++i)
310 { 317 {
318 SV **svp = av_fetch (av, i, 0);
319
311 encode_indent (enc); 320 encode_indent (enc);
312 encode_sv (enc, *av_fetch (av, i, 0)); 321
322 if (svp)
323 encode_sv (enc, *svp);
324 else
325 encode_str (enc, "null", 4, 0);
313 326
314 if (i < len) 327 if (i < len)
315 encode_comma (enc); 328 encode_comma (enc);
316 } 329 }
317 330
471 SvGETMAGIC (sv); 484 SvGETMAGIC (sv);
472 svt = SvTYPE (sv); 485 svt = SvTYPE (sv);
473 486
474 if (expect_false (SvOBJECT (sv))) 487 if (expect_false (SvOBJECT (sv)))
475 { 488 {
489 HV *stash = !JSON_SLOW || json_boolean_stash
490 ? json_boolean_stash
491 : gv_stashpv ("JSON::XS::Boolean", 1);
492
476 if (SvSTASH (sv) == json_boolean_stash) 493 if (SvSTASH (sv) == stash)
477 { 494 {
478 if (SvIV (sv)) 495 if (SvIV (sv))
479 encode_str (enc, "true", 4, 0); 496 encode_str (enc, "true", 4, 0);
480 else 497 else
481 encode_str (enc, "false", 5, 0); 498 encode_str (enc, "false", 5, 0);
931 is_nv = 1; 948 is_nv = 1;
932 } 949 }
933 950
934 if (!is_nv) 951 if (!is_nv)
935 { 952 {
953 int len = dec->cur - start;
954
936 // 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
937 if (*start == '-') 956 if (*start == '-')
938 switch (dec->cur - start) 957 switch (len)
939 { 958 {
940 case 2: return newSViv (-( start [1] - '0' * 1)); 959 case 2: return newSViv (-( start [1] - '0' * 1));
941 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 960 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)); 961 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)); 962 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
944 } 963 }
945 else 964 else
946 switch (dec->cur - start) 965 switch (len)
947 { 966 {
948 case 1: return newSViv ( start [0] - '0' * 1); 967 case 1: return newSViv ( start [0] - '0' * 1);
949 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 968 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); 969 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); 970 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
952 } 971 }
953 972
954 { 973 {
955 UV uv; 974 UV uv;
956 int numtype = grok_number (start, dec->cur - start, &uv); 975 int numtype = grok_number (start, len, &uv);
957 if (numtype & IS_NUMBER_IN_UV) 976 if (numtype & IS_NUMBER_IN_UV)
958 if (numtype & IS_NUMBER_NEG) 977 if (numtype & IS_NUMBER_NEG)
959 { 978 {
960 if (uv < (UV)IV_MIN) 979 if (uv < (UV)IV_MIN)
961 return newSViv (-(IV)uv); 980 return newSViv (-(IV)uv);
962 } 981 }
963 else 982 else
964 return newSVuv (uv); 983 return newSVuv (uv);
965
966 // here would likely be the place for bigint support
967 } 984 }
968 }
969 985
970 // 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
971 return newSVnv (Atof (start)); 1002 return newSVnv (Atof (start));
972 1003
973fail: 1004fail:
974 return 0; 1005 return 0;
975} 1006}
1340 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);
1341} 1372}
1342 1373
1343PROTOTYPES: DISABLE 1374PROTOTYPES: DISABLE
1344 1375
1376void CLONE (...)
1377 CODE:
1378 json_stash = 0;
1379 json_boolean_stash = 0;
1380
1345void new (char *klass) 1381void new (char *klass)
1346 PPCODE: 1382 PPCODE:
1347{ 1383{
1384 HV *stash = !JSON_SLOW || json_stash
1385 ? json_stash
1386 : gv_stashpv ("JSON::XS", 1);
1348 SV *pv = NEWSV (0, sizeof (JSON)); 1387 SV *pv = NEWSV (0, sizeof (JSON));
1349 SvPOK_only (pv); 1388 SvPOK_only (pv);
1350 Zero (SvPVX (pv), 1, JSON); 1389 Zero (SvPVX (pv), 1, JSON);
1351 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1390 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1352 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1391 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), stash)));
1353} 1392}
1354 1393
1355void ascii (JSON *self, int enable = 1) 1394void ascii (JSON *self, int enable = 1)
1356 ALIAS: 1395 ALIAS:
1357 ascii = F_ASCII 1396 ascii = F_ASCII

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines