Skip to content
Merged
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
5 changes: 3 additions & 2 deletions src/main/java/org/apache/commons/csv/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2454,10 +2454,11 @@ private void printWithQuotes(final Object object, final CharSequence charSeq, fi
}
} else {
char c = charSeq.charAt(pos);
if (c <= Constants.COMMENT) {
if (c <= Constants.COMMENT || isCommentMarkerSet() && c == commentMarker.charValue()) {
// Some other chars at the start of a value caused the parser to fail, so for now
// encapsulate if we start in anything less than '#'. We are being conservative
// by including the default comment char too.
// by including the default comment char and any configured comment marker too,
// which the parser would otherwise read back as a comment line.
quote = true;
} else {
while (pos < len) {
Expand Down
27 changes: 27 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1829,6 +1829,33 @@ void testQuoteCommaFirstChar() throws IOException {
}
}

@Test
void testQuoteCommentMarkerFirstChar() throws IOException {
final CSVFormat format = CSVFormat.DEFAULT.builder().setCommentMarker(';').get();
final StringWriter sw = new StringWriter();
final String col1 = ";comment-like";
try (CSVPrinter printer = new CSVPrinter(sw, format)) {
// A real comment is written with the marker, unquoted.
printer.printComment("a real comment");
// A value starting with the marker is quoted, so it does not read back as a comment.
printer.printRecord(col1, "b");
// The marker past the first character does not start a comment, so only the leading-marker value is quoted.
printer.printRecord("a;b", ";c");
}
assertEquals("; a real comment" + RECORD_SEPARATOR +
"\";comment-like\",b" + RECORD_SEPARATOR +
"a;b,\";c\"" + RECORD_SEPARATOR, sw.toString());
// The comment is dropped on read; both data records survive intact.
try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
final List<CSVRecord> records = parser.getRecords();
assertEquals(2, records.size());
assertEquals(col1, records.get(0).get(0));
assertEquals("b", records.get(0).get(1));
assertEquals("a;b", records.get(1).get(0));
assertEquals(";c", records.get(1).get(1));
}
}

@Test
void testQuoteNonNumeric() throws IOException {
final StringWriter sw = new StringWriter();
Expand Down
Loading