Skip to content

Commit bead49e

Browse files
fix: harmonise TS and AS execution
ref #17
1 parent 4872cf6 commit bead49e

File tree

2 files changed

+39
-13
lines changed

2 files changed

+39
-13
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# prettier doesn't support decorators on functions :-(
2+
assembly/char.ts

assembly/char.ts

+37-13
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ export const enum Char {
55
FormFeed = 0x0c,
66
CarriageReturn = 0x0d,
77
LineFeed = 0x0a,
8+
Space = 0x20,
89
Dollar = 0x24, // "$"
910
LeftParenthesis = 0x28,
1011
RightParenthesis = 0x29,
@@ -14,11 +15,13 @@ export const enum Char {
1415
Minus = 0x2d, // "-"
1516
Dot = 0x2e, // "."
1617
Zero = 0x30,
18+
Nine = 0x39,
1719
Question = 0x3f, // "?"
1820
A = 0x41,
1921
D = 0x44,
2022
S = 0x53,
2123
W = 0x57,
24+
Z = 0x5a,
2225
LeftSquareBracket = 0x5b, // "["
2326
Backslash = 0x5c, // "\"
2427
RightSquareBracket = 0x5d, // "]"
@@ -35,40 +38,58 @@ export const enum Char {
3538
v = 0x76,
3639
w = 0x77,
3740
x = 0x78,
41+
z = 0x7a,
3842
LeftCurlyBrace = 0x7b /* { */,
3943
VerticalBar = 0x7c /* | */,
40-
RightCurlyBrace = 0x7d /* */,
44+
RightCurlyBrace = 0x7d /* { */,
45+
NonBreakingSpace = 0xa0,
46+
}
47+
48+
// @ts-ignore
49+
@inline
50+
function inRange(value: u32, from: u32, to: u32): bool {
51+
if (ASC_TARGET == 1) {
52+
// makes use of unsigned integer operations, making this
53+
// approach a little faster when compiled to WASM
54+
return value - from < (to - from + 1);
55+
} else {
56+
return value >= from && value <= to;
57+
}
4158
}
4259

4360
export function isDigit(code: u32): bool {
44-
return code - Char.Zero < 10;
61+
return inRange(code, Char.Zero, Char.Nine);
4562
}
4663

4764
export function isHexadecimalDigit(code: u32): bool {
48-
return isDigit(code) || code - Char.a < 6;
65+
return isDigit(code) || inRange(code, Char.a, Char.f);
4966
}
5067

5168
export function isLowercaseAlpha(code: u32): bool {
52-
return code - Char.a < 26;
69+
return inRange(code, Char.a, Char.z);
5370
}
5471

5572
export function isUppercaseAlpha(code: u32): bool {
56-
return code - Char.A < 26;
73+
return inRange(code, Char.A, Char.Z);
5774
}
5875

5976
export function isAlpha(code: u32): bool {
60-
return (code | 32) - Char.a < 26;
77+
if (ASC_TARGET == 1) {
78+
return (code | 32) - Char.a < 26;
79+
} else {
80+
return inRange(code, Char.a, Char.z) || inRange(code, Char.A, Char.Z);
81+
}
6182
}
6283

6384
export function isWhitespace(code: u32): bool {
64-
if (code < 0x1680) {
65-
// < <LS> (1)
66-
// <SP>, <TAB>, <LF>, <VT>, <FF>, <CR> and <NBSP>
67-
// @ts-ignore: cast
68-
return ((code | 0x80) == 0xa0) | (code - 0x09 <= 0x0d - 0x09);
69-
}
70-
if (code - 0x2000 <= 0x200a - 0x2000) return true;
7185
switch (code) {
86+
case Char.Space:
87+
case Char.HorizontalTab:
88+
case Char.VerticalTab:
89+
case Char.FormFeed:
90+
case Char.LineFeed:
91+
case Char.CarriageReturn:
92+
case Char.NonBreakingSpace:
7293
case 0x1680: // <LS> (1)
7394
case 0x2028: // <LS> (2)
7495
case 0x2029: // <PS>
@@ -78,5 +99,8 @@ export function isWhitespace(code: u32): bool {
7899
case 0xfeff:
79100
return true; // <ZWNBSP>
80101
}
102+
if (inRange(code, 0x2000, 0x200a)) {
103+
return true;
104+
}
81105
return false;
82106
}

0 commit comments

Comments
 (0)