Implement String#padStart/padEnd; Refactor internal string copy, compare and repeat (#171)

This commit is contained in:
Max Graey 2018-07-25 17:25:53 +03:00 committed by Daniel Wirtz
parent 298a8f1688
commit 671121bf70
15 changed files with 6385 additions and 3983 deletions

View File

@ -489,6 +489,8 @@ declare class String {
trim(): string;
trimLeft(): string;
trimRight(): string;
padStart(targetLength: i32, padString?: string): string;
padEnd(targetLength: i32, padString?: string): string;
repeat(count?: i32): string;
toString(): string;
toUTF8(): usize;

View File

@ -17,7 +17,7 @@ export const EMPTY = changetype<String>(""); // TODO: is this a bad idea with '=
@inline
export function clamp<T>(val: T, lo: T, hi: T): T {
return max<T>(min<T>(val, hi), lo);
return min<T>(max<T>(val, lo), hi);
}
/** Allocates a raw String with uninitialized contents. */
@ -28,6 +28,15 @@ export function allocate(length: i32): String {
return changetype<String>(buffer);
}
@inline
export function copyUnsafe(dest: String, destOffset: usize, src: String, srcOffset: usize, len: usize): void {
memory.copy(
changetype<usize>(dest) + (destOffset << 1) + HEADER_SIZE,
changetype<usize>(src) + (srcOffset << 1) + HEADER_SIZE,
len << 1
);
}
export function isWhiteSpaceOrLineTerminator(c: u16): bool {
switch (c) {
case 10: // <LF>
@ -76,24 +85,19 @@ export const enum CharCode {
export function parse<T>(str: String, radix: i32 = 0): T {
var len: i32 = str.length;
if (!len) {
return <T>NaN;
}
if (!len) return <T>NaN;
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEADER_SIZE);
// determine sign
var sign: T;
if (code == CharCode.MINUS) {
if (!--len) {
return <T>NaN;
}
if (!--len) return <T>NaN;
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = -1;
} else if (code == CharCode.PLUS) {
if (!--len) {
return <T>NaN;
}
if (!--len) return <T>NaN;
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = 1;
} else {
@ -122,9 +126,7 @@ export function parse<T>(str: String, radix: i32 = 0): T {
radix = 16;
break;
}
default: {
radix = 10;
}
default: radix = 10;
}
} else radix = 10;
} else if (radix < 2 || radix > 36) {
@ -141,22 +143,79 @@ export function parse<T>(str: String, radix: i32 = 0): T {
code -= CharCode.A - 10;
} else if (code >= CharCode.a && code <= CharCode.z) {
code -= CharCode.a - 10;
} else {
break;
}
if (code >= radix) {
break;
}
} else break;
if (code >= radix) break;
num = (num * radix) + code;
ptr += 2;
}
return sign * num;
}
export function compareUTF16(ptr1: usize, ptr2: usize, len: usize): i32 {
export function compareUnsafe(str1: String, offset1: usize, str2: String, offset2: usize, len: usize): i32 {
var cmp: i32 = 0;
var ptr1 = changetype<usize>(str1) + (offset1 << 1);
var ptr2 = changetype<usize>(str2) + (offset2 << 1);
while (len && !(cmp = <i32>load<u16>(ptr1, HEADER_SIZE) - <i32>load<u16>(ptr2, HEADER_SIZE))) {
--len, ++ptr1, ++ptr2;
}
return cmp;
}
export function repeatUnsafe(dest: String, destOffset: usize, src: String, count: i32): void {
var length = src.length;
if (ASC_SHRINK_LEVEL > 1) {
let strLen = length << 1;
let to = changetype<usize>(dest) + HEADER_SIZE + (destOffset << 1);
let from = changetype<usize>(src) + HEADER_SIZE;
for (let i = 0, len = strLen * count; i < len; i += strLen) {
memory.copy(to + i, from, strLen);
}
} else {
switch (length) {
case 0: break;
case 1: {
let cc = load<u16>(changetype<usize>(src), HEADER_SIZE);
let out = changetype<usize>(dest) + (destOffset << 1);
for (let i = 0; i < count; ++i) {
store<u16>(out + (i << 1), cc, HEADER_SIZE);
}
break;
}
case 2: {
let cc = load<u32>(changetype<usize>(src), HEADER_SIZE);
let out = changetype<usize>(dest) + (destOffset << 1);
for (let i = 0; i < count; ++i) {
store<u32>(out + (i << 2), cc, HEADER_SIZE);
}
break;
}
case 3: {
let cc1 = load<u32>(changetype<usize>(src), HEADER_SIZE + 0);
let cc2 = load<u16>(changetype<usize>(src), HEADER_SIZE + 4);
let out = changetype<usize>(dest) + (destOffset << 1);
for (let i = 0; i < count; ++i) {
store<u32>(out + (i << 2), cc1, HEADER_SIZE + 0);
store<u16>(out + (i << 1), cc2, HEADER_SIZE + 4);
}
break;
}
case 4: {
let cc = load<u64>(changetype<usize>(src), HEADER_SIZE);
let out = changetype<usize>(dest) + (destOffset << 1);
for (let i = 0; i < count; ++i) {
store<u64>(out + (i << 3), cc, HEADER_SIZE);
}
break;
}
default: {
let strLen = length << 1;
let to = changetype<usize>(dest) + HEADER_SIZE + (destOffset << 1);
let from = changetype<usize>(src) + HEADER_SIZE;
for (let i = 0, len = strLen * count; i < len; i += strLen) {
memory.copy(to + i, from, strLen);
}
break;
}
}
}
}

View File

@ -4,7 +4,9 @@ import {
EMPTY,
clamp,
allocate,
compareUTF16,
compareUnsafe,
repeatUnsafe,
copyUnsafe,
isWhiteSpaceOrLineTerminator,
CharCode,
parse
@ -73,9 +75,8 @@ export class String {
charCodeAt(pos: i32): i32 {
assert(this !== null);
if (<u32>pos >= <u32>this.length) {
return -1; // (NaN)
}
if (<u32>pos >= <u32>this.length) return -1; // (NaN)
return load<u16>(
changetype<usize>(this) + (<usize>pos << 1),
HEADER_SIZE
@ -84,9 +85,8 @@ export class String {
codePointAt(pos: i32): i32 {
assert(this !== null);
if (<u32>pos >= <u32>this.length) {
return -1; // (undefined)
}
if (<u32>pos >= <u32>this.length) return -1; // (undefined)
var first = <i32>load<u16>(
changetype<usize>(this) + (<usize>pos << 1),
HEADER_SIZE
@ -111,24 +111,14 @@ export class String {
concat(other: String): String {
assert(this !== null);
if (other === null) other = changetype<String>("null");
var thisLen: isize = this.length;
var otherLen: isize = other.length;
var outLen: usize = thisLen + otherLen;
if (outLen == 0) return EMPTY;
var out = allocate(outLen);
memory.copy(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE,
thisLen << 1
);
memory.copy(
changetype<usize>(out) + HEADER_SIZE + (thisLen << 1),
changetype<usize>(other) + HEADER_SIZE,
otherLen << 1
);
copyUnsafe(out, 0, this, 0, thisLen);
copyUnsafe(out, thisLen, other, 0, otherLen);
return out;
}
@ -139,11 +129,7 @@ export class String {
var searchLength: isize = searchString.length;
var start: isize = end - searchLength;
if (start < 0) return false;
return !compareUTF16(
changetype<usize>(this) + (start << 1),
changetype<usize>(searchString),
searchLength
);
return !compareUnsafe(this, start, searchString, 0, searchLength);
}
@operator("==")
@ -154,11 +140,7 @@ export class String {
var leftLength = left.length;
if (leftLength != right.length) return false;
return !compareUTF16(
changetype<usize>(left),
changetype<usize>(right),
leftLength
);
return !compareUnsafe(left, 0, right, 0, leftLength);
}
@operator("!=")
@ -177,11 +159,7 @@ export class String {
if (!rightLength) return true;
var length = <usize>min<i32>(leftLength, rightLength);
return compareUTF16(
changetype<usize>(left),
changetype<usize>(right),
length
) > 0;
return compareUnsafe(left, 0, right, 0, length) > 0;
}
@operator(">=")
@ -196,11 +174,7 @@ export class String {
if (!rightLength) return true;
var length = <usize>min<i32>(leftLength, rightLength);
return compareUTF16(
changetype<usize>(left),
changetype<usize>(right),
length
) >= 0;
return compareUnsafe(left, 0, right, 0, length) >= 0;
}
@operator("<")
@ -214,11 +188,7 @@ export class String {
if (!leftLength) return true;
var length = <usize>min<i32>(leftLength, rightLength);
return compareUTF16(
changetype<usize>(left),
changetype<usize>(right),
length
) < 0;
return compareUnsafe(left, 0, right, 0, length) < 0;
}
@operator("<=")
@ -233,11 +203,7 @@ export class String {
if (!leftLength) return true;
var length = <usize>min<i32>(leftLength, rightLength);
return compareUTF16(
changetype<usize>(left),
changetype<usize>(right),
length
) <= 0;
return compareUnsafe(left, 0, right, 0, length) <= 0;
}
includes(searchString: String, position: i32 = 0): bool {
@ -247,6 +213,7 @@ export class String {
indexOf(searchString: String, fromIndex: i32 = 0): i32 {
assert(this !== null);
if (searchString === null) searchString = changetype<String>("null");
var searchLen: isize = searchString.length;
if (!searchLen) return 0;
var len: isize = this.length;
@ -254,13 +221,7 @@ export class String {
var start = clamp<isize>(fromIndex, 0, len);
len -= searchLen;
for (let k: isize = start; k <= len; ++k) {
if (!compareUTF16(
changetype<usize>(this) + (k << 1),
changetype<usize>(searchString),
searchLen
)) {
return <i32>k;
}
if (!compareUnsafe(this, k, searchString, 0, searchLen)) return <i32>k;
}
return -1;
}
@ -268,21 +229,14 @@ export class String {
lastIndexOf(searchString: String, fromIndex: i32 = i32.MAX_VALUE): i32 {
assert(this !== null);
if (searchString === null) searchString = changetype<String>("null");
var len: isize = this.length;
var searchLen: isize = searchString.length;
if (!searchLen) return len;
if (!len) return -1;
var start = clamp<isize>(fromIndex, 0, len - searchLen);
// TODO: multiple char codes
for (let k = start; k >= 0; --k) {
if (!compareUTF16(
changetype<usize>(this) + (k << 1),
changetype<usize>(searchString),
searchLen
)) {
return <i32>k;
}
if (!compareUnsafe(this, k, searchString, 0, searchLen)) return <i32>k;
}
return -1;
}
@ -296,12 +250,7 @@ export class String {
var start = clamp<isize>(pos, 0, len);
var searchLength: isize = searchString.length;
if (searchLength + start > len) return false;
return !compareUTF16(
changetype<usize>(this) + (start << 1),
changetype<usize>(searchString),
searchLength
);
return !compareUnsafe(this, start, searchString, 0, searchLength);
}
substr(start: i32, length: i32 = i32.MAX_VALUE): String {
@ -309,19 +258,11 @@ export class String {
var intStart: isize = start;
var end: isize = length;
var size: isize = this.length;
if (intStart < 0) {
intStart = max<isize>(size + intStart, 0);
}
if (intStart < 0) intStart = max<isize>(size + intStart, 0);
var resultLength = clamp<isize>(end, 0, size - intStart);
if (resultLength <= 0) {
return EMPTY;
}
if (resultLength <= 0) return EMPTY;
var out = allocate(resultLength);
memory.copy(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (intStart << 1),
<usize>resultLength << 1
);
copyUnsafe(out, 0, this, intStart, resultLength);
return out;
}
@ -333,18 +274,10 @@ export class String {
var from = min<i32>(finalStart, finalEnd);
var to = max<i32>(finalStart, finalEnd);
len = to - from;
if (!len) {
return EMPTY;
}
if (!from && to == this.length) {
return this;
}
if (!len) return EMPTY;
if (!from && to == this.length) return this;
var out = allocate(len);
memory.copy(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (from << 1),
len << 1
);
copyUnsafe(out, 0, this, from, len);
return out;
}
@ -369,18 +302,10 @@ export class String {
) {
++start, --length;
}
if (!length) {
return EMPTY;
}
if (!start && length == this.length) {
return this;
}
if (!length) return EMPTY;
if (!start && length == this.length) return this;
var out = allocate(length);
memory.copy(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (start << 1),
length << 1
);
copyUnsafe(out, 0, this, start, length);
return out;
}
@ -396,19 +321,11 @@ export class String {
) {
++start;
}
if (!start) {
return this;
}
if (!start) return this;
var outLen = len - start;
if (!outLen) {
return EMPTY;
}
if (!outLen) return EMPTY;
var out = allocate(outLen);
memory.copy(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE + (start << 1),
outLen << 1
);
copyUnsafe(out, 0, this, start, outLen);
return out;
}
@ -423,18 +340,50 @@ export class String {
) {
--len;
}
if (len <= 0) {
return EMPTY;
}
if (<i32>len == this.length) {
return this;
}
if (len <= 0) return EMPTY;
if (<i32>len == this.length) return this;
var out = allocate(len);
memory.copy(
changetype<usize>(out) + HEADER_SIZE,
changetype<usize>(this) + HEADER_SIZE,
len << 1
);
copyUnsafe(out, 0, this, 0, len);
return out;
}
padStart(targetLength: i32, padString: String = changetype<String>(" ")): String {
assert(this !== null);
var length = this.length;
var padLen = padString.length;
if (targetLength < length || !padLen) return this;
var len = targetLength - length;
var out = allocate(targetLength);
if (len > padLen) {
let count = (len - 1) / padLen;
let base = count * padLen;
let rest = len - base;
repeatUnsafe(out, 0, padString, count);
if (rest) copyUnsafe(out, base, padString, 0, rest);
} else {
copyUnsafe(out, 0, padString, 0, len);
}
if (length) copyUnsafe(out, len, this, 0, length);
return out;
}
padEnd(targetLength: i32, padString: String = changetype<String>(" ")): String {
assert(this !== null);
var length = this.length;
var padLen = padString.length;
if (targetLength < length || !padLen) return this;
var len = targetLength - length;
var out = allocate(targetLength);
if (length) copyUnsafe(out, 0, this, 0, length);
if (len > padLen) {
let count = (len - 1) / padLen;
let base = count * padLen;
let rest = len - base;
repeatUnsafe(out, length, padString, count);
if (rest) copyUnsafe(out, base + length, padString, 0, rest);
} else {
copyUnsafe(out, length, padString, 0, len);
}
return out;
}
@ -451,20 +400,7 @@ export class String {
if (count === 1) return this;
var result = allocate(length * count);
var strLen = length << 1;
/*
* TODO possible improvments: reuse existing result for exponentially concats like:
* 'a' + 'a' => 'aa' + 'aa' => 'aaaa' + 'aaaa' etc
*/
for (let offset = 0, len = strLen * count; offset < len; offset += strLen) {
memory.copy(
changetype<usize>(result) + HEADER_SIZE + offset,
changetype<usize>(this) + HEADER_SIZE,
strLen
);
}
repeatUnsafe(result, 0, this, count);
return result;
}
@ -551,24 +487,19 @@ export function parseI64(str: String, radix: i32 = 0): i64 {
// FIXME: naive implementation
export function parseFloat(str: String): f64 {
var len: i32 = str.length;
if (!len) {
return NaN;
}
if (!len) return NaN;
var ptr = changetype<usize>(str) /* + HEAD -> offset */;
var code = <i32>load<u16>(ptr, HEADER_SIZE);
// determine sign
var sign: f64;
if (code == CharCode.MINUS) {
if (!--len) {
return NaN;
}
if (!--len) return NaN;
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = -1;
} else if (code == CharCode.PLUS) {
if (!--len) {
return NaN;
}
if (!--len) return NaN;
code = <i32>load<u16>(ptr += 2, HEADER_SIZE);
sign = 1;
} else {
@ -588,9 +519,7 @@ export function parseFloat(str: String): f64 {
assert(false); // TODO
}
code -= CharCode._0;
if (<u32>code > 9) {
break;
}
if (<u32>code > 9) break;
num += <f64>code * fac;
fac *= 0.1;
ptr += 2;
@ -598,9 +527,7 @@ export function parseFloat(str: String): f64 {
break;
}
code -= CharCode._0;
if (<u32>code >= 10) {
break;
}
if (<u32>code >= 10) break;
num = (num * 10) + code;
ptr += 2;
}

View File

@ -290,14 +290,21 @@ declare class String {
private constructor();
charAt(index: i32): string;
charCodeAt(index: i32): i32;
concat(other: string): string;
indexOf(other: string, fromIndex?: i32): i32;
lastIndexOf(other: string, fromIndex?: i32): i32;
includes(other: string): bool;
charAt(index: i32): string;
charCodeAt(index: i32): i32;
substring(from: i32, to?: i32): string;
startsWith(other: string): bool;
endsWith(other: string): bool;
substr(start: u32, length?: u32): string;
substring(from: i32, to?: i32): string;
trim(): string;
trimLeft(): string;
trimRight(): string;
padStart(targetLength: i32, padString?: string): string;
padEnd(targetLength: i32, padString?: string): string;
replace(search: string, replacement: string): string;
repeat(count?: i32): string;
toString(): string;

View File

@ -3,7 +3,7 @@
(type $iv (func (param i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $iii (func (param i32 i32) (result i32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $~lib/allocator/arena/startOffset (mut i32) (i32.const 0))
@ -104,36 +104,50 @@
(get_local $0)
)
)
(func $~lib/internal/string/compareUTF16 (; 3 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(func $~lib/internal/string/compareUnsafe (; 3 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(set_local $1
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(i32.eqz
(tee_local $3
(i32.sub
(i32.load16_u offset=4
(get_local $0)
)
(i32.load16_u offset=4
(get_local $1)
(tee_local $0
(if (result i32)
(get_local $4)
(i32.eqz
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $1)
)
(i32.load16_u offset=4
(get_local $2)
)
)
)
)
(get_local $4)
)
(get_local $2)
)
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(i32.const 1)
)
)
(set_local $0
(i32.add
(get_local $0)
(get_local $4)
(i32.const 1)
)
)
@ -143,11 +157,17 @@
(i32.const 1)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String.__eq (; 4 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
@ -196,9 +216,11 @@
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $2)
)
)

View File

@ -3,7 +3,7 @@
(type $iv (func (param i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $iii (func (param i32 i32) (result i32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $~lib/internal/allocator/AL_BITS i32 (i32.const 3))
@ -134,47 +134,67 @@
)
)
)
(func $~lib/internal/string/compareUTF16 (; 3 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(set_local $3
(func $~lib/internal/string/compareUnsafe (; 3 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(set_local $5
(i32.const 0)
)
(set_local $6
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $7
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(block $break|0
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(get_local $4)
(i32.eqz
(tee_local $3
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $0)
(get_local $6)
)
(i32.load16_u offset=4
(get_local $1)
(get_local $7)
)
)
)
)
(get_local $2)
(get_local $4)
)
(block
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(get_local $4)
(i32.const 1)
)
)
(set_local $0
(set_local $6
(i32.add
(get_local $0)
(get_local $6)
(i32.const 1)
)
)
(set_local $1
(set_local $7
(i32.add
(get_local $1)
(get_local $7)
(i32.const 1)
)
)
@ -184,7 +204,7 @@
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String.__eq (; 4 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
@ -233,9 +253,11 @@
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $3)
)
)

View File

@ -3,6 +3,7 @@
(type $iii (func (param i32 i32) (result i32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(memory $0 1)
(data (i32.const 12) "\0e\00\00\00~\00l\00i\00b\00/\00s\00t\00r\00i\00n\00g\00.\00t\00s")
@ -59,36 +60,50 @@
)
)
)
(func $~lib/internal/string/compareUTF16 (; 4 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(func $~lib/internal/string/compareUnsafe (; 4 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(set_local $1
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(i32.eqz
(tee_local $3
(i32.sub
(i32.load16_u offset=4
(get_local $0)
)
(i32.load16_u offset=4
(get_local $1)
(tee_local $0
(if (result i32)
(get_local $4)
(i32.eqz
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $1)
)
(i32.load16_u offset=4
(get_local $2)
)
)
)
)
(get_local $4)
)
(get_local $2)
)
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(i32.const 1)
)
)
(set_local $0
(i32.add
(get_local $0)
(get_local $4)
(i32.const 1)
)
)
@ -98,11 +113,17 @@
(i32.const 1)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String#startsWith (; 5 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
@ -115,7 +136,7 @@
(call $~lib/env/abort
(i32.const 0)
(i32.const 12)
(i32.const 291)
(i32.const 245)
(i32.const 4)
)
(unreachable)
@ -137,46 +158,42 @@
(get_local $1)
)
)
(tee_local $2
(tee_local $3
(select
(tee_local $2
(tee_local $3
(select
(get_local $2)
(tee_local $3
(i32.load
(get_local $0)
)
)
(i32.lt_s
(i32.const 0)
(i32.gt_s
(get_local $2)
(get_local $3)
(i32.const 0)
)
)
)
(i32.const 0)
(i32.gt_s
(tee_local $2
(i32.load
(get_local $0)
)
)
(i32.lt_s
(get_local $3)
(get_local $2)
(i32.const 0)
)
)
)
)
(get_local $3)
(get_local $2)
)
(return
(i32.const 0)
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(i32.add
(get_local $0)
(i32.shl
(get_local $2)
(i32.const 1)
)
)
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(get_local $3)
(get_local $1)
(i32.const 0)
(get_local $4)
)
)

View File

@ -3,6 +3,7 @@
(type $iii (func (param i32 i32) (result i32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $iiiiv (func (param i32 i32 i32 i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(global $~lib/internal/allocator/AL_BITS i32 (i32.const 3))
(global $~lib/internal/allocator/AL_SIZE i32 (i32.const 8))
@ -130,47 +131,67 @@
)
)
)
(func $~lib/internal/string/compareUTF16 (; 6 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(set_local $3
(func $~lib/internal/string/compareUnsafe (; 6 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(set_local $5
(i32.const 0)
)
(set_local $6
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $7
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(block $break|0
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(get_local $4)
(i32.eqz
(tee_local $3
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $0)
(get_local $6)
)
(i32.load16_u offset=4
(get_local $1)
(get_local $7)
)
)
)
)
(get_local $2)
(get_local $4)
)
(block
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(get_local $4)
(i32.const 1)
)
)
(set_local $0
(set_local $6
(i32.add
(get_local $0)
(get_local $6)
(i32.const 1)
)
)
(set_local $1
(set_local $7
(i32.add
(get_local $1)
(get_local $7)
(i32.const 1)
)
)
@ -180,7 +201,7 @@
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String#startsWith (; 7 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
@ -201,7 +222,7 @@
(call $~lib/env/abort
(i32.const 0)
(i32.const 12)
(i32.const 291)
(i32.const 245)
(i32.const 4)
)
(unreachable)
@ -236,18 +257,18 @@
(get_local $3)
)
(tee_local $7
(get_local $4)
(get_local $5)
)
(i32.lt_s
(i32.gt_s
(get_local $6)
(get_local $7)
)
)
)
(tee_local $7
(get_local $5)
(get_local $4)
)
(i32.gt_s
(i32.lt_s
(get_local $6)
(get_local $7)
)
@ -272,15 +293,11 @@
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(i32.add
(get_local $0)
(i32.shl
(get_local $8)
(i32.const 1)
)
)
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(get_local $8)
(get_local $1)
(i32.const 0)
(get_local $9)
)
)

View File

@ -12,6 +12,7 @@
(type $Iv (func (param i64)))
(type $II (func (param i64) (result i64)))
(type $iv (func (param i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(import "JSMath" "random" (func $~lib/math/JSMath.random (result f64)))
@ -5473,36 +5474,50 @@
)
)
)
(func $~lib/internal/string/compareUTF16 (; 90 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(func $~lib/internal/string/compareUnsafe (; 90 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(set_local $1
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(i32.eqz
(tee_local $3
(i32.sub
(i32.load16_u offset=4
(get_local $0)
)
(i32.load16_u offset=4
(get_local $1)
(tee_local $0
(if (result i32)
(get_local $4)
(i32.eqz
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $1)
)
(i32.load16_u offset=4
(get_local $2)
)
)
)
)
(get_local $4)
)
(get_local $2)
)
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(i32.const 1)
)
)
(set_local $0
(i32.add
(get_local $0)
(get_local $4)
(i32.const 1)
)
)
@ -5512,11 +5527,17 @@
(i32.const 1)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String.__gt (; 91 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
@ -5578,9 +5599,11 @@
)
)
(i32.gt_s
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(select
(get_local $2)
(get_local $3)
@ -5653,9 +5676,11 @@
)
)
(i32.lt_s
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(select
(get_local $2)
(get_local $3)
@ -5727,9 +5752,11 @@
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $2)
)
)
@ -5861,7 +5888,7 @@
(call $~lib/env/abort
(i32.const 0)
(i32.const 696)
(i32.const 56)
(i32.const 58)
(i32.const 4)
)
(unreachable)
@ -5956,20 +5983,21 @@
(get_local $0)
(i32.const 4)
)
(tee_local $0
(i32.shl
(get_local $3)
(i32.const 1)
)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
(call $~lib/memory/memory.copy
(i32.add
(i32.add
(get_local $2)
(i32.const 4)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
(get_local $0)
(i32.const 4)
)
(i32.add
(get_local $1)

View File

@ -12,6 +12,7 @@
(type $Iv (func (param i64)))
(type $II (func (param i64) (result i64)))
(type $iv (func (param i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
(import "JSMath" "random" (func $~lib/math/JSMath.random (result f64)))
@ -7794,47 +7795,67 @@
)
)
)
(func $~lib/internal/string/compareUTF16 (; 121 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(set_local $3
(func $~lib/internal/string/compareUnsafe (; 121 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(set_local $5
(i32.const 0)
)
(set_local $6
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $7
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(block $break|0
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(get_local $4)
(i32.eqz
(tee_local $3
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $0)
(get_local $6)
)
(i32.load16_u offset=4
(get_local $1)
(get_local $7)
)
)
)
)
(get_local $2)
(get_local $4)
)
(block
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(get_local $4)
(i32.const 1)
)
)
(set_local $0
(set_local $6
(i32.add
(get_local $0)
(get_local $6)
(i32.const 1)
)
)
(set_local $1
(set_local $7
(i32.add
(get_local $1)
(get_local $7)
(i32.const 1)
)
)
@ -7844,7 +7865,7 @@
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String.__gt (; 122 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
@ -7920,9 +7941,11 @@
)
)
(i32.gt_s
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $6)
)
(i32.const 0)
@ -8002,9 +8025,11 @@
)
)
(i32.lt_s
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $6)
)
(i32.const 0)
@ -8480,9 +8505,11 @@
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $3)
)
)
@ -8691,7 +8718,7 @@
(call $~lib/env/abort
(i32.const 0)
(i32.const 696)
(i32.const 56)
(i32.const 58)
(i32.const 4)
)
(unreachable)
@ -8732,6 +8759,8 @@
(local $3 i32)
(local $4 i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(if
(i32.eqz
(i32.ne
@ -8788,24 +8817,32 @@
(get_local $4)
)
)
(call $~lib/memory/memory.copy
(i32.add
(get_local $5)
(get_global $~lib/internal/string/HEADER_SIZE)
(block $~lib/internal/string/copyUnsafe|inlined.0
(set_local $6
(i32.const 0)
)
(i32.add
(get_local $0)
(get_global $~lib/internal/string/HEADER_SIZE)
(set_local $7
(i32.const 0)
)
(i32.shl
(get_local $2)
(i32.const 1)
)
)
(call $~lib/memory/memory.copy
(i32.add
(call $~lib/memory/memory.copy
(i32.add
(get_local $5)
(i32.add
(get_local $5)
(i32.shl
(get_local $6)
(i32.const 1)
)
)
(get_global $~lib/internal/string/HEADER_SIZE)
)
(i32.add
(i32.add
(get_local $0)
(i32.shl
(get_local $7)
(i32.const 1)
)
)
(get_global $~lib/internal/string/HEADER_SIZE)
)
(i32.shl
@ -8813,13 +8850,36 @@
(i32.const 1)
)
)
(i32.add
(get_local $1)
(get_global $~lib/internal/string/HEADER_SIZE)
)
(block $~lib/internal/string/copyUnsafe|inlined.1
(set_local $7
(i32.const 0)
)
(i32.shl
(get_local $3)
(i32.const 1)
(call $~lib/memory/memory.copy
(i32.add
(i32.add
(get_local $5)
(i32.shl
(get_local $2)
(i32.const 1)
)
)
(get_global $~lib/internal/string/HEADER_SIZE)
)
(i32.add
(i32.add
(get_local $1)
(i32.shl
(get_local $7)
(i32.const 1)
)
)
(get_global $~lib/internal/string/HEADER_SIZE)
)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(get_local $5)

File diff suppressed because it is too large Load Diff

View File

@ -25,6 +25,24 @@ assert(str.startsWith("hi"));
assert(str.endsWith("string"));
assert(str.includes("I'm"));
assert(str.padStart(0) == str);
assert(str.padStart(15) == str);
assert("".padStart(3) == " ");
assert("".padStart(10, "") == "");
assert("a".padStart(100, "") == "a");
assert("abc".padStart(5) == " abc");
assert("abc".padStart(6, "123") == "123abc");
assert("abc".padStart(8, "123") == "12312abc");
assert(str.padEnd(0) == str);
assert(str.padEnd(15) == str);
assert("".padEnd(3) == " ");
assert("".padEnd(10, "") == "");
assert("a".padEnd(100, "") == "a");
assert("abc".padEnd(5) == "abc ");
assert("abc".padEnd(6, "abc") == "abcabc");
assert("abc".padEnd(8, "abc") == "abcabcab");
assert("".indexOf("") == 0);
assert("".indexOf("hi") == -1);
assert(str.indexOf("") == 0);

File diff suppressed because it is too large Load Diff

View File

@ -5,6 +5,7 @@
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $iiiv (func (param i32 i32 i32)))
(type $iii (func (param i32 i32) (result i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(type $iiv (func (param i32 i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
@ -679,36 +680,50 @@
)
(get_local $2)
)
(func $~lib/internal/string/compareUTF16 (; 12 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(func $~lib/internal/string/compareUnsafe (; 12 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(set_local $1
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(i32.eqz
(tee_local $3
(i32.sub
(i32.load16_u offset=4
(get_local $0)
)
(i32.load16_u offset=4
(get_local $1)
(tee_local $0
(if (result i32)
(get_local $4)
(i32.eqz
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $1)
)
(i32.load16_u offset=4
(get_local $2)
)
)
)
)
(get_local $4)
)
(get_local $2)
)
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(i32.const 1)
)
)
(set_local $0
(i32.add
(get_local $0)
(get_local $4)
(i32.const 1)
)
)
@ -718,11 +733,17 @@
(i32.const 1)
)
)
(set_local $2
(i32.add
(get_local $2)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String.__eq (; 13 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
@ -771,9 +792,11 @@
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $2)
)
)

View File

@ -5,6 +5,7 @@
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $iiiv (func (param i32 i32 i32)))
(type $iii (func (param i32 i32) (result i32)))
(type $iiiiii (func (param i32 i32 i32 i32 i32) (result i32)))
(type $iiv (func (param i32 i32)))
(type $v (func))
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
@ -854,47 +855,67 @@
)
(get_local $1)
)
(func $~lib/internal/string/compareUTF16 (; 14 ;) (type $iiii) (param $0 i32) (param $1 i32) (param $2 i32) (result i32)
(local $3 i32)
(set_local $3
(func $~lib/internal/string/compareUnsafe (; 14 ;) (type $iiiiii) (param $0 i32) (param $1 i32) (param $2 i32) (param $3 i32) (param $4 i32) (result i32)
(local $5 i32)
(local $6 i32)
(local $7 i32)
(set_local $5
(i32.const 0)
)
(set_local $6
(i32.add
(get_local $0)
(i32.shl
(get_local $1)
(i32.const 1)
)
)
)
(set_local $7
(i32.add
(get_local $2)
(i32.shl
(get_local $3)
(i32.const 1)
)
)
)
(block $break|0
(loop $continue|0
(if
(if (result i32)
(get_local $2)
(get_local $4)
(i32.eqz
(tee_local $3
(tee_local $5
(i32.sub
(i32.load16_u offset=4
(get_local $0)
(get_local $6)
)
(i32.load16_u offset=4
(get_local $1)
(get_local $7)
)
)
)
)
(get_local $2)
(get_local $4)
)
(block
(block
(set_local $2
(set_local $4
(i32.sub
(get_local $2)
(get_local $4)
(i32.const 1)
)
)
(set_local $0
(set_local $6
(i32.add
(get_local $0)
(get_local $6)
(i32.const 1)
)
)
(set_local $1
(set_local $7
(i32.add
(get_local $1)
(get_local $7)
(i32.const 1)
)
)
@ -904,7 +925,7 @@
)
)
)
(get_local $3)
(get_local $5)
)
(func $~lib/string/String.__eq (; 15 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
@ -953,9 +974,11 @@
)
)
(i32.eqz
(call $~lib/internal/string/compareUTF16
(call $~lib/internal/string/compareUnsafe
(get_local $0)
(i32.const 0)
(get_local $1)
(i32.const 0)
(get_local $3)
)
)