Skip to content

Commit

Permalink
perf: optimize helper for skipping whitespace before a match
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Feb 26, 2022
1 parent 879d5a8 commit 2a98010
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 9 deletions.
15 changes: 11 additions & 4 deletions src/core/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -398,10 +398,17 @@ export class Parser {

// --------------------------

lookPastWhitespaceFor(str: string, start = 1) {
let ahead = start;
while (isWhitespaceCode(this.lookAtCharCodeAhead(ahead))) ahead++;
return !!this.lookAheadFor(str, this.pos + ahead);
consumeWhitespaceIfBefore(str: string, start = 0) {
const { pos, data } = this;
let cur = pos + start;
while (isWhitespaceCode(data.charCodeAt(cur))) cur++;

if (this.lookAheadFor(str, cur)) {
this.pos = cur;
return true;
}

return false;
}

getPreviousNonWhitespaceCharCode(start = -1) {
Expand Down
4 changes: 1 addition & 3 deletions src/states/ATTRIBUTE.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,12 @@ export const ATTRIBUTE: StateDefinition<AttrMeta> = {
end: childPart.end,
};

if (this.lookPastWhitespaceFor("{")) {
if (this.consumeWhitespaceIfBefore("{")) {
attr.args = {
start,
end,
value,
};
this.consumeWhitespace();
this.rewind(1);
} else {
attr.args = true;
this.emit({
Expand Down
3 changes: 1 addition & 2 deletions src/states/OPEN_TAG.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,7 @@ export const OPEN_TAG: StateDefinition<OpenTagMeta> = {
end: childPart.end,
};

if (this.lookPastWhitespaceFor("{")) {
this.consumeWhitespace();
if (this.consumeWhitespaceIfBefore("{")) {
const attr = this.enterState(STATE.ATTRIBUTE);
attr.start = start;
attr.args = { start, end, value };
Expand Down

0 comments on commit 2a98010

Please sign in to comment.