Skip to content
This repository was archived by the owner on Jan 8, 2020. It is now read-only.

Commit 8b3de71

Browse files
committed
[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.
1 parent e76c4d8 commit 8b3de71

3 files changed

Lines changed: 14 additions & 6 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@
4040
<body>
4141
<release version="1.6" date="2017-MM-DD" description="Feature and bug fix release">
4242
<action issue="CSV-217" type="add" dev="ggregory" due-to="Korolyov Alexei">Add autoFlush option for CsvPrinter. PR #24.</action>
43+
<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>
44+
<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>
4345
</release>
4446
<release version="1.5" date="2017-09-03" description="Feature and bug fix release">
4547
<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>

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1186,10 +1186,7 @@ private void printAndQuote(final Object object, final CharSequence value, final
11861186
} else {
11871187
char c = value.charAt(pos);
11881188

1189-
// RFC4180 (https://tools.ietf.org/html/rfc4180) TEXTDATA = %x20-21 / %x23-2B / %x2D-7E
1190-
if (newRecord && (c < 0x20 || c > 0x21 && c < 0x23 || c > 0x2B && c < 0x2D || c > 0x7E)) {
1191-
quote = true;
1192-
} else if (c <= COMMENT) {
1189+
if (c <= COMMENT) {
11931190
// Some other chars at the start of a value caused the parser to fail, so for now
11941191
// encapsulate if we start in anything less than '#'. We are being conservative
11951192
// by including the default comment char too.

src/test/java/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1033,11 +1033,20 @@ public void testPrinter1() throws IOException {
10331033
}
10341034

10351035
@Test
1036-
public void testRfc4180QuoteSingleChar() throws IOException {
1036+
public void testDontQuoteEuroFirstChar() throws IOException {
10371037
final StringWriter sw = new StringWriter();
10381038
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180)) {
10391039
printer.printRecord(EURO_CH, "Deux");
1040-
assertEquals("\"" + EURO_CH + "\",Deux" + recordSeparator, sw.toString());
1040+
assertEquals(EURO_CH + ",Deux" + recordSeparator, sw.toString());
1041+
}
1042+
}
1043+
1044+
@Test
1045+
public void testQuoteCommaFirstChar() throws IOException {
1046+
final StringWriter sw = new StringWriter();
1047+
try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.RFC4180)) {
1048+
printer.printRecord(",");
1049+
assertEquals("\",\"" + recordSeparator, sw.toString());
10411050
}
10421051
}
10431052

0 commit comments

Comments
 (0)