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.24 by root, Tue Apr 3 23:59:04 2007 UTC vs.
Revision 1.26 by root, Fri Apr 6 21:17:09 2007 UTC

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"
9
10#if defined(__BORLANDC__) || defined(_MSC_VER)
11# define snprintf _snprintf // C compilers have this in stdio.h
12#endif
8 13
9#define F_ASCII 0x00000001UL 14#define F_ASCII 0x00000001UL
10#define F_UTF8 0x00000002UL 15#define F_UTF8 0x00000002UL
11#define F_INDENT 0x00000004UL 16#define F_INDENT 0x00000004UL
12#define F_CANONICAL 0x00000008UL 17#define F_CANONICAL 0x00000008UL
408 413
409 encode_nl (enc); 414 encode_nl (enc);
410 } 415 }
411 else 416 else
412 { 417 {
413 SV *sv;
414 HE *he = hv_iternext (hv); 418 HE *he = hv_iternext (hv);
415 419
416 for (;;) 420 for (;;)
417 { 421 {
418 encode_indent (enc); 422 encode_indent (enc);
433 437
434// encode objects, arrays and special \0=false and \1=true values. 438// encode objects, arrays and special \0=false and \1=true values.
435static void 439static void
436encode_rv (enc_t *enc, SV *sv) 440encode_rv (enc_t *enc, SV *sv)
437{ 441{
442 svtype svt;
443
438 SvGETMAGIC (sv); 444 SvGETMAGIC (sv);
439
440 svtype svt = SvTYPE (sv); 445 svt = SvTYPE (sv);
441 446
442 if (svt == SVt_PVHV) 447 if (svt == SVt_PVHV)
443 encode_hv (enc, (HV *)sv); 448 encode_hv (enc, (HV *)sv);
444 else if (svt == SVt_PVAV) 449 else if (svt == SVt_PVAV)
445 encode_av (enc, (AV *)sv); 450 encode_av (enc, (AV *)sv);
495} 500}
496 501
497static SV * 502static SV *
498encode_json (SV *scalar, U32 flags) 503encode_json (SV *scalar, U32 flags)
499{ 504{
505 enc_t enc;
506
500 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 507 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
501 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 508 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
502 509
503 enc_t enc;
504 enc.flags = flags; 510 enc.flags = flags;
505 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 511 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
506 enc.cur = SvPVX (enc.sv); 512 enc.cur = SvPVX (enc.sv);
507 enc.end = SvEND (enc.sv); 513 enc.end = SvEND (enc.sv);
508 enc.indent = 0; 514 enc.indent = 0;
670 } 676 }
671 else if (ch >= 0x20 && ch <= 0x7f) 677 else if (ch >= 0x20 && ch <= 0x7f)
672 *cur++ = ch; 678 *cur++ = ch;
673 else if (ch >= 0x80) 679 else if (ch >= 0x80)
674 { 680 {
681 STRLEN clen;
682 UV uch;
683
675 --dec->cur; 684 --dec->cur;
676 685
677 STRLEN clen;
678 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 686 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
679 if (clen == (STRLEN)-1) 687 if (clen == (STRLEN)-1)
680 ERR ("malformed UTF-8 character in JSON string"); 688 ERR ("malformed UTF-8 character in JSON string");
681 689
682 do 690 do
683 *cur++ = *dec->cur++; 691 *cur++ = *dec->cur++;
695 ERR ("invalid character encountered while parsing JSON string"); 703 ERR ("invalid character encountered while parsing JSON string");
696 } 704 }
697 } 705 }
698 while (cur < buf + SHORT_STRING_LEN); 706 while (cur < buf + SHORT_STRING_LEN);
699 707
708 {
700 STRLEN len = cur - buf; 709 STRLEN len = cur - buf;
701 710
702 if (sv) 711 if (sv)
703 { 712 {
704 SvGROW (sv, SvCUR (sv) + len + 1); 713 SvGROW (sv, SvCUR (sv) + len + 1);
705 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 714 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
706 SvCUR_set (sv, SvCUR (sv) + len); 715 SvCUR_set (sv, SvCUR (sv) + len);
707 } 716 }
708 else 717 else
709 sv = newSVpvn (buf, len); 718 sv = newSVpvn (buf, len);
719 }
710 } 720 }
711 while (*dec->cur != '"'); 721 while (*dec->cur != '"');
712 722
713 ++dec->cur; 723 ++dec->cur;
714 724
969} 979}
970 980
971static SV * 981static SV *
972decode_json (SV *string, U32 flags) 982decode_json (SV *string, U32 flags)
973{ 983{
984 dec_t dec;
974 SV *sv; 985 SV *sv;
975 986
976 SvUPGRADE (string, SVt_PV); 987 SvUPGRADE (string, SVt_PV);
977 988
978 if (flags & F_UTF8) 989 if (flags & F_UTF8)
980 else 991 else
981 sv_utf8_upgrade (string); 992 sv_utf8_upgrade (string);
982 993
983 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 994 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
984 995
985 dec_t dec;
986 dec.flags = flags; 996 dec.flags = flags;
987 dec.cur = SvPVX (string); 997 dec.cur = SvPVX (string);
988 dec.end = SvEND (string); 998 dec.end = SvEND (string);
989 dec.err = 0; 999 dec.err = 0;
990 dec.depth = 0; 1000 dec.depth = 0;
1074 RETVAL = newSVsv (self); 1084 RETVAL = newSVsv (self);
1075} 1085}
1076 OUTPUT: 1086 OUTPUT:
1077 RETVAL 1087 RETVAL
1078 1088
1079SV *max_depth (SV *self, int max_depth = 0x80000000UL) 1089SV *max_depth (SV *self, UV max_depth = 0x80000000UL)
1080 CODE: 1090 CODE:
1081{ 1091{
1082 UV *uv = SvJSON (self); 1092 UV *uv = SvJSON (self);
1083 UV log2 = 0; 1093 UV log2 = 0;
1084 1094

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines