Skip to content

Commit 711f73b

Browse files
MaxGraeydcodeIO
authored andcommitted
Implement String#split (AssemblyScript#301)
1 parent 086d96b commit 711f73b

13 files changed

+10169
-5990
lines changed

std/assembly/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -491,6 +491,7 @@ declare class String {
491491
padStart(targetLength: i32, padString?: string): string;
492492
padEnd(targetLength: i32, padString?: string): string;
493493
repeat(count?: i32): string;
494+
split(separator?: string, limit?: i32): string[];
494495
toString(): string;
495496
static fromUTF8(ptr: usize, len: usize): string;
496497
toUTF8(): usize;

std/assembly/string.ts

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ import {
1010
parse
1111
} from "./internal/string";
1212

13+
import {
14+
storeUnsafe as storeUnsafeArray
15+
} from "./internal/arraybuffer";
16+
1317
@sealed
1418
export class String {
1519

@@ -200,6 +204,7 @@ export class String {
200204
return compareUnsafe(left, 0, right, 0, length) <= 0;
201205
}
202206

207+
@inline
203208
includes(searchString: String, position: i32 = 0): bool {
204209
return this.indexOf(searchString, position) != -1;
205210
}
@@ -400,14 +405,69 @@ export class String {
400405
throw new RangeError("Invalid count value");
401406
}
402407

403-
if (count === 0 || !length) return changetype<String>("");
404-
if (count === 1) return this;
408+
if (count == 0 || !length) return changetype<String>("");
409+
if (count == 1) return this;
405410

406411
var result = allocateUnsafe(length * count);
407412
repeatUnsafe(result, 0, this, count);
408413
return result;
409414
}
410415

416+
split(separator: String = null, limit: i32 = i32.MAX_VALUE): String[] {
417+
assert(this !== null);
418+
if (!limit) return new Array<String>();
419+
if (separator === null) return <String[]>[this];
420+
var length: isize = this.length;
421+
var sepLen: isize = separator.length;
422+
if (limit < 0) limit = i32.MAX_VALUE;
423+
if (!sepLen) {
424+
if (!length) return new Array<String>();
425+
// split by chars
426+
length = min<isize>(length, <isize>limit);
427+
let result = new Array<String>(length);
428+
let buffer = <ArrayBuffer>result.buffer_;
429+
for (let i: isize = 0; i < length; ++i) {
430+
let char = allocateUnsafe(1);
431+
store<u16>(
432+
changetype<usize>(char),
433+
load<u16>(
434+
changetype<usize>(this) + (<usize>i << 1),
435+
HEADER_SIZE
436+
),
437+
HEADER_SIZE
438+
);
439+
storeUnsafeArray<String,String>(buffer, i, char);
440+
}
441+
return result;
442+
} else if (!length) {
443+
return <String[]>[changetype<String>("")];
444+
}
445+
var result = new Array<String>();
446+
var end = 0, start = 0, i = 0;
447+
while ((end = this.indexOf(separator, start)) != -1) {
448+
let len = end - start;
449+
if (len > 0) {
450+
let out = allocateUnsafe(len);
451+
copyUnsafe(out, 0, this, start, len);
452+
result.push(out);
453+
} else {
454+
result.push(changetype<String>(""));
455+
}
456+
if (++i == limit) return result;
457+
start = end + sepLen;
458+
}
459+
if (!start) return <String[]>[this];
460+
var len = length - start;
461+
if (len > 0) {
462+
let out = allocateUnsafe(len);
463+
copyUnsafe(out, 0, this, start, len);
464+
result.push(out);
465+
} else {
466+
result.push(changetype<String>(""));
467+
}
468+
return result;
469+
}
470+
411471
toString(): String {
412472
return this;
413473
}

std/portable/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,7 @@ declare class String {
310310
padEnd(targetLength: i32, padString?: string): string;
311311
replace(search: string, replacement: string): string;
312312
repeat(count?: i32): string;
313+
split(separator?: string, limit?: i32): string[];
313314
toString(): string;
314315
}
315316

tests/compiler/std/array-access.optimized.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
(call $~lib/env/abort
133133
(i32.const 0)
134134
(i32.const 16)
135-
(i32.const 239)
135+
(i32.const 244)
136136
(i32.const 4)
137137
)
138138
(unreachable)

tests/compiler/std/array-access.untouched.wat

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@
225225
(call $~lib/env/abort
226226
(i32.const 0)
227227
(i32.const 16)
228-
(i32.const 239)
228+
(i32.const 244)
229229
(i32.const 4)
230230
)
231231
(unreachable)

tests/compiler/std/array.optimized.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7930,7 +7930,7 @@
79307930
(call $~lib/env/abort
79317931
(i32.const 0)
79327932
(i32.const 1616)
7933-
(i32.const 106)
7933+
(i32.const 110)
79347934
(i32.const 4)
79357935
)
79367936
(unreachable)
@@ -8104,7 +8104,7 @@
81048104
(call $~lib/env/abort
81058105
(i32.const 0)
81068106
(i32.const 1616)
8107-
(i32.const 264)
8107+
(i32.const 269)
81088108
(i32.const 4)
81098109
)
81108110
(unreachable)

tests/compiler/std/array.untouched.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12670,7 +12670,7 @@
1267012670
(call $~lib/env/abort
1267112671
(i32.const 0)
1267212672
(i32.const 1616)
12673-
(i32.const 54)
12673+
(i32.const 58)
1267412674
(i32.const 4)
1267512675
)
1267612676
(unreachable)
@@ -12764,7 +12764,7 @@
1276412764
(call $~lib/env/abort
1276512765
(i32.const 0)
1276612766
(i32.const 1616)
12767-
(i32.const 106)
12767+
(i32.const 110)
1276812768
(i32.const 4)
1276912769
)
1277012770
(unreachable)
@@ -13051,7 +13051,7 @@
1305113051
(call $~lib/env/abort
1305213052
(i32.const 0)
1305313053
(i32.const 1616)
13054-
(i32.const 264)
13054+
(i32.const 269)
1305513055
(i32.const 4)
1305613056
)
1305713057
(unreachable)

tests/compiler/std/hash.untouched.wat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
(global $~lib/internal/allocator/AL_SIZE i32 (i32.const 8))
1414
(global $~lib/internal/allocator/AL_MASK i32 (i32.const 7))
1515
(global $~lib/internal/allocator/MAX_SIZE_32 i32 (i32.const 1073741824))
16+
(global $~lib/internal/arraybuffer/HEADER_SIZE i32 (i32.const 8))
17+
(global $~lib/internal/arraybuffer/MAX_BLENGTH i32 (i32.const 1073741816))
1618
(global $~lib/internal/string/HEADER_SIZE i32 (i32.const 4))
1719
(global $~lib/internal/string/MAX_LENGTH i32 (i32.const 536870910))
1820
(global $~lib/internal/hash/FNV_OFFSET i32 (i32.const -2128831035))

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,7 +2365,7 @@
23652365
(call $~lib/env/abort
23662366
(i32.const 0)
23672367
(i32.const 72)
2368-
(i32.const 450)
2368+
(i32.const 510)
23692369
(i32.const 8)
23702370
)
23712371
(unreachable)
@@ -2440,7 +2440,7 @@
24402440
(call $~lib/env/abort
24412441
(i32.const 0)
24422442
(i32.const 72)
2443-
(i32.const 454)
2443+
(i32.const 514)
24442444
(i32.const 8)
24452445
)
24462446
(unreachable)
@@ -2559,7 +2559,7 @@
25592559
(call $~lib/env/abort
25602560
(i32.const 0)
25612561
(i32.const 72)
2562-
(i32.const 466)
2562+
(i32.const 526)
25632563
(i32.const 8)
25642564
)
25652565
(unreachable)
@@ -2645,7 +2645,7 @@
26452645
(call $~lib/env/abort
26462646
(i32.const 0)
26472647
(i32.const 72)
2648-
(i32.const 475)
2648+
(i32.const 535)
26492649
(i32.const 4)
26502650
)
26512651
(unreachable)

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,7 +2911,7 @@
29112911
(call $~lib/env/abort
29122912
(i32.const 0)
29132913
(i32.const 72)
2914-
(i32.const 450)
2914+
(i32.const 510)
29152915
(i32.const 8)
29162916
)
29172917
(unreachable)
@@ -2988,7 +2988,7 @@
29882988
(call $~lib/env/abort
29892989
(i32.const 0)
29902990
(i32.const 72)
2991-
(i32.const 454)
2991+
(i32.const 514)
29922992
(i32.const 8)
29932993
)
29942994
(unreachable)
@@ -3132,7 +3132,7 @@
31323132
(call $~lib/env/abort
31333133
(i32.const 0)
31343134
(i32.const 72)
3135-
(i32.const 466)
3135+
(i32.const 526)
31363136
(i32.const 8)
31373137
)
31383138
(unreachable)
@@ -3225,7 +3225,7 @@
32253225
(call $~lib/env/abort
32263226
(i32.const 0)
32273227
(i32.const 72)
3228-
(i32.const 475)
3228+
(i32.const 535)
32293229
(i32.const 4)
32303230
)
32313231
(unreachable)

0 commit comments

Comments
 (0)