Implement String#slice (#404)

This commit is contained in:
LiaoPeng 2019-01-10 19:10:23 +08:00 committed by Daniel Wirtz
parent d82995c686
commit 201bd5f2b1
8 changed files with 3270 additions and 2803 deletions

View File

@ -682,6 +682,7 @@ declare class String {
padStart(targetLength: i32, padString?: string): string;
padEnd(targetLength: i32, padString?: string): string;
repeat(count?: i32): string;
slice(beginIndex: i32, endIndex?: i32): string;
split(separator?: string, limit?: i32): string[];
toString(): string;
static fromUTF8(ptr: usize, len: usize): string;

View File

@ -413,6 +413,17 @@ export class String {
return result;
}
slice(beginIndex: i32, endIndex: i32 = i32.MAX_VALUE): String {
var length = this.length;
var begin = (beginIndex < 0) ? max(beginIndex + length, 0) : min(beginIndex, length);
var end = (endIndex < 0) ? max(endIndex + length, 0) : min(endIndex, length);
var len = end - begin;
if (len <= 0) return changetype<String>("");
var out = allocateUnsafe(len);
copyUnsafe(out, 0, this, begin, len);
return out;
}
split(separator: String = null, limit: i32 = i32.MAX_VALUE): String[] {
assert(this !== null);
if (!limit) return new Array<String>();

View File

@ -427,6 +427,7 @@ declare class String {
padEnd(targetLength: i32, padString?: string): string;
replace(search: string, replacement: string): string;
repeat(count?: i32): string;
slice(beginIndex: i32, endIndex?: i32): string;
split(separator?: string, limit?: i32): string[];
toString(): string;
}

View File

@ -1575,7 +1575,7 @@
if
i32.const 0
i32.const 72
i32.const 510
i32.const 521
i32.const 8
call $~lib/env/abort
unreachable
@ -1622,7 +1622,7 @@
if
i32.const 0
i32.const 72
i32.const 514
i32.const 525
i32.const 8
call $~lib/env/abort
unreachable
@ -1695,7 +1695,7 @@
if
i32.const 0
i32.const 72
i32.const 526
i32.const 537
i32.const 8
call $~lib/env/abort
unreachable
@ -1748,7 +1748,7 @@
if
i32.const 0
i32.const 72
i32.const 535
i32.const 546
i32.const 4
call $~lib/env/abort
unreachable

View File

@ -2001,7 +2001,7 @@
if
i32.const 0
i32.const 72
i32.const 510
i32.const 521
i32.const 8
call $~lib/env/abort
unreachable
@ -2055,7 +2055,7 @@
if
i32.const 0
i32.const 72
i32.const 514
i32.const 525
i32.const 8
call $~lib/env/abort
unreachable
@ -2150,7 +2150,7 @@
if
i32.const 0
i32.const 72
i32.const 526
i32.const 537
i32.const 8
call $~lib/env/abort
unreachable
@ -2213,7 +2213,7 @@
if
i32.const 0
i32.const 72
i32.const 535
i32.const 546
i32.const 4
call $~lib/env/abort
unreachable

File diff suppressed because it is too large Load Diff

View File

@ -4,7 +4,6 @@ import {
itoa32,
utoa64,
itoa64,
itoa,
dtoa
} from "internal/number";
@ -143,6 +142,16 @@ assert("a".repeat(5) == "aaaaa");
assert("a".repeat(6) == "aaaaaa");
assert("a".repeat(7) == "aaaaaaa");
// test cases for slice method
str = "abcdefghijklmn";
assert(str.slice(0) == "abcdefghijklmn");
assert(str.slice(-1) == "n");
assert(str.slice(-5) == "jklmn");
assert(str.slice(2, 7) == "cdefg");
assert(str.slice(-11, -6) == "defgh");
assert(str.slice(4, 3) == "");
assert(str.slice(0, -1) == "abcdefghijklm");
var sa: string[];
sa = "".split();

File diff suppressed because it is too large Load Diff