regexec_flags(): keep stringarg constant
authorDavid Mitchell <davem@iabyn.com>
Wed, 10 Jul 2013 13:28:02 +0000 (14:28 +0100)
committerDavid Mitchell <davem@iabyn.com>
Sun, 28 Jul 2013 09:33:37 +0000 (10:33 +0100)
stringarg is the arg passed to Perl_regexec_flags() to indicate where to
start matching. Currently the code adjusts this under \G, then copies it
to startpos, then later tinkers with startpos further.

Change it so that stringarg is never changed, and all the adjusting is to
startpos. Shouldn't make any logical difference, but makes the code
slightly cleaner and easier to understand (and doesn't require minend to
be adjusted any more).

regexec.c

index 6aacd0b..89fafde 100644 (file)
--- a/regexec.c
+++ b/regexec.c
@@ -2240,6 +2240,8 @@ Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
         "Matching");
     );
 
+    startpos = stringarg;
+
     if (prog->extflags & RXf_GPOS_SEEN) {
         /* in the presence of \G, we may need to start looking earlier in
          * the string than the suggested start point of stringarg:
@@ -2251,34 +2253,26 @@ Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
          * to the start of the string, e.g. /w+\G/
          */
         if (prog->gofs) {
-            if (stringarg - prog->gofs < strbeg) {
-                minend += (stringarg - strbeg);
-                stringarg = strbeg;
-            }
-            else {
-                stringarg -= prog->gofs;
-                minend    += prog->gofs;
-            }
-        }
-        else if (prog->extflags & RXf_GPOS_FLOAT) {
-            minend += (stringarg - strbeg);
-            stringarg = strbeg;
+            if (startpos - prog->gofs < strbeg)
+                startpos = strbeg;
+            else
+                startpos -= prog->gofs;
         }
+        else if (prog->extflags & RXf_GPOS_FLOAT)
+            startpos = strbeg;
     }
 
     minlen = prog->minlen;
-    if ((stringarg + minlen) > strend || stringarg < strbeg) {
+    if ((startpos + minlen) > strend || startpos < strbeg) {
         DEBUG_r(PerlIO_printf(Perl_debug_log,
                     "Regex match can't succeed, so not even tried\n"));
         return 0;
     }
 
-    startpos = stringarg;
-
     if ((RX_EXTFLAGS(rx) & RXf_USE_INTUIT)
         && !(flags & REXEC_CHECKED))
     {
-       startpos = re_intuit_start(rx, sv, strbeg, stringarg, strend,
+       startpos = re_intuit_start(rx, sv, strbeg, startpos, strend,
                                     flags, NULL);
        if (!startpos)
            return 0;
@@ -2338,7 +2332,7 @@ Perl_regexec_flags(pTHX_ REGEXP * const rx, char *stringarg, char *strend,
     reginfo->poscache_maxiter = 0; /* not yet started a countdown */
     reginfo->strend = strend;
     /* see how far we have to get to not match where we matched before */
-    reginfo->till = stringarg + minend;
+    reginfo->till = startpos + minend;
 
     if (prog->extflags & RXf_EVAL_SEEN && SvPADTMP(sv) && !IS_PADGV(sv)) {
         /* SAVEFREESV, not sv_mortalcopy, as this SV must last until after