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
8 changes: 6 additions & 2 deletions src/main/java/org/apache/commons/csv/CSVFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -2324,12 +2324,14 @@ private void printWithEscapes(final CharSequence charSeq, final Appendable appen
final char[] delimArray = getDelimiterCharArray();
final int delimLength = delimArray.length;
final char escape = getEscapeChar();
final boolean quoteSet = isQuoteCharacterSet();
final char quote = quoteSet ? getQuoteCharacter().charValue() : 0;
while (pos < end) {
char c = charSeq.charAt(pos);
final boolean isDelimiterStart = isDelimiter(c, charSeq, pos, delimArray, delimLength);
final boolean isCr = c == Constants.CR;
final boolean isLf = c == Constants.LF;
if (isCr || isLf || c == escape || isDelimiterStart) {
if (isCr || isLf || c == escape || quoteSet && c == quote || isDelimiterStart) {
// write out segment up until this char
if (pos > start) {
appendable.append(charSeq, start, pos);
Expand Down Expand Up @@ -2368,6 +2370,8 @@ private void printWithEscapes(final Reader reader, final Appendable appendable)
final char[] delimArray = getDelimiterCharArray();
final int delimLength = delimArray.length;
final char escape = getEscapeChar();
final boolean quoteSet = isQuoteCharacterSet();
final char quote = quoteSet ? getQuoteCharacter().charValue() : 0;
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
int c;
final char[] lookAheadBuffer = new char[delimLength - 1];
Expand All @@ -2379,7 +2383,7 @@ private void printWithEscapes(final Reader reader, final Appendable appendable)
final boolean isDelimiterStart = isDelimiter((char) c, test, pos, delimArray, delimLength);
final boolean isCr = c == Constants.CR;
final boolean isLf = c == Constants.LF;
if (isCr || isLf || c == escape || isDelimiterStart) {
if (isCr || isLf || c == escape || quoteSet && c == quote || isDelimiterStart) {
// write out segment up until this char
if (pos > start) {
append(builder.substring(start, pos), appendable);
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVPrinterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,26 @@ void testDelimeterStringQuoteNone() throws IOException {
}
}

@Test
void testQuoteCharEscapedWithQuoteModeNone() throws IOException {
final CSVFormat format = CSVFormat.DEFAULT.builder().setQuote('"').setEscape('?').setQuoteMode(QuoteMode.NONE).get();
final StringWriter sw = new StringWriter();
try (CSVPrinter printer = new CSVPrinter(sw, format)) {
printer.printRecord("\"abc", "x\"y");
printer.printRecord(new StringReader("\"abc"), new StringReader("x\"y"));
}
assertEquals("?\"abc,x?\"y" + RECORD_SEPARATOR + "?\"abc,x?\"y" + RECORD_SEPARATOR, sw.toString());
// The emitted records must read back as the original values.
try (CSVParser parser = CSVParser.parse(sw.toString(), format)) {
final List<CSVRecord> records = parser.getRecords();
assertEquals(2, records.size());
for (final CSVRecord record : records) {
assertEquals("\"abc", record.get(0));
assertEquals("x\"y", record.get(1));
}
}
}

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