refactor(csv): remove runeCount() function (#5298)

This commit is contained in:
Tim Reichen 2024-07-04 09:15:48 +02:00 committed by GitHub
parent 73f236d2f2
commit c58879e6be
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 8 additions and 26 deletions

View File

@ -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.

View File

@ -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);