Skip to content

Commit c4bbbbc

Browse files
authored
replace string slices with startsWith comparison (#9)
1 parent 4d56d33 commit c4bbbbc

File tree

1 file changed

+40
-40
lines changed

1 file changed

+40
-40
lines changed

lexer.js

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ function parseSource (cjsSource) {
7979
if (openTokenDepth === 0) {
8080
switch (ch) {
8181
case 105/*i*/:
82-
if (source.slice(pos + 1, pos + 6) === 'mport' && keywordStart(pos))
82+
if (source.startsWith('mport', pos + 1) && keywordStart(pos))
8383
throwIfImportStatement();
8484
lastTokenPos = pos;
8585
continue;
@@ -90,9 +90,9 @@ function parseSource (cjsSource) {
9090
lastTokenPos = pos;
9191
continue;
9292
case 95/*_*/:
93-
if (source.slice(pos + 1, pos + 8) === '_export' && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) {
93+
if (source.startsWith('_export', pos + 1) && (keywordStart(pos) || source.charCodeAt(pos - 1) === 46/*.*/)) {
9494
pos += 8;
95-
if (source.slice(pos, pos + 4) === 'Star')
95+
if (source.startsWith('Star', pos))
9696
pos += 4;
9797
if (source.charCodeAt(pos) === 40/*(*/) {
9898
openTokenPosStack[openTokenDepth++] = lastTokenPos;
@@ -107,23 +107,23 @@ function parseSource (cjsSource) {
107107

108108
switch (ch) {
109109
case 101/*e*/:
110-
if (source.slice(pos + 1, pos + 6) === 'xport' && keywordStart(pos)) {
110+
if (source.startsWith('xport', pos + 1) && keywordStart(pos)) {
111111
if (source.charCodeAt(pos + 6) === 115/*s*/)
112112
tryParseExportsDotAssign(false);
113113
else if (openTokenDepth === 0)
114114
throwIfExportStatement();
115115
}
116116
break;
117117
case 99/*c*/:
118-
if (keywordStart(pos) && source.slice(pos + 1, pos + 5) === 'lass' && isBrOrWs(source.charCodeAt(pos + 5)))
118+
if (keywordStart(pos) && source.startsWith('lass', pos + 1) && isBrOrWs(source.charCodeAt(pos + 5)))
119119
nextBraceIsClass = true;
120120
break;
121121
case 109/*m*/:
122-
if (source.slice(pos + 1, pos + 6) === 'odule' && keywordStart(pos))
122+
if (source.startsWith('odule', pos + 1) && keywordStart(pos))
123123
tryParseModuleExportsDotAssign();
124124
break;
125125
case 79/*O*/:
126-
if (source.slice(pos + 1, pos + 6) === 'bject' && keywordStart(pos))
126+
if (source.startsWith('bject', pos + 1) && keywordStart(pos))
127127
tryParseObjectDefineOrKeys(openTokenDepth === 0);
128128
break;
129129
case 40/*(*/:
@@ -233,11 +233,11 @@ function tryBacktrackAddStarExportBinding (bPos) {
233233
bPos--;
234234
switch (source.charCodeAt(bPos)) {
235235
case 114/*r*/:
236-
if (source.slice(bPos - 2, bPos) !== 'va')
236+
if (!source.startsWith('va', bPos - 2))
237237
return;
238238
break;
239239
case 116/*t*/:
240-
if (source.slice(bPos - 2, bPos) !== 'le' && source.slice(bPos - 4, bPos) !== 'cons')
240+
if (!source.startsWith('le', bPos - 2) && !source.startsWith('cons', bPos - 4))
241241
return;
242242
break;
243243
default: return;
@@ -254,7 +254,7 @@ function tryParseObjectDefineOrKeys (keys) {
254254
if (ch === 46/*.*/) {
255255
pos++;
256256
ch = commentWhitespace();
257-
if (ch === 100/*d*/ && source.slice(pos + 1, pos + 14) === 'efineProperty') {
257+
if (ch === 100/*d*/ && source.startsWith('efineProperty', pos + 1)) {
258258
pos += 14;
259259
revertPos = pos - 1;
260260
ch = commentWhitespace();
@@ -279,7 +279,7 @@ function tryParseObjectDefineOrKeys (keys) {
279279
}
280280
}
281281
}
282-
else if (keys && ch === 107/*k*/ && source.slice(pos + 1, pos + 4) === 'eys') {
282+
else if (keys && ch === 107/*k*/ && source.startsWith('eys', pos + 1)) {
283283
while (true) {
284284
pos += 4;
285285
revertPos = pos - 1;
@@ -298,14 +298,14 @@ function tryParseObjectDefineOrKeys (keys) {
298298
if (ch !== 46/*.*/) break;
299299
pos++;
300300
ch = commentWhitespace();
301-
if (ch !== 102/*f*/ || source.slice(pos + 1, pos + 7) !== 'orEach') break;
301+
if (ch !== 102/*f*/ || !source.startsWith('orEach', pos + 1)) break;
302302
pos += 7;
303303
ch = commentWhitespace();
304304
revertPos = pos - 1;
305305
if (ch !== 40/*(*/) break;
306306
pos++;
307307
ch = commentWhitespace();
308-
if (ch !== 102/*f*/ || source.slice(pos + 1, pos + 8) !== 'unction') break;
308+
if (ch !== 102/*f*/ || !source.startsWith('unction', pos + 1)) break;
309309
pos += 8;
310310
ch = commentWhitespace();
311311
if (ch !== 40/*(*/) break;
@@ -321,23 +321,23 @@ function tryParseObjectDefineOrKeys (keys) {
321321
if (ch !== 123/*{*/) break;
322322
pos++;
323323
ch = commentWhitespace();
324-
if (ch !== 105/*i*/ || source.slice(pos + 1, pos + 3) !== 'f ') break;
324+
if (ch !== 105/*i*/ || !source.startsWith('f ', pos + 1)) break;
325325
pos += 3;
326326
ch = commentWhitespace();
327327
if (ch !== 40/*(*/) break;
328328
pos++;
329329
ch = commentWhitespace();
330-
if (it_id !== source.slice(pos, pos + it_id.length)) break;
330+
if (!source.startsWith(it_id, pos)) break;
331331
pos += it_id.length;
332332
ch = commentWhitespace();
333333
// `if (` IDENTIFIER$2 `===` ( `'default'` | `"default"` ) `||` IDENTIFIER$2 `===` ( '__esModule' | `"__esModule"` ) `) return` `;`? |
334334
if (ch === 61/*=*/) {
335-
if (source.slice(pos + 1, pos + 3) !== '==') break;
335+
if (!source.startsWith('==', pos + 1)) break;
336336
pos += 3;
337337
ch = commentWhitespace();
338338
if (ch !== 34/*"*/ && ch !== 39/*'*/) break;
339339
let quot = ch;
340-
if (source.slice(pos + 1, pos + 8) !== 'default') break;
340+
if (!source.startsWith('default', pos + 1)) break;
341341
pos += 8;
342342
ch = commentWhitespace();
343343
if (ch !== quot) break;
@@ -354,7 +354,7 @@ function tryParseObjectDefineOrKeys (keys) {
354354
ch = commentWhitespace();
355355
if (ch !== 34/*"*/ && ch !== 39/*'*/) break;
356356
quot = ch;
357-
if (source.slice(pos + 1, pos + 11) !== '__esModule') break;
357+
if (!source.startsWith('__esModule', pos + 1)) break;
358358
pos += 11;
359359
ch = commentWhitespace();
360360
if (ch !== quot) break;
@@ -363,7 +363,7 @@ function tryParseObjectDefineOrKeys (keys) {
363363
if (ch !== 41/*)*/) break;
364364
pos += 1;
365365
ch = commentWhitespace();
366-
if (ch !== 114/*r*/ || source.slice(pos + 1, pos + 6) !== 'eturn') break;
366+
if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break;
367367
pos += 6;
368368
ch = commentWhitespace();
369369
if (ch === 59/*;*/)
@@ -372,12 +372,12 @@ function tryParseObjectDefineOrKeys (keys) {
372372
}
373373
// `if (` IDENTIFIER$2 `!==` ( `'default'` | `"default"` ) `)`
374374
else if (ch === 33/*!*/) {
375-
if (source.slice(pos + 1, pos + 3) !== '==') break;
375+
if (!source.startsWith('==', pos + 1)) break;
376376
pos += 3;
377377
ch = commentWhitespace();
378378
if (ch !== 34/*"*/ && ch !== 39/*'*/) break;
379379
const quot = ch;
380-
if (source.slice(pos + 1, pos + 8) !== 'default') break;
380+
if (!source.startsWith('default', pos + 1)) break;
381381
pos += 8;
382382
ch = commentWhitespace();
383383
if (ch !== quot) break;
@@ -429,7 +429,7 @@ function tryParseObjectDefineOrKeys (keys) {
429429
if (ch !== 46/*.*/) break;
430430
pos++;
431431
ch = commentWhitespace();
432-
if (ch !== 100/*d*/ || source.slice(pos + 1, pos + 14) !== 'efineProperty') break;
432+
if (ch !== 100/*d*/ || !source.startsWith('efineProperty', pos + 1)) break;
433433
pos += 14;
434434
ch = commentWhitespace();
435435
if (ch !== 40/*(*/) break;
@@ -440,7 +440,7 @@ function tryParseObjectDefineOrKeys (keys) {
440440
if (ch !== 44/*,*/) break;
441441
pos++;
442442
ch = commentWhitespace();
443-
if (source.slice(pos, pos + it_id.length) !== it_id) break;
443+
if (!source.startsWith(it_id, pos)) break;
444444
pos += it_id.length;
445445
ch = commentWhitespace();
446446
if (ch !== 44/*,*/) break;
@@ -449,25 +449,25 @@ function tryParseObjectDefineOrKeys (keys) {
449449
if (ch !== 123/*{*/) break;
450450
pos++;
451451
ch = commentWhitespace();
452-
if (ch !== 101/*e*/ || source.slice(pos + 1, pos + 10) !== 'numerable') break;
452+
if (ch !== 101/*e*/ || !source.startsWith('numerable', pos + 1)) break;
453453
pos += 10;
454454
ch = commentWhitespace();
455455
if (ch !== 58/*:*/) break;
456456
pos++;
457457
ch = commentWhitespace();
458-
if (ch !== 116/*t*/ && source.slice(pos + 1, pos + 4) !== 'rue') break;
458+
if (ch !== 116/*t*/ && !source.startsWith('rue', pos + 1)) break;
459459
pos += 4;
460460
ch = commentWhitespace();
461461
if (ch !== 44/*,*/) break;
462462
pos++;
463463
ch = commentWhitespace();
464-
if (ch !== 103/*g*/ || source.slice(pos + 1, pos + 3) !== 'et') break;
464+
if (ch !== 103/*g*/ || !source.startsWith('et', pos + 1)) break;
465465
pos += 3;
466466
ch = commentWhitespace();
467467
if (ch !== 58/*:*/) break;
468468
pos++;
469469
ch = commentWhitespace();
470-
if (ch !== 102/*f*/ || source.slice(pos + 1, pos + 8) !== 'unction') break;
470+
if (ch !== 102/*f*/ || !source.startsWith('unction', pos + 1)) break;
471471
pos += 8;
472472
ch = commentWhitespace();
473473
if (ch !== 40/*(*/) break;
@@ -479,16 +479,16 @@ function tryParseObjectDefineOrKeys (keys) {
479479
if (ch !== 123/*{*/) break;
480480
pos++;
481481
ch = commentWhitespace();
482-
if (ch !== 114/*r*/ || source.slice(pos + 1, pos + 6) !== 'eturn') break;
482+
if (ch !== 114/*r*/ || !source.startsWith('eturn', pos + 1)) break;
483483
pos += 6;
484484
ch = commentWhitespace();
485-
if (source.slice(pos, pos + id.length) !== id) break;
485+
if (!source.startsWith(id, pos)) break;
486486
pos += id.length;
487487
ch = commentWhitespace();
488488
if (ch !== 91/*[*/) break;
489489
pos++;
490490
ch = commentWhitespace();
491-
if (source.slice(pos, pos + it_id.length) !== it_id) break;
491+
if (!source.startsWith(it_id, pos)) break;
492492
pos += it_id.length;
493493
ch = commentWhitespace();
494494
if (ch !== 93/*]*/) break;
@@ -534,7 +534,7 @@ function tryParseObjectDefineOrKeys (keys) {
534534

535535
function readExportsOrModuleDotExports (ch) {
536536
const revertPos = pos;
537-
if (ch === 109/*m*/ && source.slice(pos + 1, pos + 6) === 'odule') {
537+
if (ch === 109/*m*/ && source.startsWith('odule', pos + 1)) {
538538
pos += 6;
539539
ch = commentWhitespace();
540540
if (ch !== 46/*.*/) {
@@ -544,7 +544,7 @@ function readExportsOrModuleDotExports (ch) {
544544
pos++;
545545
ch = commentWhitespace();
546546
}
547-
if (ch === 101/*e*/ && source.slice(pos + 1, pos + 7) === 'xports') {
547+
if (ch === 101/*e*/ && source.startsWith('xports', pos + 1)) {
548548
pos += 7;
549549
return true;
550550
}
@@ -561,7 +561,7 @@ function tryParseModuleExportsDotAssign () {
561561
if (ch === 46/*.*/) {
562562
pos++;
563563
ch = commentWhitespace();
564-
if (ch === 101/*e*/ && source.slice(pos + 1, pos + 7) === 'xports') {
564+
if (ch === 101/*e*/ && source.startsWith('xports', pos + 1)) {
565565
tryParseExportsDotAssign(true);
566566
return;
567567
}
@@ -632,7 +632,7 @@ function tryParseExportsDotAssign (assign) {
632632

633633
function tryParseRequire (directStarExport) {
634634
// require('...')
635-
if (source.slice(pos + 1, pos + 7) === 'equire') {
635+
if (source.startsWith('equire', pos + 1)) {
636636
pos += 7;
637637
const revertPos = pos - 1;
638638
let ch = commentWhitespace();
@@ -1029,7 +1029,7 @@ function keywordStart (pos) {
10291029
function readPrecedingKeyword (pos, match) {
10301030
if (pos < match.length - 1)
10311031
return false;
1032-
return source.slice(pos - match.length + 1, pos + 1) === match && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - match.length)));
1032+
return source.startsWith(match, pos - match.length + 1) && (pos === 0 || isBrOrWsOrPunctuatorNotDot(source.charCodeAt(pos - match.length)));
10331033
}
10341034

10351035
function readPrecedingKeyword1 (pos, ch) {
@@ -1111,8 +1111,8 @@ function isExpressionKeyword (pos) {
11111111
}
11121112

11131113
function isParenKeyword (curPos) {
1114-
return source.charCodeAt(curPos) === 101/*e*/ && source.slice(curPos - 4, curPos) === 'whil' ||
1115-
source.charCodeAt(curPos) === 114/*r*/ && source.slice(curPos - 2, curPos) === 'fo' ||
1114+
return source.charCodeAt(curPos) === 101/*e*/ && source.startsWith('whil', curPos - 4) ||
1115+
source.charCodeAt(curPos) === 114/*r*/ && source.startsWith('fo', curPos - 2) ||
11161116
source.charCodeAt(curPos - 1) === 105/*i*/ && source.charCodeAt(curPos) === 102/*f*/;
11171117
}
11181118

@@ -1142,11 +1142,11 @@ function isExpressionTerminator (curPos) {
11421142
case 41/*)*/:
11431143
return true;
11441144
case 104/*h*/:
1145-
return source.slice(curPos - 4, curPos) === 'catc';
1145+
return source.startsWith('catc', curPos - 4);
11461146
case 121/*y*/:
1147-
return source.slice(curPos - 6, curPos) === 'finall';
1147+
return source.startsWith('finall', curPos - 6);
11481148
case 101/*e*/:
1149-
return source.slice(curPos - 3, curPos) === 'els';
1149+
return source.startsWith('els', curPos - 3);
11501150
}
11511151
return false;
11521152
}

0 commit comments

Comments
 (0)