Skip to content

Commit

Permalink
Improve isSpace util, refactor fromCharCode and fix String#trim{End} (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxGraey authored and dcodeIO committed Jun 27, 2019
1 parent cf0f4f5 commit 316b2d4
Show file tree
Hide file tree
Showing 7 changed files with 2,962 additions and 2,137 deletions.
51 changes: 13 additions & 38 deletions std/assembly/string.ts
@@ -1,7 +1,7 @@
/// <reference path="./rt/index.d.ts" />

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

static fromCharCode(unit: i32, surr: i32 = -1): string {
var out: usize;
if (~surr) {
out = __alloc(4, idof<string>());
store<u16>(out, <u16>unit);
store<u16>(out, <u16>surr, 2);
} else {
out = __alloc(2, idof<string>());
store<u16>(out, <u16>unit);
}
var hasSur = surr > 0;
var out = __alloc(2 << i32(hasSur), idof<string>());
store<u16>(out, <u16>unit);
if (hasSur) store<u16>(out, <u16>surr, 2);
return changetype<string>(out); // retains
}

static fromCodePoint(code: i32): string {
assert(<u32>code <= 0x10FFFF);
var sur = code > 0xFFFF;
var out = __alloc((i32(sur) + 1) << 1, idof<string>());
if (!sur) {
var hasSur = code > 0xFFFF;
var out = __alloc(2 << i32(hasSur), idof<string>());
if (!hasSur) {
store<u16>(out, <u16>code);
} else {
code -= 0x10000;
Expand Down Expand Up @@ -205,25 +200,15 @@ import { idof } from "./builtins";
trim(): String {
var length = this.length;
var size: usize = length << 1;
while (
size &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + size)
)
) {
while (size && isSpace(load<u16>(changetype<usize>(this) + size - 2))) {
size -= 2;
}
var offset: usize = 0;
while (
offset < size &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + offset)
)
) {
while (offset < size && isSpace(load<u16>(changetype<usize>(this) + offset))) {
offset += 2; size -= 2;
}
if (!size) return changetype<String>("");
if (!start && size == length << 1) return this;
if (!offset && size == length << 1) return this;
var out = __alloc(size, idof<String>());
memory.copy(out, changetype<usize>(this) + offset, size);
return changetype<String>(out); // retains
Expand All @@ -242,12 +227,7 @@ import { idof } from "./builtins";
trimStart(): String {
var size = <usize>this.length << 1;
var offset: usize = 0;
while (
offset < size &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + offset)
)
) {
while (offset < size && isSpace(load<u16>(changetype<usize>(this) + offset))) {
offset += 2;
}
if (!offset) return this;
Expand All @@ -261,12 +241,7 @@ import { idof } from "./builtins";
trimEnd(): String {
var originalSize = <usize>this.length << 1;
var size = originalSize;
while (
size &&
isWhiteSpaceOrLineTerminator(
load<u16>(changetype<usize>(this) + size)
)
) {
while (size && isSpace(load<u16>(changetype<usize>(this) + size - 2))) {
size -= 2;
}
if (!size) return changetype<String>("");
Expand Down
42 changes: 26 additions & 16 deletions std/assembly/util/string.ts
@@ -1,5 +1,5 @@
export function compareImpl(str1: string, index1: usize, str2: string, index2: usize, len: usize): i32 {
var result: i32 = 0;
var result = 0;
var ptr1 = changetype<usize>(str1) + (index1 << 1);
var ptr2 = changetype<usize>(str2) + (index2 << 1);
while (len && !(result = <i32>load<u16>(ptr1) - <i32>load<u16>(ptr2))) {
Expand Down Expand Up @@ -30,7 +30,7 @@ export const enum CharCode {
N = 0x4E,
O = 0x4F,
X = 0x58,
Z = 0x5a,
Z = 0x5A,
a = 0x61,
b = 0x62,
e = 0x65,
Expand All @@ -40,20 +40,30 @@ export const enum CharCode {
z = 0x7A
}

export function isWhiteSpaceOrLineTerminator(c: i32): bool {
export function isSpace(c: i32): bool {
if (c <= 0xFF) {
switch (c) {
case 0x09: // <TAB>
case 0x0A: // <LF>
case 0x0B: // <VT>
case 0x0C: // <FF>
case 0x0D: // <CR>
case 0x20: // <SP>
case 0xA0: return true; // <NBSP>
}
return false;
}
if (c >= 0x2000 && c <= 0x200A) return true;
switch (c) {
case 9: // <TAB>
case 10: // <LF>
case 13: // <CR>
case 11: // <VT>
case 12: // <FF>
case 32: // <SP>
case 160: // <NBSP>
case 8232: // <LS>
case 8233: // <PS>
case 65279: return true; // <ZWNBSP>
default: return false;
case 0x1680: // <LS> (1)
case 0x2028: // <LS> (2)
case 0x2029: // <PS>
case 0x202F: // <NNS>
case 0x205F: // <MMSP>
case 0x3000: // <IS>
case 0xFEFF: return true; // <ZWNBSP>
}
return false;
}

/** Parses a string to an integer (usually), using the specified radix. */
Expand All @@ -69,7 +79,7 @@ export function strtol<T>(str: string, radix: i32 = 0): T {
// @ts-ignore: cast
var sign: T = 1;
// trim white spaces
while (isWhiteSpaceOrLineTerminator(code)) {
while (isSpace(code)) {
code = <i32>load<u16>(ptr += 2);
--len;
}
Expand Down Expand Up @@ -147,7 +157,7 @@ export function strtod(str: string): f64 {
// determine sign
var sign = 1.0;
// trim white spaces
while (isWhiteSpaceOrLineTerminator(code)) {
while (isSpace(code)) {
code = <i32>load<u16>(ptr += 2);
--len;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/std/string-encoding.optimized.wat
Expand Up @@ -2497,7 +2497,7 @@
if
i32.const 0
i32.const 480
i32.const 592
i32.const 567
i32.const 8
call $~lib/builtins/abort
unreachable
Expand All @@ -2520,7 +2520,7 @@
if
i32.const 0
i32.const 480
i32.const 596
i32.const 571
i32.const 8
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -2839,7 +2839,7 @@
if
i32.const 0
i32.const 480
i32.const 610
i32.const 585
i32.const 6
call $~lib/builtins/abort
unreachable
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/std/string-encoding.untouched.wat
Expand Up @@ -4086,7 +4086,7 @@
if
i32.const 0
i32.const 480
i32.const 592
i32.const 567
i32.const 8
call $~lib/builtins/abort
unreachable
Expand All @@ -4110,7 +4110,7 @@
if
i32.const 0
i32.const 480
i32.const 596
i32.const 571
i32.const 8
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -4466,7 +4466,7 @@
if
i32.const 0
i32.const 480
i32.const 610
i32.const 585
i32.const 6
call $~lib/builtins/abort
unreachable
Expand Down

0 comments on commit 316b2d4

Please sign in to comment.