further Symbian/S90 fixes from alexander smishlajev
[p5sagit/p5-mst-13.2.git] / ext / Data / Dumper / Dumper.xs
index e45550b..b09a023 100644 (file)
@@ -4,6 +4,10 @@
 #include "XSUB.h"
 #include "ppport.h"
 
+#if PERL_VERSION < 6
+#  define DD_USE_OLD_ID_FORMAT
+#endif
+
 static I32 num_q (const char *s, STRLEN slen);
 static I32 esc_q (char *dest, const char *src, STRLEN slen);
 static I32 esc_q_utf8 (pTHX_ SV *sv, const char *src, STRLEN slen);
@@ -252,7 +256,13 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
 {
     char tmpbuf[128];
     U32 i;
-    char *c, *r, *realpack, id[128];
+    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;
@@ -264,6 +274,14 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
     if (!val)
        return 0;
 
+    /* If the ouput buffer has less than some arbitary 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))
@@ -288,7 +306,12 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
        
        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
@@ -339,7 +362,11 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
                    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;
                }
            }
@@ -360,7 +387,7 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
                av_push(seenentry, namesv);
                (void)SvREFCNT_inc(val);
                av_push(seenentry, val);
-               (void)hv_store(seenhv, id, strlen(id),
+               (void)hv_store(seenhv, id, idlen,
                               newRV_inc((SV*)seenentry), 0);
                SvREFCNT_dec(seenentry);
            }
@@ -412,7 +439,13 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
        (*levelp)++;
        ipad = sv_x(aTHX_ Nullsv, SvPVX_const(xpad), SvCUR(xpad), *levelp);
 
-       if (realtype <= SVt_PVBM) {                          /* scalar ref */
+       if (
+#if PERL_VERSION < 9
+               realtype <= SVt_PVBM
+#else
+               realtype <= SVt_PVMG
+#endif
+       ) {                          /* scalar ref */
            SV * const namesv = newSVpvn("${", 2);
            sv_catpvn(namesv, name, namelen);
            sv_catpvn(namesv, "}", 1);
@@ -765,7 +798,12 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
        STRLEN i;
        
        if (namelen) {
+#ifdef DD_USE_OLD_ID_FORMAT
            idlen = my_snprintf(id, sizeof(id), "0x%"UVxf, PTR2UV(val));
+#else
+           id_buffer = PTR2UV(val);
+           idlen = sizeof(id_buffer);
+#endif
            if ((svp = hv_fetch(seenhv, id, idlen, FALSE)) &&
                (sv = *svp) && SvROK(sv) &&
                (seenentry = (AV*)SvRV(sv)))
@@ -786,7 +824,7 @@ DD_dump(pTHX_ SV *val, const char *name, STRLEN namelen, SV *retval, HV *seenhv,
                seenentry = newAV();
                av_push(seenentry, namesv);
                av_push(seenentry, newRV_inc(val));
-               (void)hv_store(seenhv, id, strlen(id), newRV_inc((SV*)seenentry), 0);
+               (void)hv_store(seenhv, id, idlen, newRV_inc((SV*)seenentry), 0);
                SvREFCNT_dec(seenentry);
            }
        }