Reviewers: Erik Corry,

Message:
PTAL.

This caused an assertion failure in debug mode when the subcapture is not an
actual substring.

Description:
Fix creating substring in string.replace(<global regexp>, <function>).


BUG=
TEST=regexp-global.js


Please review this at http://codereview.chromium.org/10454032/

SVN Base: https://v8.googlecode.com/svn/branches/bleeding_edge

Affected files:
  M src/runtime.cc
  M test/mjsunit/regexp-global.js


Index: src/runtime.cc
diff --git a/src/runtime.cc b/src/runtime.cc
index af9cef575b5a96c46062520aec63226241151c7c..d18a158c44d0442eb42ec67c5bea514031c15b48 100644
--- a/src/runtime.cc
+++ b/src/runtime.cc
@@ -3955,7 +3955,6 @@ static int SearchRegExpMultiple(
             match = isolate->factory()->NewSubString(subject,
                                                      match_start,
                                                      match_end);
-            first = false;
           }
           elements->set(0, *match);
           for (int i = 1; i <= capture_count; i++) {
@@ -3963,8 +3962,14 @@ static int SearchRegExpMultiple(
             if (start >= 0) {
               int end = current_match[i * 2 + 1];
               ASSERT(start <= end);
-              Handle<String> substring =
- isolate->factory()->NewProperSubString(subject, start, end);
+              Handle<String> substring;
+              if (!first) {
+                substring =
+ isolate->factory()->NewProperSubString(subject, start, end);
+              } else {
+                substring =
+                    isolate->factory()->NewSubString(subject, start, end);
+              }
               elements->set(i, *substring);
             } else {
               ASSERT(current_match[i * 2 + 1] < 0);
@@ -3975,6 +3980,7 @@ static int SearchRegExpMultiple(
           elements->set(capture_count + 2, *subject);
builder->Add(*isolate->factory()->NewJSArrayWithElements(elements));
         }
+        first = false;
       }

       // If we did not get the maximum number of matches, we can stop here
Index: test/mjsunit/regexp-global.js
diff --git a/test/mjsunit/regexp-global.js b/test/mjsunit/regexp-global.js
index fac5c6af03fd11dfcb3a4527d948c0e56b47315d..4b833d7c6c69a037878e8ae13109e38a69250810 100644
--- a/test/mjsunit/regexp-global.js
+++ b/test/mjsunit/regexp-global.js
@@ -125,3 +125,8 @@ str = str.replace(/(FOUR|TWO) \u817f (GOOD|BAD)/g,
                     return match.length - 7;
                   });
 assertEquals("4, 2!", str);
+
+// Test capture that is a real substring.
+var str = "Beasts of England, beasts of Ireland";
+str = str.replace(/(.*)/g, function(match) { return '~'; });
+assertEquals("~~");


--
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev

Reply via email to