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