This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-csv.git


The following commit(s) were added to refs/heads/master by this push:
     new a423734  [CSV-263] Print from Reader with embedded quotes generates 
incorrect output.
a423734 is described below

commit a4237345a1de8c9ada81a73fe1b8e44d0174dcf7
Author: Gary Gregory <[email protected]>
AuthorDate: Wed Jul 7 19:18:12 2021 -0400

    [CSV-263] Print from Reader with embedded quotes generates incorrect
    output.
    
    - Resolve conflicts from PR #78 by Jason A. Guild.
    - Don't use depreacted methods.
    - Javadoc.
    - Use final.
---
 src/changes/changes.xml                            |  1 +
 .../java/org/apache/commons/csv/CSVFormat.java     |  4 +-
 .../java/org/apache/commons/csv/CSVFormatTest.java |  2 +-
 .../apache/commons/csv/issues/JiraCsv263Test.java  | 77 ++++++++++++++++++++++
 4 files changed, 81 insertions(+), 3 deletions(-)

diff --git a/src/changes/changes.xml b/src/changes/changes.xml
index 72ac11f..3db75c4 100644
--- a/src/changes/changes.xml
+++ b/src/changes/changes.xml
@@ -59,6 +59,7 @@
       <action                 type="fix" dev="ggregory" due-to="Gary 
Gregory">Update CSVParser.parse(File, Charset, CSVFormat) from IO to 
NIO.</action>
       <action issue="CSV-271" type="fix" dev="ggregory" due-to="Amar Prakash 
Pandey">Missing separator with print(object) followed by printRecord(Object[]) 
#157.</action>
       <action issue="CSV-158" type="fix" dev="ggregory" due-to="Alexander 
Bondarev, Benedikt Ritter, Gary Gregory, Chen">Fix EOL checking for read array 
in ExtendedBufferedReader #5.</action>
+      <action issue="CSV-263" type="fix" dev="ggregory" due-to="Jason A. 
Guild, Gary Gregory">Print from Reader with embedded quotes generates incorrect 
output #78.</action>
       <!-- ADD -->
       <action issue="CSV-275" type="add" dev="ggregory" due-to="Michael Wyraz, 
Gary Gregory">Make CSVRecord#toList() public.</action>
       <action                 type="add" dev="ggregory" due-to="Gary 
Gregory">Add CSVRecord#stream().</action>
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java 
b/src/main/java/org/apache/commons/csv/CSVFormat.java
index 5281a34..550f516 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -1747,7 +1747,7 @@ public final class CSVFormat implements Serializable {
 
     /**
      * Prints the {@code value} as the next value on the line to {@code out}. 
The value will be escaped or encapsulated as needed. Useful when one wants to
-     * avoid creating CSVPrinters. Trims the value if {@link #getTrim()} is 
true
+     * avoid creating CSVPrinters. Trims the value if {@link #getTrim()} is 
true.
      *
      * @param value     value to output.
      * @param out       where to print the value.
@@ -2120,11 +2120,11 @@ public final class CSVFormat implements Serializable {
                 // write out segment up until this char
                 if (pos > 0) {
                     append(builder.substring(0, pos), appendable);
+                    append(quote, appendable);
                     builder.setLength(0);
                     pos = -1;
                 }
 
-                append(quote, appendable);
                 append((char) c, appendable);
             }
             pos++;
diff --git a/src/test/java/org/apache/commons/csv/CSVFormatTest.java 
b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
index 400cc07..ecb02a0 100644
--- a/src/test/java/org/apache/commons/csv/CSVFormatTest.java
+++ b/src/test/java/org/apache/commons/csv/CSVFormatTest.java
@@ -971,7 +971,7 @@ public class CSVFormatTest {
         final Appendable out = new StringBuilder();
         final CSVFormat format = 
CSVFormat.RFC4180.withDelimiter(',').withQuote('"').withEscape('?').withQuoteMode(QuoteMode.NON_NUMERIC);
         format.print(in, out, true);
-        assertEquals("\"\"\"\"a,b,c\r\nx,y,z\"", out.toString());
+        assertEquals("\"\"\"a,b,c\r\nx,y,z\"", out.toString());
     }
 
     @Test
diff --git a/src/test/java/org/apache/commons/csv/issues/JiraCsv263Test.java 
b/src/test/java/org/apache/commons/csv/issues/JiraCsv263Test.java
new file mode 100644
index 0000000..08436b7
--- /dev/null
+++ b/src/test/java/org/apache/commons/csv/issues/JiraCsv263Test.java
@@ -0,0 +1,77 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.csv.issues;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+import java.io.IOException;
+import java.io.Reader;
+import java.io.StringReader;
+
+import org.apache.commons.csv.CSVFormat;
+import org.apache.commons.csv.QuoteMode;
+import org.junit.jupiter.api.Test;
+
+/**
+ * Tests [CSV-263] Print from Reader with embedded quotes generates incorrect 
output.
+ */
+public class JiraCsv263Test {
+
+    @Test
+    public void testPrintFromReaderWithQuotes() throws IOException {
+        // @formatter:off
+        final CSVFormat format = CSVFormat.RFC4180.builder()
+            .setDelimiter(',')
+            .setQuote('"')
+            .setEscape('?')
+            .setQuoteMode(QuoteMode.NON_NUMERIC)
+            .build();
+        // @formatter:on
+        final StringBuilder out = new StringBuilder();
+
+        final Reader atStartOnly = new StringReader("\"a,b,c\r\nx,y,z");
+        format.print(atStartOnly, out, true);
+        assertEquals("\"\"\"a,b,c\r\nx,y,z\"", out.toString());
+
+        final Reader atEndOnly = new StringReader("a,b,c\r\nx,y,z\"");
+        out.setLength(0);
+        format.print(atEndOnly, out, true);
+        assertEquals("\"a,b,c\r\nx,y,z\"\"\"", out.toString());
+
+        final Reader atBeginEnd = new StringReader("\"a,b,c\r\nx,y,z\"");
+        out.setLength(0);
+        format.print(atBeginEnd, out, true);
+        assertEquals("\"\"\"a,b,c\r\nx,y,z\"\"\"", out.toString());
+
+        final Reader embeddedBeginMiddle = new 
StringReader("\"a\",b,c\r\nx,\"y\",z");
+        out.setLength(0);
+        format.print(embeddedBeginMiddle, out, true);
+        assertEquals("\"\"\"a\"\",b,c\r\nx,\"\"y\"\",z\"", out.toString());
+
+        final Reader embeddedMiddleEnd = new 
StringReader("a,\"b\",c\r\nx,y,\"z\"");
+        out.setLength(0);
+        format.print(embeddedMiddleEnd, out, true);
+        assertEquals("\"a,\"\"b\"\",c\r\nx,y,\"\"z\"\"\"", out.toString());
+
+        final Reader nested = new StringReader("a,\"b \"and\" c\",d");
+        out.setLength(0);
+        format.print(nested, out, true);
+        assertEquals("\"a,\"\"b \"\"and\"\" c\"\",d\"", out.toString());
+    }
+
+}
\ No newline at end of file

Reply via email to