StringPrototype.js [plain text]
function match(regexp)
{
"use strict";
if (this == null) {
if (this === null)
throw new @TypeError("String.prototype.match requires that |this| not be null");
throw new @TypeError("String.prototype.match requires that |this| not be undefined");
}
if (regexp != null) {
var matcher = regexp.@matchSymbol;
if (matcher != @undefined)
return matcher.@call(regexp, this);
}
let thisString = @toString(this);
let createdRegExp = @regExpCreate(regexp, @undefined);
return createdRegExp.@matchSymbol(thisString);
}
@globalPrivate
function repeatSlowPath(string, count)
{
"use strict";
if (count === 0 || string.length === 0)
return "";
if (count === 1)
return string;
if (string.length * count > @MAX_STRING_LENGTH)
throw new @Error("Out of memory");
var result = "";
var operand = string;
while (true) {
if (count & 1)
result += operand;
count >>= 1;
if (!count)
return result;
operand += operand;
}
}
@globalPrivate
function repeatCharactersSlowPath(string, count)
{
"use strict";
var repeatCount = (count / string.length) | 0;
var remainingCharacters = count - repeatCount * string.length;
var result = "";
var operand = string;
while (true) {
if (repeatCount & 1)
result += operand;
repeatCount >>= 1;
if (!repeatCount)
break;
operand += operand;
}
if (remainingCharacters)
result += @stringSubstrInternal.@call(string, 0, remainingCharacters);
return result;
}
function repeat(count)
{
"use strict";
if (this == null) {
var message = "String.prototype.repeat requires that |this| not be undefined";
if (this === null)
message = "String.prototype.repeat requires that |this| not be null";
throw new @TypeError(message);
}
var string = @toString(this);
count = @toInteger(count);
if (count < 0 || count === @Infinity)
throw new @RangeError("String.prototype.repeat argument must be greater than or equal to 0 and not be Infinity");
if (string.length === 1) {
var result = @repeatCharacter(string, count);
if (result !== null)
return result;
}
return @repeatSlowPath(string, count);
}
function padStart(maxLength)
{
"use strict";
if (this === null)
throw new @TypeError("String.prototype.padStart requires that |this| not be null");
if (this === @undefined)
throw new @TypeError("String.prototype.padStart requires that |this| not be undefined");
var string = @toString(this);
maxLength = @toLength(maxLength);
var stringLength = string.length;
if (maxLength <= stringLength)
return string;
var filler;
var fillString = arguments[1];
if (fillString === @undefined)
filler = " ";
else {
filler = @toString(fillString);
if (filler === "")
return string;
}
if (maxLength > @MAX_STRING_LENGTH)
throw new @Error("Out of memory");
var fillLength = maxLength - stringLength;
var truncatedStringFiller;
if (filler.length === 1)
truncatedStringFiller = @repeatCharacter(filler, fillLength);
else
truncatedStringFiller = @repeatCharactersSlowPath(filler, fillLength);
return truncatedStringFiller + string;
}
function padEnd(maxLength)
{
"use strict";
if (this === null)
throw new @TypeError("String.prototype.padEnd requires that |this| not be null");
if (this === @undefined)
throw new @TypeError("String.prototype.padEnd requires that |this| not be undefined");
var string = @toString(this);
maxLength = @toLength(maxLength);
var stringLength = string.length;
if (maxLength <= stringLength)
return string;
var filler;
var fillString = arguments[1];
if (fillString === @undefined)
filler = " ";
else {
filler = @toString(fillString);
if (filler === "")
return string;
}
if (maxLength > @MAX_STRING_LENGTH)
throw new @Error("Out of memory");
var fillLength = maxLength - stringLength;
var truncatedStringFiller;
if (filler.length === 1)
truncatedStringFiller = @repeatCharacter(filler, fillLength);
else
truncatedStringFiller = @repeatCharactersSlowPath(filler, fillLength);
return string + truncatedStringFiller;
}
@globalPrivate
function hasObservableSideEffectsForStringReplace(regexp, replacer) {
if (replacer !== @regExpPrototypeSymbolReplace)
return true;
let regexpExec = @tryGetById(regexp, "exec");
if (regexpExec !== @regExpBuiltinExec)
return true;
let regexpGlobal = @tryGetById(regexp, "global");
if (regexpGlobal !== @regExpProtoGlobalGetter)
return true;
let regexpUnicode = @tryGetById(regexp, "unicode");
if (regexpUnicode !== @regExpProtoUnicodeGetter)
return true;
return !@isRegExpObject(regexp);
}
@intrinsic=StringPrototypeReplaceIntrinsic
function replace(search, replace)
{
"use strict";
if (this == null) {
if (this === null)
throw new @TypeError("String.prototype.replace requires that |this| not be null");
throw new @TypeError("String.prototype.replace requires that |this| not be undefined");
}
if (search != null) {
let replacer = search.@replaceSymbol;
if (replacer !== @undefined) {
if (!@hasObservableSideEffectsForStringReplace(search, replacer))
return @toString(this).@replaceUsingRegExp(search, replace);
return replacer.@call(search, this, replace);
}
}
let thisString = @toString(this);
let searchString = @toString(search);
return thisString.@replaceUsingStringSearch(searchString, replace);
}
function localeCompare(that)
{
"use strict";
if (this === null)
throw new @TypeError("String.prototype.localeCompare requires that |this| not be null");
if (this === @undefined)
throw new @TypeError("String.prototype.localeCompare requires that |this| not be undefined");
var thisString = @toString(this);
var thatString = @toString(that);
if (arguments[1] === @undefined && arguments[2] === @undefined)
return @Collator.prototype.compare(thisString, thatString);
var collator = new @Collator(arguments[1], arguments[2]);
return collator.compare(thisString, thatString);
}
function search(regexp)
{
"use strict";
if (this == null) {
if (this === null)
throw new @TypeError("String.prototype.search requires that |this| not be null");
throw new @TypeError("String.prototype.search requires that |this| not be undefined");
}
if (regexp != null) {
var searcher = regexp.@searchSymbol;
if (searcher != @undefined)
return searcher.@call(regexp, this);
}
var thisString = @toString(this);
var createdRegExp = @regExpCreate(regexp, @undefined);
return createdRegExp.@searchSymbol(thisString);
}
function split(separator, limit)
{
"use strict";
if (this == null) {
if (this === null)
throw new @TypeError("String.prototype.split requires that |this| not be null");
throw new @TypeError("String.prototype.split requires that |this| not be undefined");
}
if (separator != null) {
var splitter = separator.@splitSymbol;
if (splitter != @undefined)
return splitter.@call(separator, this, limit);
}
return @stringSplitFast.@call(this, separator, limit);
}