mirror of
https://github.com/denoland/std.git
synced 2024-11-21 20:50:22 +00:00
refactor(csv): remove runeCount()
function (#5298)
This commit is contained in:
parent
73f236d2f2
commit
c58879e6be
17
csv/_io.ts
17
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.
|
||||
|
17
csv/parse.ts
17
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);
|
||||
|
Loading…
Reference in New Issue
Block a user