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.59 by root, Mon Aug 13 16:14:20 2007 UTC vs.
Revision 1.66 by root, Sun Nov 25 19:02:42 2007 UTC

27#define F_SPACE_AFTER 0x00000040UL 27#define F_SPACE_AFTER 0x00000040UL
28#define F_ALLOW_NONREF 0x00000100UL 28#define F_ALLOW_NONREF 0x00000100UL
29#define F_SHRINK 0x00000200UL 29#define F_SHRINK 0x00000200UL
30#define F_ALLOW_BLESSED 0x00000400UL 30#define F_ALLOW_BLESSED 0x00000400UL
31#define F_CONV_BLESSED 0x00000800UL 31#define F_CONV_BLESSED 0x00000800UL
32#define F_RELAXED 0x00001000UL
33
32#define F_MAXDEPTH 0xf8000000UL 34#define F_MAXDEPTH 0xf8000000UL
33#define S_MAXDEPTH 27 35#define S_MAXDEPTH 27
34#define F_MAXSIZE 0x01f00000UL 36#define F_MAXSIZE 0x01f00000UL
35#define S_MAXSIZE 20 37#define S_MAXSIZE 20
36#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing 38#define F_HOOK 0x00080000UL // some hooks exist, so slow-path processing
310 int i, len = av_len (av); 312 int i, len = av_len (av);
311 313
312 if (enc->indent >= enc->maxdepth) 314 if (enc->indent >= enc->maxdepth)
313 croak ("data structure too deep (hit recursion limit)"); 315 croak ("data structure too deep (hit recursion limit)");
314 316
315 encode_ch (enc, '['); encode_nl (enc); 317 encode_ch (enc, '[');
316 ++enc->indent; 318
319 if (len >= 0)
320 {
321 encode_nl (enc); ++enc->indent;
317 322
318 for (i = 0; i <= len; ++i) 323 for (i = 0; i <= len; ++i)
319 { 324 {
320 SV **svp = av_fetch (av, i, 0); 325 SV **svp = av_fetch (av, i, 0);
321 326
322 encode_indent (enc); 327 encode_indent (enc);
323 328
324 if (svp) 329 if (svp)
325 encode_sv (enc, *svp); 330 encode_sv (enc, *svp);
326 else 331 else
327 encode_str (enc, "null", 4, 0); 332 encode_str (enc, "null", 4, 0);
328 333
329 if (i < len) 334 if (i < len)
330 encode_comma (enc); 335 encode_comma (enc);
331 } 336 }
332 337
338 encode_nl (enc); --enc->indent; encode_indent (enc);
339 }
340
333 encode_nl (enc); 341 encode_ch (enc, ']');
334
335 --enc->indent;
336 encode_indent (enc); encode_ch (enc, ']');
337} 342}
338 343
339static void 344static void
340encode_he (enc_t *enc, HE *he) 345encode_hk (enc_t *enc, HE *he)
341{ 346{
342 encode_ch (enc, '"'); 347 encode_ch (enc, '"');
343 348
344 if (HeKLEN (he) == HEf_SVKEY) 349 if (HeKLEN (he) == HEf_SVKEY)
345 { 350 {
358 encode_ch (enc, '"'); 363 encode_ch (enc, '"');
359 364
360 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc); 365 if (enc->json.flags & F_SPACE_BEFORE) encode_space (enc);
361 encode_ch (enc, ':'); 366 encode_ch (enc, ':');
362 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc); 367 if (enc->json.flags & F_SPACE_AFTER ) encode_space (enc);
363 encode_sv (enc, HeVAL (he));
364} 368}
365 369
366// compare hash entries, used when all keys are bytestrings 370// compare hash entries, used when all keys are bytestrings
367static int 371static int
368he_cmp_fast (const void *a_, const void *b_) 372he_cmp_fast (const void *a_, const void *b_)
373 HE *b = *(HE **)b_; 377 HE *b = *(HE **)b_;
374 378
375 STRLEN la = HeKLEN (a); 379 STRLEN la = HeKLEN (a);
376 STRLEN lb = HeKLEN (b); 380 STRLEN lb = HeKLEN (b);
377 381
378 if (!(cmp = memcmp (HeKEY (a), HeKEY (b), la < lb ? la : lb))) 382 if (!(cmp = memcmp (HeKEY (b), HeKEY (a), lb < la ? lb : la)))
379 cmp = la - lb; 383 cmp = lb - la;
380 384
381 return cmp; 385 return cmp;
382} 386}
383 387
384// compare hash entries, used when some keys are sv's or utf-x 388// compare hash entries, used when some keys are sv's or utf-x
385static int 389static int
386he_cmp_slow (const void *a, const void *b) 390he_cmp_slow (const void *a, const void *b)
387{ 391{
388 return sv_cmp (HeSVKEY_force (*(HE **)a), HeSVKEY_force (*(HE **)b)); 392 return sv_cmp (HeSVKEY_force (*(HE **)b), HeSVKEY_force (*(HE **)a));
389} 393}
390 394
391static void 395static void
392encode_hv (enc_t *enc, HV *hv) 396encode_hv (enc_t *enc, HV *hv)
393{ 397{
398 HE *he;
394 int count, i; 399 int count;
395 400
396 if (enc->indent >= enc->maxdepth) 401 if (enc->indent >= enc->maxdepth)
397 croak ("data structure too deep (hit recursion limit)"); 402 croak ("data structure too deep (hit recursion limit)");
398 403
399 encode_ch (enc, '{'); encode_nl (enc); ++enc->indent; 404 encode_ch (enc, '{');
400 405
401 if ((count = hv_iterinit (hv)))
402 {
403 // for canonical output we have to sort by keys first 406 // for canonical output we have to sort by keys first
404 // actually, this is mostly due to the stupid so-called 407 // actually, this is mostly due to the stupid so-called
405 // security workaround added somewhere in 5.8.x. 408 // security workaround added somewhere in 5.8.x.
406 // that randomises hash orderings 409 // that randomises hash orderings
407 if (enc->json.flags & F_CANONICAL) 410 if (enc->json.flags & F_CANONICAL)
411 {
412 int count = hv_iterinit (hv);
413
414 if (SvMAGICAL (hv))
408 { 415 {
416 // need to count by iterating. could improve by dynamically building the vector below
417 // but I don't care for the speed of this special case.
418 // note also that we will run into undefined behaviour when the two iterations
419 // do not result in the same count, something I might care for in some later release.
420
421 count = 0;
422 while (hv_iternext (hv))
423 ++count;
424
425 hv_iterinit (hv);
426 }
427
428 if (count)
429 {
409 int fast = 1; 430 int i, fast = 1;
410 HE *he;
411#if defined(__BORLANDC__) || defined(_MSC_VER) 431#if defined(__BORLANDC__) || defined(_MSC_VER)
412 HE **hes = _alloca (count * sizeof (HE)); 432 HE **hes = _alloca (count * sizeof (HE));
413#else 433#else
414 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode 434 HE *hes [count]; // if your compiler dies here, you need to enable C99 mode
415#endif 435#endif
442 462
443 FREETMPS; 463 FREETMPS;
444 LEAVE; 464 LEAVE;
445 } 465 }
446 466
447 for (i = 0; i < count; ++i) 467 encode_nl (enc); ++enc->indent;
468
469 while (count--)
448 { 470 {
449 encode_indent (enc); 471 encode_indent (enc);
472 he = hes [count];
450 encode_he (enc, hes [i]); 473 encode_hk (enc, he);
474 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
451 475
452 if (i < count - 1) 476 if (count)
453 encode_comma (enc); 477 encode_comma (enc);
454 } 478 }
455 479
456 encode_nl (enc); 480 encode_nl (enc); --enc->indent; encode_indent (enc);
457 } 481 }
482 }
458 else 483 else
484 {
485 if (hv_iterinit (hv) || SvMAGICAL (hv))
486 if ((he = hv_iternext (hv)))
459 { 487 {
460 HE *he = hv_iternext (hv); 488 encode_nl (enc); ++enc->indent;
461 489
462 for (;;) 490 for (;;)
463 { 491 {
464 encode_indent (enc); 492 encode_indent (enc);
465 encode_he (enc, he); 493 encode_hk (enc, he);
494 encode_sv (enc, expect_false (SvMAGICAL (hv)) ? hv_iterval (hv, he) : HeVAL (he));
466 495
467 if (!(he = hv_iternext (hv))) 496 if (!(he = hv_iternext (hv)))
468 break; 497 break;
469 498
470 encode_comma (enc); 499 encode_comma (enc);
471 } 500 }
472 501
473 encode_nl (enc); 502 encode_nl (enc); --enc->indent; encode_indent (enc);
474 } 503 }
475 } 504 }
476 505
477 --enc->indent; encode_indent (enc); encode_ch (enc, '}'); 506 encode_ch (enc, '}');
478} 507}
479 508
480// encode objects, arrays and special \0=false and \1=true values. 509// encode objects, arrays and special \0=false and \1=true values.
481static void 510static void
482encode_rv (enc_t *enc, SV *sv) 511encode_rv (enc_t *enc, SV *sv)
682 U32 depth; // recursion depth 711 U32 depth; // recursion depth
683 U32 maxdepth; // recursion depth limit 712 U32 maxdepth; // recursion depth limit
684} dec_t; 713} dec_t;
685 714
686inline void 715inline void
716decode_comment (dec_t *dec)
717{
718 // only '#'-style comments allowed a.t.m.
719
720 while (*dec->cur && *dec->cur != 0x0a && *dec->cur != 0x0d)
721 ++dec->cur;
722}
723
724inline void
687decode_ws (dec_t *dec) 725decode_ws (dec_t *dec)
688{ 726{
689 for (;;) 727 for (;;)
690 { 728 {
691 char ch = *dec->cur; 729 char ch = *dec->cur;
692 730
693 if (ch > 0x20 731 if (ch > 0x20)
732 {
733 if (expect_false (ch == '#'))
734 {
735 if (dec->json.flags & F_RELAXED)
736 decode_comment (dec);
737 else
738 break;
739 }
740 else
741 break;
742 }
694 || (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)) 743 else if (ch != 0x20 && ch != 0x0a && ch != 0x0d && ch != 0x09)
695 break; 744 break; // parse error, but let higher level handle it, gives better error messages
696 745
697 ++dec->cur; 746 ++dec->cur;
698 } 747 }
699} 748}
700 749
1037 1086
1038 if (*dec->cur != ',') 1087 if (*dec->cur != ',')
1039 ERR (", or ] expected while parsing array"); 1088 ERR (", or ] expected while parsing array");
1040 1089
1041 ++dec->cur; 1090 ++dec->cur;
1091
1092 decode_ws (dec);
1093
1094 if (*dec->cur == ']' && dec->json.flags & F_RELAXED)
1095 {
1096 ++dec->cur;
1097 break;
1098 }
1042 } 1099 }
1043 1100
1044 DEC_DEC_DEPTH; 1101 DEC_DEC_DEPTH;
1045 return newRV_noinc ((SV *)av); 1102 return newRV_noinc ((SV *)av);
1046 1103
1062 if (*dec->cur == '}') 1119 if (*dec->cur == '}')
1063 ++dec->cur; 1120 ++dec->cur;
1064 else 1121 else
1065 for (;;) 1122 for (;;)
1066 { 1123 {
1067 decode_ws (dec); EXPECT_CH ('"'); 1124 EXPECT_CH ('"');
1068 1125
1069 // heuristic: assume that 1126 // heuristic: assume that
1070 // a) decode_str + hv_store_ent are abysmally slow. 1127 // a) decode_str + hv_store_ent are abysmally slow.
1071 // b) most hash keys are short, simple ascii text. 1128 // b) most hash keys are short, simple ascii text.
1072 // => try to "fast-match" such strings to avoid 1129 // => try to "fast-match" such strings to avoid
1086 if (!key) 1143 if (!key)
1087 goto fail; 1144 goto fail;
1088 1145
1089 decode_ws (dec); EXPECT_CH (':'); 1146 decode_ws (dec); EXPECT_CH (':');
1090 1147
1148 decode_ws (dec);
1091 value = decode_sv (dec); 1149 value = decode_sv (dec);
1092 if (!value) 1150 if (!value)
1093 { 1151 {
1094 SvREFCNT_dec (key); 1152 SvREFCNT_dec (key);
1095 goto fail; 1153 goto fail;
1107 int len = p - key; 1165 int len = p - key;
1108 dec->cur = p + 1; 1166 dec->cur = p + 1;
1109 1167
1110 decode_ws (dec); EXPECT_CH (':'); 1168 decode_ws (dec); EXPECT_CH (':');
1111 1169
1170 decode_ws (dec);
1112 value = decode_sv (dec); 1171 value = decode_sv (dec);
1113 if (!value) 1172 if (!value)
1114 goto fail; 1173 goto fail;
1115 1174
1116 hv_store (hv, key, len, value, 0); 1175 hv_store (hv, key, len, value, 0);
1132 1191
1133 if (*dec->cur != ',') 1192 if (*dec->cur != ',')
1134 ERR (", or } expected while parsing object/hash"); 1193 ERR (", or } expected while parsing object/hash");
1135 1194
1136 ++dec->cur; 1195 ++dec->cur;
1196
1197 decode_ws (dec);
1198
1199 if (*dec->cur == '}' && dec->json.flags & F_RELAXED)
1200 {
1201 ++dec->cur;
1202 break;
1203 }
1137 } 1204 }
1138 1205
1139 DEC_DEC_DEPTH; 1206 DEC_DEC_DEPTH;
1140 sv = newRV_noinc ((SV *)hv); 1207 sv = newRV_noinc ((SV *)hv);
1141 1208
1206} 1273}
1207 1274
1208static SV * 1275static SV *
1209decode_sv (dec_t *dec) 1276decode_sv (dec_t *dec)
1210{ 1277{
1211 decode_ws (dec);
1212
1213 // the beauty of JSON: you need exactly one character lookahead 1278 // the beauty of JSON: you need exactly one character lookahead
1214 // to parse anything. 1279 // to parse anything.
1215 switch (*dec->cur) 1280 switch (*dec->cur)
1216 { 1281 {
1217 case '"': ++dec->cur; return decode_str (dec); 1282 case '"': ++dec->cur; return decode_str (dec);
1225 1290
1226 case 't': 1291 case 't':
1227 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4)) 1292 if (dec->end - dec->cur >= 4 && !memcmp (dec->cur, "true", 4))
1228 { 1293 {
1229 dec->cur += 4; 1294 dec->cur += 4;
1295#if JSON_SLOW
1296 json_true = get_sv ("JSON::XS::true", 1); SvREADONLY_on (json_true);
1297#endif
1230 return SvREFCNT_inc (json_true); 1298 return SvREFCNT_inc (json_true);
1231 } 1299 }
1232 else 1300 else
1233 ERR ("'true' expected"); 1301 ERR ("'true' expected");
1234 1302
1236 1304
1237 case 'f': 1305 case 'f':
1238 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5)) 1306 if (dec->end - dec->cur >= 5 && !memcmp (dec->cur, "false", 5))
1239 { 1307 {
1240 dec->cur += 5; 1308 dec->cur += 5;
1309#if JSON_SLOW
1310 json_false = get_sv ("JSON::XS::false", 1); SvREADONLY_on (json_false);
1311#endif
1241 return SvREFCNT_inc (json_false); 1312 return SvREFCNT_inc (json_false);
1242 } 1313 }
1243 else 1314 else
1244 ERR ("'false' expected"); 1315 ERR ("'false' expected");
1245 1316
1295 1366
1296 if (dec.json.cb_object || dec.json.cb_sk_object) 1367 if (dec.json.cb_object || dec.json.cb_sk_object)
1297 dec.json.flags |= F_HOOK; 1368 dec.json.flags |= F_HOOK;
1298 1369
1299 *dec.end = 0; // this should basically be a nop, too, but make sure it's there 1370 *dec.end = 0; // this should basically be a nop, too, but make sure it's there
1371
1372 decode_ws (&dec);
1300 sv = decode_sv (&dec); 1373 sv = decode_sv (&dec);
1301 1374
1302 if (!(offset_return || !sv)) 1375 if (!(offset_return || !sv))
1303 { 1376 {
1304 // check for trailing garbage 1377 // check for trailing garbage
1401 pretty = F_PRETTY 1474 pretty = F_PRETTY
1402 allow_nonref = F_ALLOW_NONREF 1475 allow_nonref = F_ALLOW_NONREF
1403 shrink = F_SHRINK 1476 shrink = F_SHRINK
1404 allow_blessed = F_ALLOW_BLESSED 1477 allow_blessed = F_ALLOW_BLESSED
1405 convert_blessed = F_CONV_BLESSED 1478 convert_blessed = F_CONV_BLESSED
1479 relaxed = F_RELAXED
1406 PPCODE: 1480 PPCODE:
1407{ 1481{
1408 if (enable) 1482 if (enable)
1409 self->flags |= ix; 1483 self->flags |= ix;
1410 else 1484 else
1411 self->flags &= ~ix; 1485 self->flags &= ~ix;
1412 1486
1413 XPUSHs (ST (0)); 1487 XPUSHs (ST (0));
1414} 1488}
1415 1489
1490void get_ascii (JSON *self)
1491 ALIAS:
1492 get_ascii = F_ASCII
1493 get_latin1 = F_LATIN1
1494 get_utf8 = F_UTF8
1495 get_indent = F_INDENT
1496 get_canonical = F_CANONICAL
1497 get_space_before = F_SPACE_BEFORE
1498 get_space_after = F_SPACE_AFTER
1499 get_pretty = F_PRETTY
1500 get_allow_nonref = F_ALLOW_NONREF
1501 get_shrink = F_SHRINK
1502 get_allow_blessed = F_ALLOW_BLESSED
1503 get_convert_blessed = F_CONV_BLESSED
1504 get_relaxed = F_RELAXED
1505 PPCODE:
1506 XPUSHs (boolSV (self->flags & ix));
1507
1416void max_depth (JSON *self, UV max_depth = 0x80000000UL) 1508void max_depth (JSON *self, UV max_depth = 0x80000000UL)
1417 PPCODE: 1509 PPCODE:
1418{ 1510{
1419 UV log2 = 0; 1511 UV log2 = 0;
1420 1512
1426 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH); 1518 self->flags = self->flags & ~F_MAXDEPTH | (log2 << S_MAXDEPTH);
1427 1519
1428 XPUSHs (ST (0)); 1520 XPUSHs (ST (0));
1429} 1521}
1430 1522
1523int get_max_depth (JSON *self)
1524 CODE:
1525 RETVAL = DEC_DEPTH (self->flags);
1526 OUTPUT:
1527 RETVAL
1528
1431void max_size (JSON *self, UV max_size = 0) 1529void max_size (JSON *self, UV max_size = 0)
1432 PPCODE: 1530 PPCODE:
1433{ 1531{
1434 UV log2 = 0; 1532 UV log2 = 0;
1435 1533
1441 1539
1442 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE); 1540 self->flags = self->flags & ~F_MAXSIZE | (log2 << S_MAXSIZE);
1443 1541
1444 XPUSHs (ST (0)); 1542 XPUSHs (ST (0));
1445} 1543}
1544
1545int get_max_size (JSON *self)
1546 CODE:
1547 RETVAL = DEC_SIZE (self->flags);
1548 OUTPUT:
1549 RETVAL
1446 1550
1447void filter_json_object (JSON *self, SV *cb = &PL_sv_undef) 1551void filter_json_object (JSON *self, SV *cb = &PL_sv_undef)
1448 PPCODE: 1552 PPCODE:
1449{ 1553{
1450 SvREFCNT_dec (self->cb_object); 1554 SvREFCNT_dec (self->cb_object);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines