Skip to content

Commit 3bfdf55

Browse files
authored
Minor refactorings for numeric parsing in tokenizer (#2472)
1 parent 12b3f35 commit 3bfdf55

File tree

2 files changed

+30
-33
lines changed

2 files changed

+30
-33
lines changed

src/tokenizer.ts

+22-30
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
isIdentifierPart,
3232
isDecimal,
3333
isOctal,
34+
isHexBase,
3435
isHighSurrogate,
3536
isLowSurrogate
3637
} from "./util";
@@ -1313,30 +1314,24 @@ export class Tokenizer extends DiagnosticEmitter {
13131314
var end = this.end;
13141315
var start = pos;
13151316
var sepEnd = start;
1316-
var value = i64_new(0);
1317+
var value = i64_zero;
13171318
var i64_4 = i64_new(4);
13181319
var nextValue = value;
13191320
var overflowOccurred = false;
13201321

13211322
while (pos < end) {
13221323
let c = text.charCodeAt(pos);
1323-
if (c >= CharCode._0 && c <= CharCode._9) {
1324-
// value = (value << 4) + c - CharCode._0;
1324+
if (isDecimal(c)) {
1325+
// (value << 4) + c - CharCode._0
13251326
nextValue = i64_add(
13261327
i64_shl(value, i64_4),
13271328
i64_new(c - CharCode._0)
13281329
);
1329-
} else if (c >= CharCode.A && c <= CharCode.F) {
1330-
// value = (value << 4) + 10 + c - CharCode.A;
1330+
} else if (isHexBase(c)) {
1331+
// (value << 4) + (c | 32) + (10 - CharCode.a)
13311332
nextValue = i64_add(
13321333
i64_shl(value, i64_4),
1333-
i64_new(10 + c - CharCode.A)
1334-
);
1335-
} else if (c >= CharCode.a && c <= CharCode.f) {
1336-
// value = (value << 4) + 10 + c - CharCode.a;
1337-
nextValue = i64_add(
1338-
i64_shl(value, i64_4),
1339-
i64_new(10 + c - CharCode.a)
1334+
i64_new((c | 32) + (10 - CharCode.a))
13401335
);
13411336
} else if (c == CharCode._) {
13421337
if (sepEnd == pos) {
@@ -1386,14 +1381,14 @@ export class Tokenizer extends DiagnosticEmitter {
13861381
var end = this.end;
13871382
var start = pos;
13881383
var sepEnd = start;
1389-
var value = i64_new(0);
1384+
var value = i64_zero;
13901385
var i64_10 = i64_new(10);
13911386
var nextValue = value;
13921387
var overflowOccurred = false;
13931388

13941389
while (pos < end) {
13951390
let c = text.charCodeAt(pos);
1396-
if (c >= CharCode._0 && c <= CharCode._9) {
1391+
if (isDecimal(c)) {
13971392
// value = value * 10 + c - CharCode._0;
13981393
nextValue = i64_add(
13991394
i64_mul(value, i64_10),
@@ -1451,15 +1446,15 @@ export class Tokenizer extends DiagnosticEmitter {
14511446
var end = this.end;
14521447
var start = pos;
14531448
var sepEnd = start;
1454-
var value = i64_new(0);
1449+
var value = i64_zero;
14551450
var i64_3 = i64_new(3);
14561451
var nextValue = value;
14571452
var overflowOccurred = false;
14581453

14591454
while (pos < end) {
14601455
let c = text.charCodeAt(pos);
1461-
if (c >= CharCode._0 && c <= CharCode._7) {
1462-
// value = (value << 3) + c - CharCode._0;
1456+
if (isOctal(c)) {
1457+
// (value << 3) + c - CharCode._0
14631458
nextValue = i64_add(
14641459
i64_shl(value, i64_3),
14651460
i64_new(c - CharCode._0)
@@ -1511,21 +1506,20 @@ export class Tokenizer extends DiagnosticEmitter {
15111506
var end = this.end;
15121507
var start = pos;
15131508
var sepEnd = start;
1514-
var value = i64_new(0);
1515-
var i64_1 = i64_new(1);
1509+
var value = i64_zero;
15161510
var nextValue = value;
15171511
var overflowOccurred = false;
15181512

15191513
while (pos < end) {
15201514
let c = text.charCodeAt(pos);
15211515
if (c == CharCode._0) {
1522-
// value = (value << 1);
1523-
nextValue = i64_shl(value, i64_1);
1516+
// value << 1 | 0
1517+
nextValue = i64_shl(value, i64_one);
15241518
} else if (c == CharCode._1) {
1525-
// value = (value << 1) + 1;
1526-
nextValue = i64_add(
1527-
i64_shl(value, i64_1),
1528-
i64_1
1519+
// value << 1 | 1
1520+
nextValue = i64_or(
1521+
i64_shl(value, i64_one),
1522+
i64_one
15291523
);
15301524
} else if (c == CharCode._) {
15311525
if (sepEnd == pos) {
@@ -1665,12 +1659,10 @@ export class Tokenizer extends DiagnosticEmitter {
16651659
var end = this.end;
16661660
while (pos < end) {
16671661
let c = text.charCodeAt(pos++);
1668-
if (c >= CharCode._0 && c <= CharCode._9) {
1662+
if (isDecimal(c)) {
16691663
value = (value << 4) + c - CharCode._0;
1670-
} else if (c >= CharCode.A && c <= CharCode.F) {
1671-
value = (value << 4) + c + (10 - CharCode.A);
1672-
} else if (c >= CharCode.a && c <= CharCode.f) {
1673-
value = (value << 4) + c + (10 - CharCode.a);
1664+
} else if (isHexBase(c)) {
1665+
value = (value << 4) + (c | 32) + (10 - CharCode.a);
16741666
} else if (~startIfTaggedTemplate) {
16751667
this.pos = --pos;
16761668
return text.substring(startIfTaggedTemplate, pos);

src/util/text.ts

+8-3
Original file line numberDiff line numberDiff line change
@@ -234,10 +234,15 @@ export function isOctal(c: i32): bool {
234234
return c >= CharCode._0 && c <= CharCode._7;
235235
}
236236

237-
/** Tests if the specified character code is a valid hexadecimal digit. */
238-
export function isHex(c: i32): bool {
237+
/** Tests if the specified character code is a valid hexadecimal symbol [a-f]. */
238+
export function isHexBase(c: i32): bool {
239239
let c0 = c | 32; // unify uppercases and lowercases a|A - f|F
240-
return isDecimal(c) || (c0 >= CharCode.a && c0 <= CharCode.f);
240+
return c0 >= CharCode.a && c0 <= CharCode.f;
241+
}
242+
243+
/** Tests if the specified character code is a valid hexadecimal digit. */
244+
export function isHexOrDecimal(c: i32): bool {
245+
return isDecimal(c) || isHexBase(c);
241246
}
242247

243248
/** Tests if the specified character code is trivially alphanumeric. */

0 commit comments

Comments
 (0)