From 8b3de71fd99d0fa07cb6a3a35b583bbb170aab66 Mon Sep 17 00:00:00 2001 From: Gary Gregory Date: Mon, 11 Dec 2017 11:16:01 -0700 Subject: [PATCH] [CSV-219] The behavior of quote char using is not similar as Excel does when the first string contains CJK char(s). [CSV-172] Don't quote cells just because they have UTF-8 encoded characters. --- src/changes/changes.xml | 2 ++ src/main/java/org/apache/commons/csv/CSVFormat.java | 5 +---- .../java/org/apache/commons/csv/CSVPrinterTest.java | 13 +++++++++++-- 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b56b75f2..302632e9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -40,6 +40,8 @@ Add autoFlush option for CsvPrinter. PR #24. + The behavior of quote char using is not similar as Excel does when the first string contains CJK char(s). + Don't quote cells just because they have UTF-8 encoded characters. withNullString value is printed without quotes when QuoteMode.ALL is specified; add QuoteMode.ALL_NON_NULL. PR #17. diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 58948fdc..dc7588b1 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -1186,10 +1186,7 @@ public final class CSVFormat implements Serializable { } else { char c = value.charAt(pos); - // RFC4180 (https://tools.ietf.org/html/rfc4180) TEXTDATA = %x20-21 / %x23-2B / %x2D-7E - if (newRecord && (c < 0x20 || c > 0x21 && c < 0x23 || c > 0x2B && c < 0x2D || c > 0x7E)) { - quote = true; - } else if (c <= COMMENT) { + if (c <= COMMENT) { // Some other chars at the start of a value caused the parser to fail, so for now // encapsulate if we start in anything less than '#'. We are being conservative // by including the default comment char too. diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index ae7aae26..5a096270 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -1033,11 +1033,20 @@ public class CSVPrinterTest { } @Test - public void testRfc4180QuoteSingleChar() throws IOException { + public void testDontQuoteEuroFirstChar() throws IOException { final StringWriter sw = new StringWriter(); try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180)) { printer.printRecord(EURO_CH, "Deux"); - assertEquals("\"" + EURO_CH + "\",Deux" + recordSeparator, sw.toString()); + assertEquals(EURO_CH + ",Deux" + recordSeparator, sw.toString()); + } + } + + @Test + public void testQuoteCommaFirstChar() throws IOException { + final StringWriter sw = new StringWriter(); + try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180)) { + printer.printRecord(","); + assertEquals("\",\"" + recordSeparator, sw.toString()); } }