You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2016/07/31 07:21:38 UTC

commons-csv git commit: [CSV-171] Negative numeric values in the first column are always quoted in minimal mode.

Repository: commons-csv
Updated Branches:
  refs/heads/master 1023690dc -> 190390bf5


[CSV-171] Negative numeric values in the first column are always quoted
in minimal mode.

Project: http://git-wip-us.apache.org/repos/asf/commons-csv/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-csv/commit/190390bf
Tree: http://git-wip-us.apache.org/repos/asf/commons-csv/tree/190390bf
Diff: http://git-wip-us.apache.org/repos/asf/commons-csv/diff/190390bf

Branch: refs/heads/master
Commit: 190390bf5dd83d6137ca3045902fcecbeafa3227
Parents: 1023690
Author: Gary Gregory <gg...@apache.org>
Authored: Sun Jul 31 00:21:35 2016 -0700
Committer: Gary Gregory <gg...@apache.org>
Committed: Sun Jul 31 00:21:35 2016 -0700

----------------------------------------------------------------------
 src/changes/changes.xml                         |  1 +
 .../java/org/apache/commons/csv/CSVFormat.java  |  4 +-
 .../org/apache/commons/csv/CSVPrinterTest.java  | 68 ++++++++++++++++----
 3 files changed, 60 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-csv/blob/190390bf/src/changes/changes.xml
----------------------------------------------------------------------
diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index eb7df9e..ee0070a 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -39,6 +39,7 @@
   </properties>
   <body>
     <release version="1.5" date="2016-MM-DD" description="Bug fix release">
+      <action issue="CSV-171" type="fix" dev="ggregory" due-to="Gary Gregory, Michael Graessle, Adrian Bridgett">Negative numeric values in the first column are always quoted in minimal mode.</action>
       <action issue="CSV-187" type="update" dev="ggregory" due-to="Gary Gregory">Update platform requirement from Java 6 to 7.</action>
       <action issue="CSV-189" type="add" dev="ggregory" due-to="Peter Holzwarth, Gary Gregory">CSVParser: Add factory method accepting InputStream.</action>
       <action issue="CSV-190" type="add" dev="ggregory" due-to="Gary Gregory">Add convenience API CSVFormat.print(File, Charset)</action>

http://git-wip-us.apache.org/repos/asf/commons-csv/blob/190390bf/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 30a8f88..eee099d 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -1035,8 +1035,8 @@ public final class CSVFormat implements Serializable {
             } else {
                 char c = value.charAt(pos);
 
-                // TODO where did this rule come from?
-                if (newRecord && (c < '0' || c > '9' && c < 'A' || c > 'Z' && c < 'a' || c > 'z')) {
+                // 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) {
                     // Some other chars at the start of a value caused the parser to fail, so for now

http://git-wip-us.apache.org/repos/asf/commons-csv/blob/190390bf/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 86b852f..bc7ac9b 100644
--- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java
@@ -53,6 +53,8 @@ import org.junit.Test;
  */
 public class CSVPrinterTest {
 
+    private static final char DQUOTE_CHAR = '"';
+    private static final char BACKSLASH_CH = '\\';
     private static final char QUOTE_CH = '\'';
     private static final int ITERATIONS_FOR_RANDOM_TEST = 50000;
 
@@ -184,13 +186,13 @@ public class CSVPrinterTest {
                 ch = ',';
                 break;
             case 6:
-                ch = '"';
+                ch = DQUOTE_CHAR;
                 break;
             case 7:
                 ch = '\'';
                 break;
             case 8:
-                ch = '\\';
+                ch = BACKSLASH_CH;
                 break;
             default:
                 ch = (char) r.nextInt(300);
@@ -296,7 +298,7 @@ public class CSVPrinterTest {
         try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(QUOTE_CH))) {
             printer.print("\\");
         }
-        assertEquals("'\\'", sw.toString());
+        assertEquals("\\", sw.toString());
     }
 
     @Test
@@ -306,7 +308,6 @@ public class CSVPrinterTest {
             printer.print("\\\r");
         }
         assertEquals("'\\\r'", sw.toString());
-
     }
 
     @Test
@@ -324,7 +325,7 @@ public class CSVPrinterTest {
         try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(QUOTE_CH))) {
             printer.print("\\\\");
         }
-        assertEquals("'\\\\'", sw.toString());
+        assertEquals("\\\\", sw.toString());
     }
 
     @Test
@@ -333,7 +334,52 @@ public class CSVPrinterTest {
         try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withQuote(QUOTE_CH))) {
             printer.print("\\\\");
         }
-        assertEquals("'\\\\'", sw.toString());
+        assertEquals("\\\\", sw.toString());
+    }
+
+    @Test
+    public void testEscapeNull1() throws IOException {
+        StringWriter sw = new StringWriter();
+        try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape(null))) {
+            printer.print("\\");
+        }
+        assertEquals("\\", sw.toString());
+    }
+
+    @Test
+    public void testEscapeNull2() throws IOException {
+        StringWriter sw = new StringWriter();
+        try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape(null))) {
+            printer.print("\\\r");
+        }
+        assertEquals("\"\\\r\"", sw.toString());
+    }
+
+    @Test
+    public void testEscapeNull3() throws IOException {
+        StringWriter sw = new StringWriter();
+        try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape(null))) {
+            printer.print("X\\\r");
+        }
+        assertEquals("\"X\\\r\"", sw.toString());
+    }
+
+    @Test
+    public void testEscapeNull4() throws IOException {
+        StringWriter sw = new StringWriter();
+        try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape(null))) {
+            printer.print("\\\\");
+        }
+        assertEquals("\\\\", sw.toString());
+    }
+
+    @Test
+    public void testEscapeNull5() throws IOException {
+        StringWriter sw = new StringWriter();
+        try (final CSVPrinter printer = new CSVPrinter(sw, CSVFormat.DEFAULT.withEscape(null))) {
+            printer.print("\\\\");
+        }
+        assertEquals("\\\\", sw.toString());
     }
 
     @Test
@@ -490,7 +536,7 @@ public class CSVPrinterTest {
     @Test
     @Ignore
     public void testJira135_part1() throws IOException {
-        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
+        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH);
         final StringWriter sw = new StringWriter();
         final List<String> list = new LinkedList<>();
         try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
@@ -506,7 +552,7 @@ public class CSVPrinterTest {
     @Test
     @Ignore
     public void testJira135_part2() throws IOException {
-        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
+        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH);
         final StringWriter sw = new StringWriter();
         final List<String> list = new LinkedList<>();
         try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
@@ -522,7 +568,7 @@ public class CSVPrinterTest {
     @Test
     @Ignore
     public void testJira135_part3() throws IOException {
-        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
+        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH);
         final StringWriter sw = new StringWriter();
         final List<String> list = new LinkedList<>();
         try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
@@ -538,7 +584,7 @@ public class CSVPrinterTest {
     @Test
     @Ignore
     public void testJira135All() throws IOException {
-        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote('"').withEscape('\\');
+        final CSVFormat format = CSVFormat.DEFAULT.withRecordSeparator('\n').withQuote(DQUOTE_CHAR).withEscape(BACKSLASH_CH);
         final StringWriter sw = new StringWriter();
         final List<String> list = new LinkedList<>();
         try (final CSVPrinter printer = new CSVPrinter(sw, format)) {
@@ -567,7 +613,7 @@ public class CSVPrinterTest {
     @Test
     public void testMySqlNullOutput() throws IOException {
         Object[] s = new String[] { "NULL", null };
-        CSVFormat format = CSVFormat.MYSQL.withQuote('"').withNullString("NULL").withQuoteMode(QuoteMode.NON_NUMERIC);
+        CSVFormat format = CSVFormat.MYSQL.withQuote(DQUOTE_CHAR).withNullString("NULL").withQuoteMode(QuoteMode.NON_NUMERIC);
         StringWriter writer = new StringWriter();
         try (final CSVPrinter printer = new CSVPrinter(writer, format)) {
             printer.printRecord(s);