Re: useless use of void context work-around
[p5sagit/p5-mst-13.2.git] / pp_hot.c
index c12e986..2904d9f 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -142,19 +142,19 @@ PP(pp_concat)
     dPOPTOPssrl;
     STRLEN len;
     U8 *s;
-    bool left_utf;
-    bool right_utf;
+    bool left_utf8;
+    bool right_utf8;
 
     if (TARG == right && SvGMAGICAL(right))
         mg_get(right);
     if (SvGMAGICAL(left))
         mg_get(left);
 
-    left_utf  = DO_UTF8(left);
-    right_utf = DO_UTF8(right);
-    if (left_utf != right_utf) {
-        if (TARG == right && !right_utf) {
+    left_utf8  = DO_UTF8(left);
+    right_utf8 = DO_UTF8(right);
+
+    if (left_utf8 != right_utf8) {
+        if (TARG == right && !right_utf8) {
             sv_utf8_upgrade(TARG); /* Now straight binary copy */
             SvUTF8_on(TARG);
         }
@@ -163,7 +163,7 @@ PP(pp_concat)
             U8 *l, *c, *olds = NULL;
             STRLEN targlen;
            s = (U8*)SvPV(right,len);
-           right_utf |= DO_UTF8(right);
+           right_utf8 |= DO_UTF8(right);
             if (TARG == right) {
                /* Take a copy since we're about to overwrite TARG */
                olds = s = (U8*)savepvn((char*)s, len);
@@ -175,28 +175,28 @@ PP(pp_concat)
                    sv_setpv(left, ""); /* Suppress warning. */
            }
             l = (U8*)SvPV(left, targlen);
-           left_utf |= DO_UTF8(left);
+           left_utf8 |= DO_UTF8(left);
             if (TARG != left)
                 sv_setpvn(TARG, (char*)l, targlen);
-            if (!left_utf)
+            if (!left_utf8)
                 sv_utf8_upgrade(TARG);
             /* Extend TARG to length of right (s) */
             targlen = SvCUR(TARG) + len;
-            if (!right_utf) {
+            if (!right_utf8) {
                 /* plus one for each hi-byte char if we have to upgrade */
                 for (c = s; c < s + len; c++)  {
-                    if (*c & 0x80)
+                    if (UTF8_IS_CONTINUED(*c))
                         targlen++;
                 }
             }
             SvGROW(TARG, targlen+1);
             /* And now copy, maybe upgrading right to UTF8 on the fly */
-            for (c = (U8*)SvEND(TARG); len--; s++) {
-                 if (*s & 0x80 && !right_utf)
-                     c = uv_to_utf8(c, *s);
-                 else
-                     *c++ = *s;
-            }
+           if (right_utf8)
+               Copy(s, SvEND(TARG), len, U8);
+           else {
+               for (c = (U8*)SvEND(TARG); len--; s++)
+                   c = uv_to_utf8(c, *s);
+           }
             SvCUR_set(TARG, targlen);
             *SvEND(TARG) = '\0';
             SvUTF8_on(TARG);
@@ -235,7 +235,7 @@ PP(pp_concat)
     }
     else
        sv_setpvn(TARG, (char *)s, len);        /* suppress warning */
-    if (left_utf)
+    if (left_utf8)
        SvUTF8_on(TARG);
     SETTARG;
     RETURN;
@@ -279,6 +279,69 @@ PP(pp_readline)
 PP(pp_eq)
 {
     djSP; tryAMAGICbinSET(eq,0);
+#ifdef PERL_PRESERVE_IVUV
+    SvIV_please(TOPs);
+    if (SvIOK(TOPs)) {
+       /* Unless the left argument is integer in range we are going to have to
+          use NV maths. Hence only attempt to coerce the right argument if
+          we know the left is integer.  */
+      SvIV_please(TOPm1s);
+       if (SvIOK(TOPm1s)) {
+           bool auvok = SvUOK(TOPm1s);
+           bool buvok = SvUOK(TOPs);
+           
+           if (!auvok && !buvok) { /* ## IV == IV ## */
+               IV aiv = SvIVX(TOPm1s);
+               IV biv = SvIVX(TOPs);
+               
+               SP--;
+               SETs(boolSV(aiv == biv));
+               RETURN;
+           }
+           if (auvok && buvok) { /* ## UV == UV ## */
+               UV auv = SvUVX(TOPm1s);
+               UV buv = SvUVX(TOPs);
+               
+               SP--;
+               SETs(boolSV(auv == buv));
+               RETURN;
+           }
+           {                   /* ## Mixed IV,UV ## */
+               IV iv;
+               UV uv;
+               
+               /* == is commutative so swap if needed (save code) */
+               if (auvok) {
+                   /* swap. top of stack (b) is the iv */
+                   iv = SvIVX(TOPs);
+                   SP--;
+                   if (iv < 0) {
+                       /* As (a) is a UV, it's >0, so it cannot be == */
+                       SETs(&PL_sv_no);
+                       RETURN;
+                   }
+                   uv = SvUVX(TOPs);
+               } else {
+                   iv = SvIVX(TOPm1s);
+                   SP--;
+                   if (iv < 0) {
+                       /* As (b) is a UV, it's >0, so it cannot be == */
+                       SETs(&PL_sv_no);
+                       RETURN;
+                   }
+                   uv = SvUVX(*(SP+1)); /* Do I want TOPp1s() ? */
+               }
+               /* we know iv is >= 0 */
+               if (uv > (UV) IV_MAX) {
+                   SETs(&PL_sv_no);
+                   RETURN;
+               }
+               SETs(boolSV((UV)iv == uv));
+               RETURN;
+           }
+       }
+    }
+#endif
     {
       dPOPnv;
       SETs(boolSV(TOPn == value));
@@ -297,7 +360,7 @@ PP(pp_preinc)
        ++SvIVX(TOPs);
        SvFLAGS(TOPs) &= ~(SVp_NOK|SVp_POK);
     }
-    else
+    else /* Do all the PERL_PRESERVE_IVUV conditionals in sv_inc */
        sv_inc(TOPs);
     SvSETMAGIC(TOPs);
     return NORMAL;
@@ -316,11 +379,125 @@ PP(pp_or)
 
 PP(pp_add)
 {
-    djSP; dATARGET; tryAMAGICbin(add,opASSIGN);
+    djSP; dATARGET; bool useleft; tryAMAGICbin(add,opASSIGN);
+    useleft = USE_LEFT(TOPm1s);
+#ifdef PERL_PRESERVE_IVUV
+    /* We must see if we can perform the addition with integers if possible,
+       as the integer code detects overflow while the NV code doesn't.
+       If either argument hasn't had a numeric conversion yet attempt to get
+       the IV. It's important to do this now, rather than just assuming that
+       it's not IOK as a PV of "9223372036854775806" may not take well to NV
+       addition, and an SV which is NOK, NV=6.0 ought to be coerced to
+       integer in case the second argument is IV=9223372036854775806
+       We can (now) rely on sv_2iv to do the right thing, only setting the
+       public IOK flag if the value in the NV (or PV) slot is truly integer.
+
+       A side effect is that this also aggressively prefers integer maths over
+       fp maths for integer values.  */
+    SvIV_please(TOPs);
+    if (SvIOK(TOPs)) {
+       /* Unless the left argument is integer in range we are going to have to
+          use NV maths. Hence only attempt to coerce the right argument if
+          we know the left is integer.  */
+       if (!useleft) {
+           /* left operand is undef, treat as zero. + 0 is identity. */
+           if (SvUOK(TOPs)) {
+               dPOPuv; /* Scary macros. Lets put a sequence point (;) here */
+               SETu(value);
+               RETURN;
+           } else {
+               dPOPiv;
+               SETi(value);
+               RETURN;
+           }
+       }
+       /* Left operand is defined, so is it IV? */
+       SvIV_please(TOPm1s);
+       if (SvIOK(TOPm1s)) {
+           bool auvok = SvUOK(TOPm1s);
+           bool buvok = SvUOK(TOPs);
+           
+           if (!auvok && !buvok) { /* ## IV + IV ## */
+               IV aiv = SvIVX(TOPm1s);
+               IV biv = SvIVX(TOPs);
+               IV result = aiv + biv;
+               
+               if (biv >= 0 ? (result >= aiv) : (result < aiv)) {
+                   SP--;
+                   SETi( result );
+                   RETURN;
+               }
+               if (biv >=0 && aiv >= 0) {
+                   UV result = (UV)aiv + (UV)biv;
+                   /* UV + UV can only get bigger... */
+                   if (result >= (UV) aiv) {
+                       SP--;
+                       SETu( result );
+                       RETURN;
+                   }
+               }
+               /* Overflow, drop through to NVs (beyond next if () else ) */
+           } else if (auvok && buvok) {        /* ## UV + UV ## */
+               UV auv = SvUVX(TOPm1s);
+               UV buv = SvUVX(TOPs);
+               UV result = auv + buv;
+               if (result >= auv) {
+                   SP--;
+                   SETu( result );
+                   RETURN;
+               }
+               /* Overflow, drop through to NVs (beyond next if () else ) */
+           } else {                    /* ## Mixed IV,UV ## */
+               IV aiv;
+               UV buv;
+               
+               /* addition is commutative so swap if needed (save code) */
+               if (buvok) {
+                   aiv = SvIVX(TOPm1s);
+                   buv = SvUVX(TOPs);
+               } else {
+                   aiv = SvIVX(TOPs);
+                   buv = SvUVX(TOPm1s);
+               }
+           
+               if (aiv >= 0) {
+                   UV result = (UV)aiv + buv;
+                   if (result >= buv) {
+                       SP--;
+                       SETu( result );
+                       RETURN;
+                   }
+               } else if (buv > (UV) IV_MAX) {
+                   /* assuming 2s complement means that IV_MIN == -IV_MIN,
+                      and (UV)-IV_MIN *is* the value -IV_MIN (or IV_MAX + 1)
+                      as buv > IV_MAX, it is >= (IV_MAX + 1), and therefore
+                      as the value we can be subtracting from it only lies in
+                      the range (-IV_MIN to -1) it can't overflow a UV */
+                   SP--;
+                   SETu( buv - (UV)-aiv );
+                   RETURN;
+               } else {
+                   IV result = (IV) buv + aiv;
+                   /* aiv < 0 so it must get smaller.  */
+                   if (result < (IV) buv) {
+                       SP--;
+                       SETi( result );
+                       RETURN;
+                   }
+               }
+           } /* end of IV+IV / UV+UV / mixed */
+       }
+    }
+#endif
     {
-      dPOPTOPnnrl_ul;
-      SETn( left + right );
-      RETURN;
+       dPOPnv;
+       if (!useleft) {
+           /* left operand is undef, treat as zero. + 0.0 is identity. */
+           SETn(value);
+           RETURN;
+       }
+       SETn( value + TOPn );
+       RETURN;
     }
 }
 
@@ -415,21 +592,8 @@ PP(pp_print)
     }
     else if (!(fp = IoOFP(io))) {
        if (ckWARN2(WARN_CLOSED, WARN_IO))  {
-           if (IoIFP(io)) {
-               /* integrate with report_evil_fh()? */
-               char *name = NULL;
-               if (isGV(gv)) {
-                   SV* sv = sv_newmortal();
-                   gv_efullname4(sv, gv, Nullch, FALSE);
-                   name = SvPV_nolen(sv);
-               }
-               if (name && *name)
-                 Perl_warner(aTHX_ WARN_IO,
-                             "Filehandle %s opened only for input", name);
-               else
-                   Perl_warner(aTHX_ WARN_IO,
-                               "Filehandle opened only for input");
-           }
+           if (IoIFP(io))
+               report_evil_fh(gv, io, OP_phoney_INPUT_ONLY);
            else if (ckWARN2(WARN_UNOPENED,WARN_CLOSED))
                report_evil_fh(gv, io, PL_op->op_type);
        }
@@ -438,13 +602,13 @@ PP(pp_print)
     }
     else {
        MARK++;
-       if (PL_ofslen) {
+       if (PL_ofs_sv && SvOK(PL_ofs_sv)) {
            while (MARK <= SP) {
                if (!do_print(*MARK, fp))
                    break;
                MARK++;
                if (MARK <= SP) {
-                   if (PerlIO_write(fp, PL_ofs, PL_ofslen) == 0 || PerlIO_error(fp)) {
+                   if (!do_print(PL_ofs_sv, fp)) { /* $, */
                        MARK--;
                        break;
                    }
@@ -461,8 +625,8 @@ PP(pp_print)
        if (MARK <= SP)
            goto just_say_no;
        else {
-           if (PL_orslen)
-               if (PerlIO_write(fp, PL_ors, PL_orslen) == 0 || PerlIO_error(fp))
+           if (PL_ors_sv && SvOK(PL_ors_sv))
+               if (!do_print(PL_ors_sv, fp)) /* $\ */
                    goto just_say_no;
 
            if (IoFLAGS(io) & IOf_FLUSH)
@@ -1015,6 +1179,7 @@ PP(pp_match)
        TARG = DEFSV;
        EXTEND(SP,1);
     }
+    PL_reg_sv = TARG;
     PUTBACK;                           /* EVAL blocks need stack_sp. */
     s = SvPV(TARG, len);
     strend = s + len;
@@ -1104,27 +1269,25 @@ play_it_again:
        RX_MATCH_TAINTED_on(rx);
     TAINT_IF(RX_MATCH_TAINTED(rx));
     if (gimme == G_ARRAY) {
-       I32 iters, i, len;
+       I32 nparens, i, len;
 
-       iters = rx->nparens;
-       if (global && !iters)
+       nparens = rx->nparens;
+       if (global && !nparens)
            i = 1;
        else
            i = 0;
        SPAGAIN;                        /* EVAL blocks could move the stack. */
-       EXTEND(SP, iters + i);
-       EXTEND_MORTAL(iters + i);
-       for (i = !i; i <= iters; i++) {
+       EXTEND(SP, nparens + i);
+       EXTEND_MORTAL(nparens + i);
+       for (i = !i; i <= nparens; i++) {
            PUSHs(sv_newmortal());
            /*SUPPRESS 560*/
            if ((rx->startp[i] != -1) && rx->endp[i] != -1 ) {
                len = rx->endp[i] - rx->startp[i];
                s = rx->startp[i] + truebase;
                sv_setpvn(*SP, s, len);
-               if ((pm->op_pmdynflags & PMdf_UTF8) && !IN_BYTE) {
+               if (DO_UTF8(TARG))
                    SvUTF8_on(*SP);
-                   sv_utf8_downgrade(*SP, TRUE);
-               }
            }
        }
        if (global) {
@@ -1134,7 +1297,7 @@ play_it_again:
            r_flags |= REXEC_IGNOREPOS | REXEC_NOT_FIRST;
            goto play_it_again;
        }
-       else if (!iters)
+       else if (!nparens)
            XPUSHs(&PL_sv_yes);
        LEAVE_SCOPE(oldsave);
        RETURN;
@@ -1256,159 +1419,15 @@ Perl_do_readline(pTHX)
                    (void)do_close(PL_last_in_gv, FALSE); /* now it does*/
                }
            }
-           else if (type == OP_GLOB) {
-               SV *tmpcmd = NEWSV(55, 0);
-               SV *tmpglob = POPs;
-               ENTER;
-               SAVEFREESV(tmpcmd);
-#ifdef VMS /* expand the wildcards right here, rather than opening a pipe, */
-           /* since spawning off a process is a real performance hit */
-               {
-#include <descrip.h>
-#include <lib$routines.h>
-#include <nam.h>
-#include <rmsdef.h>
-                   char rslt[NAM$C_MAXRSS+1+sizeof(unsigned short int)] = {'\0','\0'};
-                   char vmsspec[NAM$C_MAXRSS+1];
-                   char *rstr = rslt + sizeof(unsigned short int), *begin, *end, *cp;
-                   char tmpfnam[L_tmpnam] = "SYS$SCRATCH:";
-                   $DESCRIPTOR(dfltdsc,"SYS$DISK:[]*.*;");
-                   PerlIO *tmpfp;
-                   STRLEN i;
-                   struct dsc$descriptor_s wilddsc
-                      = {0, DSC$K_DTYPE_T, DSC$K_CLASS_S, 0};
-                   struct dsc$descriptor_vs rsdsc
-                      = {sizeof rslt, DSC$K_DTYPE_VT, DSC$K_CLASS_VS, rslt};
-                   unsigned long int cxt = 0, sts = 0, ok = 1, hasdir = 0, hasver = 0, isunix = 0;
-
-                   /* We could find out if there's an explicit dev/dir or version
-                      by peeking into lib$find_file's internal context at
-                      ((struct NAM *)((struct FAB *)cxt)->fab$l_nam)->nam$l_fnb
-                      but that's unsupported, so I don't want to do it now and
-                      have it bite someone in the future. */
-                   strcat(tmpfnam,PerlLIO_tmpnam(NULL));
-                   cp = SvPV(tmpglob,i);
-                   for (; i; i--) {
-                      if (cp[i] == ';') hasver = 1;
-                      if (cp[i] == '.') {
-                          if (sts) hasver = 1;
-                          else sts = 1;
-                      }
-                      if (cp[i] == '/') {
-                         hasdir = isunix = 1;
-                         break;
-                      }
-                      if (cp[i] == ']' || cp[i] == '>' || cp[i] == ':') {
-                          hasdir = 1;
-                          break;
-                      }
-                   }
-                   if ((tmpfp = PerlIO_open(tmpfnam,"w+","fop=dlt")) != NULL) {
-                       Stat_t st;
-                       if (!PerlLIO_stat(SvPVX(tmpglob),&st) && S_ISDIR(st.st_mode))
-                         ok = ((wilddsc.dsc$a_pointer = tovmspath(SvPVX(tmpglob),vmsspec)) != NULL);
-                       else ok = ((wilddsc.dsc$a_pointer = tovmsspec(SvPVX(tmpglob),vmsspec)) != NULL);
-                       if (ok) wilddsc.dsc$w_length = (unsigned short int) strlen(wilddsc.dsc$a_pointer);
-                       while (ok && ((sts = lib$find_file(&wilddsc,&rsdsc,&cxt,
-                                                   &dfltdsc,NULL,NULL,NULL))&1)) {
-                           end = rstr + (unsigned long int) *rslt;
-                           if (!hasver) while (*end != ';') end--;
-                           *(end++) = '\n';  *end = '\0';
-                           for (cp = rstr; *cp; cp++) *cp = _tolower(*cp);
-                           if (hasdir) {
-                             if (isunix) trim_unixpath(rstr,SvPVX(tmpglob),1);
-                             begin = rstr;
-                           }
-                           else {
-                               begin = end;
-                               while (*(--begin) != ']' && *begin != '>') ;
-                               ++begin;
-                           }
-                           ok = (PerlIO_puts(tmpfp,begin) != EOF);
-                       }
-                       if (cxt) (void)lib$find_file_end(&cxt);
-                       if (ok && sts != RMS$_NMF &&
-                           sts != RMS$_DNF && sts != RMS$_FNF) ok = 0;
-                       if (!ok) {
-                           if (!(sts & 1)) {
-                             SETERRNO((sts == RMS$_SYN ? EINVAL : EVMSERR),sts);
-                           }
-                           PerlIO_close(tmpfp);
-                           fp = NULL;
-                       }
-                       else {
-                          PerlIO_rewind(tmpfp);
-                          IoTYPE(io) = IoTYPE_RDONLY;
-                          IoIFP(io) = fp = tmpfp;
-                          IoFLAGS(io) &= ~IOf_UNTAINT;  /* maybe redundant */
-                       }
-                   }
-               }
-#else /* !VMS */
-#ifdef MACOS_TRADITIONAL
-               sv_setpv(tmpcmd, "glob ");
-               sv_catsv(tmpcmd, tmpglob);
-               sv_catpv(tmpcmd, " |");
-#else
-#ifdef DOSISH
-#ifdef OS2
-               sv_setpv(tmpcmd, "for a in ");
-               sv_catsv(tmpcmd, tmpglob);
-               sv_catpv(tmpcmd, "; do echo \"$a\\0\\c\"; done |");
-#else
-#ifdef DJGPP
-               sv_setpv(tmpcmd, "/dev/dosglob/"); /* File System Extension */
-               sv_catsv(tmpcmd, tmpglob);
-#else
-               sv_setpv(tmpcmd, "perlglob ");
-               sv_catsv(tmpcmd, tmpglob);
-               sv_catpv(tmpcmd, " |");
-#endif /* !DJGPP */
-#endif /* !OS2 */
-#else /* !DOSISH */
-#if defined(CSH)
-               sv_setpvn(tmpcmd, PL_cshname, PL_cshlen);
-               sv_catpv(tmpcmd, " -cf 'set nonomatch; glob ");
-               sv_catsv(tmpcmd, tmpglob);
-               sv_catpv(tmpcmd, "' 2>/dev/null |");
-#else
-               sv_setpv(tmpcmd, "echo ");
-               sv_catsv(tmpcmd, tmpglob);
-#if 'z' - 'a' == 25
-               sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\012\\012\\012\\012'|");
-#else
-               sv_catpv(tmpcmd, "|tr -s ' \t\f\r' '\\n\\n\\n\\n'|");
-#endif
-#endif /* !CSH */
-#endif /* !DOSISH */
-#endif /* MACOS_TRADITIONAL */
-               (void)do_open(PL_last_in_gv, SvPVX(tmpcmd), SvCUR(tmpcmd),
-                             FALSE, O_RDONLY, 0, Nullfp);
-               fp = IoIFP(io);
-#endif /* !VMS */
-               LEAVE;
-           }
+           else if (type == OP_GLOB)
+               fp = Perl_start_glob(aTHX_ POPs, io);
        }
        else if (type == OP_GLOB)
            SP--;
        else if (ckWARN(WARN_IO)        /* stdout/stderr or other write fh */
                 && (IoTYPE(io) == IoTYPE_WRONLY || fp == PerlIO_stdout()
                     || fp == PerlIO_stderr()))
-       {
-           /* integrate with report_evil_fh()? */
-           char *name = NULL;
-           if (isGV(PL_last_in_gv)) { /* can this ever fail? */
-               SV* sv = sv_newmortal();
-               gv_efullname4(sv, PL_last_in_gv, Nullch, FALSE);
-               name = SvPV_nolen(sv);
-           }
-           if (name && *name)
-               Perl_warner(aTHX_ WARN_IO,
-                           "Filehandle %s opened only for output", name);
-           else
-               Perl_warner(aTHX_ WARN_IO,
-                           "Filehandle opened only for output");
-       }
+           report_evil_fh(PL_last_in_gv, io, OP_phoney_OUTPUT_ONLY);
     }
     if (!fp) {
        if (ckWARN2(WARN_GLOB, WARN_CLOSED)
@@ -1559,8 +1578,11 @@ PP(pp_helem)
     U32 defer = PL_op->op_private & OPpLVAL_DEFER;
     SV *sv;
     U32 hash = (SvFAKE(keysv) && SvREADONLY(keysv)) ? SvUVX(keysv) : 0;
+    I32 preeminent;
 
     if (SvTYPE(hv) == SVt_PVHV) {
+       if (PL_op->op_private & OPpLVAL_INTRO)
+           preeminent = SvRMAGICAL(hv) ? 1 : hv_exists_ent(hv, keysv, 0);
        he = hv_fetch_ent(hv, keysv, lval && !defer, hash);
        svp = he ? &HeVAL(he) : 0;
     }
@@ -1593,8 +1615,14 @@ PP(pp_helem)
        if (PL_op->op_private & OPpLVAL_INTRO) {
            if (HvNAME(hv) && isGV(*svp))
                save_gp((GV*)*svp, !(PL_op->op_flags & OPf_SPECIAL));
-           else
-               save_helem(hv, keysv, svp);
+           else {
+               if (!preeminent) {
+                   STRLEN keylen;
+                   char *key = SvPV(keysv, keylen);
+                   save_delete(hv, key, keylen);
+               } else 
+                   save_helem(hv, keysv, svp);
+            }
        }
        else if (PL_op->op_private & OPpDEREF)
            vivify_ref(*svp, PL_op->op_private & OPpDEREF);
@@ -1802,6 +1830,7 @@ PP(pp_subst)
        TARG = DEFSV;
        EXTEND(SP,1);
     }
+    PL_reg_sv = TARG;
     if (SvFAKE(TARG) && SvREADONLY(TARG))
        sv_force_normal(TARG);
     if (SvREADONLY(TARG)
@@ -1818,7 +1847,7 @@ PP(pp_subst)
     if (PL_tainted)
        rxtainted |= 2;
     TAINT_NOT;
-
+    
   force_it:
     if (!pm || !s)
        DIE(aTHX_ "panic: do_subst");
@@ -1975,6 +2004,8 @@ PP(pp_subst)
        rxtainted |= RX_MATCH_TAINTED(rx);
        dstr = NEWSV(25, len);
        sv_setpvn(dstr, m, s-m);
+       if (DO_UTF8(TARG))
+           SvUTF8_on(dstr);
        PL_curpm = pm;
        if (!c) {
            register PERL_CONTEXT *cx;
@@ -2001,7 +2032,8 @@ PP(pp_subst)
                sv_catpvn(dstr, c, clen);
            if (once)
                break;
-       } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m, TARG, NULL, r_flags));
+       } while (CALLREGEXEC(aTHX_ rx, s, strend, orig, s == m,
+                            TARG, NULL, r_flags));
        sv_catpvn(dstr, s, strend - s);
 
        (void)SvOOK_off(TARG);