Skip to content

Commit

Permalink
fix: harmonise TS and AS execution
Browse files Browse the repository at this point in the history
ref #17
  • Loading branch information
ColinEberhardt committed Feb 8, 2021
1 parent 4872cf6 commit bead49e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# prettier doesn't support decorators on functions :-(
assembly/char.ts
50 changes: 37 additions & 13 deletions assembly/char.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export const enum Char {
FormFeed = 0x0c,
CarriageReturn = 0x0d,
LineFeed = 0x0a,
Space = 0x20,
Dollar = 0x24, // "$"
LeftParenthesis = 0x28,
RightParenthesis = 0x29,
Expand All @@ -14,11 +15,13 @@ export const enum Char {
Minus = 0x2d, // "-"
Dot = 0x2e, // "."
Zero = 0x30,
Nine = 0x39,
Question = 0x3f, // "?"
A = 0x41,
D = 0x44,
S = 0x53,
W = 0x57,
Z = 0x5a,
LeftSquareBracket = 0x5b, // "["
Backslash = 0x5c, // "\"
RightSquareBracket = 0x5d, // "]"
Expand All @@ -35,40 +38,58 @@ export const enum Char {
v = 0x76,
w = 0x77,
x = 0x78,
z = 0x7a,
LeftCurlyBrace = 0x7b /* { */,
VerticalBar = 0x7c /* | */,
RightCurlyBrace = 0x7d /* */,
RightCurlyBrace = 0x7d /* { */,
NonBreakingSpace = 0xa0,
}

// @ts-ignore
@inline
function inRange(value: u32, from: u32, to: u32): bool {
if (ASC_TARGET == 1) {
// makes use of unsigned integer operations, making this
// approach a little faster when compiled to WASM
return value - from < (to - from + 1);
} else {
return value >= from && value <= to;
}
}

export function isDigit(code: u32): bool {
return code - Char.Zero < 10;
return inRange(code, Char.Zero, Char.Nine);
}

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

export function isLowercaseAlpha(code: u32): bool {
return code - Char.a < 26;
return inRange(code, Char.a, Char.z);
}

export function isUppercaseAlpha(code: u32): bool {
return code - Char.A < 26;
return inRange(code, Char.A, Char.Z);
}

export function isAlpha(code: u32): bool {
return (code | 32) - Char.a < 26;
if (ASC_TARGET == 1) {
return (code | 32) - Char.a < 26;
} else {
return inRange(code, Char.a, Char.z) || inRange(code, Char.A, Char.Z);
}
}

export function isWhitespace(code: u32): bool {
if (code < 0x1680) {
// < <LS> (1)
// <SP>, <TAB>, <LF>, <VT>, <FF>, <CR> and <NBSP>
// @ts-ignore: cast
return ((code | 0x80) == 0xa0) | (code - 0x09 <= 0x0d - 0x09);
}
if (code - 0x2000 <= 0x200a - 0x2000) return true;
switch (code) {
case Char.Space:
case Char.HorizontalTab:
case Char.VerticalTab:
case Char.FormFeed:
case Char.LineFeed:
case Char.CarriageReturn:
case Char.NonBreakingSpace:
case 0x1680: // <LS> (1)
case 0x2028: // <LS> (2)
case 0x2029: // <PS>
Expand All @@ -78,5 +99,8 @@ export function isWhitespace(code: u32): bool {
case 0xfeff:
return true; // <ZWNBSP>
}
if (inRange(code, 0x2000, 0x200a)) {
return true;
}
return false;
}

0 comments on commit bead49e

Please sign in to comment.