This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Take advantage of SvGROW's return value
authorKarl Williamson <khw@cpan.org>
Thu, 11 Aug 2016 22:42:20 +0000 (16:42 -0600)
committerKarl Williamson <khw@cpan.org>
Wed, 17 Aug 2016 21:23:59 +0000 (15:23 -0600)
I had not realized that SvGROW returned the new string pointer.  Using
that makes a one-step process from a two-step process.

I examined the code for other possible occurrences, and found others
where it seemed that the two-step seemed clearer, so left those alone.

perl.c
pp.c
toke.c

diff --git a/perl.c b/perl.c
index 21a8b30..6618535 100644 (file)
--- a/perl.c
+++ b/perl.c
@@ -3224,8 +3224,7 @@ Perl_moreswitches(pTHX_ const char *s)
                   s--;
              }
              PL_rs = newSVpvs("");
-             SvGROW(PL_rs, (STRLEN)(UVCHR_SKIP(rschar) + 1));
-             tmps = (U8*)SvPVX(PL_rs);
+             tmps = (U8*) SvGROW(PL_rs, (STRLEN)(UVCHR_SKIP(rschar) + 1));
              uvchr_to_utf8(tmps, rschar);
              SvCUR_set(PL_rs, UVCHR_SKIP(rschar));
              SvUTF8_on(PL_rs);
diff --git a/pp.c b/pp.c
index 124bf63..49b6abe 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -4109,8 +4109,7 @@ PP(pp_uc)
                      * allocate without allocating too much.  Such is life.
                      * See corresponding comment in lc code for another option
                      * */
-                    SvGROW(dest, min);
-                    d = (U8*)SvPVX(dest) + o;
+                    d = o + (U8*) SvGROW(dest, min);
                 }
                 Copy(tmpbuf, d, ulen, U8);
                 d += ulen;
@@ -4174,8 +4173,7 @@ PP(pp_uc)
                         * ASCII.  If not enough room, grow the string */
                        if (SvLEN(dest) < ++min) {      
                            const UV o = d - (U8*)SvPVX_const(dest);
-                           SvGROW(dest, min);
-                           d = (U8*)SvPVX(dest) + o;
+                           d = o + (U8*) SvGROW(dest, min);
                        }
                        *d++ = 'S'; *d = 'S'; /* upper case is 'SS' */
                        continue;   /* Back to the tight loop; still in ASCII */
@@ -4325,8 +4323,7 @@ PP(pp_lc)
                 * Another option would be to grow an extra byte or two more
                 * each time we need to grow, which would cut down the million
                 * to 500K, with little waste */
-               SvGROW(dest, min);
-               d = (U8*)SvPVX(dest) + o;
+               d = o + (U8*) SvGROW(dest, min);
            }
 
            /* Copy the newly lowercased letter to the output buffer we're
@@ -4520,8 +4517,7 @@ PP(pp_fc)
 
             if (ulen > u && (SvLEN(dest) < (min += ulen - u))) {
                 const UV o = d - (U8*)SvPVX_const(dest);
-                SvGROW(dest, min);
-                d = (U8*)SvPVX(dest) + o;
+                d = o + (U8*) SvGROW(dest, min);
             }
 
             Copy(tmpbuf, d, ulen, U8);
@@ -4600,8 +4596,7 @@ PP(pp_fc)
                      * becomes "ss", which may require growing the SV. */
                     if (SvLEN(dest) < ++min) {
                         const UV o = d - (U8*)SvPVX_const(dest);
-                        SvGROW(dest, min);
-                        d = (U8*)SvPVX(dest) + o;
+                        d = o + (U8*) SvGROW(dest, min);
                      }
                     *(d)++ = 's';
                     *d = 's';
diff --git a/toke.c b/toke.c
index b0e3736..ddc7e15 100644 (file)
--- a/toke.c
+++ b/toke.c
@@ -3098,8 +3098,7 @@ S_scan_const(pTHX_ char *start)
 
                 /* Subtract 3 for the bytes that were already accounted for
                  * (min, max, and the hyphen) */
-                SvGROW(sv, SvLEN(sv) + grow - 3);
-               d = SvPVX(sv) + save_offset;    /* refresh d after realloc */
+                d = save_offset + SvGROW(sv, SvLEN(sv) + grow - 3);
 
                 /* Here, we expand out the range.  On ASCII platforms, the
                  * compiler should optimize out the 'convert_unicode==TRUE'