This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Re: Data::Dumper tests with -Mutf8
[perl5.git] / ext / Data / Dumper / Dumper.xs
CommitLineData
c5be433b 1#define PERL_NO_GET_CONTEXT
823edd99
GS
2#include "EXTERN.h"
3#include "perl.h"
4#include "XSUB.h"
823edd99 5
cceca5ed 6#ifndef PERL_VERSION
0f4592ef 7#include "patchlevel.h"
5a930efa 8#define PERL_VERSION PATCHLEVEL
cceca5ed 9#endif
0f4592ef 10
cceca5ed 11#if PERL_VERSION < 5
7820172a
GS
12# ifndef PL_sv_undef
13# define PL_sv_undef sv_undef
14# endif
15# ifndef ERRSV
16# define ERRSV GvSV(errgv)
17# endif
18# ifndef newSVpvn
19# define newSVpvn newSVpv
20# endif
21#endif
823edd99 22
20ce7b12
GS
23static I32 num_q (char *s, STRLEN slen);
24static I32 esc_q (char *dest, char *src, STRLEN slen);
6cde4e94 25static I32 esc_q_utf8 (pTHX_ SV *sv, char *src, STRLEN slen);
cea2e8a9
GS
26static SV *sv_x (pTHX_ SV *sv, char *str, STRLEN len, I32 n);
27static I32 DD_dump (pTHX_ SV *val, char *name, STRLEN namelen, SV *retval,
20ce7b12
GS
28 HV *seenhv, AV *postav, I32 *levelp, I32 indent,
29 SV *pad, SV *xpad, SV *apad, SV *sep,
30 SV *freezer, SV *toaster,
a2126434 31 I32 purity, I32 deepcopy, I32 quotekeys, SV *bless,
e9105f86 32 I32 maxdepth, SV *sortkeys);
823edd99
GS
33
34/* does a string need to be protected? */
35static I32
36needs_quote(register char *s)
37{
38TOP:
39 if (s[0] == ':') {
40 if (*++s) {
41 if (*s++ != ':')
42 return 1;
43 }
44 else
45 return 1;
46 }
47 if (isIDFIRST(*s)) {
48 while (*++s)
7b0972df 49 if (!isALNUM(*s)) {
823edd99
GS
50 if (*s == ':')
51 goto TOP;
52 else
53 return 1;
7b0972df 54 }
823edd99 55 }
6cde4e94 56 else
823edd99
GS
57 return 1;
58 return 0;
59}
60
61/* count the number of "'"s and "\"s in string */
62static I32
6c1ab3c2 63num_q(register char *s, register STRLEN slen)
823edd99
GS
64{
65 register I32 ret = 0;
6c1ab3c2
SR
66
67 while (slen > 0) {
823edd99
GS
68 if (*s == '\'' || *s == '\\')
69 ++ret;
70 ++s;
6c1ab3c2 71 --slen;
823edd99
GS
72 }
73 return ret;
74}
75
76
77/* returns number of chars added to escape "'"s and "\"s in s */
78/* slen number of characters in s will be escaped */
79/* destination must be long enough for additional chars */
80static I32
81esc_q(register char *d, register char *s, register STRLEN slen)
82{
83 register I32 ret = 0;
6cde4e94 84
823edd99
GS
85 while (slen > 0) {
86 switch (*s) {
87 case '\'':
88 case '\\':
89 *d = '\\';
90 ++d; ++ret;
91 default:
92 *d = *s;
93 ++d; ++s; --slen;
94 break;
95 }
96 }
97 return ret;
98}
99
dc71dc59 100static I32
6cde4e94 101esc_q_utf8(pTHX_ SV* sv, register char *src, register STRLEN slen)
dc71dc59 102{
f052740f
NC
103 char *s, *send, *r, *rstart;
104 STRLEN j, cur = SvCUR(sv);
105 /* Could count 128-255 and 256+ in two variables, if we want to
106 be like &qquote and make a distinction. */
107 STRLEN grow = 0; /* bytes needed to represent chars 128+ */
108 /* STRLEN topbit_grow = 0; bytes needed to represent chars 128-255 */
109 STRLEN backslashes = 0;
110 STRLEN single_quotes = 0;
111 STRLEN qq_escapables = 0; /* " $ @ will need a \ in "" strings. */
112 STRLEN normal = 0;
6cde4e94 113
dc71dc59
JH
114 /* this will need EBCDICification */
115 for (s = src, send = src + slen; s < send; s += UTF8SKIP(s)) {
f052740f
NC
116 UV k = utf8_to_uvchr((U8*)s, NULL);
117
118 if (k > 127) {
119 /* 4: \x{} then count the number of hex digits. */
120 grow += 4 + (k <= 0xFF ? 2 : k <= 0xFFF ? 3 : k <= 0xFFFF ? 4 :
121#if UVSIZE == 4
122 8 /* We may allocate a bit more than the minimum here. */
123#else
124 k <= 0xFFFFFFFF ? 8 : UVSIZE * 4
125#endif
126 );
127 } else if (k == '\\') {
128 backslashes++;
129 } else if (k == '\'') {
130 single_quotes++;
131 } else if (k == '"' || k == '$' || k == '@') {
132 qq_escapables++;
133 } else {
134 normal++;
135 }
dc71dc59 136 }
f052740f
NC
137 if (grow) {
138 /* We have something needing hex. 3 is ""\0 */
34231210
NC
139 sv_grow(sv, cur + 3 + grow + 2*backslashes + single_quotes
140 + 2*qq_escapables + normal);
f052740f
NC
141 rstart = r = SvPVX(sv) + cur;
142
143 *r++ = '"';
144
145 for (s = src; s < send; s += UTF8SKIP(s)) {
146 UV k = utf8_to_uvchr((U8*)s, NULL);
147
148 if (k == '"' || k == '\\' || k == '$' || k == '@') {
149 *r++ = '\\';
7c436af3 150 *r++ = (char)k;
f052740f
NC
151 }
152 else if (k < 0x80)
7c436af3 153 *r++ = (char)k;
f052740f
NC
154 else {
155 r += sprintf(r, "\\x{%"UVxf"}", k);
156 }
157 }
158 *r++ = '"';
159 } else {
160 /* Single quotes. */
34231210
NC
161 sv_grow(sv, cur + 3 + 2*backslashes + 2*single_quotes
162 + qq_escapables + normal);
f052740f
NC
163 rstart = r = SvPVX(sv) + cur;
164 *r++ = '\'';
165 for (s = src; s < send; s ++) {
166 char k = *s;
167 if (k == '\'' || k == '\\')
168 *r++ = '\\';
169 *r++ = k;
170 }
171 *r++ = '\'';
dc71dc59 172 }
f052740f
NC
173 *r = '\0';
174 j = r - rstart;
175 SvCUR_set(sv, cur + j);
dc71dc59
JH
176
177 return j;
178}
179
823edd99
GS
180/* append a repeated string to an SV */
181static SV *
cea2e8a9 182sv_x(pTHX_ SV *sv, register char *str, STRLEN len, I32 n)
823edd99
GS
183{
184 if (sv == Nullsv)
7820172a 185 sv = newSVpvn("", 0);
823edd99
GS
186 else
187 assert(SvTYPE(sv) >= SVt_PV);
188
189 if (n > 0) {
190 SvGROW(sv, len*n + SvCUR(sv) + 1);
191 if (len == 1) {
192 char *start = SvPVX(sv) + SvCUR(sv);
193 SvCUR(sv) += n;
194 start[n] = '\0';
195 while (n > 0)
196 start[--n] = str[0];
197 }
198 else
199 while (n > 0) {
200 sv_catpvn(sv, str, len);
201 --n;
202 }
203 }
204 return sv;
205}
206
207/*
208 * This ought to be split into smaller functions. (it is one long function since
209 * it exactly parallels the perl version, which was one long thing for
210 * efficiency raisins.) Ugggh!
211 */
212static I32
cea2e8a9 213DD_dump(pTHX_ SV *val, char *name, STRLEN namelen, SV *retval, HV *seenhv,
823edd99
GS
214 AV *postav, I32 *levelp, I32 indent, SV *pad, SV *xpad,
215 SV *apad, SV *sep, SV *freezer, SV *toaster, I32 purity,
e9105f86 216 I32 deepcopy, I32 quotekeys, SV *bless, I32 maxdepth, SV *sortkeys)
823edd99
GS
217{
218 char tmpbuf[128];
219 U32 i;
220 char *c, *r, *realpack, id[128];
221 SV **svp;
7820172a 222 SV *sv, *ipad, *ival;
823edd99 223 SV *blesspad = Nullsv;
7820172a 224 AV *seenentry = Nullav;
823edd99
GS
225 char *iname;
226 STRLEN inamelen, idlen = 0;
823edd99
GS
227 U32 realtype;
228
229 if (!val)
230 return 0;
231
823edd99 232 realtype = SvTYPE(val);
6cde4e94 233
823edd99
GS
234 if (SvGMAGICAL(val))
235 mg_get(val);
823edd99
GS
236 if (SvROK(val)) {
237
238 if (SvOBJECT(SvRV(val)) && freezer &&
239 SvPOK(freezer) && SvCUR(freezer))
240 {
241 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
242 XPUSHs(val); PUTBACK;
243 i = perl_call_method(SvPVX(freezer), G_EVAL|G_SCALAR);
244 SPAGAIN;
7820172a 245 if (SvTRUE(ERRSV))
823edd99 246 warn("WARNING(Freezer method call failed): %s",
7820172a 247 SvPVX(ERRSV));
823edd99
GS
248 else if (i)
249 val = newSVsv(POPs);
250 PUTBACK; FREETMPS; LEAVE;
251 if (i)
252 (void)sv_2mortal(val);
253 }
254
255 ival = SvRV(val);
823edd99 256 realtype = SvTYPE(ival);
c623ac67 257 (void) sprintf(id, "0x%"UVxf, PTR2UV(ival));
823edd99
GS
258 idlen = strlen(id);
259 if (SvOBJECT(ival))
260 realpack = HvNAME(SvSTASH(ival));
261 else
262 realpack = Nullch;
7820172a
GS
263
264 /* if it has a name, we need to either look it up, or keep a tab
265 * on it so we know when we hit it later
266 */
267 if (namelen) {
268 if ((svp = hv_fetch(seenhv, id, idlen, FALSE))
269 && (sv = *svp) && SvROK(sv) && (seenentry = (AV*)SvRV(sv)))
270 {
271 SV *othername;
272 if ((svp = av_fetch(seenentry, 0, FALSE))
273 && (othername = *svp))
274 {
275 if (purity && *levelp > 0) {
276 SV *postentry;
277
278 if (realtype == SVt_PVHV)
279 sv_catpvn(retval, "{}", 2);
280 else if (realtype == SVt_PVAV)
281 sv_catpvn(retval, "[]", 2);
282 else
5df59fb6 283 sv_catpvn(retval, "do{my $o}", 9);
7820172a
GS
284 postentry = newSVpvn(name, namelen);
285 sv_catpvn(postentry, " = ", 3);
286 sv_catsv(postentry, othername);
287 av_push(postav, postentry);
288 }
289 else {
290 if (name[0] == '@' || name[0] == '%') {
291 if ((SvPVX(othername))[0] == '\\' &&
292 (SvPVX(othername))[1] == name[0]) {
293 sv_catpvn(retval, SvPVX(othername)+1,
294 SvCUR(othername)-1);
295 }
296 else {
297 sv_catpvn(retval, name, 1);
298 sv_catpvn(retval, "{", 1);
299 sv_catsv(retval, othername);
300 sv_catpvn(retval, "}", 1);
301 }
823edd99 302 }
7820172a 303 else
823edd99 304 sv_catsv(retval, othername);
823edd99 305 }
7820172a
GS
306 return 1;
307 }
308 else {
309 warn("ref name not found for %s", id);
310 return 0;
823edd99 311 }
823edd99 312 }
7820172a
GS
313 else { /* store our name and continue */
314 SV *namesv;
315 if (name[0] == '@' || name[0] == '%') {
316 namesv = newSVpvn("\\", 1);
317 sv_catpvn(namesv, name, namelen);
318 }
319 else if (realtype == SVt_PVCV && name[0] == '*') {
320 namesv = newSVpvn("\\", 2);
321 sv_catpvn(namesv, name, namelen);
322 (SvPVX(namesv))[1] = '&';
323 }
324 else
325 namesv = newSVpvn(name, namelen);
326 seenentry = newAV();
327 av_push(seenentry, namesv);
328 (void)SvREFCNT_inc(val);
329 av_push(seenentry, val);
330 (void)hv_store(seenhv, id, strlen(id),
331 newRV((SV*)seenentry), 0);
332 SvREFCNT_dec(seenentry);
823edd99 333 }
823edd99 334 }
823edd99 335
a2126434
JN
336 if (realpack && *realpack == 'R' && strEQ(realpack, "Regexp")) {
337 STRLEN rlen;
338 char *rval = SvPV(val, rlen);
339 char *slash = strchr(rval, '/');
340 sv_catpvn(retval, "qr/", 3);
341 while (slash) {
342 sv_catpvn(retval, rval, slash-rval);
343 sv_catpvn(retval, "\\/", 2);
344 rlen -= slash-rval+1;
345 rval = slash+1;
346 slash = strchr(rval, '/');
7894fbab 347 }
a2126434
JN
348 sv_catpvn(retval, rval, rlen);
349 sv_catpvn(retval, "/", 1);
350 return 1;
351 }
352
353 /* If purity is not set and maxdepth is set, then check depth:
354 * if we have reached maximum depth, return the string
355 * representation of the thing we are currently examining
6cde4e94 356 * at this depth (i.e., 'Foo=ARRAY(0xdeadbeef)').
a2126434
JN
357 */
358 if (!purity && maxdepth > 0 && *levelp >= maxdepth) {
359 STRLEN vallen;
360 char *valstr = SvPV(val,vallen);
361 sv_catpvn(retval, "'", 1);
362 sv_catpvn(retval, valstr, vallen);
363 sv_catpvn(retval, "'", 1);
364 return 1;
365 }
366
367 if (realpack) { /* we have a blessed ref */
368 STRLEN blesslen;
369 char *blessstr = SvPV(bless, blesslen);
370 sv_catpvn(retval, blessstr, blesslen);
371 sv_catpvn(retval, "( ", 2);
372 if (indent >= 2) {
373 blesspad = apad;
374 apad = newSVsv(apad);
375 sv_x(aTHX_ apad, " ", 1, blesslen+2);
823edd99
GS
376 }
377 }
378
7894fbab
GS
379 (*levelp)++;
380 ipad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp);
381
7820172a
GS
382 if (realtype <= SVt_PVBM) { /* scalar ref */
383 SV *namesv = newSVpvn("${", 2);
384 sv_catpvn(namesv, name, namelen);
385 sv_catpvn(namesv, "}", 1);
6cde4e94 386 if (realpack) { /* blessed */
823edd99 387 sv_catpvn(retval, "do{\\(my $o = ", 13);
cea2e8a9 388 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
7820172a 389 postav, levelp, indent, pad, xpad, apad, sep,
a2126434 390 freezer, toaster, purity, deepcopy, quotekeys, bless,
e9105f86 391 maxdepth, sortkeys);
823edd99 392 sv_catpvn(retval, ")}", 2);
7820172a 393 } /* plain */
823edd99
GS
394 else {
395 sv_catpvn(retval, "\\", 1);
cea2e8a9 396 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
7820172a 397 postav, levelp, indent, pad, xpad, apad, sep,
a2126434 398 freezer, toaster, purity, deepcopy, quotekeys, bless,
e9105f86 399 maxdepth, sortkeys);
823edd99 400 }
7820172a
GS
401 SvREFCNT_dec(namesv);
402 }
403 else if (realtype == SVt_PVGV) { /* glob ref */
404 SV *namesv = newSVpvn("*{", 2);
405 sv_catpvn(namesv, name, namelen);
406 sv_catpvn(namesv, "}", 1);
407 sv_catpvn(retval, "\\", 1);
cea2e8a9 408 DD_dump(aTHX_ ival, SvPVX(namesv), SvCUR(namesv), retval, seenhv,
7820172a 409 postav, levelp, indent, pad, xpad, apad, sep,
a2126434 410 freezer, toaster, purity, deepcopy, quotekeys, bless,
e9105f86 411 maxdepth, sortkeys);
7820172a 412 SvREFCNT_dec(namesv);
823edd99
GS
413 }
414 else if (realtype == SVt_PVAV) {
415 SV *totpad;
416 I32 ix = 0;
417 I32 ixmax = av_len((AV *)ival);
6cde4e94 418
823edd99
GS
419 SV *ixsv = newSViv(0);
420 /* allowing for a 24 char wide array index */
421 New(0, iname, namelen+28, char);
422 (void)strcpy(iname, name);
423 inamelen = namelen;
424 if (name[0] == '@') {
425 sv_catpvn(retval, "(", 1);
426 iname[0] = '$';
427 }
428 else {
429 sv_catpvn(retval, "[", 1);
7820172a
GS
430 /* omit "->" in $foo{bar}->[0], but not in ${$foo}->[0] */
431 /*if (namelen > 0
432 && name[namelen-1] != ']' && name[namelen-1] != '}'
433 && (namelen < 4 || (name[1] != '{' && name[2] != '{')))*/
434 if ((namelen > 0
435 && name[namelen-1] != ']' && name[namelen-1] != '}')
436 || (namelen > 4
437 && (name[1] == '{'
438 || (name[0] == '\\' && name[2] == '{'))))
439 {
823edd99
GS
440 iname[inamelen++] = '-'; iname[inamelen++] = '>';
441 iname[inamelen] = '\0';
442 }
443 }
444 if (iname[0] == '*' && iname[inamelen-1] == '}' && inamelen >= 8 &&
445 (instr(iname+inamelen-8, "{SCALAR}") ||
446 instr(iname+inamelen-7, "{ARRAY}") ||
447 instr(iname+inamelen-6, "{HASH}"))) {
448 iname[inamelen++] = '-'; iname[inamelen++] = '>';
449 }
450 iname[inamelen++] = '['; iname[inamelen] = '\0';
451 totpad = newSVsv(sep);
452 sv_catsv(totpad, pad);
453 sv_catsv(totpad, apad);
454
455 for (ix = 0; ix <= ixmax; ++ix) {
456 STRLEN ilen;
457 SV *elem;
458 svp = av_fetch((AV*)ival, ix, FALSE);
459 if (svp)
460 elem = *svp;
461 else
3280af22 462 elem = &PL_sv_undef;
823edd99
GS
463
464 ilen = inamelen;
465 sv_setiv(ixsv, ix);
7b0972df 466 (void) sprintf(iname+ilen, "%"IVdf, (IV)ix);
823edd99
GS
467 ilen = strlen(iname);
468 iname[ilen++] = ']'; iname[ilen] = '\0';
469 if (indent >= 3) {
470 sv_catsv(retval, totpad);
471 sv_catsv(retval, ipad);
472 sv_catpvn(retval, "#", 1);
473 sv_catsv(retval, ixsv);
474 }
475 sv_catsv(retval, totpad);
476 sv_catsv(retval, ipad);
cea2e8a9 477 DD_dump(aTHX_ elem, iname, ilen, retval, seenhv, postav,
823edd99 478 levelp, indent, pad, xpad, apad, sep,
a2126434 479 freezer, toaster, purity, deepcopy, quotekeys, bless,
e9105f86 480 maxdepth, sortkeys);
823edd99
GS
481 if (ix < ixmax)
482 sv_catpvn(retval, ",", 1);
483 }
484 if (ixmax >= 0) {
cea2e8a9 485 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), (*levelp)-1);
823edd99
GS
486 sv_catsv(retval, totpad);
487 sv_catsv(retval, opad);
488 SvREFCNT_dec(opad);
489 }
490 if (name[0] == '@')
491 sv_catpvn(retval, ")", 1);
492 else
493 sv_catpvn(retval, "]", 1);
494 SvREFCNT_dec(ixsv);
495 SvREFCNT_dec(totpad);
496 Safefree(iname);
497 }
498 else if (realtype == SVt_PVHV) {
499 SV *totpad, *newapad;
500 SV *iname, *sname;
501 HE *entry;
502 char *key;
503 I32 klen;
504 SV *hval;
e9105f86 505 AV *keys = Nullav;
6cde4e94 506
7820172a 507 iname = newSVpvn(name, namelen);
823edd99
GS
508 if (name[0] == '%') {
509 sv_catpvn(retval, "(", 1);
510 (SvPVX(iname))[0] = '$';
511 }
512 else {
513 sv_catpvn(retval, "{", 1);
7820172a
GS
514 /* omit "->" in $foo[0]->{bar}, but not in ${$foo}->{bar} */
515 if ((namelen > 0
516 && name[namelen-1] != ']' && name[namelen-1] != '}')
517 || (namelen > 4
518 && (name[1] == '{'
519 || (name[0] == '\\' && name[2] == '{'))))
520 {
823edd99
GS
521 sv_catpvn(iname, "->", 2);
522 }
523 }
524 if (name[0] == '*' && name[namelen-1] == '}' && namelen >= 8 &&
525 (instr(name+namelen-8, "{SCALAR}") ||
526 instr(name+namelen-7, "{ARRAY}") ||
527 instr(name+namelen-6, "{HASH}"))) {
528 sv_catpvn(iname, "->", 2);
529 }
530 sv_catpvn(iname, "{", 1);
531 totpad = newSVsv(sep);
532 sv_catsv(totpad, pad);
533 sv_catsv(totpad, apad);
6cde4e94 534
e9105f86
BI
535 /* If requested, get a sorted/filtered array of hash keys */
536 if (sortkeys) {
537 if (sortkeys == &PL_sv_yes) {
538 keys = newAV();
539 (void)hv_iterinit((HV*)ival);
20d72259 540 while ((entry = hv_iternext((HV*)ival))) {
e9105f86
BI
541 sv = hv_iterkeysv(entry);
542 SvREFCNT_inc(sv);
543 av_push(keys, sv);
544 }
503ec68f 545#ifdef USE_LOCALE_NUMERIC
e9105f86
BI
546 sortsv(AvARRAY(keys),
547 av_len(keys)+1,
3c253d0e 548 IN_LOCALE ? Perl_sv_cmp_locale : Perl_sv_cmp);
02a99678 549#else
503ec68f
JH
550 sortsv(AvARRAY(keys),
551 av_len(keys)+1,
552 Perl_sv_cmp);
02a99678 553#endif
e9105f86
BI
554 }
555 else {
556 dSP; ENTER; SAVETMPS; PUSHMARK(sp);
557 XPUSHs(sv_2mortal(newRV_inc(ival))); PUTBACK;
558 i = perl_call_sv(sortkeys, G_SCALAR | G_EVAL);
559 SPAGAIN;
560 if (i) {
561 sv = POPs;
562 if (SvROK(sv) && (SvTYPE(SvRV(sv)) == SVt_PVAV))
563 keys = (AV*)SvREFCNT_inc(SvRV(sv));
564 }
565 if (! keys)
566 warn("Sortkeys subroutine did not return ARRAYREF\n");
567 PUTBACK; FREETMPS; LEAVE;
568 }
569 if (keys)
570 sv_2mortal((SV*)keys);
571 }
572 else
573 (void)hv_iterinit((HV*)ival);
ecfc8647
AMS
574
575 /* foreach (keys %hash) */
576 for (i = 0; 1; i++) {
dc71dc59 577 char *nkey = NULL;
823edd99 578 I32 nticks = 0;
dc71dc59
JH
579 SV* keysv;
580 STRLEN keylen;
581 bool do_utf8 = FALSE;
ecfc8647 582
91f3b821 583 if ((sortkeys && !(keys && (I32)i <= av_len(keys))) ||
ecfc8647
AMS
584 !(entry = hv_iternext((HV *)ival)))
585 break;
586
823edd99
GS
587 if (i)
588 sv_catpvn(retval, ",", 1);
e9105f86
BI
589
590 if (sortkeys) {
591 char *key;
592 svp = av_fetch(keys, i, FALSE);
593 keysv = svp ? *svp : sv_mortalcopy(&PL_sv_undef);
594 key = SvPV(keysv, keylen);
d075f8ed
NC
595 svp = hv_fetch((HV*)ival, key,
596 SvUTF8(keysv) ? -keylen : keylen, 0);
e9105f86
BI
597 hval = svp ? *svp : sv_mortalcopy(&PL_sv_undef);
598 }
599 else {
600 keysv = hv_iterkeysv(entry);
601 hval = hv_iterval((HV*)ival, entry);
602 }
603
dc71dc59
JH
604 do_utf8 = DO_UTF8(keysv);
605 key = SvPV(keysv, keylen);
606 klen = keylen;
607
608 if (do_utf8) {
d075f8ed 609 STRLEN ocur;
dc71dc59
JH
610 I32 nlen;
611
612 sv_catsv(retval, totpad);
613 sv_catsv(retval, ipad);
d075f8ed 614 ocur = SvCUR(retval);
6cde4e94 615 nlen = esc_q_utf8(aTHX_ retval, key, klen);
dc71dc59
JH
616
617 sname = newSVsv(iname);
d075f8ed 618 sv_catpvn(sname, SvPVX(retval) + ocur, nlen);
dc71dc59 619 sv_catpvn(sname, "}", 1);
823edd99
GS
620 }
621 else {
dc71dc59
JH
622 if (quotekeys || needs_quote(key)) {
623 nticks = num_q(key, klen);
624 New(0, nkey, klen+nticks+3, char);
625 nkey[0] = '\'';
626 if (nticks)
627 klen += esc_q(nkey+1, key, klen);
628 else
629 (void)Copy(key, nkey+1, klen, char);
630 nkey[++klen] = '\'';
631 nkey[++klen] = '\0';
632 }
633 else {
634 New(0, nkey, klen, char);
635 (void)Copy(key, nkey, klen, char);
636 }
823edd99 637
dc71dc59
JH
638 sname = newSVsv(iname);
639 sv_catpvn(sname, nkey, klen);
640 sv_catpvn(sname, "}", 1);
641
642 sv_catsv(retval, totpad);
643 sv_catsv(retval, ipad);
644 sv_catpvn(retval, nkey, klen);
645 }
823edd99
GS
646 sv_catpvn(retval, " => ", 4);
647 if (indent >= 2) {
648 char *extra;
649 I32 elen = 0;
650 newapad = newSVsv(apad);
651 New(0, extra, klen+4+1, char);
652 while (elen < (klen+4))
653 extra[elen++] = ' ';
654 extra[elen] = '\0';
655 sv_catpvn(newapad, extra, elen);
656 Safefree(extra);
657 }
658 else
659 newapad = apad;
660
cea2e8a9 661 DD_dump(aTHX_ hval, SvPVX(sname), SvCUR(sname), retval, seenhv,
823edd99 662 postav, levelp, indent, pad, xpad, newapad, sep,
a2126434 663 freezer, toaster, purity, deepcopy, quotekeys, bless,
e9105f86 664 maxdepth, sortkeys);
823edd99
GS
665 SvREFCNT_dec(sname);
666 Safefree(nkey);
667 if (indent >= 2)
668 SvREFCNT_dec(newapad);
669 }
670 if (i) {
cea2e8a9 671 SV *opad = sv_x(aTHX_ Nullsv, SvPVX(xpad), SvCUR(xpad), *levelp-1);
823edd99
GS
672 sv_catsv(retval, totpad);
673 sv_catsv(retval, opad);
674 SvREFCNT_dec(opad);
675 }
676 if (name[0] == '%')
677 sv_catpvn(retval, ")", 1);
678 else
679 sv_catpvn(retval, "}", 1);
680 SvREFCNT_dec(iname);
681 SvREFCNT_dec(totpad);
682 }
683 else if (realtype == SVt_PVCV) {
684 sv_catpvn(retval, "sub { \"DUMMY\" }", 15);
685 if (purity)
686 warn("Encountered CODE ref, using dummy placeholder");
687 }
688 else {
689 warn("cannot handle ref type %ld", realtype);
690 }
691
692 if (realpack) { /* free blessed allocs */
693 if (indent >= 2) {
694 SvREFCNT_dec(apad);
695 apad = blesspad;
696 }
697 sv_catpvn(retval, ", '", 3);
698 sv_catpvn(retval, realpack, strlen(realpack));
699 sv_catpvn(retval, "' )", 3);
700 if (toaster && SvPOK(toaster) && SvCUR(toaster)) {
701 sv_catpvn(retval, "->", 2);
702 sv_catsv(retval, toaster);
703 sv_catpvn(retval, "()", 2);
704 }
705 }
706 SvREFCNT_dec(ipad);
707 (*levelp)--;
708 }
709 else {
710 STRLEN i;
711
712 if (namelen) {
c623ac67 713 (void) sprintf(id, "0x%"UVxf, PTR2UV(val));
823edd99
GS
714 if ((svp = hv_fetch(seenhv, id, (idlen = strlen(id)), FALSE)) &&
715 (sv = *svp) && SvROK(sv) &&
7820172a
GS
716 (seenentry = (AV*)SvRV(sv)))
717 {
823edd99 718 SV *othername;
7820172a
GS
719 if ((svp = av_fetch(seenentry, 0, FALSE)) && (othername = *svp)
720 && (svp = av_fetch(seenentry, 2, FALSE)) && *svp && SvIV(*svp) > 0)
721 {
722 sv_catpvn(retval, "${", 2);
823edd99 723 sv_catsv(retval, othername);
7820172a 724 sv_catpvn(retval, "}", 1);
823edd99
GS
725 return 1;
726 }
727 }
728 else {
729 SV *namesv;
7820172a 730 namesv = newSVpvn("\\", 1);
823edd99
GS
731 sv_catpvn(namesv, name, namelen);
732 seenentry = newAV();
733 av_push(seenentry, namesv);
7820172a 734 av_push(seenentry, newRV(val));
823edd99
GS
735 (void)hv_store(seenhv, id, strlen(id), newRV((SV*)seenentry), 0);
736 SvREFCNT_dec(seenentry);
737 }
738 }
7820172a 739
823edd99
GS
740 if (SvIOK(val)) {
741 STRLEN len;
0e8b3009 742 if (SvIsUV(val))
5e8f63cb 743 (void) sprintf(tmpbuf, "%"UVuf, SvUV(val));
0e8b3009
GS
744 else
745 (void) sprintf(tmpbuf, "%"IVdf, SvIV(val));
823edd99 746 len = strlen(tmpbuf);
c4cce848
NC
747 /* For 5.6.x and earlier will need to change this test to check
748 NV if NOK, as there NOK trumps IOK, and NV=3.5,IV=3 is valid.
749 Current code will Dump that as $VAR1 = 3;
750 Changes in 5.7 series mean that now IOK is only set if scalar
751 is precisely integer. */
752 if (SvPOK(val)) {
753 /* Need to check to see if this is a string such as " 0".
754 I'm assuming from sprintf isn't going to clash with utf8.
755 Is this valid on EBCDIC? */
756 STRLEN pvlen;
757 const char *pv = SvPV(val, pvlen);
758 if (pvlen != len || memNE(pv, tmpbuf, len))
759 goto integer_came_from_string;
760 }
761 if (len > 10) {
762 /* Looks like we're on a 64 bit system. Make it a string so that
763 if a 32 bit system reads the number it will cope better. */
764 sv_catpvf(retval, "'%s'", tmpbuf);
765 } else
766 sv_catpvn(retval, tmpbuf, len);
823edd99
GS
767 }
768 else if (realtype == SVt_PVGV) {/* GLOBs can end up with scribbly names */
769 c = SvPV(val, i);
770 ++c; --i; /* just get the name */
771 if (i >= 6 && strncmp(c, "main::", 6) == 0) {
772 c += 4;
773 i -= 4;
774 }
775 if (needs_quote(c)) {
776 sv_grow(retval, SvCUR(retval)+6+2*i);
777 r = SvPVX(retval)+SvCUR(retval);
778 r[0] = '*'; r[1] = '{'; r[2] = '\'';
779 i += esc_q(r+3, c, i);
780 i += 3;
781 r[i++] = '\''; r[i++] = '}';
782 r[i] = '\0';
783 }
784 else {
785 sv_grow(retval, SvCUR(retval)+i+2);
786 r = SvPVX(retval)+SvCUR(retval);
787 r[0] = '*'; strcpy(r+1, c);
788 i++;
789 }
7820172a 790 SvCUR_set(retval, SvCUR(retval)+i);
823edd99
GS
791
792 if (purity) {
793 static char *entries[] = { "{SCALAR}", "{ARRAY}", "{HASH}" };
794 static STRLEN sizes[] = { 8, 7, 6 };
795 SV *e;
7820172a
GS
796 SV *nname = newSVpvn("", 0);
797 SV *newapad = newSVpvn("", 0);
823edd99
GS
798 GV *gv = (GV*)val;
799 I32 j;
800
801 for (j=0; j<3; j++) {
802 e = ((j == 0) ? GvSV(gv) : (j == 1) ? (SV*)GvAV(gv) : (SV*)GvHV(gv));
7820172a
GS
803 if (!e)
804 continue;
805 if (j == 0 && !SvOK(e))
806 continue;
807
808 {
823edd99 809 I32 nlevel = 0;
7820172a 810 SV *postentry = newSVpvn(r,i);
823edd99
GS
811
812 sv_setsv(nname, postentry);
813 sv_catpvn(nname, entries[j], sizes[j]);
814 sv_catpvn(postentry, " = ", 3);
815 av_push(postav, postentry);
816 e = newRV(e);
817
818 SvCUR(newapad) = 0;
819 if (indent >= 2)
cea2e8a9 820 (void)sv_x(aTHX_ newapad, " ", 1, SvCUR(postentry));
823edd99 821
cea2e8a9 822 DD_dump(aTHX_ e, SvPVX(nname), SvCUR(nname), postentry,
823edd99
GS
823 seenhv, postav, &nlevel, indent, pad, xpad,
824 newapad, sep, freezer, toaster, purity,
e9105f86
BI
825 deepcopy, quotekeys, bless, maxdepth,
826 sortkeys);
823edd99
GS
827 SvREFCNT_dec(e);
828 }
829 }
830
831 SvREFCNT_dec(newapad);
832 SvREFCNT_dec(nname);
833 }
834 }
7820172a
GS
835 else if (val == &PL_sv_undef || !SvOK(val)) {
836 sv_catpvn(retval, "undef", 5);
837 }
823edd99 838 else {
c4cce848 839 integer_came_from_string:
823edd99 840 c = SvPV(val, i);
dc71dc59 841 if (DO_UTF8(val))
6cde4e94 842 i += esc_q_utf8(aTHX_ retval, c, i);
dc71dc59
JH
843 else {
844 sv_grow(retval, SvCUR(retval)+3+2*i); /* 3: ""\0 */
845 r = SvPVX(retval) + SvCUR(retval);
846 r[0] = '\'';
847 i += esc_q(r+1, c, i);
848 ++i;
849 r[i++] = '\'';
850 r[i] = '\0';
851 SvCUR_set(retval, SvCUR(retval)+i);
852 }
823edd99 853 }
823edd99
GS
854 }
855
7820172a
GS
856 if (idlen) {
857 if (deepcopy)
858 (void)hv_delete(seenhv, id, idlen, G_DISCARD);
859 else if (namelen && seenentry) {
860 SV *mark = *av_fetch(seenentry, 2, TRUE);
861 sv_setiv(mark,1);
862 }
863 }
823edd99
GS
864 return 1;
865}
866
867
868MODULE = Data::Dumper PACKAGE = Data::Dumper PREFIX = Data_Dumper_
869
870#
871# This is the exact equivalent of Dump. Well, almost. The things that are
872# different as of now (due to Laziness):
873# * doesnt do double-quotes yet.
874#
875
876void
877Data_Dumper_Dumpxs(href, ...)
878 SV *href;
879 PROTOTYPE: $;$$
880 PPCODE:
881 {
882 HV *hv;
883 SV *retval, *valstr;
884 HV *seenhv = Nullhv;
885 AV *postav, *todumpav, *namesav;
886 I32 level = 0;
497b47a8 887 I32 indent, terse, i, imax, postlen;
823edd99 888 SV **svp;
497b47a8 889 SV *val, *name, *pad, *xpad, *apad, *sep, *varname;
e9105f86 890 SV *freezer, *toaster, *bless, *sortkeys;
7b0972df 891 I32 purity, deepcopy, quotekeys, maxdepth = 0;
823edd99
GS
892 char tmpbuf[1024];
893 I32 gimme = GIMME;
894
895 if (!SvROK(href)) { /* call new to get an object first */
0f1923bd
GS
896 if (items < 2)
897 croak("Usage: Data::Dumper::Dumpxs(PACKAGE, VAL_ARY_REF, [NAME_ARY_REF])");
823edd99
GS
898
899 ENTER;
900 SAVETMPS;
901
902 PUSHMARK(sp);
903 XPUSHs(href);
0f1923bd
GS
904 XPUSHs(sv_2mortal(newSVsv(ST(1))));
905 if (items >= 3)
906 XPUSHs(sv_2mortal(newSVsv(ST(2))));
823edd99
GS
907 PUTBACK;
908 i = perl_call_method("new", G_SCALAR);
909 SPAGAIN;
910 if (i)
911 href = newSVsv(POPs);
912
913 PUTBACK;
914 FREETMPS;
915 LEAVE;
916 if (i)
917 (void)sv_2mortal(href);
918 }
919
920 todumpav = namesav = Nullav;
921 seenhv = Nullhv;
497b47a8 922 val = pad = xpad = apad = sep = varname
3280af22 923 = freezer = toaster = bless = &PL_sv_undef;
823edd99
GS
924 name = sv_newmortal();
925 indent = 2;
497b47a8 926 terse = purity = deepcopy = 0;
823edd99 927 quotekeys = 1;
6cde4e94 928
7820172a 929 retval = newSVpvn("", 0);
823edd99
GS
930 if (SvROK(href)
931 && (hv = (HV*)SvRV((SV*)href))
932 && SvTYPE(hv) == SVt_PVHV) {
933
934 if ((svp = hv_fetch(hv, "seen", 4, FALSE)) && SvROK(*svp))
935 seenhv = (HV*)SvRV(*svp);
936 if ((svp = hv_fetch(hv, "todump", 6, FALSE)) && SvROK(*svp))
937 todumpav = (AV*)SvRV(*svp);
938 if ((svp = hv_fetch(hv, "names", 5, FALSE)) && SvROK(*svp))
939 namesav = (AV*)SvRV(*svp);
940 if ((svp = hv_fetch(hv, "indent", 6, FALSE)))
941 indent = SvIV(*svp);
942 if ((svp = hv_fetch(hv, "purity", 6, FALSE)))
943 purity = SvIV(*svp);
944 if ((svp = hv_fetch(hv, "terse", 5, FALSE)))
945 terse = SvTRUE(*svp);
497b47a8 946#if 0 /* useqq currently unused */
823edd99
GS
947 if ((svp = hv_fetch(hv, "useqq", 5, FALSE)))
948 useqq = SvTRUE(*svp);
497b47a8 949#endif
823edd99
GS
950 if ((svp = hv_fetch(hv, "pad", 3, FALSE)))
951 pad = *svp;
952 if ((svp = hv_fetch(hv, "xpad", 4, FALSE)))
953 xpad = *svp;
954 if ((svp = hv_fetch(hv, "apad", 4, FALSE)))
955 apad = *svp;
956 if ((svp = hv_fetch(hv, "sep", 3, FALSE)))
957 sep = *svp;
958 if ((svp = hv_fetch(hv, "varname", 7, FALSE)))
959 varname = *svp;
960 if ((svp = hv_fetch(hv, "freezer", 7, FALSE)))
961 freezer = *svp;
962 if ((svp = hv_fetch(hv, "toaster", 7, FALSE)))
963 toaster = *svp;
964 if ((svp = hv_fetch(hv, "deepcopy", 8, FALSE)))
965 deepcopy = SvTRUE(*svp);
966 if ((svp = hv_fetch(hv, "quotekeys", 9, FALSE)))
967 quotekeys = SvTRUE(*svp);
968 if ((svp = hv_fetch(hv, "bless", 5, FALSE)))
969 bless = *svp;
a2126434
JN
970 if ((svp = hv_fetch(hv, "maxdepth", 8, FALSE)))
971 maxdepth = SvIV(*svp);
e9105f86
BI
972 if ((svp = hv_fetch(hv, "sortkeys", 8, FALSE))) {
973 sortkeys = *svp;
974 if (! SvTRUE(sortkeys))
975 sortkeys = NULL;
976 else if (! (SvROK(sortkeys) &&
977 SvTYPE(SvRV(sortkeys)) == SVt_PVCV) )
978 {
979 /* flag to use qsortsv() for sorting hash keys */
980 sortkeys = &PL_sv_yes;
981 }
982 }
823edd99
GS
983 postav = newAV();
984
985 if (todumpav)
986 imax = av_len(todumpav);
987 else
988 imax = -1;
7820172a 989 valstr = newSVpvn("",0);
823edd99
GS
990 for (i = 0; i <= imax; ++i) {
991 SV *newapad;
6cde4e94 992
823edd99
GS
993 av_clear(postav);
994 if ((svp = av_fetch(todumpav, i, FALSE)))
995 val = *svp;
996 else
3280af22 997 val = &PL_sv_undef;
823edd99
GS
998 if ((svp = av_fetch(namesav, i, TRUE)))
999 sv_setsv(name, *svp);
1000 else
8063af02 1001 (void)SvOK_off(name);
6cde4e94 1002
823edd99
GS
1003 if (SvOK(name)) {
1004 if ((SvPVX(name))[0] == '*') {
1005 if (SvROK(val)) {
1006 switch (SvTYPE(SvRV(val))) {
1007 case SVt_PVAV:
1008 (SvPVX(name))[0] = '@';
1009 break;
1010 case SVt_PVHV:
1011 (SvPVX(name))[0] = '%';
1012 break;
1013 case SVt_PVCV:
1014 (SvPVX(name))[0] = '*';
1015 break;
1016 default:
1017 (SvPVX(name))[0] = '$';
1018 break;
1019 }
1020 }
1021 else
1022 (SvPVX(name))[0] = '$';
1023 }
1024 else if ((SvPVX(name))[0] != '$')
1025 sv_insert(name, 0, 0, "$", 1);
1026 }
1027 else {
1028 STRLEN nchars = 0;
1029 sv_setpvn(name, "$", 1);
1030 sv_catsv(name, varname);
faccc32b 1031 (void) sprintf(tmpbuf, "%"IVdf, (IV)(i+1));
823edd99
GS
1032 nchars = strlen(tmpbuf);
1033 sv_catpvn(name, tmpbuf, nchars);
1034 }
6cde4e94 1035
823edd99 1036 if (indent >= 2) {
cea2e8a9 1037 SV *tmpsv = sv_x(aTHX_ Nullsv, " ", 1, SvCUR(name)+3);
823edd99
GS
1038 newapad = newSVsv(apad);
1039 sv_catsv(newapad, tmpsv);
1040 SvREFCNT_dec(tmpsv);
1041 }
1042 else
1043 newapad = apad;
6cde4e94 1044
cea2e8a9 1045 DD_dump(aTHX_ val, SvPVX(name), SvCUR(name), valstr, seenhv,
823edd99
GS
1046 postav, &level, indent, pad, xpad, newapad, sep,
1047 freezer, toaster, purity, deepcopy, quotekeys,
e9105f86 1048 bless, maxdepth, sortkeys);
6cde4e94 1049
823edd99
GS
1050 if (indent >= 2)
1051 SvREFCNT_dec(newapad);
1052
1053 postlen = av_len(postav);
1054 if (postlen >= 0 || !terse) {
1055 sv_insert(valstr, 0, 0, " = ", 3);
1056 sv_insert(valstr, 0, 0, SvPVX(name), SvCUR(name));
1057 sv_catpvn(valstr, ";", 1);
1058 }
1059 sv_catsv(retval, pad);
1060 sv_catsv(retval, valstr);
1061 sv_catsv(retval, sep);
1062 if (postlen >= 0) {
1063 I32 i;
1064 sv_catsv(retval, pad);
1065 for (i = 0; i <= postlen; ++i) {
1066 SV *elem;
1067 svp = av_fetch(postav, i, FALSE);
1068 if (svp && (elem = *svp)) {
1069 sv_catsv(retval, elem);
1070 if (i < postlen) {
1071 sv_catpvn(retval, ";", 1);
1072 sv_catsv(retval, sep);
1073 sv_catsv(retval, pad);
1074 }
1075 }
1076 }
1077 sv_catpvn(retval, ";", 1);
1078 sv_catsv(retval, sep);
1079 }
1080 sv_setpvn(valstr, "", 0);
1081 if (gimme == G_ARRAY) {
1082 XPUSHs(sv_2mortal(retval));
1083 if (i < imax) /* not the last time thro ? */
7820172a 1084 retval = newSVpvn("",0);
823edd99
GS
1085 }
1086 }
1087 SvREFCNT_dec(postav);
1088 SvREFCNT_dec(valstr);
1089 }
1090 else
1091 croak("Call to new() method failed to return HASH ref");
1092 if (gimme == G_SCALAR)
1093 XPUSHs(sv_2mortal(retval));
1094 }