2012-03-13 15:44:19 +00:00
|
|
|
// Copyright 2012 the V8 project authors. All rights reserved.
|
2008-07-03 15:10:15 +00:00
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
// modification, are permitted provided that the following conditions are
|
|
|
|
// met:
|
|
|
|
//
|
|
|
|
// * Redistributions of source code must retain the above copyright
|
|
|
|
// notice, this list of conditions and the following disclaimer.
|
|
|
|
// * Redistributions in binary form must reproduce the above
|
|
|
|
// copyright notice, this list of conditions and the following
|
|
|
|
// disclaimer in the documentation and/or other materials provided
|
|
|
|
// with the distribution.
|
|
|
|
// * Neither the name of Google Inc. nor the names of its
|
|
|
|
// contributors may be used to endorse or promote products derived
|
|
|
|
// from this software without specific prior written permission.
|
|
|
|
//
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
|
|
|
// This file relies on the fact that the following declaration has been made
|
|
|
|
// in runtime.js:
|
2012-02-20 13:48:24 +00:00
|
|
|
// var $String = global.String;
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
// -------------------------------------------------------------------
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-04-11 12:15:25 +00:00
|
|
|
function StringConstructor(x) {
|
2010-02-19 13:07:37 +00:00
|
|
|
var value = %_ArgumentsLength() == 0 ? '' : TO_STRING_INLINE(x);
|
2009-06-29 08:14:06 +00:00
|
|
|
if (%_IsConstructCall()) {
|
2008-07-03 15:10:15 +00:00
|
|
|
%_SetValueOf(this, value);
|
|
|
|
} else {
|
|
|
|
return value;
|
|
|
|
}
|
2013-04-11 12:15:25 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
// ECMA-262 section 15.5.4.2
|
|
|
|
function StringToString() {
|
2011-11-28 12:11:00 +00:00
|
|
|
if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) {
|
2008-10-03 07:58:18 +00:00
|
|
|
throw new $TypeError('String.prototype.toString is not generic');
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
return %_ValueOf(this);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.3
|
|
|
|
function StringValueOf() {
|
2011-11-28 12:11:00 +00:00
|
|
|
if (!IS_STRING(this) && !IS_STRING_WRAPPER(this)) {
|
2008-10-03 07:58:18 +00:00
|
|
|
throw new $TypeError('String.prototype.valueOf is not generic');
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-10-03 07:14:31 +00:00
|
|
|
return %_ValueOf(this);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262, section 15.5.4.4
|
|
|
|
function StringCharAt(pos) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.charAt");
|
|
|
|
|
2010-05-26 14:23:19 +00:00
|
|
|
var result = %_StringCharAt(this, pos);
|
|
|
|
if (%_IsSmi(result)) {
|
|
|
|
result = %_StringCharAt(TO_STRING_INLINE(this), TO_INTEGER(pos));
|
|
|
|
}
|
|
|
|
return result;
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.5
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringCharCodeAt(pos) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.charCodeAt");
|
|
|
|
|
2010-05-26 14:23:19 +00:00
|
|
|
var result = %_StringCharCodeAt(this, pos);
|
|
|
|
if (!%_IsSmi(result)) {
|
|
|
|
result = %_StringCharCodeAt(TO_STRING_INLINE(this), TO_INTEGER(pos));
|
2008-07-30 08:49:36 +00:00
|
|
|
}
|
2010-05-26 14:23:19 +00:00
|
|
|
return result;
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262, section 15.5.4.6
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringConcat() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.concat");
|
|
|
|
|
2010-02-08 14:00:50 +00:00
|
|
|
var len = %_ArgumentsLength();
|
2010-02-19 13:07:37 +00:00
|
|
|
var this_as_string = TO_STRING_INLINE(this);
|
2010-02-08 14:00:50 +00:00
|
|
|
if (len === 1) {
|
|
|
|
return this_as_string + %_Arguments(0);
|
2010-01-07 09:40:50 +00:00
|
|
|
}
|
2011-03-03 11:49:03 +00:00
|
|
|
var parts = new InternalArray(len + 1);
|
2010-02-08 14:00:50 +00:00
|
|
|
parts[0] = this_as_string;
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
var part = %_Arguments(i);
|
2010-02-19 13:07:37 +00:00
|
|
|
parts[i + 1] = TO_STRING_INLINE(part);
|
2010-02-08 14:00:50 +00:00
|
|
|
}
|
|
|
|
return %StringBuilderConcat(parts, len + 1, "");
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Match ES3 and Safari
|
2008-10-03 07:58:18 +00:00
|
|
|
%FunctionSetLength(StringConcat, 1);
|
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.7
|
2010-12-13 12:19:10 +00:00
|
|
|
function StringIndexOf(pattern /* position */) { // length == 1
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.indexOf");
|
|
|
|
|
2010-12-13 12:19:10 +00:00
|
|
|
var subject = TO_STRING_INLINE(this);
|
2011-02-11 13:30:37 +00:00
|
|
|
pattern = TO_STRING_INLINE(pattern);
|
2008-10-03 07:58:18 +00:00
|
|
|
var index = 0;
|
|
|
|
if (%_ArgumentsLength() > 1) {
|
2011-02-11 12:33:30 +00:00
|
|
|
index = %_Arguments(1); // position
|
|
|
|
index = TO_INTEGER(index);
|
|
|
|
if (index < 0) index = 0;
|
|
|
|
if (index > subject.length) index = subject.length;
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2011-02-11 13:30:37 +00:00
|
|
|
return %StringIndexOf(subject, pattern, index);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.8
|
2010-12-13 12:19:10 +00:00
|
|
|
function StringLastIndexOf(pat /* position */) { // length == 1
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.lastIndexOf");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
var sub = TO_STRING_INLINE(this);
|
2009-05-26 15:42:06 +00:00
|
|
|
var subLength = sub.length;
|
2010-12-13 12:19:10 +00:00
|
|
|
var pat = TO_STRING_INLINE(pat);
|
2009-05-26 15:42:06 +00:00
|
|
|
var patLength = pat.length;
|
|
|
|
var index = subLength - patLength;
|
|
|
|
if (%_ArgumentsLength() > 1) {
|
|
|
|
var position = ToNumber(%_Arguments(1));
|
2010-12-22 13:19:25 +00:00
|
|
|
if (!NUMBER_IS_NAN(position)) {
|
2009-05-26 15:42:06 +00:00
|
|
|
position = TO_INTEGER(position);
|
|
|
|
if (position < 0) {
|
|
|
|
position = 0;
|
|
|
|
}
|
|
|
|
if (position + patLength < subLength) {
|
2011-08-22 13:55:25 +00:00
|
|
|
index = position;
|
2009-05-26 15:42:06 +00:00
|
|
|
}
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
|
|
|
}
|
2009-05-26 15:42:06 +00:00
|
|
|
if (index < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return %StringLastIndexOf(sub, pat, index);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.9
|
|
|
|
//
|
|
|
|
// This function is implementation specific. For now, we do not
|
|
|
|
// do anything locale specific.
|
|
|
|
function StringLocaleCompare(other) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.localeCompare");
|
|
|
|
|
2011-08-19 05:24:39 +00:00
|
|
|
return %StringLocaleCompare(TO_STRING_INLINE(this),
|
2010-12-13 12:19:10 +00:00
|
|
|
TO_STRING_INLINE(other));
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.10
|
|
|
|
function StringMatch(regexp) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.match");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
var subject = TO_STRING_INLINE(this);
|
2010-04-21 08:33:04 +00:00
|
|
|
if (IS_REGEXP(regexp)) {
|
2012-12-05 12:32:25 +00:00
|
|
|
// Emulate RegExp.prototype.exec's side effect in step 5, even though
|
|
|
|
// value is discarded.
|
2013-07-05 12:57:38 +00:00
|
|
|
var lastIndex = regexp.lastIndex;
|
|
|
|
TO_INTEGER_FOR_SIDE_EFFECT(lastIndex);
|
2010-12-17 11:57:10 +00:00
|
|
|
if (!regexp.global) return RegExpExecNoTests(regexp, subject, 0);
|
2010-04-13 09:31:03 +00:00
|
|
|
%_Log('regexp', 'regexp-match,%0S,%1r', [subject, regexp]);
|
|
|
|
// lastMatchInfo is defined in regexp.js.
|
2012-04-10 10:42:25 +00:00
|
|
|
var result = %StringMatch(subject, regexp, lastMatchInfo);
|
|
|
|
if (result !== null) lastMatchInfoOverride = null;
|
2013-01-25 10:53:26 +00:00
|
|
|
regexp.lastIndex = 0;
|
2012-04-10 10:42:25 +00:00
|
|
|
return result;
|
2010-03-17 10:23:06 +00:00
|
|
|
}
|
2010-04-13 09:31:03 +00:00
|
|
|
// Non-regexp argument.
|
|
|
|
regexp = new $RegExp(regexp);
|
|
|
|
return RegExpExecNoTests(regexp, subject, 0);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2014-01-31 08:09:17 +00:00
|
|
|
var NORMALIZATION_FORMS = ['NFC', 'NFD', 'NFKC', 'NFKD'];
|
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 v6, section 21.1.3.12
|
|
|
|
//
|
|
|
|
// For now we do nothing, as proper normalization requires big tables.
|
|
|
|
// If Intl is enabled, then i18n.js will override it and provide the the
|
|
|
|
// proper functionality.
|
|
|
|
function StringNormalize(form) {
|
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.normalize");
|
|
|
|
|
|
|
|
var form = form ? TO_STRING_INLINE(form) : 'NFC';
|
|
|
|
var normalizationForm = NORMALIZATION_FORMS.indexOf(form);
|
|
|
|
if (normalizationForm === -1) {
|
|
|
|
throw new $RangeError('The normalization form should be one of '
|
|
|
|
+ NORMALIZATION_FORMS.join(', ') + '.');
|
|
|
|
}
|
|
|
|
|
|
|
|
return %_ValueOf(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-09-23 12:32:24 +00:00
|
|
|
// This has the same size as the lastMatchInfo array, and can be used for
|
|
|
|
// functions that expect that structure to be returned. It is used when the
|
|
|
|
// needle is a string rather than a regexp. In this case we can't update
|
|
|
|
// lastMatchArray without erroneously affecting the properties on the global
|
|
|
|
// RegExp object.
|
|
|
|
var reusableMatchInfo = [2, "", "", -1, -1];
|
|
|
|
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ECMA-262, section 15.5.4.11
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringReplace(search, replace) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.replace");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
var subject = TO_STRING_INLINE(this);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-02-27 14:14:45 +00:00
|
|
|
// Decision tree for dispatch
|
|
|
|
// .. regexp search
|
|
|
|
// .... string replace
|
|
|
|
// ...... non-global search
|
|
|
|
// ........ empty string replace
|
|
|
|
// ........ non-empty string replace (with $-expansion)
|
|
|
|
// ...... global search
|
|
|
|
// ........ no need to circumvent last match info override
|
|
|
|
// ........ need to circument last match info override
|
|
|
|
// .... function replace
|
|
|
|
// ...... global search
|
|
|
|
// ...... non-global search
|
|
|
|
// .. string search
|
|
|
|
// .... special case that replaces with one single character
|
|
|
|
// ...... function replace
|
|
|
|
// ...... string replace (with $-expansion)
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
if (IS_REGEXP(search)) {
|
2013-02-27 14:14:45 +00:00
|
|
|
// Emulate RegExp.prototype.exec's side effect in step 5, even if
|
2012-12-05 12:32:25 +00:00
|
|
|
// value is discarded.
|
2013-07-05 12:57:38 +00:00
|
|
|
var lastIndex = search.lastIndex;
|
|
|
|
TO_INTEGER_FOR_SIDE_EFFECT(lastIndex);
|
2009-01-06 13:24:52 +00:00
|
|
|
%_Log('regexp', 'regexp-replace,%0r,%1S', [search, subject]);
|
2013-02-27 14:14:45 +00:00
|
|
|
|
|
|
|
if (!IS_SPEC_FUNCTION(replace)) {
|
2013-02-27 15:12:30 +00:00
|
|
|
replace = TO_STRING_INLINE(replace);
|
|
|
|
|
2013-02-27 14:14:45 +00:00
|
|
|
if (!search.global) {
|
|
|
|
// Non-global regexp search, string replace.
|
|
|
|
var match = DoRegExpExec(search, subject, 0);
|
|
|
|
if (match == null) {
|
|
|
|
search.lastIndex = 0
|
2013-01-25 10:53:26 +00:00
|
|
|
return subject;
|
|
|
|
}
|
2013-02-27 14:14:45 +00:00
|
|
|
if (replace.length == 0) {
|
|
|
|
return %_SubString(subject, 0, match[CAPTURE0]) +
|
|
|
|
%_SubString(subject, match[CAPTURE1], subject.length)
|
|
|
|
}
|
|
|
|
return ExpandReplacement(replace, subject, lastMatchInfo,
|
|
|
|
%_SubString(subject, 0, match[CAPTURE0])) +
|
|
|
|
%_SubString(subject, match[CAPTURE1], subject.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Global regexp search, string replace.
|
|
|
|
search.lastIndex = 0;
|
|
|
|
if (lastMatchInfoOverride == null) {
|
|
|
|
return %StringReplaceGlobalRegExpWithString(
|
|
|
|
subject, search, replace, lastMatchInfo);
|
2012-04-23 18:56:07 +00:00
|
|
|
} else {
|
|
|
|
// We use this hack to detect whether StringReplaceRegExpWithString
|
2013-01-25 10:53:26 +00:00
|
|
|
// found at least one hit. In that case we need to remove any
|
2012-04-23 18:56:07 +00:00
|
|
|
// override.
|
|
|
|
var saved_subject = lastMatchInfo[LAST_SUBJECT_INDEX];
|
|
|
|
lastMatchInfo[LAST_SUBJECT_INDEX] = 0;
|
2013-02-27 14:14:45 +00:00
|
|
|
var answer = %StringReplaceGlobalRegExpWithString(
|
|
|
|
subject, search, replace, lastMatchInfo);
|
2012-04-23 18:56:07 +00:00
|
|
|
if (%_IsSmi(lastMatchInfo[LAST_SUBJECT_INDEX])) {
|
|
|
|
lastMatchInfo[LAST_SUBJECT_INDEX] = saved_subject;
|
|
|
|
} else {
|
|
|
|
lastMatchInfoOverride = null;
|
|
|
|
}
|
|
|
|
return answer;
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2013-02-27 14:14:45 +00:00
|
|
|
|
|
|
|
if (search.global) {
|
|
|
|
// Global regexp search, function replace.
|
|
|
|
return StringReplaceGlobalRegExpWithFunction(subject, search, replace);
|
|
|
|
}
|
|
|
|
// Non-global regexp search, function replace.
|
|
|
|
return StringReplaceNonGlobalRegExpWithFunction(subject, search, replace);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
search = TO_STRING_INLINE(search);
|
2013-02-27 14:14:45 +00:00
|
|
|
|
2012-01-16 15:21:38 +00:00
|
|
|
if (search.length == 1 &&
|
2012-01-17 14:29:17 +00:00
|
|
|
subject.length > 0xFF &&
|
2012-01-16 15:21:38 +00:00
|
|
|
IS_STRING(replace) &&
|
|
|
|
%StringIndexOf(replace, '$', 0) < 0) {
|
2012-01-17 14:29:17 +00:00
|
|
|
// Searching by traversing a cons string tree and replace with cons of
|
|
|
|
// slices works only when the replaced string is a single character, being
|
|
|
|
// replaced by a simple string and only pays off for long strings.
|
2012-01-16 15:21:38 +00:00
|
|
|
return %StringReplaceOneCharWithString(subject, search, replace);
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
var start = %StringIndexOf(subject, search, 0);
|
|
|
|
if (start < 0) return subject;
|
|
|
|
var end = start + search.length;
|
|
|
|
|
2013-02-20 14:29:40 +00:00
|
|
|
var result = %_SubString(subject, 0, start);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// Compute the string to replace with.
|
2011-09-13 11:42:57 +00:00
|
|
|
if (IS_SPEC_FUNCTION(replace)) {
|
2011-08-26 13:53:00 +00:00
|
|
|
var receiver = %GetDefaultReceiver(replace);
|
2012-04-26 11:43:59 +00:00
|
|
|
result += %_CallFunction(receiver, search, start, subject, replace);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2009-03-11 14:00:55 +00:00
|
|
|
reusableMatchInfo[CAPTURE0] = start;
|
|
|
|
reusableMatchInfo[CAPTURE1] = end;
|
2013-02-27 14:14:45 +00:00
|
|
|
result = ExpandReplacement(TO_STRING_INLINE(replace),
|
|
|
|
subject,
|
|
|
|
reusableMatchInfo,
|
|
|
|
result);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 14:29:40 +00:00
|
|
|
return result + %_SubString(subject, end, subject.length);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Expand the $-expressions in the string and return a new string with
|
|
|
|
// the result.
|
2012-04-26 11:43:59 +00:00
|
|
|
function ExpandReplacement(string, subject, matchInfo, result) {
|
2010-12-17 11:57:10 +00:00
|
|
|
var length = string.length;
|
2008-07-03 15:10:15 +00:00
|
|
|
var next = %StringIndexOf(string, '$', 0);
|
|
|
|
if (next < 0) {
|
2012-04-26 11:43:59 +00:00
|
|
|
if (length > 0) result += string;
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 14:29:40 +00:00
|
|
|
if (next > 0) result += %_SubString(string, 0, next);
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
while (true) {
|
|
|
|
var expansion = '$';
|
|
|
|
var position = next + 1;
|
|
|
|
if (position < length) {
|
2010-05-26 14:23:19 +00:00
|
|
|
var peek = %_StringCharCodeAt(string, position);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (peek == 36) { // $$
|
|
|
|
++position;
|
2012-04-26 11:43:59 +00:00
|
|
|
result += '$';
|
2008-07-03 15:10:15 +00:00
|
|
|
} else if (peek == 38) { // $& - match
|
|
|
|
++position;
|
2013-02-20 14:29:40 +00:00
|
|
|
result +=
|
|
|
|
%_SubString(subject, matchInfo[CAPTURE0], matchInfo[CAPTURE1]);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else if (peek == 96) { // $` - prefix
|
|
|
|
++position;
|
2013-02-20 14:29:40 +00:00
|
|
|
result += %_SubString(subject, 0, matchInfo[CAPTURE0]);
|
2008-07-03 15:10:15 +00:00
|
|
|
} else if (peek == 39) { // $' - suffix
|
|
|
|
++position;
|
2013-02-20 14:29:40 +00:00
|
|
|
result += %_SubString(subject, matchInfo[CAPTURE1], subject.length);
|
2013-02-27 14:14:45 +00:00
|
|
|
} else if (peek >= 48 && peek <= 57) {
|
|
|
|
// Valid indices are $1 .. $9, $01 .. $09 and $10 .. $99
|
|
|
|
var scaled_index = (peek - 48) << 1;
|
|
|
|
var advance = 1;
|
|
|
|
var number_of_captures = NUMBER_OF_CAPTURES(matchInfo);
|
|
|
|
if (position + 1 < string.length) {
|
|
|
|
var next = %_StringCharCodeAt(string, position + 1);
|
|
|
|
if (next >= 48 && next <= 57) {
|
|
|
|
var new_scaled_index = scaled_index * 10 + ((next - 48) << 1);
|
|
|
|
if (new_scaled_index < number_of_captures) {
|
|
|
|
scaled_index = new_scaled_index;
|
|
|
|
advance = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (scaled_index != 0 && scaled_index < number_of_captures) {
|
|
|
|
var start = matchInfo[CAPTURE(scaled_index)];
|
|
|
|
if (start >= 0) {
|
|
|
|
result +=
|
|
|
|
%_SubString(subject, start, matchInfo[CAPTURE(scaled_index + 1)]);
|
|
|
|
}
|
|
|
|
position += advance;
|
|
|
|
} else {
|
|
|
|
result += '$';
|
|
|
|
}
|
2009-04-22 11:43:05 +00:00
|
|
|
} else {
|
2012-04-26 11:43:59 +00:00
|
|
|
result += '$';
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-12-08 09:22:24 +00:00
|
|
|
} else {
|
2012-04-26 11:43:59 +00:00
|
|
|
result += '$';
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2008-12-08 09:22:24 +00:00
|
|
|
// Go the the next $ in the string.
|
2008-07-03 15:10:15 +00:00
|
|
|
next = %StringIndexOf(string, '$', position);
|
|
|
|
|
|
|
|
// Return if there are no more $ characters in the string. If we
|
|
|
|
// haven't reached the end, we need to append the suffix.
|
|
|
|
if (next < 0) {
|
|
|
|
if (position < length) {
|
2013-02-20 14:29:40 +00:00
|
|
|
result += %_SubString(string, position, length);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2012-04-26 11:43:59 +00:00
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Append substring between the previous and the next $ character.
|
2010-12-17 11:57:10 +00:00
|
|
|
if (next > position) {
|
2013-02-20 14:29:40 +00:00
|
|
|
result += %_SubString(string, position, next);
|
2010-12-17 11:57:10 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2012-04-26 11:43:59 +00:00
|
|
|
return result;
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2009-03-11 14:00:55 +00:00
|
|
|
// Compute the string of a given regular expression capture.
|
|
|
|
function CaptureString(string, lastCaptureInfo, index) {
|
2008-07-03 15:10:15 +00:00
|
|
|
// Scale the index.
|
|
|
|
var scaled = index << 1;
|
|
|
|
// Compute start and end.
|
2009-03-11 14:00:55 +00:00
|
|
|
var start = lastCaptureInfo[CAPTURE(scaled)];
|
2010-05-13 12:13:27 +00:00
|
|
|
// If start isn't valid, return undefined.
|
|
|
|
if (start < 0) return;
|
2009-03-11 14:00:55 +00:00
|
|
|
var end = lastCaptureInfo[CAPTURE(scaled + 1)];
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(string, start, end);
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2010-03-25 12:57:58 +00:00
|
|
|
// TODO(lrn): This array will survive indefinitely if replace is never
|
2010-03-30 07:15:23 +00:00
|
|
|
// called again. However, it will be empty, since the contents are cleared
|
2010-03-25 12:57:58 +00:00
|
|
|
// in the finally block.
|
2011-03-03 11:49:03 +00:00
|
|
|
var reusableReplaceArray = new InternalArray(16);
|
2008-12-08 09:22:24 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// Helper function for replacing regular expressions with the result of a
|
2010-03-25 12:57:58 +00:00
|
|
|
// function application in String.prototype.replace.
|
2010-05-13 12:13:27 +00:00
|
|
|
function StringReplaceGlobalRegExpWithFunction(subject, regexp, replace) {
|
|
|
|
var resultArray = reusableReplaceArray;
|
|
|
|
if (resultArray) {
|
|
|
|
reusableReplaceArray = null;
|
|
|
|
} else {
|
|
|
|
// Inside a nested replace (replace called from the replacement function
|
|
|
|
// of another replace) or we have failed to set the reusable array
|
|
|
|
// back due to an exception in a replacement function. Create a new
|
|
|
|
// array to use in the future, or until the original is written back.
|
2011-03-03 11:49:03 +00:00
|
|
|
resultArray = new InternalArray(16);
|
2010-05-13 12:13:27 +00:00
|
|
|
}
|
|
|
|
var res = %RegExpExecMultiple(regexp,
|
|
|
|
subject,
|
|
|
|
lastMatchInfo,
|
|
|
|
resultArray);
|
|
|
|
regexp.lastIndex = 0;
|
|
|
|
if (IS_NULL(res)) {
|
|
|
|
// No matches at all.
|
|
|
|
reusableReplaceArray = resultArray;
|
|
|
|
return subject;
|
|
|
|
}
|
|
|
|
var len = res.length;
|
|
|
|
if (NUMBER_OF_CAPTURES(lastMatchInfo) == 2) {
|
2012-04-23 18:56:07 +00:00
|
|
|
// If the number of captures is two then there are no explicit captures in
|
|
|
|
// the regexp, just the implicit capture that captures the whole match. In
|
|
|
|
// this case we can simplify quite a bit and end up with something faster.
|
|
|
|
// The builder will consist of some integers that indicate slices of the
|
|
|
|
// input string and some replacements that were returned from the replace
|
|
|
|
// function.
|
2010-05-13 12:13:27 +00:00
|
|
|
var match_start = 0;
|
2013-02-15 15:20:05 +00:00
|
|
|
var override = new InternalPackedArray(null, 0, subject);
|
2011-08-26 13:53:00 +00:00
|
|
|
var receiver = %GetDefaultReceiver(replace);
|
2012-04-23 18:56:07 +00:00
|
|
|
for (var i = 0; i < len; i++) {
|
2012-04-20 15:20:52 +00:00
|
|
|
var elem = res[i];
|
|
|
|
if (%_IsSmi(elem)) {
|
2012-04-23 18:56:07 +00:00
|
|
|
// Integers represent slices of the original string. Use these to
|
|
|
|
// get the offsets we need for the override array (so things like
|
|
|
|
// RegExp.leftContext work during the callback function.
|
2012-04-20 15:20:52 +00:00
|
|
|
if (elem > 0) {
|
|
|
|
match_start = (elem >> 11) + (elem & 0x7ff);
|
2012-04-20 11:06:12 +00:00
|
|
|
} else {
|
2012-04-20 15:20:52 +00:00
|
|
|
match_start = res[++i] - elem;
|
2012-04-20 11:06:12 +00:00
|
|
|
}
|
2012-04-20 15:20:52 +00:00
|
|
|
} else {
|
|
|
|
override[0] = elem;
|
|
|
|
override[1] = match_start;
|
|
|
|
lastMatchInfoOverride = override;
|
|
|
|
var func_result =
|
|
|
|
%_CallFunction(receiver, elem, match_start, subject, replace);
|
2012-04-23 18:56:07 +00:00
|
|
|
// Overwrite the i'th element in the results with the string we got
|
|
|
|
// back from the callback function.
|
2012-04-20 15:20:52 +00:00
|
|
|
res[i] = TO_STRING_INLINE(func_result);
|
|
|
|
match_start += elem.length;
|
2012-04-20 11:06:12 +00:00
|
|
|
}
|
2010-05-13 12:13:27 +00:00
|
|
|
}
|
|
|
|
} else {
|
2011-09-14 12:33:57 +00:00
|
|
|
var receiver = %GetDefaultReceiver(replace);
|
2012-04-23 18:56:07 +00:00
|
|
|
for (var i = 0; i < len; i++) {
|
2010-05-13 12:13:27 +00:00
|
|
|
var elem = res[i];
|
|
|
|
if (!%_IsSmi(elem)) {
|
|
|
|
// elem must be an Array.
|
|
|
|
// Use the apply argument as backing for global RegExp properties.
|
|
|
|
lastMatchInfoOverride = elem;
|
2011-09-14 12:33:57 +00:00
|
|
|
var func_result = %Apply(replace, receiver, elem, 0, elem.length);
|
2012-04-23 18:56:07 +00:00
|
|
|
// Overwrite the i'th element in the results with the string we got
|
|
|
|
// back from the callback function.
|
2011-02-11 13:30:37 +00:00
|
|
|
res[i] = TO_STRING_INLINE(func_result);
|
2010-03-25 12:57:58 +00:00
|
|
|
}
|
2010-03-10 12:00:22 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2013-06-19 08:59:56 +00:00
|
|
|
var result = %StringBuilderConcat(res, res.length, subject);
|
2010-05-13 12:13:27 +00:00
|
|
|
resultArray.length = 0;
|
|
|
|
reusableReplaceArray = resultArray;
|
|
|
|
return result;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-13 12:13:27 +00:00
|
|
|
function StringReplaceNonGlobalRegExpWithFunction(subject, regexp, replace) {
|
|
|
|
var matchInfo = DoRegExpExec(regexp, subject, 0);
|
2012-12-05 12:32:25 +00:00
|
|
|
if (IS_NULL(matchInfo)) {
|
|
|
|
regexp.lastIndex = 0;
|
|
|
|
return subject;
|
|
|
|
}
|
2010-05-13 12:13:27 +00:00
|
|
|
var index = matchInfo[CAPTURE0];
|
2013-02-20 14:29:40 +00:00
|
|
|
var result = %_SubString(subject, 0, index);
|
2010-05-13 12:13:27 +00:00
|
|
|
var endOfMatch = matchInfo[CAPTURE1];
|
2008-07-03 15:10:15 +00:00
|
|
|
// Compute the parameter list consisting of the match, captures, index,
|
|
|
|
// and subject for the replace function invocation.
|
|
|
|
// The number of captures plus one for the match.
|
2009-09-23 12:32:24 +00:00
|
|
|
var m = NUMBER_OF_CAPTURES(matchInfo) >> 1;
|
2010-05-13 12:13:27 +00:00
|
|
|
var replacement;
|
2011-09-14 12:33:57 +00:00
|
|
|
var receiver = %GetDefaultReceiver(replace);
|
2008-07-03 15:10:15 +00:00
|
|
|
if (m == 1) {
|
2010-05-13 12:13:27 +00:00
|
|
|
// No captures, only the match, which is always valid.
|
2013-02-20 14:29:40 +00:00
|
|
|
var s = %_SubString(subject, index, endOfMatch);
|
2009-02-16 10:18:34 +00:00
|
|
|
// Don't call directly to avoid exposing the built-in global object.
|
2012-04-20 11:06:12 +00:00
|
|
|
replacement = %_CallFunction(receiver, s, index, subject, replace);
|
2010-05-13 12:13:27 +00:00
|
|
|
} else {
|
2011-03-03 11:49:03 +00:00
|
|
|
var parameters = new InternalArray(m + 2);
|
2010-05-13 12:13:27 +00:00
|
|
|
for (var j = 0; j < m; j++) {
|
|
|
|
parameters[j] = CaptureString(subject, matchInfo, j);
|
|
|
|
}
|
|
|
|
parameters[j] = index;
|
|
|
|
parameters[j + 1] = subject;
|
|
|
|
|
2011-09-14 12:33:57 +00:00
|
|
|
replacement = %Apply(replace, receiver, parameters, 0, j + 2);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2010-05-13 12:13:27 +00:00
|
|
|
|
2012-04-26 11:43:59 +00:00
|
|
|
result += replacement; // The add method converts to string if necessary.
|
2010-05-13 12:13:27 +00:00
|
|
|
// Can't use matchInfo any more from here, since the function could
|
|
|
|
// overwrite it.
|
2013-02-20 14:29:40 +00:00
|
|
|
return result + %_SubString(subject, endOfMatch, subject.length);
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2010-05-13 12:13:27 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ECMA-262 section 15.5.4.12
|
2010-03-30 07:15:23 +00:00
|
|
|
function StringSearch(re) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.search");
|
|
|
|
|
2010-04-14 14:46:15 +00:00
|
|
|
var regexp;
|
|
|
|
if (IS_STRING(re)) {
|
|
|
|
regexp = %_GetFromCache(STRING_TO_REGEXP_CACHE_ID, re);
|
|
|
|
} else if (IS_REGEXP(re)) {
|
|
|
|
regexp = re;
|
|
|
|
} else {
|
|
|
|
regexp = new $RegExp(re);
|
|
|
|
}
|
2010-12-13 12:19:10 +00:00
|
|
|
var match = DoRegExpExec(regexp, TO_STRING_INLINE(this), 0);
|
2010-03-25 09:09:42 +00:00
|
|
|
if (match) {
|
|
|
|
return match[CAPTURE0];
|
|
|
|
}
|
|
|
|
return -1;
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.13
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringSlice(start, end) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.slice");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
var s = TO_STRING_INLINE(this);
|
2008-07-03 15:10:15 +00:00
|
|
|
var s_len = s.length;
|
|
|
|
var start_i = TO_INTEGER(start);
|
|
|
|
var end_i = s_len;
|
2013-10-17 10:02:45 +00:00
|
|
|
if (!IS_UNDEFINED(end)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
end_i = TO_INTEGER(end);
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
if (start_i < 0) {
|
|
|
|
start_i += s_len;
|
2011-11-28 12:11:00 +00:00
|
|
|
if (start_i < 0) {
|
2008-07-03 15:10:15 +00:00
|
|
|
start_i = 0;
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2011-11-28 12:11:00 +00:00
|
|
|
if (start_i > s_len) {
|
2012-03-13 11:38:37 +00:00
|
|
|
return '';
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (end_i < 0) {
|
|
|
|
end_i += s_len;
|
2011-11-28 12:11:00 +00:00
|
|
|
if (end_i < 0) {
|
2012-03-13 11:38:37 +00:00
|
|
|
return '';
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
} else {
|
2011-11-28 12:11:00 +00:00
|
|
|
if (end_i > s_len) {
|
2008-07-03 15:10:15 +00:00
|
|
|
end_i = s_len;
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2012-03-13 11:38:37 +00:00
|
|
|
if (end_i <= start_i) {
|
|
|
|
return '';
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(s, start_i, end_i);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.14
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringSplit(separator, limit) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.split");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
var subject = TO_STRING_INLINE(this);
|
2010-01-06 14:40:21 +00:00
|
|
|
limit = (IS_UNDEFINED(limit)) ? 0xffffffff : TO_UINT32(limit);
|
2008-10-03 07:14:31 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
var length = subject.length;
|
2010-02-19 13:07:37 +00:00
|
|
|
if (!IS_REGEXP(separator)) {
|
2013-12-23 10:01:22 +00:00
|
|
|
var separator_string = TO_STRING_INLINE(separator);
|
2011-09-22 08:18:58 +00:00
|
|
|
|
|
|
|
if (limit === 0) return [];
|
|
|
|
|
2013-12-23 10:01:22 +00:00
|
|
|
// ECMA-262 says that if separator is undefined, the result should
|
|
|
|
// be an array of size 1 containing the entire string.
|
|
|
|
if (IS_UNDEFINED(separator)) return [subject];
|
|
|
|
|
|
|
|
var separator_length = separator_string.length;
|
2010-02-19 13:07:37 +00:00
|
|
|
|
2009-06-16 13:31:31 +00:00
|
|
|
// If the separator string is empty then return the elements in the subject.
|
2010-11-10 12:34:28 +00:00
|
|
|
if (separator_length === 0) return %StringToArray(subject, limit);
|
2010-02-19 13:07:37 +00:00
|
|
|
|
2013-12-23 10:01:22 +00:00
|
|
|
var result = %StringSplit(subject, separator_string, limit);
|
2010-02-19 13:07:37 +00:00
|
|
|
|
|
|
|
return result;
|
2009-01-06 13:24:52 +00:00
|
|
|
}
|
2008-10-03 07:14:31 +00:00
|
|
|
|
2011-09-22 08:18:58 +00:00
|
|
|
if (limit === 0) return [];
|
|
|
|
|
2012-03-13 15:44:19 +00:00
|
|
|
// Separator is a regular expression.
|
|
|
|
return StringSplitOnRegExp(subject, separator, limit, length);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-06-20 08:13:21 +00:00
|
|
|
var ArrayPushBuiltin = $Array.prototype.push;
|
|
|
|
|
2012-03-13 15:44:19 +00:00
|
|
|
function StringSplitOnRegExp(subject, separator, limit, length) {
|
2010-02-19 13:07:37 +00:00
|
|
|
%_Log('regexp', 'regexp-split,%0S,%1r', [subject, separator]);
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
if (length === 0) {
|
2010-11-04 10:24:17 +00:00
|
|
|
if (DoRegExpExec(separator, subject, 0, 0) != null) {
|
2010-03-17 10:23:06 +00:00
|
|
|
return [];
|
|
|
|
}
|
2009-06-16 13:31:31 +00:00
|
|
|
return [subject];
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-03 07:14:31 +00:00
|
|
|
|
2009-06-16 13:31:31 +00:00
|
|
|
var currentIndex = 0;
|
|
|
|
var startIndex = 0;
|
2010-12-17 11:57:10 +00:00
|
|
|
var startMatch = 0;
|
2014-04-10 13:17:48 +00:00
|
|
|
var result = new InternalArray();
|
2009-06-16 13:31:31 +00:00
|
|
|
|
2010-04-08 14:42:27 +00:00
|
|
|
outer_loop:
|
2008-07-03 15:10:15 +00:00
|
|
|
while (true) {
|
|
|
|
|
|
|
|
if (startIndex === length) {
|
2013-06-20 08:13:21 +00:00
|
|
|
%_CallFunction(result, %_SubString(subject, currentIndex, length),
|
|
|
|
ArrayPushBuiltin);
|
2010-04-08 14:42:27 +00:00
|
|
|
break;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-03 07:14:31 +00:00
|
|
|
|
2010-12-17 11:57:10 +00:00
|
|
|
var matchInfo = DoRegExpExec(separator, subject, startIndex);
|
|
|
|
if (matchInfo == null || length === (startMatch = matchInfo[CAPTURE0])) {
|
2013-06-20 08:13:21 +00:00
|
|
|
%_CallFunction(result, %_SubString(subject, currentIndex, length),
|
|
|
|
ArrayPushBuiltin);
|
2010-04-08 14:42:27 +00:00
|
|
|
break;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2009-09-23 12:32:24 +00:00
|
|
|
var endIndex = matchInfo[CAPTURE1];
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
// We ignore a zero-length match at the currentIndex.
|
|
|
|
if (startIndex === endIndex && endIndex === currentIndex) {
|
|
|
|
startIndex++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2013-06-20 08:13:21 +00:00
|
|
|
%_CallFunction(result, %_SubString(subject, currentIndex, startMatch),
|
|
|
|
ArrayPushBuiltin);
|
2010-12-17 11:57:10 +00:00
|
|
|
|
2010-04-08 14:42:27 +00:00
|
|
|
if (result.length === limit) break;
|
2008-10-03 07:14:31 +00:00
|
|
|
|
2010-12-13 12:19:10 +00:00
|
|
|
var matchinfo_len = NUMBER_OF_CAPTURES(matchInfo) + REGEXP_FIRST_CAPTURE;
|
|
|
|
for (var i = REGEXP_FIRST_CAPTURE + 2; i < matchinfo_len; ) {
|
|
|
|
var start = matchInfo[i++];
|
|
|
|
var end = matchInfo[i++];
|
|
|
|
if (end != -1) {
|
2013-06-20 08:13:21 +00:00
|
|
|
%_CallFunction(result, %_SubString(subject, start, end),
|
|
|
|
ArrayPushBuiltin);
|
2009-03-11 14:00:55 +00:00
|
|
|
} else {
|
2013-10-17 10:02:45 +00:00
|
|
|
%_CallFunction(result, UNDEFINED, ArrayPushBuiltin);
|
2009-03-11 14:00:55 +00:00
|
|
|
}
|
2010-04-08 14:42:27 +00:00
|
|
|
if (result.length === limit) break outer_loop;
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
2008-10-03 07:14:31 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
startIndex = currentIndex = endIndex;
|
|
|
|
}
|
2014-04-10 13:17:48 +00:00
|
|
|
var array_result = [];
|
|
|
|
%MoveArrayContents(result, array_result);
|
|
|
|
return array_result;
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262 section 15.5.4.15
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringSubstring(start, end) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.subString");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
var s = TO_STRING_INLINE(this);
|
2008-07-03 15:10:15 +00:00
|
|
|
var s_len = s.length;
|
2010-01-06 14:40:21 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
var start_i = TO_INTEGER(start);
|
2010-01-06 14:40:21 +00:00
|
|
|
if (start_i < 0) {
|
|
|
|
start_i = 0;
|
|
|
|
} else if (start_i > s_len) {
|
|
|
|
start_i = s_len;
|
|
|
|
}
|
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
var end_i = s_len;
|
2010-01-06 14:40:21 +00:00
|
|
|
if (!IS_UNDEFINED(end)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
end_i = TO_INTEGER(end);
|
2010-01-06 14:40:21 +00:00
|
|
|
if (end_i > s_len) {
|
|
|
|
end_i = s_len;
|
|
|
|
} else {
|
|
|
|
if (end_i < 0) end_i = 0;
|
|
|
|
if (start_i > end_i) {
|
|
|
|
var tmp = end_i;
|
|
|
|
end_i = start_i;
|
|
|
|
start_i = tmp;
|
|
|
|
}
|
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
}
|
|
|
|
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(s, start_i, end_i);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// This is not a part of ECMA-262.
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringSubstr(start, n) {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.substr");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
var s = TO_STRING_INLINE(this);
|
2008-07-03 15:10:15 +00:00
|
|
|
var len;
|
|
|
|
|
|
|
|
// Correct n: If not given, set to string length; if explicitly
|
|
|
|
// set to undefined, zero, or negative, returns empty string.
|
2013-10-17 10:02:45 +00:00
|
|
|
if (IS_UNDEFINED(n)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
len = s.length;
|
|
|
|
} else {
|
|
|
|
len = TO_INTEGER(n);
|
|
|
|
if (len <= 0) return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Correct start: If not given (or undefined), set to zero; otherwise
|
|
|
|
// convert to integer and handle negative case.
|
2013-10-17 10:02:45 +00:00
|
|
|
if (IS_UNDEFINED(start)) {
|
2008-07-03 15:10:15 +00:00
|
|
|
start = 0;
|
|
|
|
} else {
|
|
|
|
start = TO_INTEGER(start);
|
|
|
|
// If positive, and greater than or equal to the string length,
|
|
|
|
// return empty string.
|
|
|
|
if (start >= s.length) return '';
|
|
|
|
// If negative and absolute value is larger than the string length,
|
|
|
|
// use zero.
|
|
|
|
if (start < 0) {
|
|
|
|
start += s.length;
|
|
|
|
if (start < 0) start = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var end = start + len;
|
|
|
|
if (end > s.length) end = s.length;
|
|
|
|
|
2013-02-20 14:29:40 +00:00
|
|
|
return %_SubString(s, start, end);
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262, 15.5.4.16
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringToLowerCase() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLowerCase");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
return %StringToLowerCase(TO_STRING_INLINE(this));
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262, 15.5.4.17
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringToLocaleLowerCase() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLocaleLowerCase");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
return %StringToLowerCase(TO_STRING_INLINE(this));
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262, 15.5.4.18
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringToUpperCase() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.toUpperCase");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
return %StringToUpperCase(TO_STRING_INLINE(this));
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// ECMA-262, 15.5.4.19
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringToLocaleUpperCase() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.toLocaleUpperCase");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
return %StringToUpperCase(TO_STRING_INLINE(this));
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2009-10-13 08:13:45 +00:00
|
|
|
// ES5, 15.5.4.20
|
|
|
|
function StringTrim() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.trim");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
return %StringTrim(TO_STRING_INLINE(this), true, true);
|
2009-10-13 08:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function StringTrimLeft() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.trimLeft");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
return %StringTrim(TO_STRING_INLINE(this), true, false);
|
2009-10-13 08:13:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function StringTrimRight() {
|
2014-01-22 12:15:57 +00:00
|
|
|
CHECK_OBJECT_COERCIBLE(this, "String.prototype.trimRight");
|
|
|
|
|
2010-02-19 13:07:37 +00:00
|
|
|
return %StringTrim(TO_STRING_INLINE(this), false, true);
|
2009-10-13 08:13:45 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-12-05 15:49:22 +00:00
|
|
|
|
2008-07-03 15:10:15 +00:00
|
|
|
// ECMA-262, section 15.5.3.2
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringFromCharCode(code) {
|
2008-07-03 15:10:15 +00:00
|
|
|
var n = %_ArgumentsLength();
|
2010-03-10 11:13:02 +00:00
|
|
|
if (n == 1) {
|
|
|
|
if (!%_IsSmi(code)) code = ToNumber(code);
|
2010-05-26 14:23:19 +00:00
|
|
|
return %_StringCharFromCode(code & 0xffff);
|
2010-03-10 11:13:02 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2012-12-05 15:49:22 +00:00
|
|
|
var one_byte = %NewString(n, NEW_ONE_BYTE_STRING);
|
|
|
|
var i;
|
|
|
|
for (i = 0; i < n; i++) {
|
2010-03-10 11:13:02 +00:00
|
|
|
var code = %_Arguments(i);
|
2012-12-05 15:49:22 +00:00
|
|
|
if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff;
|
2012-12-18 12:37:57 +00:00
|
|
|
if (code < 0) code = code & 0xffff;
|
2013-02-18 10:27:46 +00:00
|
|
|
if (code > 0xff) break;
|
2012-12-05 15:49:22 +00:00
|
|
|
%_OneByteSeqStringSetChar(one_byte, i, code);
|
|
|
|
}
|
|
|
|
if (i == n) return one_byte;
|
|
|
|
one_byte = %TruncateString(one_byte, i);
|
|
|
|
|
|
|
|
var two_byte = %NewString(n - i, NEW_TWO_BYTE_STRING);
|
|
|
|
for (var j = 0; i < n; i++, j++) {
|
|
|
|
var code = %_Arguments(i);
|
|
|
|
if (!%_IsSmi(code)) code = ToNumber(code) & 0xffff;
|
|
|
|
%_TwoByteSeqStringSetChar(two_byte, j, code);
|
2010-03-10 11:13:02 +00:00
|
|
|
}
|
2012-12-05 15:49:22 +00:00
|
|
|
return one_byte + two_byte;
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Helper function for very basic XSS protection.
|
|
|
|
function HtmlEscape(str) {
|
2010-02-19 13:07:37 +00:00
|
|
|
return TO_STRING_INLINE(str).replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/'/g, "'");
|
2011-11-28 12:11:00 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Compatibility support for KJS.
|
|
|
|
// Tested by mozilla/js/tests/js1_5/Regress/regress-276103.js.
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringLink(s) {
|
2008-10-03 07:14:31 +00:00
|
|
|
return "<a href=\"" + HtmlEscape(s) + "\">" + this + "</a>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringAnchor(name) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<a name=\"" + HtmlEscape(name) + "\">" + this + "</a>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringFontcolor(color) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<font color=\"" + HtmlEscape(color) + "\">" + this + "</font>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringFontsize(size) {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<font size=\"" + HtmlEscape(size) + "\">" + this + "</font>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringBig() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<big>" + this + "</big>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringBlink() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<blink>" + this + "</blink>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringBold() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<b>" + this + "</b>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringFixed() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<tt>" + this + "</tt>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringItalics() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<i>" + this + "</i>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringSmall() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<small>" + this + "</small>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringStrike() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<strike>" + this + "</strike>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringSub() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<sub>" + this + "</sub>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
function StringSup() {
|
2008-07-03 15:10:15 +00:00
|
|
|
return "<sup>" + this + "</sup>";
|
2008-10-03 07:58:18 +00:00
|
|
|
}
|
2008-07-03 15:10:15 +00:00
|
|
|
|
2008-10-03 07:58:18 +00:00
|
|
|
// -------------------------------------------------------------------
|
|
|
|
|
2011-09-05 07:30:35 +00:00
|
|
|
function SetUpString() {
|
|
|
|
%CheckIsBootstrapping();
|
2013-04-11 12:15:25 +00:00
|
|
|
|
|
|
|
// Set the String function and constructor.
|
|
|
|
%SetCode($String, StringConstructor);
|
|
|
|
%FunctionSetPrototype($String, new $String());
|
|
|
|
|
2011-09-05 07:30:35 +00:00
|
|
|
// Set up the constructor property on the String prototype object.
|
2008-10-03 12:14:29 +00:00
|
|
|
%SetProperty($String.prototype, "constructor", $String, DONT_ENUM);
|
2008-10-03 07:58:18 +00:00
|
|
|
|
2011-09-05 07:30:35 +00:00
|
|
|
// Set up the non-enumerable functions on the String object.
|
2008-10-03 07:58:18 +00:00
|
|
|
InstallFunctions($String, DONT_ENUM, $Array(
|
|
|
|
"fromCharCode", StringFromCharCode
|
|
|
|
));
|
|
|
|
|
2011-09-05 07:30:35 +00:00
|
|
|
// Set up the non-enumerable functions on the String prototype object.
|
2011-11-15 09:44:57 +00:00
|
|
|
InstallFunctions($String.prototype, DONT_ENUM, $Array(
|
2008-10-03 07:58:18 +00:00
|
|
|
"valueOf", StringValueOf,
|
|
|
|
"toString", StringToString,
|
|
|
|
"charAt", StringCharAt,
|
|
|
|
"charCodeAt", StringCharCodeAt,
|
|
|
|
"concat", StringConcat,
|
|
|
|
"indexOf", StringIndexOf,
|
|
|
|
"lastIndexOf", StringLastIndexOf,
|
|
|
|
"localeCompare", StringLocaleCompare,
|
|
|
|
"match", StringMatch,
|
2014-01-31 08:09:17 +00:00
|
|
|
"normalize", StringNormalize,
|
2008-10-03 07:58:18 +00:00
|
|
|
"replace", StringReplace,
|
|
|
|
"search", StringSearch,
|
|
|
|
"slice", StringSlice,
|
|
|
|
"split", StringSplit,
|
|
|
|
"substring", StringSubstring,
|
|
|
|
"substr", StringSubstr,
|
|
|
|
"toLowerCase", StringToLowerCase,
|
|
|
|
"toLocaleLowerCase", StringToLocaleLowerCase,
|
|
|
|
"toUpperCase", StringToUpperCase,
|
|
|
|
"toLocaleUpperCase", StringToLocaleUpperCase,
|
2009-10-13 08:13:45 +00:00
|
|
|
"trim", StringTrim,
|
|
|
|
"trimLeft", StringTrimLeft,
|
|
|
|
"trimRight", StringTrimRight,
|
2008-10-03 07:58:18 +00:00
|
|
|
"link", StringLink,
|
|
|
|
"anchor", StringAnchor,
|
|
|
|
"fontcolor", StringFontcolor,
|
|
|
|
"fontsize", StringFontsize,
|
|
|
|
"big", StringBig,
|
|
|
|
"blink", StringBlink,
|
|
|
|
"bold", StringBold,
|
|
|
|
"fixed", StringFixed,
|
|
|
|
"italics", StringItalics,
|
|
|
|
"small", StringSmall,
|
|
|
|
"strike", StringStrike,
|
|
|
|
"sub", StringSub,
|
2010-12-15 09:31:05 +00:00
|
|
|
"sup", StringSup
|
2008-10-03 07:58:18 +00:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
2011-09-05 07:30:35 +00:00
|
|
|
SetUpString();
|