mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-06-13 06:51:34 +00:00
Implement String#slice (#404)
This commit is contained in:
1
std/assembly/index.d.ts
vendored
1
std/assembly/index.d.ts
vendored
@ -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;
|
||||
|
@ -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>();
|
||||
|
1
std/portable/index.d.ts
vendored
1
std/portable/index.d.ts
vendored
@ -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;
|
||||
}
|
||||
|
Reference in New Issue
Block a user