diff --git a/src/main/java/org/apache/commons/csv/CSVLexer.java b/src/main/java/org/apache/commons/csv/CSVLexer.java index 15307c37..18f1f8d0 100644 --- a/src/main/java/org/apache/commons/csv/CSVLexer.java +++ b/src/main/java/org/apache/commons/csv/CSVLexer.java @@ -46,12 +46,6 @@ class CSVLexer extends Lexer { // read the next char and set eol int c = in.read(); - if (isStartOfLine(lastChar) && isCommentStart(c)) { - in.readLine(); - tkn.type = COMMENT; - return tkn; - } - /* note: unfortunately isEndOfLine may consumes a character silently. * this has no effect outside of the method. so a simple workaround * is to call 'readAgain' on the stream... @@ -83,6 +77,12 @@ class CSVLexer extends Lexer { return tkn; } + if (isStartOfLine(lastChar) && isCommentStart(c)) { + in.readLine(); + tkn.type = COMMENT; + return tkn; + } + // important: make sure a new char gets consumed in each iteration while (tkn.type == INVALID) { // ignore whitespaces at beginning of a token diff --git a/src/test/java/org/apache/commons/csv/CSVLexerTest.java b/src/test/java/org/apache/commons/csv/CSVLexerTest.java index b54b2309..195a4d07 100644 --- a/src/test/java/org/apache/commons/csv/CSVLexerTest.java +++ b/src/test/java/org/apache/commons/csv/CSVLexerTest.java @@ -58,11 +58,19 @@ public class CSVLexerTest { public void testNextToken2() throws IOException { final String code = "1,2,3,\n"+ // 1 + "\n"+ + "\n"+ "a,b x,c#no-comment\n"+ // 2 + "\n"+ + "\n"+ "#foo\n"+ // 3 "\n"+ // 4 "d,e,#no-comment\n"+ // 5 + "\n"+ + "\n"+ "# penultimate comment\n"+ // 6 + "\n"+ + "\n"+ "# Final comment\n"; // 7 CSVFormat format = CSVFormat.DEFAULT.withCommentStart('#'); assertTrue("Should ignore empty lines", format.isEmptyLinesIgnored());