This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Refactor ENAME dumping in Perl_do_sv_dump() to simplify the code slightly.
authorNicholas Clark <nick@ccl4.org>
Thu, 25 Nov 2010 11:53:10 +0000 (11:53 +0000)
committerNicholas Clark <nick@ccl4.org>
Thu, 25 Nov 2010 11:53:10 +0000 (11:53 +0000)
Simpler code avoids the need for a comment explaining how the complex code was
working. Also use newSVpvs_flags() in place of sv_newmortal() and sv_setpv().

dump.c

diff --git a/dump.c b/dump.c
index 4bda943..d778d41 100644 (file)
--- a/dump.c
+++ b/dump.c
@@ -1863,29 +1863,26 @@ Perl_do_sv_dump(pTHX_ I32 level, PerlIO *file, SV *sv, I32 nest, I32 maxnest, bo
                 (IV)HvAUX(sv)->xhv_name_count
                );
            if (HvAUX(sv)->xhv_name_u.xhvnameu_name && HvENAME_HEK_NN(sv)) {
-               if (HvAUX(sv)->xhv_name_count) {
-                   SV * const names = sv_newmortal();
-                   HEK ** const namep = HvAUX(sv)->xhv_name_u.xhvnameu_names;
-                   const I32 count = HvAUX(sv)->xhv_name_count;
-                   /* This line sets hekp to one element before the first
-                      name, so the ++hekp below will put us at the start-
-                      ing point.
-                      That starting point is the first element if count
-                      is positive and the second element if count
-                      is negative.
-                    */
-                   HEK **hekp = namep - (count > 0);
-                   sv_setpv(names, "");
-                   while (++hekp < namep + (count < 0 ? -count : count))
+               const I32 count = HvAUX(sv)->xhv_name_count;
+               if (count) {
+                   SV * const names = newSVpvs_flags("", SVs_TEMP);
+                   /* The starting point is the first element if count is
+                      positive and the second element if count is negative. */
+                   HEK *const *hekp = HvAUX(sv)->xhv_name_u.xhvnameu_names
+                       + (count < 0 ? 1 : 0);
+                   HEK *const *const endp = HvAUX(sv)->xhv_name_u.xhvnameu_names
+                       + (count < 0 ? -count : count);
+                   while (hekp < endp) {
                        if (*hekp) {
                            sv_catpvs(names, ", \"");
-                           sv_catpvn(
-                            names, HEK_KEY(*hekp), HEK_LEN(*hekp)
-                           );
+                           sv_catpvn(names, HEK_KEY(*hekp), HEK_LEN(*hekp));
                            sv_catpvs(names, "\"");
+                       } else {
+                           /* This should never happen. */
+                           sv_catpvs(names, ", (null)");
                        }
-                       /* This should never happen. */
-                       else sv_catpvs(names, ", (null)");
+                       ++hekp;
+                   }
                    Perl_dump_indent(aTHX_
                     level, file, "  ENAME = %s\n", SvPV_nolen(names)+2
                    );