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