… | |
… | |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
12 | #if defined(__BORLANDC__) || defined(_MSC_VER) |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
13 | # define snprintf _snprintf // C compilers have this in stdio.h |
14 | #endif |
14 | #endif |
15 | |
15 | |
16 | // some old perls do not have this, try to make it work, no |
16 | // some old perls do not have this, try to make it work, no |
17 | // guarentees, though. if it breaks, you get to keep the pieces. |
17 | // guarantees, though. if it breaks, you get to keep the pieces. |
18 | #ifndef UTF8_MAXBYTES |
18 | #ifndef UTF8_MAXBYTES |
19 | # define UTF8_MAXBYTES 13 |
19 | # define UTF8_MAXBYTES 13 |
20 | #endif |
20 | #endif |
21 | |
21 | |
|
|
22 | // three extra for rounding, sign, and end of string |
22 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 2) |
23 | #define IVUV_MAXCHARS (sizeof (UV) * CHAR_BIT * 28 / 93 + 3) |
23 | |
24 | |
24 | #define F_ASCII 0x00000001UL |
25 | #define F_ASCII 0x00000001UL |
25 | #define F_LATIN1 0x00000002UL |
26 | #define F_LATIN1 0x00000002UL |
26 | #define F_UTF8 0x00000004UL |
27 | #define F_UTF8 0x00000004UL |
27 | #define F_INDENT 0x00000008UL |
28 | #define F_INDENT 0x00000008UL |
… | |
… | |
76 | |
77 | |
77 | enum { |
78 | enum { |
78 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_WS = 0, // initial whitespace skipping, must be 0 |
79 | INCR_M_STR, // inside string |
80 | INCR_M_STR, // inside string |
80 | INCR_M_BS, // inside backslash |
81 | INCR_M_BS, // inside backslash |
|
|
82 | INCR_M_C0, // inside comment in initial whitespace sequence |
|
|
83 | INCR_M_C1, // inside comment in other places |
81 | INCR_M_JSON // outside anything, count nesting |
84 | INCR_M_JSON // outside anything, count nesting |
82 | }; |
85 | }; |
83 | |
86 | |
84 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
87 | #define INCR_DONE(json) ((json)->incr_nest <= 0 && (json)->incr_mode == INCR_M_JSON) |
85 | |
88 | |
… | |
… | |
175 | *s++ = 0x80 | ( ch & 0x3f); |
178 | *s++ = 0x80 | ( ch & 0x3f); |
176 | |
179 | |
177 | return s; |
180 | return s; |
178 | } |
181 | } |
179 | |
182 | |
|
|
183 | // convert offset pointer to character index, sv must be string |
|
|
184 | static STRLEN |
|
|
185 | ptr_to_index (SV *sv, char *offset) |
|
|
186 | { |
|
|
187 | return SvUTF8 (sv) |
|
|
188 | ? utf8_distance (offset, SvPVX (sv)) |
|
|
189 | : offset - SvPVX (sv); |
|
|
190 | } |
|
|
191 | |
180 | ///////////////////////////////////////////////////////////////////////////// |
192 | ///////////////////////////////////////////////////////////////////////////// |
181 | // encoder |
193 | // encoder |
182 | |
194 | |
183 | // structure used for encoding JSON |
195 | // structure used for encoding JSON |
184 | typedef struct |
196 | typedef struct |
… | |
… | |
194 | INLINE void |
206 | INLINE void |
195 | need (enc_t *enc, STRLEN len) |
207 | need (enc_t *enc, STRLEN len) |
196 | { |
208 | { |
197 | if (expect_false (enc->cur + len >= enc->end)) |
209 | if (expect_false (enc->cur + len >= enc->end)) |
198 | { |
210 | { |
199 | STRLEN cur = enc->cur - SvPVX (enc->sv); |
211 | STRLEN cur = enc->cur - (char *)SvPVX (enc->sv); |
200 | SvGROW (enc->sv, cur + len + 1); |
212 | SvGROW (enc->sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
201 | enc->cur = SvPVX (enc->sv) + cur; |
213 | enc->cur = SvPVX (enc->sv) + cur; |
202 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
214 | enc->end = SvPVX (enc->sv) + SvLEN (enc->sv) - 1; |
203 | } |
215 | } |
204 | } |
216 | } |
205 | |
217 | |
… | |
… | |
280 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
292 | (int)((uch - 0x10000) % 0x400 + 0xDC00)); |
281 | enc->cur += 12; |
293 | enc->cur += 12; |
282 | } |
294 | } |
283 | else |
295 | else |
284 | { |
296 | { |
285 | static char hexdigit [16] = "0123456789abcdef"; |
|
|
286 | need (enc, len += 5); |
297 | need (enc, len += 5); |
287 | *enc->cur++ = '\\'; |
298 | *enc->cur++ = '\\'; |
288 | *enc->cur++ = 'u'; |
299 | *enc->cur++ = 'u'; |
289 | *enc->cur++ = hexdigit [ uch >> 12 ]; |
300 | *enc->cur++ = PL_hexdigit [ uch >> 12 ]; |
290 | *enc->cur++ = hexdigit [(uch >> 8) & 15]; |
301 | *enc->cur++ = PL_hexdigit [(uch >> 8) & 15]; |
291 | *enc->cur++ = hexdigit [(uch >> 4) & 15]; |
302 | *enc->cur++ = PL_hexdigit [(uch >> 4) & 15]; |
292 | *enc->cur++ = hexdigit [(uch >> 0) & 15]; |
303 | *enc->cur++ = PL_hexdigit [(uch >> 0) & 15]; |
293 | } |
304 | } |
294 | |
305 | |
295 | str += clen; |
306 | str += clen; |
296 | } |
307 | } |
297 | else if (enc->json.flags & F_LATIN1) |
308 | else if (enc->json.flags & F_LATIN1) |
… | |
… | |
461 | |
472 | |
462 | encode_ch (enc, '{'); |
473 | encode_ch (enc, '{'); |
463 | |
474 | |
464 | // for canonical output we have to sort by keys first |
475 | // for canonical output we have to sort by keys first |
465 | // actually, this is mostly due to the stupid so-called |
476 | // actually, this is mostly due to the stupid so-called |
466 | // security workaround added somewhere in 5.8.x. |
477 | // security workaround added somewhere in 5.8.x |
467 | // that randomises hash orderings |
478 | // that randomises hash orderings |
468 | if (enc->json.flags & F_CANONICAL) |
479 | if (enc->json.flags & F_CANONICAL && !SvRMAGICAL (hv)) |
469 | { |
480 | { |
470 | int count = hv_iterinit (hv); |
481 | int count = hv_iterinit (hv); |
471 | |
482 | |
472 | if (SvMAGICAL (hv)) |
483 | if (SvMAGICAL (hv)) |
473 | { |
484 | { |
… | |
… | |
751 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
762 | : enc.json.flags & F_LATIN1 ? 0x000100UL |
752 | : 0x110000UL; |
763 | : 0x110000UL; |
753 | |
764 | |
754 | SvPOK_only (enc.sv); |
765 | SvPOK_only (enc.sv); |
755 | encode_sv (&enc, scalar); |
766 | encode_sv (&enc, scalar); |
|
|
767 | encode_nl (&enc); |
756 | |
768 | |
757 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
769 | SvCUR_set (enc.sv, enc.cur - SvPVX (enc.sv)); |
758 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
770 | *SvEND (enc.sv) = 0; // many xs functions expect a trailing 0 for text strings |
759 | |
771 | |
760 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
772 | if (!(enc.json.flags & (F_ASCII | F_LATIN1 | F_UTF8))) |
… | |
… | |
940 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
952 | else if (expect_true (ch >= 0x20 && ch < 0x80)) |
941 | *cur++ = ch; |
953 | *cur++ = ch; |
942 | else if (ch >= 0x80) |
954 | else if (ch >= 0x80) |
943 | { |
955 | { |
944 | STRLEN clen; |
956 | STRLEN clen; |
945 | UV uch; |
|
|
946 | |
957 | |
947 | --dec_cur; |
958 | --dec_cur; |
948 | |
959 | |
949 | uch = decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
960 | decode_utf8 (dec_cur, dec->end - dec_cur, &clen); |
950 | if (clen == (STRLEN)-1) |
961 | if (clen == (STRLEN)-1) |
951 | ERR ("malformed UTF-8 character in JSON string"); |
962 | ERR ("malformed UTF-8 character in JSON string"); |
952 | |
963 | |
953 | do |
964 | do |
954 | *cur++ = *dec_cur++; |
965 | *cur++ = *dec_cur++; |
… | |
… | |
971 | { |
982 | { |
972 | STRLEN len = cur - buf; |
983 | STRLEN len = cur - buf; |
973 | |
984 | |
974 | if (sv) |
985 | if (sv) |
975 | { |
986 | { |
976 | SvGROW (sv, SvCUR (sv) + len + 1); |
987 | STRLEN cur = SvCUR (sv); |
|
|
988 | |
|
|
989 | if (SvLEN (sv) <= cur + len) |
|
|
990 | SvGROW (sv, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
991 | |
977 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
992 | memcpy (SvPVX (sv) + SvCUR (sv), buf, len); |
978 | SvCUR_set (sv, SvCUR (sv) + len); |
993 | SvCUR_set (sv, SvCUR (sv) + len); |
979 | } |
994 | } |
980 | else |
995 | else |
981 | sv = newSVpvn (buf, len); |
996 | sv = newSVpvn (buf, len); |
… | |
… | |
1072 | |
1087 | |
1073 | // special case the rather common 1..5-digit-int case |
1088 | // special case the rather common 1..5-digit-int case |
1074 | if (*start == '-') |
1089 | if (*start == '-') |
1075 | switch (len) |
1090 | switch (len) |
1076 | { |
1091 | { |
1077 | case 2: return newSViv (-( start [1] - '0' * 1)); |
1092 | case 2: return newSViv (-(IV)( start [1] - '0' * 1)); |
1078 | case 3: return newSViv (-( start [1] * 10 + start [2] - '0' * 11)); |
1093 | case 3: return newSViv (-(IV)( start [1] * 10 + start [2] - '0' * 11)); |
1079 | case 4: return newSViv (-( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1094 | case 4: return newSViv (-(IV)( start [1] * 100 + start [2] * 10 + start [3] - '0' * 111)); |
1080 | case 5: return newSViv (-( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1095 | case 5: return newSViv (-(IV)( start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 1111)); |
1081 | case 6: return newSViv (-(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1096 | case 6: return newSViv (-(IV)(start [1] * 10000 + start [2] * 1000 + start [3] * 100 + start [4] * 10 + start [5] - '0' * 11111)); |
1082 | } |
1097 | } |
1083 | else |
1098 | else |
1084 | switch (len) |
1099 | switch (len) |
1085 | { |
1100 | { |
1086 | case 1: return newSViv ( start [0] - '0' * 1); |
1101 | case 1: return newSViv ( start [0] - '0' * 1); |
1087 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1102 | case 2: return newSViv ( start [0] * 10 + start [1] - '0' * 11); |
1088 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1103 | case 3: return newSViv ( start [0] * 100 + start [1] * 10 + start [2] - '0' * 111); |
1089 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1104 | case 4: return newSViv ( start [0] * 1000 + start [1] * 100 + start [2] * 10 + start [3] - '0' * 1111); |
1090 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1105 | case 5: return newSViv ( start [0] * 10000 + start [1] * 1000 + start [2] * 100 + start [3] * 10 + start [4] - '0' * 11111); |
1091 | } |
1106 | } |
1092 | |
1107 | |
1093 | { |
1108 | { |
1094 | UV uv; |
1109 | UV uv; |
1095 | int numtype = grok_number (start, len, &uv); |
1110 | int numtype = grok_number (start, len, &uv); |
… | |
… | |
1405 | fail: |
1420 | fail: |
1406 | return 0; |
1421 | return 0; |
1407 | } |
1422 | } |
1408 | |
1423 | |
1409 | static SV * |
1424 | static SV * |
1410 | decode_json (SV *string, JSON *json, STRLEN *offset_return) |
1425 | decode_json (SV *string, JSON *json, char **offset_return) |
1411 | { |
1426 | { |
1412 | dec_t dec; |
1427 | dec_t dec; |
1413 | STRLEN offset; |
|
|
1414 | SV *sv; |
1428 | SV *sv; |
1415 | |
1429 | |
|
|
1430 | /* work around bugs in 5.10 where manipulating magic values |
|
|
1431 | * will perl ignore the magic in subsequent accesses |
|
|
1432 | */ |
1416 | SvGETMAGIC (string); |
1433 | /*SvGETMAGIC (string);*/ |
|
|
1434 | if (SvMAGICAL (string)) |
|
|
1435 | string = sv_2mortal (newSVsv (string)); |
|
|
1436 | |
1417 | SvUPGRADE (string, SVt_PV); |
1437 | SvUPGRADE (string, SVt_PV); |
1418 | |
1438 | |
1419 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1439 | /* work around a bug in perl 5.10, which causes SvCUR to fail an |
1420 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1440 | * assertion with -DDEBUGGING, although SvCUR is documented to |
1421 | * return the xpv_cur field which certainly exists after upgrading. |
1441 | * return the xpv_cur field which certainly exists after upgrading. |
1422 | * according to nicholas clark, calling SvPOK fixes this. |
1442 | * according to nicholas clark, calling SvPOK fixes this. |
|
|
1443 | * But it doesn't fix it, so try another workaround, call SvPV_nolen |
|
|
1444 | * and hope for the best. |
|
|
1445 | * Damnit, SvPV_nolen still trips over yet another assertion. This |
|
|
1446 | * assertion business is seriously broken, try yet another workaround |
|
|
1447 | * for the broken -DDEBUGGING. |
1423 | */ |
1448 | */ |
1424 | SvPOK (string); |
1449 | { |
|
|
1450 | #ifdef DEBUGGING |
|
|
1451 | STRLEN offset = SvOK (string) ? sv_len (string) : 0; |
|
|
1452 | #else |
|
|
1453 | STRLEN offset = SvCUR (string); |
|
|
1454 | #endif |
1425 | |
1455 | |
1426 | if (SvCUR (string) > json->max_size && json->max_size) |
1456 | if (offset > json->max_size && json->max_size) |
1427 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1457 | croak ("attempted decode of JSON text of %lu bytes size, but max_size is set to %lu", |
1428 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
1458 | (unsigned long)SvCUR (string), (unsigned long)json->max_size); |
|
|
1459 | } |
1429 | |
1460 | |
1430 | if (json->flags & F_UTF8) |
1461 | if (json->flags & F_UTF8) |
1431 | sv_utf8_downgrade (string, 0); |
1462 | sv_utf8_downgrade (string, 0); |
1432 | else |
1463 | else |
1433 | sv_utf8_upgrade (string); |
1464 | sv_utf8_upgrade (string); |
… | |
… | |
1445 | |
1476 | |
1446 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1477 | *dec.end = 0; // this should basically be a nop, too, but make sure it's there |
1447 | |
1478 | |
1448 | decode_ws (&dec); |
1479 | decode_ws (&dec); |
1449 | sv = decode_sv (&dec); |
1480 | sv = decode_sv (&dec); |
|
|
1481 | |
|
|
1482 | if (offset_return) |
|
|
1483 | *offset_return = dec.cur; |
1450 | |
1484 | |
1451 | if (!(offset_return || !sv)) |
1485 | if (!(offset_return || !sv)) |
1452 | { |
1486 | { |
1453 | // check for trailing garbage |
1487 | // check for trailing garbage |
1454 | decode_ws (&dec); |
1488 | decode_ws (&dec); |
… | |
… | |
1457 | { |
1491 | { |
1458 | dec.err = "garbage after JSON object"; |
1492 | dec.err = "garbage after JSON object"; |
1459 | SvREFCNT_dec (sv); |
1493 | SvREFCNT_dec (sv); |
1460 | sv = 0; |
1494 | sv = 0; |
1461 | } |
1495 | } |
1462 | } |
|
|
1463 | |
|
|
1464 | if (offset_return || !sv) |
|
|
1465 | { |
|
|
1466 | offset = dec.json.flags & F_UTF8 |
|
|
1467 | ? dec.cur - SvPVX (string) |
|
|
1468 | : utf8_distance (dec.cur, SvPVX (string)); |
|
|
1469 | |
|
|
1470 | if (offset_return) |
|
|
1471 | *offset_return = offset; |
|
|
1472 | } |
1496 | } |
1473 | |
1497 | |
1474 | if (!sv) |
1498 | if (!sv) |
1475 | { |
1499 | { |
1476 | SV *uni = sv_newmortal (); |
1500 | SV *uni = sv_newmortal (); |
… | |
… | |
1482 | SAVEVPTR (PL_curcop); |
1506 | SAVEVPTR (PL_curcop); |
1483 | PL_curcop = &cop; |
1507 | PL_curcop = &cop; |
1484 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1508 | pv_uni_display (uni, dec.cur, dec.end - dec.cur, 20, UNI_DISPLAY_QQ); |
1485 | LEAVE; |
1509 | LEAVE; |
1486 | |
1510 | |
1487 | croak ("%s, at character offset %d [\"%s\"]", |
1511 | croak ("%s, at character offset %d (before \"%s\")", |
1488 | dec.err, |
1512 | dec.err, |
1489 | (int)offset, |
1513 | ptr_to_index (string, dec.cur), |
1490 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1514 | dec.cur != dec.end ? SvPV_nolen (uni) : "(end of string)"); |
1491 | } |
1515 | } |
1492 | |
1516 | |
1493 | sv = sv_2mortal (sv); |
1517 | sv = sv_2mortal (sv); |
1494 | |
1518 | |
… | |
… | |
1503 | |
1527 | |
1504 | static void |
1528 | static void |
1505 | incr_parse (JSON *self) |
1529 | incr_parse (JSON *self) |
1506 | { |
1530 | { |
1507 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
1531 | const char *p = SvPVX (self->incr_text) + self->incr_pos; |
|
|
1532 | |
|
|
1533 | // the state machine here is a bit convoluted and could be simplified a lot |
|
|
1534 | // but this would make it slower, so... |
1508 | |
1535 | |
1509 | for (;;) |
1536 | for (;;) |
1510 | { |
1537 | { |
1511 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1538 | //printf ("loop pod %d *p<%c><%s>, mode %d nest %d\n", p - SvPVX (self->incr_text), *p, p, self->incr_mode, self->incr_nest);//D |
1512 | switch (self->incr_mode) |
1539 | switch (self->incr_mode) |
1513 | { |
1540 | { |
1514 | // only used for intiial whitespace skipping |
1541 | // only used for initial whitespace skipping |
1515 | case INCR_M_WS: |
1542 | case INCR_M_WS: |
1516 | for (;;) |
1543 | for (;;) |
1517 | { |
1544 | { |
1518 | if (*p > 0x20) |
1545 | if (*p > 0x20) |
1519 | { |
1546 | { |
|
|
1547 | if (*p == '#') |
|
|
1548 | { |
|
|
1549 | self->incr_mode = INCR_M_C0; |
|
|
1550 | goto incr_m_c; |
|
|
1551 | } |
|
|
1552 | else |
|
|
1553 | { |
1520 | self->incr_mode = INCR_M_JSON; |
1554 | self->incr_mode = INCR_M_JSON; |
1521 | goto incr_m_json; |
1555 | goto incr_m_json; |
|
|
1556 | } |
1522 | } |
1557 | } |
1523 | else if (!*p) |
1558 | else if (!*p) |
1524 | goto interrupt; |
1559 | goto interrupt; |
1525 | |
1560 | |
1526 | ++p; |
1561 | ++p; |
… | |
… | |
1532 | goto interrupt; |
1567 | goto interrupt; |
1533 | |
1568 | |
1534 | ++p; |
1569 | ++p; |
1535 | self->incr_mode = INCR_M_STR; |
1570 | self->incr_mode = INCR_M_STR; |
1536 | goto incr_m_str; |
1571 | goto incr_m_str; |
|
|
1572 | |
|
|
1573 | // inside #-style comments |
|
|
1574 | case INCR_M_C0: |
|
|
1575 | case INCR_M_C1: |
|
|
1576 | incr_m_c: |
|
|
1577 | for (;;) |
|
|
1578 | { |
|
|
1579 | if (*p == '\n') |
|
|
1580 | { |
|
|
1581 | self->incr_mode = self->incr_mode == INCR_M_C0 ? INCR_M_WS : INCR_M_JSON; |
|
|
1582 | break; |
|
|
1583 | } |
|
|
1584 | else if (!*p) |
|
|
1585 | goto interrupt; |
|
|
1586 | |
|
|
1587 | ++p; |
|
|
1588 | } |
|
|
1589 | |
|
|
1590 | break; |
1537 | |
1591 | |
1538 | // inside a string |
1592 | // inside a string |
1539 | case INCR_M_STR: |
1593 | case INCR_M_STR: |
1540 | incr_m_str: |
1594 | incr_m_str: |
1541 | for (;;) |
1595 | for (;;) |
… | |
… | |
1600 | |
1654 | |
1601 | case ']': |
1655 | case ']': |
1602 | case '}': |
1656 | case '}': |
1603 | if (--self->incr_nest <= 0) |
1657 | if (--self->incr_nest <= 0) |
1604 | goto interrupt; |
1658 | goto interrupt; |
|
|
1659 | break; |
|
|
1660 | |
|
|
1661 | case '#': |
|
|
1662 | self->incr_mode = INCR_M_C1; |
|
|
1663 | goto incr_m_c; |
1605 | } |
1664 | } |
1606 | } |
1665 | } |
1607 | } |
1666 | } |
1608 | |
1667 | |
1609 | modechange: |
1668 | modechange: |
1610 | ; |
1669 | ; |
1611 | } |
1670 | } |
1612 | |
1671 | |
1613 | interrupt: |
1672 | interrupt: |
1614 | self->incr_pos = p - SvPVX (self->incr_text); |
1673 | self->incr_pos = p - SvPVX (self->incr_text); |
|
|
1674 | //printf ("interrupt<%.*s>\n", self->incr_pos, SvPVX(self->incr_text));//D |
1615 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1675 | //printf ("return pos %d mode %d nest %d\n", self->incr_pos, self->incr_mode, self->incr_nest);//D |
1616 | } |
1676 | } |
1617 | |
1677 | |
1618 | ///////////////////////////////////////////////////////////////////////////// |
1678 | ///////////////////////////////////////////////////////////////////////////// |
1619 | // XS interface functions |
1679 | // XS interface functions |
… | |
… | |
1634 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1694 | json_stash = gv_stashpv ("JSON::XS" , 1); |
1635 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1695 | json_boolean_stash = gv_stashpv ("JSON::XS::Boolean", 1); |
1636 | |
1696 | |
1637 | json_true = get_bool ("JSON::XS::true"); |
1697 | json_true = get_bool ("JSON::XS::true"); |
1638 | json_false = get_bool ("JSON::XS::false"); |
1698 | json_false = get_bool ("JSON::XS::false"); |
|
|
1699 | |
|
|
1700 | CvNODEBUG_on (get_cv ("JSON::XS::incr_text", 0)); /* the debugger completely breaks lvalue subs */ |
1639 | } |
1701 | } |
1640 | |
1702 | |
1641 | PROTOTYPES: DISABLE |
1703 | PROTOTYPES: DISABLE |
1642 | |
1704 | |
1643 | void CLONE (...) |
1705 | void CLONE (...) |
… | |
… | |
1763 | XPUSHs (decode_json (jsonstr, self, 0)); |
1825 | XPUSHs (decode_json (jsonstr, self, 0)); |
1764 | |
1826 | |
1765 | void decode_prefix (JSON *self, SV *jsonstr) |
1827 | void decode_prefix (JSON *self, SV *jsonstr) |
1766 | PPCODE: |
1828 | PPCODE: |
1767 | { |
1829 | { |
1768 | STRLEN offset; |
1830 | char *offset; |
1769 | EXTEND (SP, 2); |
1831 | EXTEND (SP, 2); |
1770 | PUSHs (decode_json (jsonstr, self, &offset)); |
1832 | PUSHs (decode_json (jsonstr, self, &offset)); |
1771 | PUSHs (sv_2mortal (newSVuv (offset))); |
1833 | PUSHs (sv_2mortal (newSVuv (ptr_to_index (jsonstr, offset)))); |
1772 | } |
1834 | } |
1773 | |
1835 | |
1774 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1836 | void incr_parse (JSON *self, SV *jsonstr = 0) |
1775 | PPCODE: |
1837 | PPCODE: |
1776 | { |
1838 | { |
… | |
… | |
1778 | self->incr_text = newSVpvn ("", 0); |
1840 | self->incr_text = newSVpvn ("", 0); |
1779 | |
1841 | |
1780 | // append data, if any |
1842 | // append data, if any |
1781 | if (jsonstr) |
1843 | if (jsonstr) |
1782 | { |
1844 | { |
1783 | if (SvUTF8 (jsonstr) && !SvUTF8 (self->incr_text)) |
1845 | if (SvUTF8 (jsonstr)) |
1784 | { |
1846 | { |
|
|
1847 | if (!SvUTF8 (self->incr_text)) |
|
|
1848 | { |
1785 | /* utf-8-ness differs, need to upgrade */ |
1849 | /* utf-8-ness differs, need to upgrade */ |
1786 | sv_utf8_upgrade (self->incr_text); |
1850 | sv_utf8_upgrade (self->incr_text); |
1787 | |
1851 | |
1788 | if (self->incr_pos) |
1852 | if (self->incr_pos) |
1789 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1853 | self->incr_pos = utf8_hop ((U8 *)SvPVX (self->incr_text), self->incr_pos) |
1790 | - (U8 *)SvPVX (self->incr_text); |
1854 | - (U8 *)SvPVX (self->incr_text); |
|
|
1855 | } |
1791 | } |
1856 | } |
|
|
1857 | else if (SvUTF8 (self->incr_text)) |
|
|
1858 | sv_utf8_upgrade (jsonstr); |
1792 | |
1859 | |
1793 | { |
1860 | { |
1794 | STRLEN len; |
1861 | STRLEN len; |
1795 | const char *str = SvPV (jsonstr, len); |
1862 | const char *str = SvPV (jsonstr, len); |
1796 | SvGROW (self->incr_text, SvCUR (self->incr_text) + len + 1); |
1863 | STRLEN cur = SvCUR (self->incr_text); |
|
|
1864 | |
|
|
1865 | if (SvLEN (self->incr_text) <= cur + len) |
|
|
1866 | SvGROW (self->incr_text, cur + (len < (cur >> 2) ? cur >> 2 : len) + 1); |
|
|
1867 | |
1797 | Move (str, SvEND (self->incr_text), len, char); |
1868 | Move (str, SvEND (self->incr_text), len, char); |
1798 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1869 | SvCUR_set (self->incr_text, SvCUR (self->incr_text) + len); |
1799 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1870 | *SvEND (self->incr_text) = 0; // this should basically be a nop, too, but make sure it's there |
1800 | } |
1871 | } |
1801 | } |
1872 | } |
1802 | |
1873 | |
1803 | if (GIMME_V != G_VOID) |
1874 | if (GIMME_V != G_VOID) |
1804 | do |
1875 | do |
1805 | { |
1876 | { |
1806 | STRLEN offset; |
1877 | char *offset; |
1807 | |
1878 | |
1808 | if (!INCR_DONE (self)) |
1879 | if (!INCR_DONE (self)) |
1809 | { |
1880 | { |
1810 | incr_parse (self); |
1881 | incr_parse (self); |
1811 | |
1882 | |
… | |
… | |
1817 | break; |
1888 | break; |
1818 | } |
1889 | } |
1819 | |
1890 | |
1820 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1891 | XPUSHs (decode_json (self->incr_text, self, &offset)); |
1821 | |
1892 | |
1822 | sv_chop (self->incr_text, SvPV_nolen (self->incr_text) + offset); |
|
|
1823 | self->incr_pos -= offset; |
1893 | self->incr_pos -= offset - SvPVX (self->incr_text); |
1824 | self->incr_nest = 0; |
1894 | self->incr_nest = 0; |
1825 | self->incr_mode = 0; |
1895 | self->incr_mode = 0; |
|
|
1896 | |
|
|
1897 | sv_chop (self->incr_text, offset); |
1826 | } |
1898 | } |
1827 | while (GIMME_V == G_ARRAY); |
1899 | while (GIMME_V == G_ARRAY); |
1828 | } |
1900 | } |
1829 | |
1901 | |
1830 | SV *incr_text (JSON *self) |
1902 | SV *incr_text (JSON *self) |