diff --git a/lib/saxes.js b/lib/saxes.js index 9947a550..bff77b7b 100644 --- a/lib/saxes.js +++ b/lib/saxes.js @@ -632,13 +632,18 @@ class SaxesParser { getCode10() { const { chunk, i } = this; this.prevI = i; + // Yes, we do this instead of doing this.i++. Doing it this way, we do not + // read this.i again, which is a bit faster. + this.i = i + 1; + + if (i >= chunk.length) { + return undefined; + } + // Using charCodeAt and handling the surrogates ourselves is faster // than using codePointAt. const code = chunk.charCodeAt(i); - // Yes, we do this instead of doing this.i++. Doing it this way, we do not - // read this.i again, which is a bit faster. - this.i = i + 1; if (code < 0xD800) { if (code >= SPACE || code === TAB) { return code; @@ -679,11 +684,6 @@ class SaxesParser { return code; } - // eslint-disable-next-line no-restricted-globals - if (isNaN(code)) { - return undefined; - } - const final = 0x10000 + ((code - 0xD800) * 0x400) + (chunk.charCodeAt(i + 1) - 0xDC00); this.i = i + 2; @@ -711,13 +711,18 @@ class SaxesParser { getCode11() { const { chunk, i } = this; this.prevI = i; + // Yes, we do this instead of doing this.i++. Doing it this way, we do not + // read this.i again, which is a bit faster. + this.i = i + 1; + + if (i >= chunk.length) { + return undefined; + } + // Using charCodeAt and handling the surrogates ourselves is faster // than using codePointAt. const code = chunk.charCodeAt(i); - // Yes, we do this instead of doing this.i++. Doing it this way, we do not - // read this.i again, which is a bit faster. - this.i = i + 1; if (code < 0xD800) { if ((code > 0x1F && code < 0x7F) || (code > 0x9F && code !== LS) || code === TAB) { @@ -761,11 +766,6 @@ class SaxesParser { return code; } - // eslint-disable-next-line no-restricted-globals - if (isNaN(code)) { - return undefined; - } - const final = 0x10000 + ((code - 0xD800) * 0x400) + (chunk.charCodeAt(i + 1) - 0xDC00); this.i = i + 2;