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;
}