Skip to content

Commit

Permalink
perf: don't increment a column number
Browse files Browse the repository at this point in the history
Rather than incrementing a column number as we go along, record the position
when a new line starts and comput a column number as needed.
  • Loading branch information
lddubeau committed Sep 12, 2019
1 parent 4d66bbb commit 490fc24
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 22 deletions.
30 changes: 9 additions & 21 deletions lib/saxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ class SaxesParser {
this.line = 1;

/** The column the parser is currently looking at. */
this.column = 0;
this.positionAtNewLine = 0;

this.fileName = this.opt.fileName;
this.onready();
Expand All @@ -416,6 +416,10 @@ class SaxesParser {
return this.chunkPosition + this.i;
}

get column() {
return this.position - this.positionAtNewLine;
}

/* eslint-disable class-methods-use-this */
/**
* Event handler for text data. The default implementation is a no-op.
Expand Down Expand Up @@ -610,8 +614,7 @@ class SaxesParser {

this.i++;
if (code < 0xD800) {
if (code >= SPACE) {
this.column++;
if (code >= SPACE || code === TAB) {
return code;
}

Expand All @@ -630,21 +633,16 @@ class SaxesParser {
/* yes, fall through */
case NL:
this.line++;
this.column = 0;
this.positionAtNewLine = this.position;
return NL;
case TAB:
this.column++;
return TAB;
default:
this.column++;
// If we get here, then code < SPACE and it is not NL CR or TAB.
this.fail("disallowed character.");
return code;
}
}

if (code > 0xDBFF) {
this.column++;
// This is a specialized version of isChar10 that takes into account
// that in this context code > 0xDBFF and code <= 0xFFFF. So it does not
// test cases that don't need testing.
Expand All @@ -662,7 +660,6 @@ class SaxesParser {

const final = 0x10000 + ((code - 0xD800) * 0x400) +
(chunk.charCodeAt(i + 1) - 0xDC00);
this.column += 2;
this.i++;

// This is a specialized version of isChar10 that takes into account that in
Expand Down Expand Up @@ -693,8 +690,7 @@ class SaxesParser {

this.i++;
if (code < 0xD800) {
if ((code > 0x1F && code < 0x7F) || code > 0x9F) {
this.column++;
if ((code > 0x1F && code < 0x7F) || code > 0x9F || code === TAB) {
return code;
}

Expand All @@ -715,20 +711,15 @@ class SaxesParser {
case NEL: // 0x85
case LS: // Ox2028
this.line++;
this.column = 0;
this.positionAtNewLine = this.position;
return NL;
case TAB: // 9
this.column++;
return TAB;
default:
this.column++;
this.fail("disallowed character.");
return code;
}
}

if (code > 0xDBFF) {
this.column++;
// This is a specialized version of isCharAndNotRestricted that takes into
// account that in this context code > 0xDBFF and code <= 0xFFFF. So it
// does not test cases that don't need testing.
Expand All @@ -746,7 +737,6 @@ class SaxesParser {

const final = 0x10000 + ((code - 0xD800) * 0x400) +
(chunk.charCodeAt(i + 1) - 0xDC00);
this.column += 2;
this.i++;

// This is a specialized version of isCharAndNotRestricted that takes into
Expand Down Expand Up @@ -924,11 +914,9 @@ class SaxesParser {
// If the initial character is 0xFEFF, ignore it.
if (c === 0xFEFF) {
this.i++;
this.column++;
}
else if (isS(c)) {
this.i++;
this.column++;
// An XML declaration cannot appear after initial spaces.
this.xmlDeclPossible = false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/trailing-non-whitespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require(".").test({
attributes: {},
isSelfClosing: false,
}],
["error", "1:36: text data outside of root node."],
["error", "1:37: text data outside of root node."],
["text", " to monkey land"],
["end", undefined],
["ready", undefined],
Expand Down

0 comments on commit 490fc24

Please sign in to comment.