Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Unified Diff: src/regexp.js

Issue 1645001: Native construction of RegExp result objects, with in-object index and input. (Closed)
Patch Set: Created 10 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/regexp.js
diff --git a/src/regexp.js b/src/regexp.js
index c76b09d37706d9025614623f27d703a17aff0fbb..9929b112e8f1fff849c2fe1f83303fd6fc3fe111 100644
--- a/src/regexp.js
+++ b/src/regexp.js
@@ -135,19 +135,52 @@ function RegExpCache() {
var regExpCache = new RegExpCache();
-function CloneRegexpAnswer(array) {
+function CloneRegExpResult(array) {
if (array == null) return null;
- var len = array.length;
- var answer = new $Array(len);
- for (var i = 0; i < len; i++) {
+ var length = array.length;
+ var answer = %_RegExpConstructResult(length, array.index, array.input);
Søren Thygesen Gjesse 2010/04/13 07:22:29 Will it be possible to pass in the array to have t
Lasse Reichstein 2010/04/13 09:50:56 It's a good idea for an optional argument. The fun
+ for (var i = 0; i < length; i++) {
answer[i] = array[i];
}
- answer.index = array.index;
- answer.input = array.input;
return answer;
}
+function BuildResultFromMatchInfo(lastMatchInfo, s) {
+ var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1;
+ var result = %_RegExpConstructResult(numResults, lastMatchInfo[CAPTURE0], s);
+ if (numResults === 1) {
+ var matchStart = lastMatchInfo[CAPTURE(0)];
+ var matchEnd = lastMatchInfo[CAPTURE(1)];
+ result[0] = SubString(s, matchStart, matchEnd);
+ } else {
+ for (var i = 0; i < numResults; i++) {
+ var matchStart = lastMatchInfo[CAPTURE(i << 1)];
+ var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)];
+ if (matchStart != -1 && matchEnd != -1) {
+ result[i] = SubString(s, matchStart, matchEnd);
+ } else {
+ // Make sure the element is present. Avoid reading the undefined
+ // property from the global object since this may change.
+ result[i] = void 0;
+ }
+ }
+ }
+ return result;
+}
+
+
+function RegExpExecNoTests(regexp, string, start) {
+ // Must be called with RegExp, string and positive integer as arguments.
+ var matchInfo = DoRegExpExec(regexp, string, start);
+ var result = null;
+ if (matchInfo !== null) {
+ result = BuildResultFromMatchInfo(matchInfo, string);
+ }
+ return result;
+}
+
+
function RegExpExec(string) {
if (!IS_REGEXP(this)) {
throw MakeTypeError('incompatible_method_receiver',
@@ -162,7 +195,7 @@ function RegExpExec(string) {
%_ObjectEquals(cache.regExp, this) &&
%_ObjectEquals(cache.subject, string)) {
if (cache.answerSaved) {
- return CloneRegexpAnswer(cache.answer);
+ return CloneRegExpResult(cache.answer);
} else {
saveAnswer = true;
}
@@ -205,36 +238,15 @@ function RegExpExec(string) {
return matchIndices; // No match.
}
- var numResults = NUMBER_OF_CAPTURES(lastMatchInfo) >> 1;
- var result;
- if (numResults === 1) {
- var matchStart = lastMatchInfo[CAPTURE(0)];
- var matchEnd = lastMatchInfo[CAPTURE(1)];
- result = [SubString(s, matchStart, matchEnd)];
- } else {
- result = new $Array(numResults);
- for (var i = 0; i < numResults; i++) {
- var matchStart = lastMatchInfo[CAPTURE(i << 1)];
- var matchEnd = lastMatchInfo[CAPTURE((i << 1) + 1)];
- if (matchStart != -1 && matchEnd != -1) {
- result[i] = SubString(s, matchStart, matchEnd);
- } else {
- // Make sure the element is present. Avoid reading the undefined
- // property from the global object since this may change.
- result[i] = void 0;
- }
- }
- }
+ var result = BuildResultFromMatchInfo(matchIndices, s);
- result.index = lastMatchInfo[CAPTURE0];
- result.input = s;
if (this.global) {
this.lastIndex = lastMatchInfo[CAPTURE1];
} else {
cache.regExp = this;
cache.subject = s;
cache.lastIndex = lastIndex;
- if (saveAnswer) cache.answer = CloneRegexpAnswer(result);
+ if (saveAnswer) cache.answer = CloneRegExpResult(result);
cache.answerSaved = saveAnswer;
cache.type = 'exec';
}

Powered by Google App Engine
This is Rietveld 408576698