Reviewers: Erik Corry,

Description:
Inline common case of one capture when using replace with a regexp and
a function.


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

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

Affected files:
   M     src/string.js


Index: src/string.js
===================================================================
--- src/string.js       (revision 3239)
+++ src/string.js       (working copy)
@@ -382,10 +382,21 @@
    if (regexp.global) {
      var previous = 0;
      do {
-      result.addSpecialSlice(previous, matchInfo[CAPTURE0]);
        var startOfMatch = matchInfo[CAPTURE0];
+      result.addSpecialSlice(previous, startOfMatch);
        previous = matchInfo[CAPTURE1];
-      result.add(ApplyReplacementFunction(replace, matchInfo, subject));
+      var numberOfCaptures = NUMBER_OF_CAPTURES(matchInfo) >> 1;
+      if (numberOfCaptures == 1) {
+        // If either start or end of match is missing use undefined.
+        var match;
+        if (startOfMatch >= 0 && previous >= 0) {
+          match = SubString(subject, startOfMatch, previous);
+        }
+        // Don't call directly to avoid exposing the built-in global  
object.
+        result.add(replace.call(null, match, startOfMatch, subject));
+      } else {
+        result.add(ApplyReplacementFunction(replace, matchInfo, subject));
+      }
        // Can't use matchInfo any more from here, since the function could
        // overwrite it.
        // Continue with the next match.



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

Reply via email to