Skip to content

Commit

Permalink
[Refactor] parse: use slice over substr, cache some values
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Apr 7, 2023
1 parent ecf2a60 commit fcb2e1a
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions parse.js
Original file line number Diff line number Diff line change
Expand Up @@ -116,28 +116,30 @@ function parseInternal(string, env, opts) {
i += 1;
var varend;
var varname;
// debugger
if (s.charAt(i) === '{') {
var char = s.charAt(i);

if (char === '{') {
i += 1;
if (s.charAt(i) === '}') {
throw new Error('Bad substitution: ' + s.substr(i - 2, 3));
throw new Error('Bad substitution: ' + s.slice(i - 2, i + 1));
}
varend = s.indexOf('}', i);
if (varend < 0) {
throw new Error('Bad substitution: ' + s.substr(i));
throw new Error('Bad substitution: ' + s.slice(i));
}
varname = s.substr(i, varend - i);
varname = s.slice(i, varend);
i = varend;
} else if ((/[*@#?$!_-]/).test(s.charAt(i))) {
varname = s.charAt(i);
} else if ((/[*@#?$!_-]/).test(char)) {
varname = char;
i += 1;
} else {
varend = s.substr(i).match(/[^\w\d_]/);
var slicedFromI = s.slice(i);
varend = slicedFromI.match(/[^\w\d_]/);
if (!varend) {
varname = s.substr(i);
varname = slicedFromI;
i = s.length;
} else {
varname = s.substr(i, varend.index);
varname = slicedFromI.slice(0, varend.index);
i += varend.index - 1;
}
}
Expand Down

0 comments on commit fcb2e1a

Please sign in to comment.