Skip to content

Commit ab5a480

Browse files
authored
Refactor text utils (#2144)
1 parent 52255f5 commit ab5a480

File tree

3 files changed

+30
-39
lines changed

3 files changed

+30
-39
lines changed

src/ast.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import {
3030
normalizePath,
3131
resolvePath,
3232
CharCode,
33-
isTrivialAlphanum
33+
isAlphaOrDecimal
3434
} from "./util";
3535

3636
import {
@@ -2347,7 +2347,7 @@ export function mangleInternalPath(path: string): string {
23472347
if (pos >= 0 && len - pos >= 2) { // at least one char plus dot
23482348
let cur = pos;
23492349
while (++cur < len) {
2350-
if (!isTrivialAlphanum(path.charCodeAt(cur))) {
2350+
if (!isAlphaOrDecimal(path.charCodeAt(cur))) {
23512351
assert(false); // not a valid external path
23522352
return path;
23532353
}

src/tokenizer.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ import {
2828
isWhiteSpace,
2929
isIdentifierStart,
3030
isIdentifierPart,
31-
isDecimalDigit,
32-
isOctalDigit
31+
isDecimal,
32+
isOctal
3333
} from "./util";
3434

3535
/** Named token types. */
@@ -703,7 +703,7 @@ export class Tokenizer extends DiagnosticEmitter {
703703
++pos;
704704
if (maxTokenLength > 1 && pos < end) {
705705
let chr = text.charCodeAt(pos);
706-
if (isDecimalDigit(chr)) {
706+
if (isDecimal(chr)) {
707707
this.pos = pos - 1;
708708
return Token.FLOATLITERAL; // expects a call to readFloat
709709
}
@@ -1177,7 +1177,7 @@ export class Tokenizer extends DiagnosticEmitter {
11771177
var c = text.charCodeAt(this.pos++);
11781178
switch (c) {
11791179
case CharCode._0: {
1180-
if (isTaggedTemplate && this.pos < end && isDecimalDigit(text.charCodeAt(this.pos))) {
1180+
if (isTaggedTemplate && this.pos < end && isDecimal(text.charCodeAt(this.pos))) {
11811181
++this.pos;
11821182
return text.substring(start, this.pos);
11831183
}
@@ -1331,7 +1331,7 @@ export class Tokenizer extends DiagnosticEmitter {
13311331
return this.readOctalInteger();
13321332
}
13331333
}
1334-
if (isOctalDigit(text.charCodeAt(pos + 1))) {
1334+
if (isOctal(text.charCodeAt(pos + 1))) {
13351335
let start = pos;
13361336
this.pos = pos + 1;
13371337
let value = this.readOctalInteger();
@@ -1578,7 +1578,7 @@ export class Tokenizer extends DiagnosticEmitter {
15781578
if (
15791579
++this.pos < end &&
15801580
(c = text.charCodeAt(this.pos)) == CharCode.MINUS || c == CharCode.PLUS &&
1581-
isDecimalDigit(text.charCodeAt(this.pos + 1))
1581+
isDecimal(text.charCodeAt(this.pos + 1))
15821582
) {
15831583
++this.pos;
15841584
}
@@ -1618,7 +1618,7 @@ export class Tokenizer extends DiagnosticEmitter {
16181618
}
16191619
sepEnd = pos + 1;
16201620
++sepCount;
1621-
} else if (!isDecimalDigit(c)) {
1621+
} else if (!isDecimal(c)) {
16221622
break;
16231623
}
16241624
++pos;

src/util/text.ts

Lines changed: 21 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ export const enum CharCode {
139139
}
140140

141141
/** Tests if the specified character code is some sort of line break. */
142-
export function isLineBreak(c: CharCode): bool {
142+
export function isLineBreak(c: i32): bool {
143143
switch (c) {
144144
case CharCode.LINEFEED:
145145
case CharCode.CARRIAGERETURN:
@@ -175,45 +175,48 @@ export function isWhiteSpace(c: i32): bool {
175175
}
176176
}
177177

178+
export function isAlpha(c: i32): bool {
179+
let c0 = c | 32; // unify uppercases and lowercases a|A - z|Z
180+
return c0 >= CharCode.a && c0 <= CharCode.z;
181+
}
182+
178183
/** Tests if the specified character code is a valid decimal digit. */
179-
export function isDecimalDigit(c: i32): bool {
184+
export function isDecimal(c: i32): bool {
180185
return c >= CharCode._0 && c <= CharCode._9;
181186
}
182187

183188
/** Tests if the specified character code is a valid octal digit. */
184-
export function isOctalDigit(c: i32): bool {
189+
export function isOctal(c: i32): bool {
185190
return c >= CharCode._0 && c <= CharCode._7;
186191
}
187192

188193
/** Tests if the specified character code is a valid hexadecimal digit. */
189-
export function isHexDigit(c: i32): bool {
190-
return isDecimalDigit(c) || ((c | 32) >= CharCode.a && (c | 32) <= CharCode.f);
194+
export function isHex(c: i32): bool {
195+
let c0 = c | 32; // unify uppercases and lowercases a|A - f|F
196+
return isDecimal(c) || (c0 >= CharCode.a && c0 <= CharCode.f);
191197
}
192198

193199
/** Tests if the specified character code is trivially alphanumeric. */
194-
export function isTrivialAlphanum(code: i32): bool {
195-
return code >= CharCode.a && code <= CharCode.z
196-
|| code >= CharCode.A && code <= CharCode.Z
197-
|| code >= CharCode._0 && code <= CharCode._9;
200+
export function isAlphaOrDecimal(c: i32): bool {
201+
return isAlpha(c) || isDecimal(c);
198202
}
199203

200204
/** Tests if the specified character code is a valid start of an identifier. */
201205
export function isIdentifierStart(c: i32): bool {
202-
let c0 = c | 32; // unify uppercases and lowercases a|A - z|Z
203-
return c0 >= CharCode.a && c0 <= CharCode.z
206+
return isAlpha(c)
204207
|| c == CharCode._
205208
|| c == CharCode.DOLLAR
206-
|| c > 0x7F && isUnicodeIdentifierStart(c);
209+
|| c >= 170 && c <= 65500
210+
&& lookupInUnicodeMap(c as u16, unicodeIdentifierStart);
207211
}
208212

209213
/** Tests if the specified character code is a valid part of an identifier. */
210214
export function isIdentifierPart(c: i32): bool {
211-
const c0 = c | 32; // unify uppercases and lowercases a|A - z|Z
212-
return c0 >= CharCode.a && c0 <= CharCode.z
213-
|| c >= CharCode._0 && c <= CharCode._9
215+
return isAlphaOrDecimal(c)
214216
|| c == CharCode._
215217
|| c == CharCode.DOLLAR
216-
|| c > 0x7F && isUnicodeIdentifierPart(c);
218+
|| c >= 170 && c <= 65500
219+
&& lookupInUnicodeMap(c as u16, unicodeIdentifierPart);
217220
}
218221

219222
// storing as u16 to save memory
@@ -354,15 +357,13 @@ const unicodeIdentifierPart: u16[] = [
354357
];
355358

356359
function lookupInUnicodeMap(code: u16, map: u16[]): bool {
357-
if (code < map[0]) return false;
358-
359360
var lo = 0;
360361
var hi = map.length;
361-
var mid: i32;
362+
var mid: u32;
362363
var midVal: u16;
363364

364365
while (lo + 1 < hi) {
365-
mid = lo + ((hi - lo) >> 1);
366+
mid = lo + ((hi - lo) >>> 1);
366367
mid -= (mid & 1);
367368
midVal = map[mid];
368369
if (midVal <= code && code <= map[mid + 1]) {
@@ -377,16 +378,6 @@ function lookupInUnicodeMap(code: u16, map: u16[]): bool {
377378
return false;
378379
}
379380

380-
function isUnicodeIdentifierStart(code: i32): bool {
381-
return code < 170 || code > 65500 ? false :
382-
lookupInUnicodeMap(code as u16, unicodeIdentifierStart);
383-
}
384-
385-
function isUnicodeIdentifierPart(code: i32): bool {
386-
return code < 170 || code > 65500 ? false :
387-
lookupInUnicodeMap(code as u16, unicodeIdentifierPart);
388-
}
389-
390381
const indentX1 = " ";
391382
const indentX2 = " ";
392383
const indentX4 = " ";

0 commit comments

Comments
 (0)