-
Notifications
You must be signed in to change notification settings - Fork 3
/
Dumper.xs
1698 lines (1555 loc) · 48.9 KB
/
Dumper.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#ifdef USE_PPPORT_H
# define NEED_my_snprintf
# define NEED_sv_2pv_flags
# include "ppport.h"
#endif
#if PERL_VERSION < 8
# define DD_USE_OLD_ID_FORMAT
#endif
/* These definitions are ASCII only. But the pure-perl .pm avoids
* calling this .xs file for releases where they aren't defined */
#ifndef isASCII
# define isASCII(c) (((UV) (c)) < 128)
#endif
#ifndef ESC_NATIVE /* \e */
# define ESC_NATIVE 27
#endif
#ifndef isPRINT
# define isPRINT(c) (((UV) (c)) >= ' ' && ((UV) (c)) < 127)
#endif
#ifndef isALPHA
# define isALPHA(c) ( (((UV) (c)) >= 'a' && ((UV) (c)) <= 'z') \
|| (((UV) (c)) <= 'Z' && ((UV) (c)) >= 'A'))
#endif
#ifndef isIDFIRST
# define isIDFIRST(c) (isALPHA(c) || (c) == '_')
#endif
#ifndef isWORDCHAR
# define isWORDCHAR(c) (isIDFIRST(c) \
|| (((UV) (c)) >= '0' && ((UV) (c)) <= '9'))
#endif
/* SvPVCLEAR only from perl 5.25.6 */
#ifndef SvPVCLEAR
# define SvPVCLEAR(sv) sv_setpvs((sv), "")
#endif
/* This struct contains almost all the user's desired configuration, and it
* is treated as constant by the recursive function. This arrangement has
* the advantage of needing less memory than passing all of them on the
* stack all the time (as was the case in an earlier implementation). */
typedef struct {
SV *pad;
SV *xpad;
SV *sep;
SV *pair;
SV *sortkeys;
SV *freezer;
SV *toaster;
SV *bless;
IV maxrecurse;
I32 indent;
I32 purity;
I32 deepcopy;
I32 quotekeys;
I32 maxdepth;
I32 useqq;
int use_sparse_seen_hash;
int trailingcomma;
int deparse;
} Style;
static STRLEN num_q (const char *s, STRLEN slen);
static STRLEN esc_q (char *dest, const char *src, STRLEN slen);
static STRLEN esc_q_utf8 (pTHX_ SV *sv, const char *src, STRLEN slen, I32 do_utf8, I32 useqq);
static bool globname_needs_quote(const char *s, STRLEN len);
static bool key_needs_quote(const char *s, STRLEN len);
static bool safe_decimal_number(const char *p, STRLEN len);
static SV *sv_x (pTHX_ SV *sv, const char *str, STRLEN len, I32 n);
static I32 DD_dump (pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval,
HV *seenhv, AV *postav, const I32 level, SV *apad,
const Style *style);
#ifndef HvNAME_get
#define HvNAME_get HvNAME
#endif
/* Perls 7 through portions of 15 used utf8_to_uvchr() which didn't have a
* length parameter. This wrongly allowed reading beyond the end of buffer
* given malformed input */
#if PERL_VERSION <= 6 /* Perl 5.6 and earlier */
UV
Perl_utf8_to_uvchr_buf(pTHX_ U8 *s, U8 *send, STRLEN *retlen)
{
const UV uv = utf8_to_uv(s, send - s, retlen,
ckWARN(WARN_UTF8) ? 0 : UTF8_ALLOW_ANY);
return UNI_TO_NATIVE(uv);
}
# if !defined(PERL_IMPLICIT_CONTEXT)
# define utf8_to_uvchr_buf Perl_utf8_to_uvchr_buf
# else
# define utf8_to_uvchr_buf(a,b,c) Perl_utf8_to_uvchr_buf(aTHX_ a,b,c)
# endif
#endif /* PERL_VERSION <= 6 */
/* Perl 5.7 through part of 5.15 */
#if PERL_VERSION > 6 && PERL_VERSION <= 15 && ! defined(utf8_to_uvchr_buf)
UV
Perl_utf8_to_uvchr_buf(pTHX_ U8 *s, U8 *send, STRLEN *retlen)
{
/* We have to discard <send> for these versions; hence can read off the
* end of the buffer if there is a malformation that indicates the
* character is longer than the space available */
return utf8_to_uvchr(s, retlen);
}
# if !defined(PERL_IMPLICIT_CONTEXT)
# define utf8_to_uvchr_buf Perl_utf8_to_uvchr_buf
# else
# define utf8_to_uvchr_buf(a,b,c) Perl_utf8_to_uvchr_buf(aTHX_ a,b,c)
# endif
#endif /* PERL_VERSION > 6 && <= 15 */
/* Changes in 5.7 series mean that now IOK is only set if scalar is
precisely integer but in 5.6 and earlier we need to do a more
complex test */
#if PERL_VERSION <= 6
#define DD_is_integer(sv) (SvIOK(sv) && (SvIsUV(val) ? SvUV(sv) == SvNV(sv) : SvIV(sv) == SvNV(sv)))
#else
#define DD_is_integer(sv) SvIOK(sv)
#endif
/* does a glob name need to be protected? */
static bool
globname_needs_quote(const char *s, STRLEN len)
{
const char *send = s+len;
TOP:
if (s[0] == ':') {
if (++s<send) {
if (*s++ != ':')
return TRUE;
}
else
return TRUE;
}
if (isIDFIRST(*s)) {
while (++s<send)
if (!isWORDCHAR(*s)) {
if (*s == ':')
goto TOP;
else
return TRUE;
}
}
else
return TRUE;
return FALSE;
}
/* does a hash key need to be quoted (to the left of => ).
Previously this used (globname_)needs_quote() which accepted strings
like '::foo', but these aren't safe as unquoted keys under strict.
*/
static bool
key_needs_quote(const char *s, STRLEN len) {
const char *send = s+len;
if (safe_decimal_number(s, len)) {
return FALSE;
}
else if (isIDFIRST(*s)) {
while (++s<send)
if (!isWORDCHAR(*s))
return TRUE;
}
else
return TRUE;
return FALSE;
}
/* Check that the SV can be represented as a simple decimal integer.
*
* The perl code does this by matching against /^(?:0|-?[1-9]\d{0,8})\z/
*/
static bool
safe_decimal_number(const char *p, STRLEN len) {
if (len == 1 && *p == '0')
return TRUE;
if (len && *p == '-') {
++p;
--len;
}
if (len == 0 || *p < '1' || *p > '9')
return FALSE;
++p;
--len;
if (len > 8)
return FALSE;
while (len > 0) {
/* the perl code checks /\d/ but we don't want unicode digits here */
if (*p < '0' || *p > '9')
return FALSE;
++p;
--len;
}
return TRUE;
}
/* count the number of "'"s and "\"s in string */
static STRLEN
num_q(const char *s, STRLEN slen)
{
STRLEN ret = 0;
while (slen > 0) {
if (*s == '\'' || *s == '\\')
++ret;
++s;
--slen;
}
return ret;
}
/* returns number of chars added to escape "'"s and "\"s in s */
/* slen number of characters in s will be escaped */
/* destination must be long enough for additional chars */
static STRLEN
esc_q(char *d, const char *s, STRLEN slen)
{
STRLEN ret = 0;
while (slen > 0) {
switch (*s) {
case '\'':
case '\\':
*d = '\\';
++d; ++ret;
/* FALLTHROUGH */
default:
*d = *s;
++d; ++s; --slen;
break;
}
}
return ret;
}
/* this function is also misused for implementing $Useqq */
static STRLEN
esc_q_utf8(pTHX_ SV* sv, const char *src, STRLEN slen, I32 do_utf8, I32 useqq)
{
char *r, *rstart;
const char *s = src;
const char * const send = src + slen;
STRLEN j, cur = SvCUR(sv);
/* Could count 128-255 and 256+ in two variables, if we want to
be like &qquote and make a distinction. */
STRLEN grow = 0; /* bytes needed to represent chars 128+ */
/* STRLEN topbit_grow = 0; bytes needed to represent chars 128-255 */
STRLEN backslashes = 0;
STRLEN single_quotes = 0;
STRLEN qq_escapables = 0; /* " $ @ will need a \ in "" strings. */
STRLEN normal = 0;
int increment;
for (s = src; s < send; s += increment) { /* Sizing pass */
UV k = *(U8*)s;
increment = 1; /* Will override if necessary for utf-8 */
if (isPRINT(k)) {
if (k == '\\') {
backslashes++;
} else if (k == '\'') {
single_quotes++;
} else if (k == '"' || k == '$' || k == '@') {
qq_escapables++;
} else {
normal++;
}
}
else if (! isASCII(k) && k > ' ') {
/* High ordinal non-printable code point. (The test that k is
* above SPACE should be optimized out by the compiler on
* non-EBCDIC platforms; otherwise we could put an #ifdef around
* it, but it's better to have just a single code path when
* possible. All but one of the non-ASCII EBCDIC controls are low
* ordinal; that one is the only one above SPACE.)
*
* If UTF-8, output as hex, regardless of useqq. This means there
* is an overhead of 4 chars '\x{}'. Then count the number of hex
* digits. */
if (do_utf8) {
k = utf8_to_uvchr_buf((U8*)s, (U8*) send, NULL);
/* treat invalid utf8 byte by byte. This loop iteration gets the
* first byte */
increment = (k == 0 && *s != '\0') ? 1 : UTF8SKIP(s);
grow += 4 + (k <= 0xFF ? 2 : k <= 0xFFF ? 3 : k <= 0xFFFF ? 4 :
#if UVSIZE == 4
8 /* We may allocate a bit more than the minimum here. */
#else
k <= 0xFFFFFFFF ? 8 : UVSIZE * 4
#endif
);
}
else if (useqq) { /* Not utf8, must be <= 0xFF, hence 2 hex
* digits. */
grow += 4 + 2;
}
else { /* Non-qq generates 3 octal digits plus backslash */
grow += 4;
}
} /* End of high-ordinal non-printable */
else if (! useqq) { /* Low ordinal, non-printable, non-qq just
* outputs the raw char */
normal++;
}
else { /* Is qq, low ordinal, non-printable. Output escape
* sequences */
if ( k == '\a' || k == '\b' || k == '\t' || k == '\n' || k == '\r'
|| k == '\f' || k == ESC_NATIVE)
{
grow += 2; /* 1 char plus backslash */
}
else /* The other low ordinals are output as an octal escape
* sequence */
if (s + 1 >= send || ( *(U8*)(s+1) >= '0'
&& *(U8*)(s+1) <= '9'))
{
/* When the following character is a digit, use 3 octal digits
* plus backslash, as using fewer digits would concatenate the
* following char into this one */
grow += 4;
}
else if (k <= 7) {
grow += 2; /* 1 octal digit, plus backslash */
}
else if (k <= 077) {
grow += 3; /* 2 octal digits plus backslash */
}
else {
grow += 4; /* 3 octal digits plus backslash */
}
}
} /* End of size-calculating loop */
if (grow || useqq) {
/* We have something needing hex. 3 is ""\0 */
sv_grow(sv, cur + 3 + grow + 2*backslashes + single_quotes
+ 2*qq_escapables + normal);
rstart = r = SvPVX(sv) + cur;
*r++ = '"';
for (s = src; s < send; s += increment) {
UV k;
if (do_utf8
&& ! isASCII(*s)
/* Exclude non-ASCII low ordinal controls. This should be
* optimized out by the compiler on ASCII platforms; if not
* could wrap it in a #ifdef EBCDIC, but better to avoid
* #if's if possible */
&& *(U8*)s > ' '
) {
/* When in UTF-8, we output all non-ascii chars as \x{}
* reqardless of useqq, except for the low ordinal controls on
* EBCDIC platforms */
k = utf8_to_uvchr_buf((U8*)s, (U8*) send, NULL);
/* treat invalid utf8 byte by byte. This loop iteration gets the
* first byte */
increment = (k == 0 && *s != '\0') ? 1 : UTF8SKIP(s);
#if PERL_VERSION < 10
sprintf(r, "\\x{%" UVxf "}", k);
r += strlen(r);
/* my_sprintf is not supported by ppport.h */
#else
r = r + my_sprintf(r, "\\x{%" UVxf "}", k);
#endif
continue;
}
/* Here 1) isn't UTF-8; or
* 2) the current character is ASCII; or
* 3) it is an EBCDIC platform and is a low ordinal
* non-ASCII control.
* In each case the character occupies just one byte */
k = *(U8*)s;
increment = 1;
if (isPRINT(k)) {
/* These need a backslash escape */
if (k == '"' || k == '\\' || k == '$' || k == '@') {
*r++ = '\\';
}
*r++ = (char)k;
}
else if (! useqq) { /* non-qq, non-printable, low-ordinal is
* output raw */
*r++ = (char)k;
}
else { /* Is qq means use escape sequences */
bool next_is_digit;
*r++ = '\\';
switch (k) {
case '\a': *r++ = 'a'; break;
case '\b': *r++ = 'b'; break;
case '\t': *r++ = 't'; break;
case '\n': *r++ = 'n'; break;
case '\f': *r++ = 'f'; break;
case '\r': *r++ = 'r'; break;
case ESC_NATIVE: *r++ = 'e'; break;
default:
/* only ASCII digits matter here, which are invariant,
* since we only encode characters \377 and under, or
* \x177 and under for a unicode string
*/
next_is_digit = (s + 1 >= send )
? FALSE
: (*(U8*)(s+1) >= '0' && *(U8*)(s+1) <= '9');
/* faster than
* r = r + my_sprintf(r, "%o", k);
*/
if (k <= 7 && !next_is_digit) {
*r++ = (char)k + '0';
} else if (k <= 63 && !next_is_digit) {
*r++ = (char)(k>>3) + '0';
*r++ = (char)(k&7) + '0';
} else {
*r++ = (char)(k>>6) + '0';
*r++ = (char)((k&63)>>3) + '0';
*r++ = (char)(k&7) + '0';
}
}
}
}
*r++ = '"';
} else {
/* Single quotes. */
sv_grow(sv, cur + 3 + 2*backslashes + 2*single_quotes
+ qq_escapables + normal);
rstart = r = SvPVX(sv) + cur;
*r++ = '\'';
for (s = src; s < send; s ++) {
const char k = *s;
if (k == '\'' || k == '\\')
*r++ = '\\';
*r++ = k;
}
*r++ = '\'';
}
*r = '\0';
j = r - rstart;
SvCUR_set(sv, cur + j);
return j;
}
/* append a repeated string to an SV */
static SV *
sv_x(pTHX_ SV *sv, const char *str, STRLEN len, I32 n)
{
if (!sv)
sv = newSVpvs("");
#ifdef DEBUGGING
else
assert(SvTYPE(sv) >= SVt_PV);
#endif
if (n > 0) {
SvGROW(sv, len*n + SvCUR(sv) + 1);
if (len == 1) {
char * const start = SvPVX(sv) + SvCUR(sv);
SvCUR_set(sv, SvCUR(sv) + n);
start[n] = '\0';
while (n > 0)
start[--n] = str[0];
}
else
while (n > 0) {
sv_catpvn(sv, str, len);
--n;
}
}
return sv;
}
static SV *
deparsed_output(pTHX_ SV *val)
{
SV *text;
int n;
dSP;
/* This is passed to load_module(), which decrements its ref count and
* modifies it (so we also can't reuse it below) */
SV *pkg = newSVpvs("B::Deparse");
load_module(PERL_LOADMOD_NOIMPORT, pkg, 0);
SAVETMPS;
PUSHMARK(SP);
mXPUSHs(newSVpvs("B::Deparse"));
PUTBACK;
n = call_method("new", G_SCALAR);
SPAGAIN;
if (n != 1) {
croak("B::Deparse->new returned %d items, but expected exactly 1", n);
}
PUSHMARK(SP - n);
XPUSHs(val);
PUTBACK;
n = call_method("coderef2text", G_SCALAR);
SPAGAIN;
if (n != 1) {
croak("$b_deparse->coderef2text returned %d items, but expected exactly 1", n);
}
text = POPs;
SvREFCNT_inc(text); /* the caller will mortalise this */
FREETMPS;
PUTBACK;
return text;
}
/*
* This ought to be split into smaller functions. (it is one long function since
* it exactly parallels the perl version, which was one long thing for
* efficiency raisins.) Ugggh!
*/
static I32
DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
AV *postav, const I32 level, SV *apad, const Style *style)
{
char tmpbuf[128];
Size_t i;
char *c, *r, *realpack;
#ifdef DD_USE_OLD_ID_FORMAT
char id[128];
#else
UV id_buffer;
char *const id = (char *)&id_buffer;
#endif
SV **svp;
SV *sv, *ipad, *ival;
SV *blesspad = Nullsv;
AV *seenentry = NULL;
char *iname;
STRLEN inamelen, idlen = 0;
U32 realtype;
bool no_bless = 0; /* when a qr// is blessed into Regexp we dont want to bless it.
in later perls we should actually check the classname of the
engine. this gets tricky as it involves lexical issues that arent so
easy to resolve */
bool is_regex = 0; /* we are dumping a regex, we need to know this before we bless */
if (!val)
return 0;
/* If the output buffer has less than some arbitrary amount of space
remaining, then enlarge it. For the test case (25M of output),
*1.1 was slower, *2.0 was the same, so the first guess of 1.5 is
deemed to be good enough. */
if (SvTYPE(retval) >= SVt_PV && (SvLEN(retval) - SvCUR(retval)) < 42) {
sv_grow(retval, SvCUR(retval) * 3 / 2);
}
realtype = SvTYPE(val);
if (SvGMAGICAL(val))
mg_get(val);
if (SvROK(val)) {
/* If a freeze method is provided and the object has it, call
it. Warn on errors. */
if (SvOBJECT(SvRV(val)) && style->freezer &&
SvPOK(style->freezer) && SvCUR(style->freezer) &&
gv_fetchmeth(SvSTASH(SvRV(val)), SvPVX_const(style->freezer),
SvCUR(style->freezer), -1) != NULL)
{
dSP; ENTER; SAVETMPS; PUSHMARK(sp);
XPUSHs(val); PUTBACK;
i = perl_call_method(SvPVX_const(style->freezer), G_EVAL|G_VOID|G_DISCARD);
SPAGAIN;
if (SvTRUE(ERRSV))
warn("WARNING(Freezer method call failed): %" SVf, ERRSV);
PUTBACK; FREETMPS; LEAVE;
}
ival = SvRV(val);
realtype = SvTYPE(ival);
#ifdef DD_USE_OLD_ID_FORMAT
idlen = my_snprintf(id, sizeof(id), "0x%" UVxf, PTR2UV(ival));
#else
id_buffer = PTR2UV(ival);
idlen = sizeof(id_buffer);
#endif
if (SvOBJECT(ival))
realpack = HvNAME_get(SvSTASH(ival));
else
realpack = NULL;
/* if it has a name, we need to either look it up, or keep a tab
* on it so we know when we hit it later
*/
if (namelen) {
if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
&& (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
{
SV *othername;
if ((svp = av_fetch(seenentry, 0, FALSE))
&& (othername = *svp))
{
if (style->purity && level > 0) {
SV *postentry;
if (realtype == SVt_PVHV)
sv_catpvs(retval, "{}");
else if (realtype == SVt_PVAV)
sv_catpvs(retval, "[]");
else
sv_catpvs(retval, "do{my $o}");
postentry = newSVpvn(name, namelen);
sv_catpvs(postentry, " = ");
sv_catsv(postentry, othername);
av_push(postav, postentry);
}
else {
if (name[0] == '@' || name[0] == '%') {
if ((SvPVX_const(othername))[0] == '\\' &&
(SvPVX_const(othername))[1] == name[0]) {
sv_catpvn(retval, SvPVX_const(othername)+1,
SvCUR(othername)-1);
}
else {
sv_catpvn(retval, name, 1);
sv_catpvs(retval, "{");
sv_catsv(retval, othername);
sv_catpvs(retval, "}");
}
}
else
sv_catsv(retval, othername);
}
return 1;
}
else {
#ifdef DD_USE_OLD_ID_FORMAT
warn("ref name not found for %s", id);
#else
warn("ref name not found for 0x%" UVxf, PTR2UV(ival));
#endif
return 0;
}
}
else { /* store our name and continue */
SV *namesv;
if (name[0] == '@' || name[0] == '%') {
namesv = newSVpvs("\\");
sv_catpvn(namesv, name, namelen);
}
else if (realtype == SVt_PVCV && name[0] == '*') {
namesv = newSVpvs("\\");
sv_catpvn(namesv, name, namelen);
(SvPVX(namesv))[1] = '&';
}
else
namesv = newSVpvn(name, namelen);
seenentry = newAV();
av_push(seenentry, namesv);
(void)SvREFCNT_inc(val);
av_push(seenentry, val);
(void)hv_store(seenhv, id, idlen,
newRV_inc((SV*)seenentry), 0);
SvREFCNT_dec(seenentry);
}
}
/* regexps dont have to be blessed into package "Regexp"
* they can be blessed into any package.
*/
#if PERL_VERSION < 8
if (realpack && *realpack == 'R' && strEQ(realpack, "Regexp"))
#elif PERL_VERSION < 11
if (realpack && realtype == SVt_PVMG && mg_find(ival, PERL_MAGIC_qr))
#else
if (realpack && realtype == SVt_REGEXP)
#endif
{
is_regex = 1;
if (strEQ(realpack, "Regexp"))
no_bless = 1;
else
no_bless = 0;
}
/* If purity is not set and maxdepth is set, then check depth:
* if we have reached maximum depth, return the string
* representation of the thing we are currently examining
* at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)').
*/
if (!style->purity && style->maxdepth > 0 && level >= style->maxdepth) {
STRLEN vallen;
const char * const valstr = SvPV(val,vallen);
sv_catpvs(retval, "'");
sv_catpvn(retval, valstr, vallen);
sv_catpvs(retval, "'");
return 1;
}
if (style->maxrecurse > 0 && level >= style->maxrecurse) {
croak("Recursion limit of %" IVdf " exceeded", style->maxrecurse);
}
if (realpack && !no_bless) { /* we have a blessed ref */
STRLEN blesslen;
const char * const blessstr = SvPV(style->bless, blesslen);
sv_catpvn(retval, blessstr, blesslen);
sv_catpvs(retval, "( ");
if (style->indent >= 2) {
blesspad = apad;
apad = newSVsv(apad);
sv_x(aTHX_ apad, " ", 1, blesslen+2);
}
}
ipad = sv_x(aTHX_ Nullsv, SvPVX_const(style->xpad), SvCUR(style->xpad), level+1);
if (is_regex)
{
STRLEN rlen;
SV *sv_pattern = NULL;
SV *sv_flags = NULL;
CV *re_pattern_cv;
const char *rval;
const char *rend;
const char *slash;
if ((re_pattern_cv = get_cv("re::regexp_pattern", 0))) {
dSP;
I32 count;
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(val);
PUTBACK;
count = call_sv((SV*)re_pattern_cv, G_ARRAY);
SPAGAIN;
if (count >= 2) {
sv_flags = POPs;
sv_pattern = POPs;
SvREFCNT_inc(sv_flags);
SvREFCNT_inc(sv_pattern);
}
PUTBACK;
FREETMPS;
LEAVE;
if (sv_pattern) {
sv_2mortal(sv_pattern);
sv_2mortal(sv_flags);
}
}
else {
sv_pattern = val;
}
assert(sv_pattern);
rval = SvPV(sv_pattern, rlen);
rend = rval+rlen;
slash = rval;
sv_catpvs(retval, "qr/");
for (;slash < rend; slash++) {
if (*slash == '\\') { ++slash; continue; }
if (*slash == '/') {
sv_catpvn(retval, rval, slash-rval);
sv_catpvs(retval, "\\/");
rlen -= slash-rval+1;
rval = slash+1;
}
}
sv_catpvn(retval, rval, rlen);
sv_catpvs(retval, "/");
if (sv_flags)
sv_catsv(retval, sv_flags);
}
else if (
#if PERL_VERSION < 9
realtype <= SVt_PVBM
#else
realtype <= SVt_PVMG
#endif
) { /* scalar ref */
SV * const namesv = newSVpvs("${");
sv_catpvn(namesv, name, namelen);
sv_catpvs(namesv, "}");
if (realpack) { /* blessed */
sv_catpvs(retval, "do{\\(my $o = ");
DD_dump(aTHX_ ival, SvPVX_const(namesv), SvCUR(namesv), retval, seenhv,
postav, level+1, apad, style);
sv_catpvs(retval, ")}");
} /* plain */
else {
sv_catpvs(retval, "\\");
DD_dump(aTHX_ ival, SvPVX_const(namesv), SvCUR(namesv), retval, seenhv,
postav, level+1, apad, style);
}
SvREFCNT_dec(namesv);
}
else if (realtype == SVt_PVGV) { /* glob ref */
SV * const namesv = newSVpvs("*{");
sv_catpvn(namesv, name, namelen);
sv_catpvs(namesv, "}");
sv_catpvs(retval, "\\");
DD_dump(aTHX_ ival, SvPVX_const(namesv), SvCUR(namesv), retval, seenhv,
postav, level+1, apad, style);
SvREFCNT_dec(namesv);
}
else if (realtype == SVt_PVAV) {
SV *totpad;
SSize_t ix = 0;
const SSize_t ixmax = av_len((AV *)ival);
SV * const ixsv = newSViv(0);
/* allowing for a 24 char wide array index */
New(0, iname, namelen+28, char);
(void)strcpy(iname, name);
inamelen = namelen;
if (name[0] == '@') {
sv_catpvs(retval, "(");
iname[0] = '$';
}
else {
sv_catpvs(retval, "[");
/* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
/*if (namelen > 0
&& name[namelen-1] != ']' && name[namelen-1] != '}'
&& (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
if ((namelen > 0
&& name[namelen-1] != ']' && name[namelen-1] != '}')
|| (namelen > 4
&& (name[1] == '{'
|| (name[0] == '\\' && name[2] == '{'))))
{
iname[inamelen++] = '-'; iname[inamelen++] = '>';
iname[inamelen] = '\0';
}
}
if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
(instr(iname+inamelen-8, "{SCALAR}") ||
instr(iname+inamelen-7, "{ARRAY}") ||
instr(iname+inamelen-6, "{HASH}"))) {
iname[inamelen++] = '-'; iname[inamelen++] = '>';
}
iname[inamelen++] = '['; iname[inamelen] = '\0';
totpad = newSVsv(style->sep);
sv_catsv(totpad, style->pad);
sv_catsv(totpad, apad);
for (ix = 0; ix <= ixmax; ++ix) {
STRLEN ilen;
SV *elem;
svp = av_fetch((AV*)ival, ix, FALSE);
if (svp)
elem = *svp;
else
elem = &PL_sv_undef;
ilen = inamelen;
sv_setiv(ixsv, ix);
#if PERL_VERSION < 10
(void) sprintf(iname+ilen, "%" IVdf, (IV)ix);
ilen = strlen(iname);
#else
ilen = ilen + my_sprintf(iname+ilen, "%" IVdf, (IV)ix);
#endif
iname[ilen++] = ']'; iname[ilen] = '\0';
if (style->indent >= 3) {
sv_catsv(retval, totpad);
sv_catsv(retval, ipad);
sv_catpvs(retval, "#");
sv_catsv(retval, ixsv);
}
sv_catsv(retval, totpad);
sv_catsv(retval, ipad);
DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
level+1, apad, style);
if (ix < ixmax || (style->trailingcomma && style->indent >= 1))
sv_catpvs(retval, ",");
}
if (ixmax >= 0) {
SV * const opad = sv_x(aTHX_ Nullsv, SvPVX_const(style->xpad), SvCUR(style->xpad), level);
sv_catsv(retval, totpad);
sv_catsv(retval, opad);
SvREFCNT_dec(opad);
}
if (name[0] == '@')
sv_catpvs(retval, ")");
else
sv_catpvs(retval, "]");
SvREFCNT_dec(ixsv);
SvREFCNT_dec(totpad);
Safefree(iname);
}
else if (realtype == SVt_PVHV) {
SV *totpad, *newapad;
SV *sname;
HE *entry = NULL;
char *key;
SV *hval;
AV *keys = NULL;
SV * const iname = newSVpvn(name, namelen);
if (name[0] == '%') {
sv_catpvs(retval, "(");
(SvPVX(iname))[0] = '$';
}
else {
sv_catpvs(retval, "{");
/* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
if ((namelen > 0
&& name[namelen-1] != ']' && name[namelen-1] != '}')
|| (namelen > 4
&& (name[1] == '{'
|| (name[0] == '\\' && name[2] == '{'))))
{
sv_catpvs(iname, "->");
}
}
if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
(instr(name+namelen-8, "{SCALAR}") ||
instr(name+namelen-7, "{ARRAY}") ||
instr(name+namelen-6, "{HASH}"))) {
sv_catpvs(iname, "->");
}
sv_catpvs(iname, "{");
totpad = newSVsv(style->sep);
sv_catsv(totpad, style->pad);
sv_catsv(totpad, apad);
/* If requested, get a sorted/filtered array of hash keys */
if (style->sortkeys) {
#if PERL_VERSION >= 8
if (style->sortkeys == &PL_sv_yes) {
keys = newAV();
(void)hv_iterinit((HV*)ival);
while ((entry = hv_iternext((HV*)ival))) {
sv = hv_iterkeysv(entry);
(void)SvREFCNT_inc(sv);
av_push(keys, sv);
}
# ifdef USE_LOCALE_COLLATE
# ifdef IN_LC /* Use this if available */
if (IN_LC(LC_COLLATE))
# else
if (IN_LOCALE)
# endif
{
sortsv(AvARRAY(keys),
av_len(keys)+1,
Perl_sv_cmp_locale);
}
else
# endif
{
sortsv(AvARRAY(keys),
av_len(keys)+1,
Perl_sv_cmp);