Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect results when ascii-control characters are used as Quote and Escape #533

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/java/com/univocity/parsers/csv/CsvParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,12 @@ private void parseQuotedValue() {
}
ch = input.nextChar();

if (trimQuotedLeading && ch <= ' ' && output.appender.length() == 0) {
if (trimQuotedLeading && (ch <= ' ' && ch != quote && ch != quoteEscape) && output.appender.length() == 0) {
while ((ch = input.nextChar()) <= ' ') ;
}

while (true) {
if (prev == quote && (ch <= ' ' && whitespaceRangeStart < ch || ch == delimiter || ch == newLine)) {
if (prev == quote && ((ch <= ' ' && ch != quote && ch != quoteEscape) && whitespaceRangeStart < ch || ch == delimiter || ch == newLine)) {
break;
}

Expand Down
18 changes: 17 additions & 1 deletion src/test/java/com/univocity/parsers/csv/CsvParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -907,5 +907,21 @@ public void parseDisablingCommentLineCheck(String csvFile, char[] lineSeparator)
assertEquals(comments.size(), 0);
assertEquals(parser.getContext().lastComment(), null);
}

@Test
public void asciiControlCharAsQuoteAndEscape() {
final Reader csv = new StringReader("\u00127\u0012," +
"\u0012EmbeddedDouble\u0012," +
"\u0012field\u0012\u0012 t\u0012\u0012ext\u0012," +
"\u0012field\u0012\u0012 t\u0012\u0012ext\u0012");
final CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setQuote('\u0012');
settings.getFormat().setQuoteEscape('\u0012');
settings.setUnescapedQuoteHandling(UnescapedQuoteHandling.STOP_AT_CLOSING_QUOTE);
final CsvParser csvParser = new CsvParser(settings);
final String[] row = csvParser.parseAll(csv).get(0);
assertEquals(row[0], "7");
assertEquals(row[1], "EmbeddedDouble");
assertEquals(row[2], "field\u0012 t\u0012ext");
assertEquals(row[3], "field\u0012 t\u0012ext");
}
}