Skip to content

Commit 201bd5f

Browse files
LiaoPengdcodeIO
authored andcommitted
Implement String#slice (AssemblyScript#404)
1 parent d82995c commit 201bd5f

File tree

8 files changed

+2254
-1787
lines changed

8 files changed

+2254
-1787
lines changed

std/assembly/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -682,6 +682,7 @@ declare class String {
682682
padStart(targetLength: i32, padString?: string): string;
683683
padEnd(targetLength: i32, padString?: string): string;
684684
repeat(count?: i32): string;
685+
slice(beginIndex: i32, endIndex?: i32): string;
685686
split(separator?: string, limit?: i32): string[];
686687
toString(): string;
687688
static fromUTF8(ptr: usize, len: usize): string;

std/assembly/string.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ export class String {
413413
return result;
414414
}
415415

416+
slice(beginIndex: i32, endIndex: i32 = i32.MAX_VALUE): String {
417+
var length = this.length;
418+
var begin = (beginIndex < 0) ? max(beginIndex + length, 0) : min(beginIndex, length);
419+
var end = (endIndex < 0) ? max(endIndex + length, 0) : min(endIndex, length);
420+
var len = end - begin;
421+
if (len <= 0) return changetype<String>("");
422+
var out = allocateUnsafe(len);
423+
copyUnsafe(out, 0, this, begin, len);
424+
return out;
425+
}
426+
416427
split(separator: String = null, limit: i32 = i32.MAX_VALUE): String[] {
417428
assert(this !== null);
418429
if (!limit) return new Array<String>();

std/portable/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ declare class String {
427427
padEnd(targetLength: i32, padString?: string): string;
428428
replace(search: string, replacement: string): string;
429429
repeat(count?: i32): string;
430+
slice(beginIndex: i32, endIndex?: i32): string;
430431
split(separator?: string, limit?: i32): string[];
431432
toString(): string;
432433
}

tests/compiler/std/string-utf8.optimized.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,7 +1575,7 @@
15751575
if
15761576
i32.const 0
15771577
i32.const 72
1578-
i32.const 510
1578+
i32.const 521
15791579
i32.const 8
15801580
call $~lib/env/abort
15811581
unreachable
@@ -1622,7 +1622,7 @@
16221622
if
16231623
i32.const 0
16241624
i32.const 72
1625-
i32.const 514
1625+
i32.const 525
16261626
i32.const 8
16271627
call $~lib/env/abort
16281628
unreachable
@@ -1695,7 +1695,7 @@
16951695
if
16961696
i32.const 0
16971697
i32.const 72
1698-
i32.const 526
1698+
i32.const 537
16991699
i32.const 8
17001700
call $~lib/env/abort
17011701
unreachable
@@ -1748,7 +1748,7 @@
17481748
if
17491749
i32.const 0
17501750
i32.const 72
1751-
i32.const 535
1751+
i32.const 546
17521752
i32.const 4
17531753
call $~lib/env/abort
17541754
unreachable

tests/compiler/std/string-utf8.untouched.wat

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,7 +2001,7 @@
20012001
if
20022002
i32.const 0
20032003
i32.const 72
2004-
i32.const 510
2004+
i32.const 521
20052005
i32.const 8
20062006
call $~lib/env/abort
20072007
unreachable
@@ -2055,7 +2055,7 @@
20552055
if
20562056
i32.const 0
20572057
i32.const 72
2058-
i32.const 514
2058+
i32.const 525
20592059
i32.const 8
20602060
call $~lib/env/abort
20612061
unreachable
@@ -2150,7 +2150,7 @@
21502150
if
21512151
i32.const 0
21522152
i32.const 72
2153-
i32.const 526
2153+
i32.const 537
21542154
i32.const 8
21552155
call $~lib/env/abort
21562156
unreachable
@@ -2213,7 +2213,7 @@
22132213
if
22142214
i32.const 0
22152215
i32.const 72
2216-
i32.const 535
2216+
i32.const 546
22172217
i32.const 4
22182218
call $~lib/env/abort
22192219
unreachable

tests/compiler/std/string.optimized.wat

Lines changed: 1538 additions & 1320 deletions
Large diffs are not rendered by default.

tests/compiler/std/string.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import {
44
itoa32,
55
utoa64,
66
itoa64,
7-
itoa,
87
dtoa
98
} from "internal/number";
109

@@ -143,6 +142,16 @@ assert("a".repeat(5) == "aaaaa");
143142
assert("a".repeat(6) == "aaaaaa");
144143
assert("a".repeat(7) == "aaaaaaa");
145144

145+
// test cases for slice method
146+
str = "abcdefghijklmn";
147+
assert(str.slice(0) == "abcdefghijklmn");
148+
assert(str.slice(-1) == "n");
149+
assert(str.slice(-5) == "jklmn");
150+
assert(str.slice(2, 7) == "cdefg");
151+
assert(str.slice(-11, -6) == "defgh");
152+
assert(str.slice(4, 3) == "");
153+
assert(str.slice(0, -1) == "abcdefghijklm");
154+
146155
var sa: string[];
147156

148157
sa = "".split();

tests/compiler/std/string.untouched.wat

Lines changed: 685 additions & 458 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)