fix: csv连续双引号处理

This commit is contained in:
octopus_yan 2024-11-22 00:00:46 +08:00
parent 904f37a15b
commit 0c9052216b

View File

@ -38,6 +38,10 @@ public final class CsvParser extends ComputeIter<CsvRow> implements Closeable, S
* 是否在引号包装内
*/
private boolean inQuotes;
/**
* 连续双引号计数
*/
private int continuousCount = 0;
/**
* 当前读取字段
*/
@ -257,9 +261,15 @@ public final class CsvParser extends ComputeIter<CsvRow> implements Closeable, S
if (inQuotes) {
//引号内作为内容直到引号结束
if (c == config.textDelimiter) {
// End of quoted text
inQuotes = false;
if(buf.canRead(1) && buf.read(1) == CharUtil.DOUBLE_QUOTES) {
continuousCount++;
} else if(continuousCount != 0 && (continuousCount + 1) % 2 == 0) {
continuousCount = 0;
} else {
inQuotes = false;
}
} else {
if(continuousCount != 0) continuousCount = 0;
// 字段内容中新行
if (isLineEnd(c, preChar)) {
inQuotesLineCount++;
@ -450,6 +460,14 @@ public final class CsvParser extends ComputeIter<CsvRow> implements Closeable, S
return this.buf[this.position++];
}
boolean canRead(int position) {
return (this.position + position - 1) < limit;
}
char read(int position) {
return this.buf[this.position + position - 1];
}
/**
* 标记位置记为下次读取位置
*/