this.replaceString = 0;
this.lastIndex = 0;
this.answer = 0;
+ // answerSaved marks whether the contents of answer is valid for a cache
+ // hit in RegExpExec, StringMatch and StringSplit.
+ this.answerSaved = false;
}
function CloneRegexpAnswer(array) {
+ if (array == null) return null;
var len = array.length;
var answer = new $Array(len);
for (var i = 0; i < len; i++) {
}
var cache = regExpCache;
+ var saveAnswer = false;
if (%_ObjectEquals(cache.type, 'exec') &&
%_ObjectEquals(cache.lastIndex, this.lastIndex) &&
%_ObjectEquals(cache.regExp, this) &&
%_ObjectEquals(cache.subject, string)) {
- var last = cache.answer;
- if (last == null) {
- return last;
+ if (cache.answerSaved) {
+ return CloneRegexpAnswer(cache.answer);
} else {
- return CloneRegexpAnswer(last);
+ saveAnswer = true;
}
+ } else {
+ cache.answerSaved = false;
}
if (%_ArgumentsLength() == 0) {
cache.regExp = this;
cache.subject = s;
cache.answer = matchIndices; // Null.
+ cache.answerSaved = true; // Safe since no cloning is needed.
cache.type = 'exec';
return matchIndices; // No match.
}
result.input = s;
if (this.global) {
this.lastIndex = lastMatchInfo[CAPTURE1];
- return result;
} else {
cache.regExp = this;
cache.subject = s;
cache.lastIndex = lastIndex;
- cache.answer = result;
+ if (saveAnswer) {
+ cache.answer = CloneRegexpAnswer(result);
+ cache.answerSaved = true;
+ }
cache.type = 'exec';
- return CloneRegexpAnswer(result);
}
+ return result;
+
}
if (!regexp.global) return regexp.exec(subject);
var cache = regExpCache;
+ var saveAnswer = false;
if (%_ObjectEquals(cache.type, 'match') &&
%_ObjectEquals(cache.regExp, regexp) &&
%_ObjectEquals(cache.subject, subject)) {
- var last = cache.answer;
- if (last == null) {
- return last;
+ if (cache.answerSaved) {
+ return CloneRegexpAnswer(cache.answer);
} else {
- return CloneRegexpAnswer(last);
+ saveAnswer = true;
}
}
cache.type = 'match';
cache.regExp = regexp;
cache.subject = subject;
- cache.answer = result;
- if (result == null) {
- return result;
- } else {
- return CloneRegexpAnswer(result);
- }
+ if (saveAnswer) cache.answer = CloneRegexpAnswer(result);
+ cache.answerSaved = saveAnswer;
+ return result;
}
}
var cache = regExpCache;
+ var saveAnswer = false;
if (%_ObjectEquals(cache.type, 'split') &&
%_ObjectEquals(cache.regExp, separator) &&
%_ObjectEquals(cache.subject, subject)) {
- return CloneRegexpAnswer(cache.answer);
+ if (cache.answerSaved) {
+ return CloneRegexpAnswer(cache.answer);
+ } else {
+ saveAnswer = true;
+ }
}
cache.type = 'split';
%_Log('regexp', 'regexp-split,%0S,%1r', [subject, separator]);
if (length === 0) {
+ cache.answerSaved = true;
if (splitMatch(separator, subject, 0, 0) != null) {
cache.answer = [];
return [];
var startIndex = 0;
var result = [];
+ outer_loop:
while (true) {
if (startIndex === length) {
result[result.length] = subject.slice(currentIndex, length);
- cache.answer = result;
- return CloneRegexpAnswer(result);
+ break;
}
var matchInfo = splitMatch(separator, subject, currentIndex, startIndex);
if (IS_NULL(matchInfo)) {
result[result.length] = subject.slice(currentIndex, length);
- cache.answer = result;
- return CloneRegexpAnswer(result);
+ break;
}
var endIndex = matchInfo[CAPTURE1];
}
result[result.length] = SubString(subject, currentIndex, matchInfo[CAPTURE0]);
- if (result.length === limit) {
- cache.answer = result;
- return CloneRegexpAnswer(result);
- }
+ if (result.length === limit) break;
var num_captures = NUMBER_OF_CAPTURES(matchInfo);
for (var i = 2; i < num_captures; i += 2) {
} else {
result[result.length] = void 0;
}
- if (result.length === limit) {
- cache.answer = result;
- return CloneRegexpAnswer(result);
- }
+ if (result.length === limit) break outer_loop;
}
startIndex = currentIndex = endIndex;
}
+ if (saveAnswer) cache.answer = CloneRegexpAnswer(result);
+ cache.answerSaved = saveAnswer;
+ return result;
+
}