Skip to content

Commit

Permalink
fixed quote escape detection issue reported in #444
Browse files Browse the repository at this point in the history
  • Loading branch information
jbax committed Apr 19, 2021
1 parent 53026fb commit ed3d0d3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void execute(char[] characters, int length) {

if (i + 1 < length) {
char next = characters[i + 1];
if (Character.isLetterOrDigit(next) || (next <= ' ' && whitespaceRangeStart < next && next != '\n' && next != '\r')) { //no special characters after quote, might be escaping
if (Character.isLetterOrDigit(next) || (next <= ' ' && whitespaceRangeStart < next && next != '\n' && next != '\r' && next != '\t')) { //no special characters after quote, might be escaping
//special character before (potentially) closing quote, might be an escape
char prev = characters[i - 1];
if (!Character.isLetterOrDigit(prev) && prev != '\n' && prev != '\r') {
Expand Down Expand Up @@ -260,7 +260,7 @@ public void execute(char[] characters, int length) {
}

escape.remove(delimiter);
char quoteEscape = doubleQuoteCount == 0 && singleQuoteCount == 0 ? suggestedQuoteEscape : max(escape, totals, quote);
char quoteEscape =doubleQuoteCount == 0 && singleQuoteCount == 0 ? suggestedQuoteEscape : max(escape, totals, quote);
apply(delimiter, quote, quoteEscape);
}

Expand Down
43 changes: 43 additions & 0 deletions src/test/java/com/univocity/parsers/issues/github/Github_444.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.univocity.parsers.issues.github;

import com.univocity.parsers.csv.*;
import org.testng.annotations.*;

import java.io.*;
import java.util.*;

import static org.testng.Assert.*;


/**
* From: https://github.com/univocity/univocity-parsers/issues/444
*
* @author Univocity Software Pty Ltd - <a href="mailto:[email protected]">[email protected]</a>
*/
public class Github_444 {

@Test
public void parseCSV() {
String rawData;
rawData = "" +
"\"\"\"quoted\"\"\n" +
")\"\t\n";

final CsvParserSettings settings = new CsvParserSettings();
settings.detectFormatAutomatically('\t', ';', ',');
settings.setIgnoreLeadingWhitespaces(false);
settings.setIgnoreTrailingWhitespaces(false);
settings.setSkipEmptyLines(false);

settings.setNullValue("");


final CsvParser parser = new CsvParser(settings);

String[] row = parser.parseAll(new StringReader(rawData)).get(0);
assertEquals(row[0], "\"quoted\"\n)");
assertEquals(row[1], "");

assertEquals(parser.getDetectedFormat().getQuoteEscape(), '"');
}
}

0 comments on commit ed3d0d3

Please sign in to comment.