#509: M1260509 implement String.padStart/padEnd (needed for testing)

This commit is contained in:
Cameron Kaiser 2018-07-08 09:21:58 -07:00
parent c7075f4527
commit 8b2316f2db
2 changed files with 51 additions and 0 deletions

View File

@ -4,6 +4,55 @@
/*global intl_Collator: false, */
/**
* A helper function implementing the logic for both String.prototype.padStart
* and String.prototype.padEnd as described in ES7 Draft March 29, 2016
*/
function String_pad(maxLength, fillString, padEnd=false) {
// Steps 1-2.
RequireObjectCoercible(this);
let str = ToString(this);
// Steps 3-4.
let intMaxLength = ToLength(maxLength);
let strLen = str.length;
// Step 5.
if (intMaxLength <= strLen)
return str;
// Steps 6-7.
let filler = fillString === undefined ? " " : ToString(fillString);
// Step 8.
if (filler === "")
return str;
// Step 9.
let fillLen = intMaxLength - strLen;
// Step 10.
let truncatedStringFiller = callFunction(String_repeat, filler,
fillLen / filler.length);
truncatedStringFiller += callFunction(String_substr, filler, 0,
fillLen % filler.length);
// Step 11.
if (padEnd === true)
return str + truncatedStringFiller;
return truncatedStringFiller + str;
}
function String_pad_start(maxLength, fillString=" ") {
return callFunction(String_pad, this, maxLength, fillString, false);
}
function String_pad_end(maxLength, fillString=" ") {
return callFunction(String_pad, this, maxLength, fillString, true);
}
/* ES6 Draft Oct 14, 2014 21.1.3.19 */
function String_substring(start, end) {
// Steps 1-3.

View File

@ -4204,6 +4204,8 @@ static const JSFunctionSpec string_methods[] = {
JS_INLINABLE_FN("charAt", str_charAt, 1,JSFUN_GENERIC_NATIVE, StringCharAt),
JS_INLINABLE_FN("charCodeAt", str_charCodeAt, 1,JSFUN_GENERIC_NATIVE, StringCharCodeAt),
JS_SELF_HOSTED_FN("substring", "String_substring", 2,0),
JS_SELF_HOSTED_FN("padStart", "String_pad_start", 2,0),
JS_SELF_HOSTED_FN("padEnd", "String_pad_end", 2,0),
JS_SELF_HOSTED_FN("codePointAt", "String_codePointAt", 1,0),
JS_FN("includes", str_includes, 1,JSFUN_GENERIC_NATIVE),
JS_FN("contains", str_contains, 1,JSFUN_GENERIC_NATIVE),