Skip to content

Commit

Permalink
Fixed error in auto-detection of CSV where quote escapes were detecte…
Browse files Browse the repository at this point in the history
…d as line endings.
  • Loading branch information
jbax committed Aug 15, 2020
1 parent c76103c commit 70b2e71
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public void execute(char[] characters, int length) {
if (Character.isLetterOrDigit(next) || (next <= ' ' && whitespaceRangeStart < next && next != '\n' && next != '\r')) { //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)) {
if (!Character.isLetterOrDigit(prev) && prev != '\n' && prev != '\r') {
increment(escape, prev);
}
}
Expand Down
81 changes: 81 additions & 0 deletions src/test/java/com/univocity/parsers/issues/github/Github_409.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*******************************************************************************
* Copyright 2020 Univocity Software Pty Ltd
*
* Licensed 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 com.univocity.parsers.issues.github;


import com.univocity.parsers.common.*;
import com.univocity.parsers.common.processor.*;
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/405
*
* @author Univocity Software Pty Ltd - <a href="mailto:[email protected]">[email protected]</a>
*/
public class Github_409 {

@Test
public void testPaddingOnFixedWidth() {
String rawData = "A\tB\tC.\t\"G\n" +
"I\n" +
"\"\t\"J\n" +
"M\"\n" +
"\n";

final List<String[]> rows = new ArrayList<String[]>();
final CsvParserSettings settings = new CsvParserSettings();
settings.detectFormatAutomatically('\t');
settings.setIgnoreLeadingWhitespaces(false);
settings.setIgnoreTrailingWhitespaces(false);
settings.setSkipEmptyLines(false);

settings.setUnescapedQuoteHandling(UnescapedQuoteHandling.RAISE_ERROR);

//Ansonsten sind leere Zeilen null-values und führen zu Fehlern.
settings.setNullValue("");

settings.setProcessor(new AbstractRowProcessor() {
@Override
public void rowProcessed(final String[] row, final ParsingContext __) {
if (row != null) {
rows.add(row);
}
}
});

final CsvParser parser = new CsvParser(settings);

parser.beginParsing(new StringReader(rawData));
assertEquals(parser.getDetectedFormat().getQuoteEscape(), '\"');
parser.stopParsing();

parser.parse(new StringReader(rawData));

String[] row = rows.get(0);
assertEquals(row[0], "A");
assertEquals(row[1], "B");
assertEquals(row[2], "C.");
assertEquals(row[3], "G\nI\n");
assertEquals(row[4], "J\nM");
}

}

0 comments on commit 70b2e71

Please sign in to comment.