Skip to content

Commit

Permalink
perf: optimize forward code
Browse files Browse the repository at this point in the history
  • Loading branch information
DylanPiercey committed Mar 21, 2022
1 parent 93901db commit fc1cef5
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions src/core/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Parser {
public data!: string;
public activeState!: StateDefinition;
public activeRange!: Meta;
public forward!: boolean;
public forward!: number;
public activeTag: STATE.OpenTagMeta | undefined; // Used to reference the closest open tag
public activeAttr: STATE.AttrMeta | undefined; // Used to reference the current attribute that is being parsed
public indent!: string; // Used to build the indent for the current concise line
Expand Down Expand Up @@ -73,7 +73,7 @@ export class Parser {
const { activeRange, activeState } = this;
const parent = (this.activeRange = activeRange.parent);
this.activeState = parent.state;
this.forward = false;
this.forward = 0;
activeRange.end = this.pos;
activeState.exit.call(this, activeRange);
this.activeState.return.call(this, activeRange, parent);
Expand Down Expand Up @@ -285,7 +285,8 @@ export class Parser {
this.data = data;
this.indent = "";
this.textPos = -1;
this.forward = this.isConcise = true;
this.forward = 1;
this.isConcise = true;
this.beginMixedMode = this.endingMixedModeAtEOL = false;
this.lines = this.activeTag = this.activeAttr = undefined;

Expand All @@ -298,31 +299,28 @@ export class Parser {

while (this.pos < maxPos) {
const code = data.charCodeAt(this.pos);
let skip = 1;

if (code === CODE.NEWLINE) {
this.forward = 1;
this.activeState.eol.call(this, 1, this.activeRange);
} else if (
code === CODE.CARRIAGE_RETURN &&
data.charCodeAt(this.pos + 1) === CODE.NEWLINE
) {
this.forward = 2;
this.activeState.eol.call(this, 2, this.activeRange);
skip = 2;
} else {
this.forward = 1;
this.activeState.char.call(this, code, this.activeRange);
}

if (this.forward) {
this.pos += skip;
} else {
this.forward = true;
}
this.pos += this.forward;
}

while (this.pos === this.maxPos) {
this.forward = true;
this.forward = 1;
this.activeState.eof.call(this, this.activeRange);
if (this.forward) break;
if (this.forward !== 0) break;
}
}
}

0 comments on commit fc1cef5

Please sign in to comment.