diff --git a/csv/_io.ts b/csv/_io.ts index ebe145b5a..9f0db09b6 100644 --- a/csv/_io.ts +++ b/csv/_io.ts @@ -94,9 +94,7 @@ export async function parseRecord( if (!opt.lazyQuotes) { const j = field.indexOf(quote); if (j >= 0) { - const col = runeCount( - fullLine.slice(0, fullLine.length - line.slice(j).length), - ); + const col = fullLine.length + j - line.length; throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE); } } @@ -134,9 +132,7 @@ export async function parseRecord( recordBuffer += quote; } else { // `"*` sequence (invalid non-escaped quote). - const col = runeCount( - fullLine.slice(0, fullLine.length - line.length - quoteLen), - ); + const col = fullLine.length - line.length - quoteLen; throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); } } else if (line.length > 0 || !reader.isEOF()) { @@ -149,7 +145,7 @@ export async function parseRecord( if (r === null) { // Abrupt end of file (EOF or error). if (!opt.lazyQuotes) { - const col = runeCount(fullLine); + const col = fullLine.length; throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); } fieldIndexes.push(recordBuffer.length); @@ -159,7 +155,7 @@ export async function parseRecord( } else { // Abrupt end of file (EOF on error). if (!opt.lazyQuotes) { - const col = runeCount(fullLine); + const col = fullLine.length; throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); } fieldIndexes.push(recordBuffer.length); @@ -177,11 +173,6 @@ export async function parseRecord( return result; } -function runeCount(s: string): number { - // Array.from considers the surrogate pair. - return Array.from(s).length; -} - /** * A ParseError is returned for parsing errors. * Line numbers are 1-indexed and columns are 0-indexed. diff --git a/csv/parse.ts b/csv/parse.ts index acd6dcf89..e3d39cad5 100644 --- a/csv/parse.ts +++ b/csv/parse.ts @@ -79,11 +79,6 @@ class Parser { return []; } - function runeCount(s: string): number { - // Array.from considers the surrogate pair. - return Array.from(s).length; - } - let lineIndex = startLine + 1; // line starting with comment character is ignored @@ -113,9 +108,7 @@ class Parser { if (!this.#options.lazyQuotes) { const j = field.indexOf(quote); if (j >= 0) { - const col = runeCount( - fullLine.slice(0, fullLine.length - line.slice(j).length), - ); + const col = fullLine.length + j - line.length; throw new ParseError(startLine + 1, lineIndex, col, ERR_BARE_QUOTE); } } @@ -153,9 +146,7 @@ class Parser { recordBuffer += quote; } else { // `"*` sequence (invalid non-escaped quote). - const col = runeCount( - fullLine.slice(0, fullLine.length - line.length - quoteLen), - ); + const col = fullLine.length - line.length - quoteLen; throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); } } else if (line.length > 0 || !(this.#isEOF())) { @@ -168,7 +159,7 @@ class Parser { if (r === null) { // Abrupt end of file (EOF or error). if (!this.#options.lazyQuotes) { - const col = runeCount(fullLine); + const col = fullLine.length; throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); } fieldIndexes.push(recordBuffer.length); @@ -178,7 +169,7 @@ class Parser { } else { // Abrupt end of file (EOF on error). if (!this.#options.lazyQuotes) { - const col = runeCount(fullLine); + const col = fullLine.length; throw new ParseError(startLine + 1, lineIndex, col, ERR_QUOTE); } fieldIndexes.push(recordBuffer.length);