Rewrite with working async
This commit is contained in:
+21
-23
@@ -2,7 +2,7 @@
|
||||
function String(x) {
|
||||
switch (typeof x) {
|
||||
case "string": return x;
|
||||
case "number": return __native_number_to_string(x);
|
||||
case "number": return __rt_number_to_string(x);
|
||||
case "boolean": return x ? "true" : "false";
|
||||
case "undefined": return "undefined";
|
||||
case "function": return "function () { [native code] }";
|
||||
@@ -14,7 +14,7 @@ function String(x) {
|
||||
}
|
||||
|
||||
String.fromCharCode = function (code) {
|
||||
return __native_string_from_char_code(code);
|
||||
return __rt_string_from_char_code(code);
|
||||
};
|
||||
|
||||
// In these methods `this` is the primitive string itself
|
||||
@@ -25,41 +25,41 @@ String.prototype = {
|
||||
// ── Characters ──
|
||||
charAt(i) {
|
||||
if (i < 0 || i >= this.length) return "";
|
||||
return __native_string_slice(this, i, i + 1);
|
||||
return __rt_string_slice(this, i, i + 1);
|
||||
},
|
||||
charCodeAt(i) {
|
||||
if (i < 0 || i >= this.length) return NaN;
|
||||
return __native_string_char_code_at(this, i);
|
||||
return __rt_string_char_code_at(this, i);
|
||||
},
|
||||
at(i) {
|
||||
if (i < 0) i += this.length;
|
||||
if (i < 0 || i >= this.length) return undefined;
|
||||
return __native_string_slice(this, i, i + 1);
|
||||
return __rt_string_slice(this, i, i + 1);
|
||||
},
|
||||
|
||||
// ── Search ──
|
||||
indexOf(search, from) {
|
||||
let start = from === undefined ? 0 : clampIndex(from, this.length);
|
||||
for (let i = start; i + search.length <= this.length; i++) {
|
||||
if (__native_string_slice(this, i, i + search.length) === search) return i;
|
||||
if (__rt_string_slice(this, i, i + search.length) === search) return i;
|
||||
}
|
||||
return -1;
|
||||
},
|
||||
lastIndexOf(search) {
|
||||
for (let i = this.length - search.length; i >= 0; i--) {
|
||||
if (__native_string_slice(this, i, i + search.length) === search) return i;
|
||||
if (__rt_string_slice(this, i, i + search.length) === search) return i;
|
||||
}
|
||||
return -1;
|
||||
},
|
||||
includes(search, from) { return this.indexOf(search, from) !== -1; },
|
||||
startsWith(search, from) {
|
||||
let start = from === undefined ? 0 : clampIndex(from, this.length);
|
||||
return __native_string_slice(this, start, start + search.length) === search;
|
||||
return __rt_string_slice(this, start, start + search.length) === search;
|
||||
},
|
||||
endsWith(search, end) {
|
||||
let stop = end === undefined ? this.length : clampIndex(end, this.length);
|
||||
let start = stop - search.length;
|
||||
return start >= 0 && __native_string_slice(this, start, stop) === search;
|
||||
return start >= 0 && __rt_string_slice(this, start, stop) === search;
|
||||
},
|
||||
|
||||
// ── Substrings ──
|
||||
@@ -67,13 +67,13 @@ String.prototype = {
|
||||
let len = this.length;
|
||||
let s = relativeIndex(start === undefined ? 0 : start, len);
|
||||
let e = relativeIndex(end === undefined ? len : end, len);
|
||||
return s < e ? __native_string_slice(this, s, e) : "";
|
||||
return s < e ? __rt_string_slice(this, s, e) : "";
|
||||
},
|
||||
substring(start, end) {
|
||||
let len = this.length;
|
||||
let s = clampIndex(start === undefined ? 0 : start, len);
|
||||
let e = clampIndex(end === undefined ? len : end, len);
|
||||
return s < e ? __native_string_slice(this, s, e) : __native_string_slice(this, e, s);
|
||||
return s < e ? __rt_string_slice(this, s, e) : __rt_string_slice(this, e, s);
|
||||
},
|
||||
|
||||
// ── Case ──
|
||||
@@ -84,27 +84,25 @@ String.prototype = {
|
||||
trim() { return this.trimStart().trimEnd(); },
|
||||
trimStart() {
|
||||
let i = 0;
|
||||
while (i < this.length && isSpace(__native_string_char_code_at(this, i))) i++;
|
||||
return __native_string_slice(this, i, this.length);
|
||||
while (i < this.length && isSpace(__rt_string_char_code_at(this, i))) i++;
|
||||
return __rt_string_slice(this, i, this.length);
|
||||
},
|
||||
trimEnd() {
|
||||
let i = this.length;
|
||||
while (i > 0 && isSpace(__native_string_char_code_at(this, i - 1))) i--;
|
||||
return __native_string_slice(this, 0, i);
|
||||
while (i > 0 && isSpace(__rt_string_char_code_at(this, i - 1))) i--;
|
||||
return __rt_string_slice(this, 0, i);
|
||||
},
|
||||
|
||||
// ── Building ──
|
||||
repeat(count) {
|
||||
let s = "";
|
||||
for (let i = 0; i < count; i++) s = __native_string_concat(s, this);
|
||||
for (let i = 0; i < count; i++) s = __rt_string_concat(s, this);
|
||||
return s;
|
||||
},
|
||||
padStart(length, fill) { return pad(this, length, fill, true); },
|
||||
padEnd(length, fill) { return pad(this, length, fill, false); },
|
||||
};
|
||||
|
||||
__link_proto_string(String.prototype);
|
||||
|
||||
// An index clamped to [0, len]
|
||||
function clampIndex(i, len) {
|
||||
return i < 0 ? 0 : i > len ? len : i;
|
||||
@@ -119,9 +117,9 @@ function relativeIndex(i, len) {
|
||||
function mapCodes(s, lo, hi, shift) {
|
||||
let out = "";
|
||||
for (let i = 0; i < s.length; i++) {
|
||||
let c = __native_string_char_code_at(s, i);
|
||||
let c = __rt_string_char_code_at(s, i);
|
||||
if (c >= lo && c <= hi) c += shift;
|
||||
out = __native_string_concat(out, __native_string_from_char_code(c));
|
||||
out = __rt_string_concat(out, __rt_string_from_char_code(c));
|
||||
}
|
||||
return out;
|
||||
}
|
||||
@@ -136,8 +134,8 @@ function pad(s, length, fill, atStart) {
|
||||
if (s.length >= length || f.length === 0) return s;
|
||||
|
||||
let padding = "";
|
||||
while (padding.length < length - s.length) padding = __native_string_concat(padding, f);
|
||||
padding = __native_string_slice(padding, 0, length - s.length);
|
||||
while (padding.length < length - s.length) padding = __rt_string_concat(padding, f);
|
||||
padding = __rt_string_slice(padding, 0, length - s.length);
|
||||
|
||||
return atStart ? __native_string_concat(padding, s) : __native_string_concat(s, padding);
|
||||
return atStart ? __rt_string_concat(padding, s) : __rt_string_concat(s, padding);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user