Skip to content

Commit

Permalink
Fixed issue with CSV format autodetection not working when a column h…
Browse files Browse the repository at this point in the history
…as a quote in the middle of a value.
  • Loading branch information
jbax committed Aug 16, 2017
1 parent 09db5bb commit e302ba5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,14 @@ public void execute(char[] characters, int length) {

inQuote = '\0';
} else if (inQuote == '\0') {
inQuote = ch;
char prev = '\0';
int j = i;
while (prev <= ' ' && --j >= 0) {
prev = characters[j];
}
if (j < 0 || !Character.isLetterOrDigit(prev)) {
inQuote = ch;
}
}
continue;
}
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/com/univocity/parsers/issues/github/Github_178.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
package com.univocity.parsers.issues.github;

import com.univocity.parsers.*;
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.*;

Expand Down Expand Up @@ -51,4 +53,32 @@ public void testDetectionOnInputWithoutExplicitCharset(char delimiter, String in
assertEquals(parser.getDetectedFormat().getDelimiter(), delimiter);

}

@Test
public void testAnotherInput() {
String input = "" +
"Matricule;Nom;Prenoms;Classe;Date de naissance;Date d'inscription;Tel\n" +
" 'A12075';Mako;yao;6eme1;'01/02/2007';15/10/2015;'07108954'\n" +
"A12076; 'Mako';yao;6eme1;01/02/2007;15/10/2015;07108954\n" +
"'A12087';Mako;yao;6eme1;01/02/2007;15/10/2015;07108954";


CsvParserSettings parserSettings = new CsvParserSettings();
parserSettings.setLineSeparatorDetectionEnabled(true);
parserSettings.setDelimiterDetectionEnabled(true);
parserSettings.setQuoteDetectionEnabled(true);

RowListProcessor rowProcessor = new RowListProcessor();
parserSettings.setProcessor(rowProcessor);
parserSettings.setHeaderExtractionEnabled(true);
CsvParser parser = new CsvParser(parserSettings);

parser.parseAll(new StringReader(input));
List<String[]> lignerslt = rowProcessor.getRows();

assertEquals(lignerslt.size(), 3);
assertEquals(Arrays.toString(lignerslt.get(0)), "[A12075, Mako, yao, 6eme1, 01/02/2007, 15/10/2015, 07108954]");
assertEquals(Arrays.toString(lignerslt.get(1)), "[A12076, Mako, yao, 6eme1, 01/02/2007, 15/10/2015, 07108954]");
assertEquals(Arrays.toString(lignerslt.get(2)), "[A12087, Mako, yao, 6eme1, 01/02/2007, 15/10/2015, 07108954]");
}
}

0 comments on commit e302ba5

Please sign in to comment.