[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.
Project: http://git-wip-us.apache.org/repos/asf/commons-csv/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-csv/commit/8b3de71f Tree: http://git-wip-us.apache.org/repos/asf/commons-csv/tree/8b3de71f Diff: http://git-wip-us.apache.org/repos/asf/commons-csv/diff/8b3de71f Branch: refs/heads/CSV-216 Commit: 8b3de71fd99d0fa07cb6a3a35b583bbb170aab66 Parents: e76c4d8 Author: Gary Gregory <ggreg...@apache.org> Authored: Mon Dec 11 11:16:01 2017 -0700 Committer: Gary Gregory <ggreg...@apache.org> Committed: Mon Dec 11 11:16:01 2017 -0700 ---------------------------------------------------------------------- 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(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-csv/blob/8b3de71f/src/changes/changes.xml ---------------------------------------------------------------------- diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b56b75f..302632e 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -40,6 +40,8 @@ <body> <release version="1.6" date="2017-MM-DD" description="Feature and bug fix release"> <action issue="CSV-217" type="add" dev="ggregory" due-to="Korolyov Alexei">Add autoFlush option for CsvPrinter. PR #24.</action> + <action issue="CSV-219" type="fix" dev="ggregory" due-to="Zhang Hongda">The behavior of quote char using is not similar as Excel does when the first string contains CJK char(s).</action> + <action issue="CSV-172" type="fix" dev="ggregory" due-to="Andrew Pennebaker">Don't quote cells just because they have UTF-8 encoded characters.</action> </release> <release version="1.5" date="2017-09-03" description="Feature and bug fix release"> <action issue="CSV-203" type="fix" dev="ggregory" due-to="Richard Wheeldon, Kai Paroth">withNullString value is printed without quotes when QuoteMode.ALL is specified; add QuoteMode.ALL_NON_NULL. PR #17.</action> http://git-wip-us.apache.org/repos/asf/commons-csv/blob/8b3de71f/src/main/java/org/apache/commons/csv/CSVFormat.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index 58948fd..dc7588b 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. http://git-wip-us.apache.org/repos/asf/commons-csv/blob/8b3de71f/src/test/java/org/apache/commons/csv/CSVPrinterTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index ae7aae2..5a09627 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()); } }