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.30 by root, Wed May 9 16:10:37 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
15#define F_LATIN1 0x00000002UL
10#define F_UTF8 0x00000002UL 16#define F_UTF8 0x00000004UL
11#define F_INDENT 0x00000004UL 17#define F_INDENT 0x00000008UL
12#define F_CANONICAL 0x00000008UL 18#define F_CANONICAL 0x00000010UL
13#define F_SPACE_BEFORE 0x00000010UL 19#define F_SPACE_BEFORE 0x00000020UL
14#define F_SPACE_AFTER 0x00000020UL 20#define F_SPACE_AFTER 0x00000040UL
15#define F_ALLOW_NONREF 0x00000080UL 21#define F_ALLOW_NONREF 0x00000100UL
16#define F_SHRINK 0x00000100UL 22#define F_SHRINK 0x00000200UL
17#define F_MAXDEPTH 0xf8000000UL 23#define F_MAXDEPTH 0xf8000000UL
18#define S_MAXDEPTH 27 24#define S_MAXDEPTH 27
19 25
20#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH)) 26#define DEC_DEPTH(flags) (1UL << ((flags & F_MAXDEPTH) >> S_MAXDEPTH))
21 27
26#define F_DEFAULT (9UL << S_MAXDEPTH) 32#define F_DEFAULT (9UL << S_MAXDEPTH)
27 33
28#define INIT_SIZE 32 // initial scalar size to be allocated 34#define INIT_SIZE 32 // initial scalar size to be allocated
29#define INDENT_STEP 3 // spaces per indentation level 35#define INDENT_STEP 3 // spaces per indentation level
30 36
31#define UTF8_MAX_LEN 11 // for perls UTF-X: max. number of octets per character
32#define SHORT_STRING_LEN 512 // special-case strings of up to this size 37#define SHORT_STRING_LEN 512 // special-case strings of up to this size
33 38
34#define SB do { 39#define SB do {
35#define SE } while (0) 40#define SE } while (0)
36 41
104 if (enc->cur + len >= enc->end) 109 if (enc->cur + len >= enc->end)
105 { 110 {
106 STRLEN cur = enc->cur - SvPVX (enc->sv); 111 STRLEN cur = enc->cur - SvPVX (enc->sv);
107 SvGROW (enc->sv, cur + len + 1); 112 SvGROW (enc->sv, cur + len + 1);
108 enc->cur = SvPVX (enc->sv) + cur; 113 enc->cur = SvPVX (enc->sv) + cur;
109 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv); 114 enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1;
110 } 115 }
111} 116}
112 117
113static void 118static void
114encode_ch (enc_t *enc, char ch) 119encode_ch (enc_t *enc, char ch)
176 } 181 }
177 182
178 if (uch > 0x10FFFFUL) 183 if (uch > 0x10FFFFUL)
179 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch); 184 croak ("out of range codepoint (0x%lx) encountered, unrepresentable in JSON", (unsigned long)uch);
180 185
181 if (uch < 0x80 || enc->flags & F_ASCII) 186 if (uch < 0x80 || enc->flags & F_ASCII || (enc->flags & F_LATIN1 && uch > 0xFF))
182 { 187 {
183 if (uch > 0xFFFFUL) 188 if (uch > 0xFFFFUL)
184 { 189 {
185 need (enc, len += 11); 190 need (enc, len += 11);
186 sprintf (enc->cur, "\\u%04x\\u%04x", 191 sprintf (enc->cur, "\\u%04x\\u%04x",
200 *enc->cur++ = hexdigit [(uch >> 0) & 15]; 205 *enc->cur++ = hexdigit [(uch >> 0) & 15];
201 } 206 }
202 207
203 str += clen; 208 str += clen;
204 } 209 }
210 else if (enc->flags & F_LATIN1)
211 {
212 *enc->cur++ = uch;
213 str += clen;
214 }
205 else if (is_utf8) 215 else if (is_utf8)
206 { 216 {
207 need (enc, len += clen); 217 need (enc, len += clen);
208 do 218 do
209 { 219 {
211 } 221 }
212 while (--clen); 222 while (--clen);
213 } 223 }
214 else 224 else
215 { 225 {
216 need (enc, len += UTF8_MAX_LEN - 1); // never more than 11 bytes needed 226 need (enc, len += UTF8_MAXBYTES - 1); // never more than 11 bytes needed
217 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0); 227 enc->cur = uvuni_to_utf8_flags (enc->cur, uch, 0);
218 ++str; 228 ++str;
219 } 229 }
220 } 230 }
221 } 231 }
408 418
409 encode_nl (enc); 419 encode_nl (enc);
410 } 420 }
411 else 421 else
412 { 422 {
413 SV *sv;
414 HE *he = hv_iternext (hv); 423 HE *he = hv_iternext (hv);
415 424
416 for (;;) 425 for (;;)
417 { 426 {
418 encode_indent (enc); 427 encode_indent (enc);
433 442
434// encode objects, arrays and special \0=false and \1=true values. 443// encode objects, arrays and special \0=false and \1=true values.
435static void 444static void
436encode_rv (enc_t *enc, SV *sv) 445encode_rv (enc_t *enc, SV *sv)
437{ 446{
447 svtype svt;
448
438 SvGETMAGIC (sv); 449 SvGETMAGIC (sv);
439
440 svtype svt = SvTYPE (sv); 450 svt = SvTYPE (sv);
441 451
442 if (svt == SVt_PVHV) 452 if (svt == SVt_PVHV)
443 encode_hv (enc, (HV *)sv); 453 encode_hv (enc, (HV *)sv);
444 else if (svt == SVt_PVAV) 454 else if (svt == SVt_PVAV)
445 encode_av (enc, (AV *)sv); 455 encode_av (enc, (AV *)sv);
495} 505}
496 506
497static SV * 507static SV *
498encode_json (SV *scalar, U32 flags) 508encode_json (SV *scalar, U32 flags)
499{ 509{
510 enc_t enc;
511
500 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar)) 512 if (!(flags & F_ALLOW_NONREF) && !SvROK (scalar))
501 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)"); 513 croak ("hash- or arrayref expected (not a simple scalar, use allow_nonref to allow this)");
502 514
503 enc_t enc;
504 enc.flags = flags; 515 enc.flags = flags;
505 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE)); 516 enc.sv = sv_2mortal (NEWSV (0, INIT_SIZE));
506 enc.cur = SvPVX (enc.sv); 517 enc.cur = SvPVX (enc.sv);
507 enc.end = SvEND (enc.sv); 518 enc.end = SvEND (enc.sv);
508 enc.indent = 0; 519 enc.indent = 0;
509 enc.maxdepth = DEC_DEPTH (flags); 520 enc.maxdepth = DEC_DEPTH (flags);
510 521
511 SvPOK_only (enc.sv); 522 SvPOK_only (enc.sv);
512 encode_sv (&enc, scalar); 523 encode_sv (&enc, scalar);
513 524
525 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
526 *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings
527
514 if (!(flags & (F_ASCII | F_UTF8))) 528 if (!(flags & (F_ASCII | F_LATIN1 | F_UTF8)))
515 SvUTF8_on (enc.sv); 529 SvUTF8_on (enc.sv);
516
517 SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv));
518 530
519 if (enc.flags & F_SHRINK) 531 if (enc.flags & F_SHRINK)
520 shrink (enc.sv); 532 shrink (enc.sv);
521 533
522 return enc.sv; 534 return enc.sv;
594 SV *sv = 0; 606 SV *sv = 0;
595 int utf8 = 0; 607 int utf8 = 0;
596 608
597 do 609 do
598 { 610 {
599 char buf [SHORT_STRING_LEN + UTF8_MAX_LEN]; 611 char buf [SHORT_STRING_LEN + UTF8_MAXBYTES];
600 char *cur = buf; 612 char *cur = buf;
601 613
602 do 614 do
603 { 615 {
604 unsigned char ch = *(unsigned char *)dec->cur++; 616 unsigned char ch = *(unsigned char *)dec->cur++;
670 } 682 }
671 else if (ch >= 0x20 && ch <= 0x7f) 683 else if (ch >= 0x20 && ch <= 0x7f)
672 *cur++ = ch; 684 *cur++ = ch;
673 else if (ch >= 0x80) 685 else if (ch >= 0x80)
674 { 686 {
687 STRLEN clen;
688 UV uch;
689
675 --dec->cur; 690 --dec->cur;
676 691
677 STRLEN clen;
678 UV uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen); 692 uch = decode_utf8 (dec->cur, dec->end - dec->cur, &clen);
679 if (clen == (STRLEN)-1) 693 if (clen == (STRLEN)-1)
680 ERR ("malformed UTF-8 character in JSON string"); 694 ERR ("malformed UTF-8 character in JSON string");
681 695
682 do 696 do
683 *cur++ = *dec->cur++; 697 *cur++ = *dec->cur++;
695 ERR ("invalid character encountered while parsing JSON string"); 709 ERR ("invalid character encountered while parsing JSON string");
696 } 710 }
697 } 711 }
698 while (cur < buf + SHORT_STRING_LEN); 712 while (cur < buf + SHORT_STRING_LEN);
699 713
714 {
700 STRLEN len = cur - buf; 715 STRLEN len = cur - buf;
701 716
702 if (sv) 717 if (sv)
703 { 718 {
704 SvGROW (sv, SvCUR (sv) + len + 1); 719 SvGROW (sv, SvCUR (sv) + len + 1);
705 memcpy (SvPVX (sv) + SvCUR (sv), buf, len); 720 memcpy (SvPVX (sv) + SvCUR (sv), buf, len);
706 SvCUR_set (sv, SvCUR (sv) + len); 721 SvCUR_set (sv, SvCUR (sv) + len);
707 } 722 }
708 else 723 else
709 sv = newSVpvn (buf, len); 724 sv = newSVpvn (buf, len);
725 }
710 } 726 }
711 while (*dec->cur != '"'); 727 while (*dec->cur != '"');
712 728
713 ++dec->cur; 729 ++dec->cur;
714 730
969} 985}
970 986
971static SV * 987static SV *
972decode_json (SV *string, U32 flags) 988decode_json (SV *string, U32 flags)
973{ 989{
990 dec_t dec;
974 SV *sv; 991 SV *sv;
975 992
993 SvGETMAGIC (string);
976 SvUPGRADE (string, SVt_PV); 994 SvUPGRADE (string, SVt_PV);
977 995
978 if (flags & F_UTF8) 996 if (flags & F_UTF8)
979 sv_utf8_downgrade (string, 0); 997 sv_utf8_downgrade (string, 0);
980 else 998 else
981 sv_utf8_upgrade (string); 999 sv_utf8_upgrade (string);
982 1000
983 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP 1001 SvGROW (string, SvCUR (string) + 1); // should basically be a NOP
984 1002
985 dec_t dec;
986 dec.flags = flags; 1003 dec.flags = flags;
987 dec.cur = SvPVX (string); 1004 dec.cur = SvPVX (string);
988 dec.end = SvEND (string); 1005 dec.end = SvEND (string);
989 dec.err = 0; 1006 dec.err = 0;
990 dec.depth = 0; 1007 dec.depth = 0;
1053 RETVAL 1070 RETVAL
1054 1071
1055SV *ascii (SV *self, int enable = 1) 1072SV *ascii (SV *self, int enable = 1)
1056 ALIAS: 1073 ALIAS:
1057 ascii = F_ASCII 1074 ascii = F_ASCII
1075 latin1 = F_LATIN1
1058 utf8 = F_UTF8 1076 utf8 = F_UTF8
1059 indent = F_INDENT 1077 indent = F_INDENT
1060 canonical = F_CANONICAL 1078 canonical = F_CANONICAL
1061 space_before = F_SPACE_BEFORE 1079 space_before = F_SPACE_BEFORE
1062 space_after = F_SPACE_AFTER 1080 space_after = F_SPACE_AFTER
1074 RETVAL = newSVsv (self); 1092 RETVAL = newSVsv (self);
1075} 1093}
1076 OUTPUT: 1094 OUTPUT:
1077 RETVAL 1095 RETVAL
1078 1096
1079SV *max_depth (SV *self, int max_depth = 0x80000000UL) 1097SV *max_depth (SV *self, UV max_depth = 0x80000000UL)
1080 CODE: 1098 CODE:
1081{ 1099{
1082 UV *uv = SvJSON (self); 1100 UV *uv = SvJSON (self);
1083 UV log2 = 0; 1101 UV log2 = 0;
1084 1102

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines