Skip to content

Commit 2cded67

Browse files
authored
Merge pull request apache#603 from garydgregory/fix/CSV-324_clean_delim_buffer
Lexer.isDelimiter() accepts a partial multi-character delimiter at EOF.
2 parents c7c54e1 + b29fb40 commit 2cded67

3 files changed

Lines changed: 28 additions & 0 deletions

File tree

src/main/java/org/apache/commons/csv/Lexer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import java.io.Closeable;
2525
import java.io.IOException;
26+
import java.util.Arrays;
2627

2728
import org.apache.commons.io.IOUtils;
2829

@@ -272,6 +273,7 @@ Token nextToken(final Token token) throws IOException {
272273
token.type = Token.Type.COMMENT;
273274
return token;
274275
}
276+
Arrays.fill(delimiterBuf, '\0');
275277
// Important: make sure a new char gets consumed in each iteration
276278
while (token.type == Token.Type.INVALID) {
277279
// ignore whitespaces at beginning of a token

src/test/java/org/apache/commons/csv/CSVParserTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,20 @@ void testParsingPrintedEmptyFirstColumn(final CSVFormat.Predefined format) throw
16211621
}
16221622
}
16231623

1624+
/**
1625+
* Tests <a href="https://issues.apache.org/jira/browse/CSV-324">CSV-324</a>.
1626+
*/
1627+
@Test
1628+
void testPartialMultiCharacterDelimiterAtEOF() throws IOException {
1629+
final CSVFormat format = CSVFormat.DEFAULT.builder().setDelimiter("[|]").get();
1630+
try (CSVParser parser = format.parse(new StringReader("a[|]b[|"))) {
1631+
final CSVRecord record = parser.nextRecord();
1632+
assertEquals("a", record.get(0));
1633+
assertEquals("b[|", record.get(1));
1634+
assertEquals(2, record.size());
1635+
}
1636+
}
1637+
16241638
@Test
16251639
void testProvidedHeader() throws Exception {
16261640
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");

src/test/java/org/apache/commons/csv/LexerTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,18 @@ void testNextToken6() throws IOException {
409409
}
410410
}
411411

412+
/**
413+
* Tests <a href="https://issues.apache.org/jira/browse/CSV-324">CSV-324</a>.
414+
*/
415+
@Test
416+
void testPartialMultiCharacterDelimiterAtEOF() throws IOException {
417+
final CSVFormat format = CSVFormat.DEFAULT.builder().setDelimiter("[|]").get();
418+
try (Lexer lexer = createLexer("a[|]b[|", format)) {
419+
assertNextToken(TOKEN, "a", lexer);
420+
assertNextToken(EOF, "b[|", lexer);
421+
}
422+
}
423+
412424
@Test
413425
void testReadEscapeBackspace() throws IOException {
414426
try (Lexer lexer = createLexer("b", CSVFormat.DEFAULT.withEscape('\b'))) {

0 commit comments

Comments
 (0)