Skip to content

Commit 316b2d4

Browse files
MaxGraeydcodeIO
authored andcommitted
Improve isSpace util, refactor fromCharCode and fix String#trim{End} (AssemblyScript#692)
1 parent cf0f4f5 commit 316b2d4

File tree

7 files changed

+2962
-2137
lines changed

7 files changed

+2962
-2137
lines changed

std/assembly/string.ts

Lines changed: 13 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path="./rt/index.d.ts" />
22

33
import { BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE } from "./rt/common";
4-
import { compareImpl, strtol, strtod, isWhiteSpaceOrLineTerminator } from "./util/string";
4+
import { compareImpl, strtol, strtod, isSpace } from "./util/string";
55
import { E_INVALIDLENGTH } from "./util/error";
66
import { ArrayBufferView } from "./arraybuffer";
77
import { idof } from "./builtins";
@@ -11,23 +11,18 @@ import { idof } from "./builtins";
1111
@lazy static readonly MAX_LENGTH: i32 = BLOCK_MAXSIZE >>> alignof<u16>();
1212

1313
static fromCharCode(unit: i32, surr: i32 = -1): string {
14-
var out: usize;
15-
if (~surr) {
16-
out = __alloc(4, idof<string>());
17-
store<u16>(out, <u16>unit);
18-
store<u16>(out, <u16>surr, 2);
19-
} else {
20-
out = __alloc(2, idof<string>());
21-
store<u16>(out, <u16>unit);
22-
}
14+
var hasSur = surr > 0;
15+
var out = __alloc(2 << i32(hasSur), idof<string>());
16+
store<u16>(out, <u16>unit);
17+
if (hasSur) store<u16>(out, <u16>surr, 2);
2318
return changetype<string>(out); // retains
2419
}
2520

2621
static fromCodePoint(code: i32): string {
2722
assert(<u32>code <= 0x10FFFF);
28-
var sur = code > 0xFFFF;
29-
var out = __alloc((i32(sur) + 1) << 1, idof<string>());
30-
if (!sur) {
23+
var hasSur = code > 0xFFFF;
24+
var out = __alloc(2 << i32(hasSur), idof<string>());
25+
if (!hasSur) {
3126
store<u16>(out, <u16>code);
3227
} else {
3328
code -= 0x10000;
@@ -205,25 +200,15 @@ import { idof } from "./builtins";
205200
trim(): String {
206201
var length = this.length;
207202
var size: usize = length << 1;
208-
while (
209-
size &&
210-
isWhiteSpaceOrLineTerminator(
211-
load<u16>(changetype<usize>(this) + size)
212-
)
213-
) {
203+
while (size && isSpace(load<u16>(changetype<usize>(this) + size - 2))) {
214204
size -= 2;
215205
}
216206
var offset: usize = 0;
217-
while (
218-
offset < size &&
219-
isWhiteSpaceOrLineTerminator(
220-
load<u16>(changetype<usize>(this) + offset)
221-
)
222-
) {
207+
while (offset < size && isSpace(load<u16>(changetype<usize>(this) + offset))) {
223208
offset += 2; size -= 2;
224209
}
225210
if (!size) return changetype<String>("");
226-
if (!start && size == length << 1) return this;
211+
if (!offset && size == length << 1) return this;
227212
var out = __alloc(size, idof<String>());
228213
memory.copy(out, changetype<usize>(this) + offset, size);
229214
return changetype<String>(out); // retains
@@ -242,12 +227,7 @@ import { idof } from "./builtins";
242227
trimStart(): String {
243228
var size = <usize>this.length << 1;
244229
var offset: usize = 0;
245-
while (
246-
offset < size &&
247-
isWhiteSpaceOrLineTerminator(
248-
load<u16>(changetype<usize>(this) + offset)
249-
)
250-
) {
230+
while (offset < size && isSpace(load<u16>(changetype<usize>(this) + offset))) {
251231
offset += 2;
252232
}
253233
if (!offset) return this;
@@ -261,12 +241,7 @@ import { idof } from "./builtins";
261241
trimEnd(): String {
262242
var originalSize = <usize>this.length << 1;
263243
var size = originalSize;
264-
while (
265-
size &&
266-
isWhiteSpaceOrLineTerminator(
267-
load<u16>(changetype<usize>(this) + size)
268-
)
269-
) {
244+
while (size && isSpace(load<u16>(changetype<usize>(this) + size - 2))) {
270245
size -= 2;
271246
}
272247
if (!size) return changetype<String>("");

std/assembly/util/string.ts

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
export function compareImpl(str1: string, index1: usize, str2: string, index2: usize, len: usize): i32 {
2-
var result: i32 = 0;
2+
var result = 0;
33
var ptr1 = changetype<usize>(str1) + (index1 << 1);
44
var ptr2 = changetype<usize>(str2) + (index2 << 1);
55
while (len && !(result = <i32>load<u16>(ptr1) - <i32>load<u16>(ptr2))) {
@@ -30,7 +30,7 @@ export const enum CharCode {
3030
N = 0x4E,
3131
O = 0x4F,
3232
X = 0x58,
33-
Z = 0x5a,
33+
Z = 0x5A,
3434
a = 0x61,
3535
b = 0x62,
3636
e = 0x65,
@@ -40,20 +40,30 @@ export const enum CharCode {
4040
z = 0x7A
4141
}
4242

43-
export function isWhiteSpaceOrLineTerminator(c: i32): bool {
43+
export function isSpace(c: i32): bool {
44+
if (c <= 0xFF) {
45+
switch (c) {
46+
case 0x09: // <TAB>
47+
case 0x0A: // <LF>
48+
case 0x0B: // <VT>
49+
case 0x0C: // <FF>
50+
case 0x0D: // <CR>
51+
case 0x20: // <SP>
52+
case 0xA0: return true; // <NBSP>
53+
}
54+
return false;
55+
}
56+
if (c >= 0x2000 && c <= 0x200A) return true;
4457
switch (c) {
45-
case 9: // <TAB>
46-
case 10: // <LF>
47-
case 13: // <CR>
48-
case 11: // <VT>
49-
case 12: // <FF>
50-
case 32: // <SP>
51-
case 160: // <NBSP>
52-
case 8232: // <LS>
53-
case 8233: // <PS>
54-
case 65279: return true; // <ZWNBSP>
55-
default: return false;
58+
case 0x1680: // <LS> (1)
59+
case 0x2028: // <LS> (2)
60+
case 0x2029: // <PS>
61+
case 0x202F: // <NNS>
62+
case 0x205F: // <MMSP>
63+
case 0x3000: // <IS>
64+
case 0xFEFF: return true; // <ZWNBSP>
5665
}
66+
return false;
5767
}
5868

5969
/** Parses a string to an integer (usually), using the specified radix. */
@@ -69,7 +79,7 @@ export function strtol<T>(str: string, radix: i32 = 0): T {
6979
// @ts-ignore: cast
7080
var sign: T = 1;
7181
// trim white spaces
72-
while (isWhiteSpaceOrLineTerminator(code)) {
82+
while (isSpace(code)) {
7383
code = <i32>load<u16>(ptr += 2);
7484
--len;
7585
}
@@ -147,7 +157,7 @@ export function strtod(str: string): f64 {
147157
// determine sign
148158
var sign = 1.0;
149159
// trim white spaces
150-
while (isWhiteSpaceOrLineTerminator(code)) {
160+
while (isSpace(code)) {
151161
code = <i32>load<u16>(ptr += 2);
152162
--len;
153163
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2497,7 +2497,7 @@
24972497
if
24982498
i32.const 0
24992499
i32.const 480
2500-
i32.const 592
2500+
i32.const 567
25012501
i32.const 8
25022502
call $~lib/builtins/abort
25032503
unreachable
@@ -2520,7 +2520,7 @@
25202520
if
25212521
i32.const 0
25222522
i32.const 480
2523-
i32.const 596
2523+
i32.const 571
25242524
i32.const 8
25252525
call $~lib/builtins/abort
25262526
unreachable
@@ -2839,7 +2839,7 @@
28392839
if
28402840
i32.const 0
28412841
i32.const 480
2842-
i32.const 610
2842+
i32.const 585
28432843
i32.const 6
28442844
call $~lib/builtins/abort
28452845
unreachable

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4086,7 +4086,7 @@
40864086
if
40874087
i32.const 0
40884088
i32.const 480
4089-
i32.const 592
4089+
i32.const 567
40904090
i32.const 8
40914091
call $~lib/builtins/abort
40924092
unreachable
@@ -4110,7 +4110,7 @@
41104110
if
41114111
i32.const 0
41124112
i32.const 480
4113-
i32.const 596
4113+
i32.const 571
41144114
i32.const 8
41154115
call $~lib/builtins/abort
41164116
unreachable
@@ -4466,7 +4466,7 @@
44664466
if
44674467
i32.const 0
44684468
i32.const 480
4469-
i32.const 610
4469+
i32.const 585
44704470
i32.const 6
44714471
call $~lib/builtins/abort
44724472
unreachable

0 commit comments

Comments
 (0)