From 0c9052216bfaf22b75143052b9bae9b6f2b22c45 Mon Sep 17 00:00:00 2001 From: octopus_yan Date: Fri, 22 Nov 2024 00:00:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20csv=E8=BF=9E=E7=BB=AD=E5=8F=8C=E5=BC=95?= =?UTF-8?q?=E5=8F=B7=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../octopusyan/dmt/utils/csv/CsvParser.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/main/java/cn/octopusyan/dmt/utils/csv/CsvParser.java b/src/main/java/cn/octopusyan/dmt/utils/csv/CsvParser.java index 99ed94a..8792536 100644 --- a/src/main/java/cn/octopusyan/dmt/utils/csv/CsvParser.java +++ b/src/main/java/cn/octopusyan/dmt/utils/csv/CsvParser.java @@ -38,6 +38,10 @@ public final class CsvParser extends ComputeIter implements Closeable, S * 是否在引号包装内 */ private boolean inQuotes; + /** + * 连续双引号计数 + */ + private int continuousCount = 0; /** * 当前读取字段 */ @@ -257,9 +261,15 @@ public final class CsvParser extends ComputeIter 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 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]; + } + /** * 标记位置记为下次读取位置 */