Bug 1011693 part 2 - Refactor some loops in DoReplace and FindReplaceLength. r=terrence
authorJan de Mooij <jdemooij@mozilla.com>
Wed, 21 May 2014 10:34:53 +0200
changeset 184192 578778d7baca8434a18318425130327defeb3272
parent 184191 d6d6c5381f9523796f574bab5ac0a6816093b1b4
child 184193 e8edc293a0818a5e71ddbf7e051460a1d581363d
push id26814
push userryanvm@gmail.com
push dateWed, 21 May 2014 19:50:12 +0000
treeherdermozilla-central@7aee2fa0f655 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersterrence
bugs1011693
milestone32.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1011693 part 2 - Refactor some loops in DoReplace and FindReplaceLength. r=terrence
js/src/jsstr.cpp
--- a/js/src/jsstr.cpp
+++ b/js/src/jsstr.cpp
@@ -2480,30 +2480,34 @@ FindReplaceLength(JSContext *cx, RegExpS
         if (!rdata.repstr)
             return false;
         *sizep = rdata.repstr->length();
         return true;
     }
 
     JSLinearString *repstr = rdata.repstr;
     CheckedInt<uint32_t> replen = repstr->length();
-    const jschar *ep = repstr->chars() + repstr->length();
-    for (const jschar *dp = rdata.dollar; dp;
-         dp = js_strchr_limit(dp, '$', ep)) {
-        JSSubString sub;
-        size_t skip;
-        if (InterpretDollar(res, dp, ep, rdata, &sub, &skip)) {
-            if (sub.length > skip)
-                replen += sub.length - skip;
-            else
-                replen -= skip - sub.length;
-            dp += skip;
-        } else {
-            dp++;
-        }
+    if (rdata.dollar) {
+        const jschar *dp = rdata.dollar;
+        const jschar *ep = repstr->chars() + repstr->length();
+        do {
+            JSSubString sub;
+            size_t skip;
+            if (InterpretDollar(res, dp, ep, rdata, &sub, &skip)) {
+                if (sub.length > skip)
+                    replen += sub.length - skip;
+                else
+                    replen -= skip - sub.length;
+                dp += skip;
+            } else {
+                dp++;
+            }
+
+            dp = js_strchr_limit(dp, '$', ep);
+        } while (dp);
     }
 
     if (!replen.isValid()) {
         js_ReportAllocationOverflow(cx);
         return false;
     }
 
     *sizep = replen.value();
@@ -2516,34 +2520,38 @@ FindReplaceLength(JSContext *cx, RegExpS
  */
 static void
 DoReplace(RegExpStatics *res, ReplaceData &rdata)
 {
     JSLinearString *repstr = rdata.repstr;
     const jschar *bp = repstr->chars();
     const jschar *cp = bp;
 
-    const jschar *dp = rdata.dollar;
-    const jschar *ep = bp + repstr->length();
-    for (; dp; dp = js_strchr_limit(dp, '$', ep)) {
-        /* Move one of the constant portions of the replacement value. */
-        size_t len = dp - cp;
-        rdata.sb.infallibleAppend(cp, len);
-        cp = dp;
-
-        JSSubString sub;
-        size_t skip;
-        if (InterpretDollar(res, dp, ep, rdata, &sub, &skip)) {
-            len = sub.length;
-            rdata.sb.infallibleAppend(sub.chars, len);
-            cp += skip;
-            dp += skip;
-        } else {
-            dp++;
-        }
+    if (rdata.dollar) {
+        const jschar *dp = rdata.dollar;
+        const jschar *ep = bp + repstr->length();
+        do {
+            /* Move one of the constant portions of the replacement value. */
+            size_t len = dp - cp;
+            rdata.sb.infallibleAppend(cp, len);
+            cp = dp;
+
+            JSSubString sub;
+            size_t skip;
+            if (InterpretDollar(res, dp, ep, rdata, &sub, &skip)) {
+                len = sub.length;
+                rdata.sb.infallibleAppend(sub.chars, len);
+                cp += skip;
+                dp += skip;
+            } else {
+                dp++;
+            }
+
+            dp = js_strchr_limit(dp, '$', ep);
+        } while (dp);
     }
     rdata.sb.infallibleAppend(cp, repstr->length() - (cp - bp));
 }
 
 static bool
 ReplaceRegExp(JSContext *cx, RegExpStatics *res, ReplaceData &rdata)
 {