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.62 by root, Sun Aug 26 22:27:32 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 {
326 --enc->indent; 335 --enc->indent;
327 encode_indent (enc); encode_ch (enc, ']'); 336 encode_indent (enc); encode_ch (enc, ']');
328} 337}
329 338
330static void 339static void
331encode_he (enc_t *enc, HE *he) 340encode_hk (enc_t *enc, HE *he)
332{ 341{
333 encode_ch (enc, '"'); 342 encode_ch (enc, '"');
334 343
335 if (HeKLEN (he) == HEf_SVKEY) 344 if (HeKLEN (he) == HEf_SVKEY)
336 { 345 {
349 encode_ch (enc, '"'); 358 encode_ch (enc, '"');
350 359
351 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 360 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
352 encode_ch (enc, ':'); 361 encode_ch (enc, ':');
353 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 362 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
354 encode_sv (enc, HeVAL (he));
355} 363}
356 364
357// compare hash entries, used when all keys are bytestrings 365// compare hash entries, used when all keys are bytestrings
358static int 366static int
359he_cmp_fast (const void *a_, const void *b_) 367he_cmp_fast (const void *a_, const void *b_)
364 HE *b = *(HE **)b_; 372 HE *b = *(HE **)b_;
365 373
366 STRLEN la = HeKLEN (a); 374 STRLEN la = HeKLEN (a);
367 STRLEN lb = HeKLEN (b); 375 STRLEN lb = HeKLEN (b);
368 376
369 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 377 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
370 cmp = la - lb; 378 cmp = lb - la;
371 379
372 return cmp; 380 return cmp;
373} 381}
374 382
375// compare hash entries, used when some keys are sv's or utf-x 383// compare hash entries, used when some keys are sv's or utf-x
376static int 384static int
377he_cmp_slow (const void *a, const void *b) 385he_cmp_slow (const void *a, const void *b)
378{ 386{
379 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 387 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
380} 388}
381 389
382static void 390static void
383encode_hv (enc_t *enc, HV *hv) 391encode_hv (enc_t *enc, HV *hv)
384{ 392{
393 HE *he;
385 int count, i; 394 int count;
386 395
387 if (enc->indent >= enc->maxdepth) 396 if (enc->indent >= enc->maxdepth)
388 croak ("data structure too deep (hit recursion limit)"); 397 croak ("data structure too deep (hit recursion limit)");
389 398
390 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent;
391 400
392 if ((count = hv_iterinit (hv)))
393 {
394 // for canonical output we have to sort by keys first 401 // for canonical output we have to sort by keys first
395 // actually, this is mostly due to the stupid so-called 402 // actually, this is mostly due to the stupid so-called
396 // security workaround added somewhere in 5.8.x. 403 // security workaround added somewhere in 5.8.x.
397 // that randomises hash orderings 404 // that randomises hash orderings
398 if (enc->json.flags & F_CANONICAL) 405 if (enc->json.flags & F_CANONICAL)
406 {
407 int count = hv_iterinit (hv);
408
409 if (SvMAGICAL (hv))
399 { 410 {
411 // need to count by iterating. could improve by dynamically building the vector below
412 // but I don't care for the speed of this special case.
413 // note also that we will run into undefined behaviour when the two iterations
414 // do not result in the same count, something I might care for in some later release.
415
416 count = 0;
417 while (hv_iternext (hv))
418 ++count;
419
420 hv_iterinit (hv);
421 }
422
423 if (count)
424 {
400 int fast = 1; 425 int i, fast = 1;
401 HE *he;
402#if defined(__BORLANDC__) || defined(_MSC_VER) 426#if defined(__BORLANDC__) || defined(_MSC_VER)
403 HE **hes = _alloca (count * sizeof (HE)); 427 HE **hes = _alloca (count * sizeof (HE));
404#else 428#else
405 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 429 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
406#endif 430#endif
433 457
434 FREETMPS; 458 FREETMPS;
435 LEAVE; 459 LEAVE;
436 } 460 }
437 461
438 for (i = 0; i < count; ++i) 462 while (count--)
439 { 463 {
440 encode_indent (enc); 464 encode_indent (enc);
465 he = hes [count];
441 encode_he (enc, hes [i]); 466 encode_hk (enc, he);
467 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
442 468
443 if (i < count - 1) 469 if (count)
444 encode_comma (enc); 470 encode_comma (enc);
445 } 471 }
446
447 encode_nl (enc);
448 } 472 }
473 }
449 else 474 else
450 { 475 {
476 if (hv_iterinit (hv) || SvMAGICAL (hv))
451 HE *he = hv_iternext (hv); 477 if ((he = hv_iternext (hv)))
452
453 for (;;) 478 for (;;)
454 { 479 {
455 encode_indent (enc); 480 encode_indent (enc);
456 encode_he (enc, he); 481 encode_hk (enc, he);
482 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
457 483
458 if (!(he = hv_iternext (hv))) 484 if (!(he = hv_iternext (hv)))
459 break; 485 break;
460 486
461 encode_comma (enc); 487 encode_comma (enc);
462 } 488 }
489 }
463 490
464 encode_nl (enc); 491 encode_nl (enc);
465 }
466 }
467 492
468 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 493 --enc->indent; encode_indent (enc); encode_ch (enc, '}');
469} 494}
470 495
471// encode objects, arrays and special \0=false and \1=true values. 496// encode objects, arrays and special \0=false and \1=true values.
477 SvGETMAGIC (sv); 502 SvGETMAGIC (sv);
478 svt = SvTYPE (sv); 503 svt = SvTYPE (sv);
479 504
480 if (expect_false (SvOBJECT (sv))) 505 if (expect_false (SvOBJECT (sv)))
481 { 506 {
507 HV *stash = !JSON_SLOW || json_boolean_stash
508 ? json_boolean_stash
509 : gv_stashpv ("JSON::XS::Boolean", 1);
510
482 if (SvSTASH (sv) == json_boolean_stash) 511 if (SvSTASH (sv) == stash)
483 { 512 {
484 if (SvIV (sv)) 513 if (SvIV (sv))
485 encode_str (enc, "true", 4, 0); 514 encode_str (enc, "true", 4, 0);
486 else 515 else
487 encode_str (enc, "false", 5, 0); 516 encode_str (enc, "false", 5, 0);
499 // we re-bless the reference to get overload and other niceties right 528 // we re-bless the reference to get overload and other niceties right
500 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0); 529 GV *to_json = gv_fetchmethod_autoload (SvSTASH (sv), "TO_JSON", 0);
501 530
502 if (to_json) 531 if (to_json)
503 { 532 {
504 int count;
505 dSP; 533 dSP;
506 534
507 ENTER; SAVETMPS; PUSHMARK (SP); 535 ENTER; SAVETMPS; PUSHMARK (SP);
508 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv))); 536 XPUSHs (sv_bless (sv_2mortal (newRV_inc (sv)), SvSTASH (sv)));
509 537
937 is_nv = 1; 965 is_nv = 1;
938 } 966 }
939 967
940 if (!is_nv) 968 if (!is_nv)
941 { 969 {
970 int len = dec->cur - start;
971
942 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so 972 // special case the rather common 1..4-digit-int case, assumes 32 bit ints or so
943 if (*start == '-') 973 if (*start == '-')
944 switch (dec->cur - start) 974 switch (len)
945 { 975 {
946 case 2: return newSViv (-( start [1] - '0' * 1)); 976 case 2: return newSViv (-( start [1] - '0' * 1));
947 case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); 977 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)); 978 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)); 979 case 5: return newSViv (-(start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111));
950 } 980 }
951 else 981 else
952 switch (dec->cur - start) 982 switch (len)
953 { 983 {
954 case 1: return newSViv ( start [0] - '0' * 1); 984 case 1: return newSViv ( start [0] - '0' * 1);
955 case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); 985 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); 986 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); 987 case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111);
958 } 988 }
959 989
960 { 990 {
961 UV uv; 991 UV uv;
962 int numtype = grok_number (start, dec->cur - start, &uv); 992 int numtype = grok_number (start, len, &uv);
963 if (numtype & IS_NUMBER_IN_UV) 993 if (numtype & IS_NUMBER_IN_UV)
964 if (numtype & IS_NUMBER_NEG) 994 if (numtype & IS_NUMBER_NEG)
965 { 995 {
966 if (uv < (UV)IV_MIN) 996 if (uv < (UV)IV_MIN)
967 return newSViv (-(IV)uv); 997 return newSViv (-(IV)uv);
968 } 998 }
969 else 999 else
970 return newSVuv (uv); 1000 return newSVuv (uv);
971
972 // here would likely be the place for bigint support
973 } 1001 }
974 }
975 1002
976 // if we ever support bigint or bigfloat, this is the place for bigfloat 1003 len -= *start == '-' ? 1 : 0;
1004
1005 // does not fit into IV or UV, try NV
1006 if ((sizeof (NV) == sizeof (double) && DBL_DIG >= len)
1007 #if defined (LDBL_DIG)
1008 || (sizeof (NV) == sizeof (long double) && LDBL_DIG >= len)
1009 #endif
1010 )
1011 // fits into NV without loss of precision
1012 return newSVnv (Atof (start));
1013
1014 // everything else fails, convert it to a string
1015 return newSVpvn (start, dec->cur - start);
1016 }
1017
1018 // loss of precision here
977 return newSVnv (Atof (start)); 1019 return newSVnv (Atof (start));
978 1020
979fail: 1021fail:
980 return 0; 1022 return 0;
981} 1023}
1199 1241
1200 case 't': 1242 case 't':
1201 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1243 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1202 { 1244 {
1203 dec->cur += 4; 1245 dec->cur += 4;
1246#if JSON_SLOW
1247 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1248#endif
1204 return SvREFCNT_inc (json_true); 1249 return SvREFCNT_inc (json_true);
1205 } 1250 }
1206 else 1251 else
1207 ERR ("'true' expected"); 1252 ERR ("'true' expected");
1208 1253
1210 1255
1211 case 'f': 1256 case 'f':
1212 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1257 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1213 { 1258 {
1214 dec->cur += 5; 1259 dec->cur += 5;
1260#if JSON_SLOW
1261 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1262#endif
1215 return SvREFCNT_inc (json_false); 1263 return SvREFCNT_inc (json_false);
1216 } 1264 }
1217 else 1265 else
1218 ERR ("'false' expected"); 1266 ERR ("'false' expected");
1219 1267
1346 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false); 1394 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1347} 1395}
1348 1396
1349PROTOTYPES: DISABLE 1397PROTOTYPES: DISABLE
1350 1398
1399void CLONE (...)
1400 CODE:
1401 json_stash = 0;
1402 json_boolean_stash = 0;
1403
1351void new (char *klass) 1404void new (char *klass)
1352 PPCODE: 1405 PPCODE:
1353{ 1406{
1354 SV *pv = NEWSV (0, sizeof (JSON)); 1407 SV *pv = NEWSV (0, sizeof (JSON));
1355 SvPOK_only (pv); 1408 SvPOK_only (pv);
1356 Zero (SvPVX (pv), 1, JSON); 1409 Zero (SvPVX (pv), 1, JSON);
1357 ((JSON *)SvPVX (pv))->flags = F_DEFAULT; 1410 ((JSON *)SvPVX (pv))->flags = F_DEFAULT;
1358 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), json_stash))); 1411 XPUSHs (sv_2mortal (sv_bless (newRV_noinc (pv), JSON_STASH)));
1359} 1412}
1360 1413
1361void ascii (JSON *self, int enable = 1) 1414void ascii (JSON *self, int enable = 1)
1362 ALIAS: 1415 ALIAS:
1363 ascii = F_ASCII 1416 ascii = F_ASCII

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines