From e168526433980cc132d6be3ff9ef05f5d650649a Mon Sep 17 00:00:00 2001 From: Attilio Date: Sun, 25 Jun 2017 08:26:29 +0200 Subject: [PATCH 1/4] Test cases for lines with only spaces. --- src/test/resources/CSVFileParser/testEmptyLine.csv | 9 +++++++++ .../resources/CSVFileParser/testEmptyLine_default.txt | 6 ++++++ 2 files changed, 15 insertions(+) create mode 100644 src/test/resources/CSVFileParser/testEmptyLine.csv create mode 100644 src/test/resources/CSVFileParser/testEmptyLine_default.txt diff --git a/src/test/resources/CSVFileParser/testEmptyLine.csv b/src/test/resources/CSVFileParser/testEmptyLine.csv new file mode 100644 index 0000000000..146c40224f --- /dev/null +++ b/src/test/resources/CSVFileParser/testEmptyLine.csv @@ -0,0 +1,9 @@ +# totally empty line "" + +# line with one space " " + +# line with one tab "\t" + +# line with one space and tab " \t" + + diff --git a/src/test/resources/CSVFileParser/testEmptyLine_default.txt b/src/test/resources/CSVFileParser/testEmptyLine_default.txt new file mode 100644 index 0000000000..9ac42dd3a4 --- /dev/null +++ b/src/test/resources/CSVFileParser/testEmptyLine_default.txt @@ -0,0 +1,6 @@ +testEmptyLine.csv CommentStart=# IgnoreEmpty=true IgnoreSpaces=true +Delimiter=<,> QuoteChar=<"> CommentStart=<#> EmptyLines:ignored SurroundingSpaces:ignored SkipHeaderRecord:false +0:[] +0:[] +0:[] +0:[] From 8a3ca1a6eb757ecd4897f09a2d62d6509a031234 Mon Sep 17 00:00:00 2001 From: Attilio Date: Sun, 25 Jun 2017 09:41:38 +0200 Subject: [PATCH 2/4] Empty lines containing white spaces do not generate a record. --- .../java/org/apache/commons/csv/Lexer.java | 8 ++++++++ .../resources/CSVFileParser/testEmptyLine.csv | 18 +++++++++--------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/Lexer.java b/src/main/java/org/apache/commons/csv/Lexer.java index 0329c356a6..338c198426 100644 --- a/src/main/java/org/apache/commons/csv/Lexer.java +++ b/src/main/java/org/apache/commons/csv/Lexer.java @@ -87,6 +87,14 @@ Token nextToken(final Token token) throws IOException { // read the next char and set eol int c = reader.read(); + + if (ignoreSurroundingSpaces) { + while (c == ' ' || c == '\t') { + c = reader.read(); + } + } + + /* * Note: The following call will swallow LF if c == CR. But we don't need to know if the last char was CR or LF * - they are equivalent here. diff --git a/src/test/resources/CSVFileParser/testEmptyLine.csv b/src/test/resources/CSVFileParser/testEmptyLine.csv index 146c40224f..0a94043500 100644 --- a/src/test/resources/CSVFileParser/testEmptyLine.csv +++ b/src/test/resources/CSVFileParser/testEmptyLine.csv @@ -1,9 +1,9 @@ -# totally empty line "" - -# line with one space " " - -# line with one tab "\t" - -# line with one space and tab " \t" - - +# totally empty line "" + +# line with one space " " + +# line with one tab "\t" + +# line with one space and tab " \t" + + From 453c7d1b6ee8c6de925225579a5f47e0dc2c22af Mon Sep 17 00:00:00 2001 From: Attilio Date: Sun, 25 Jun 2017 09:49:40 +0200 Subject: [PATCH 3/4] Only skip white space if that is not the delimiter. --- src/main/java/org/apache/commons/csv/Lexer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/apache/commons/csv/Lexer.java b/src/main/java/org/apache/commons/csv/Lexer.java index 338c198426..59821533cb 100644 --- a/src/main/java/org/apache/commons/csv/Lexer.java +++ b/src/main/java/org/apache/commons/csv/Lexer.java @@ -89,7 +89,7 @@ Token nextToken(final Token token) throws IOException { int c = reader.read(); if (ignoreSurroundingSpaces) { - while (c == ' ' || c == '\t') { + while ((delimiter != ' ' && c == ' ') || (delimiter != '\t' && c == '\t')) { c = reader.read(); } } From e37be6168f18c0df475aecae7d15ddc650413e10 Mon Sep 17 00:00:00 2001 From: Attilio Date: Sun, 25 Jun 2017 09:51:27 +0200 Subject: [PATCH 4/4] Remove unnecessary test cases. --- src/test/resources/CSVFileParser/testEmptyLine_default.txt | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/test/resources/CSVFileParser/testEmptyLine_default.txt b/src/test/resources/CSVFileParser/testEmptyLine_default.txt index 9ac42dd3a4..68ad0f488e 100644 --- a/src/test/resources/CSVFileParser/testEmptyLine_default.txt +++ b/src/test/resources/CSVFileParser/testEmptyLine_default.txt @@ -1,6 +1,3 @@ testEmptyLine.csv CommentStart=# IgnoreEmpty=true IgnoreSpaces=true Delimiter=<,> QuoteChar=<"> CommentStart=<#> EmptyLines:ignored SurroundingSpaces:ignored SkipHeaderRecord:false 0:[] -0:[] -0:[] -0:[]