Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add an option for quoting values of the first column that start with the comment character #505 #518

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ protected DummyFormat createDefaultFormat() {
};
private final int errorContentLength;

protected final boolean processComments;

/**
* All writers must support, at the very least, the settings provided by {@link CommonWriterSettings}. The AbstractWriter requires its configuration to be
* properly initialized.
Expand Down Expand Up @@ -202,6 +204,7 @@ public AbstractWriter(Writer writer, S settings) {
this.expandRows = settings.getExpandIncompleteRows();
this.columnReorderingEnabled = settings.isColumnReorderingEnabled();
this.whitespaceRangeStart = settings.getWhitespaceRangeStart();
this.processComments = settings.isCommentProcessingEnabled();
this.appender = new WriterCharAppender(settings.getMaxCharsPerColumn(), "", whitespaceRangeStart, settings.getFormat());
this.rowAppender = new WriterCharAppender(settings.getMaxCharsPerColumn(), "", whitespaceRangeStart, settings.getFormat());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public abstract class CommonWriterSettings<F extends Format> extends CommonSetti

private boolean columnReorderingEnabled = false;

private boolean commentProcessingEnabled = true;

/**
* Returns the String representation of an empty value (defaults to null)
*
Expand Down Expand Up @@ -228,4 +230,27 @@ public boolean isColumnReorderingEnabled() {
public void setColumnReorderingEnabled(boolean columnReorderingEnabled) {
this.columnReorderingEnabled = columnReorderingEnabled;
}

/**
* Indicates whether the writer will check for comment lines is enabled.
* If {@code true}, the writer will always quote values of the first column that start with the comment character.
* Defaults to {@code true}
*
* @return flag indicating whether the writer will check for comment lines
* If disabled/false then writer wont quote values of the first column that start with the comment character.
*/
public boolean isCommentProcessingEnabled() {
return commentProcessingEnabled;
}

/**
* Configures whether the writer will check for comment lines.
* Defaults to {@code true}
*
* @param commentProcessingEnabled flag determining whether comment lines check should be performed
* If disabled/false then writer wont quote values of the first column that start with the comment character.
*/
public void setCommentProcessingEnabled(boolean commentProcessingEnabled) {
this.commentProcessingEnabled = commentProcessingEnabled;
}
}
2 changes: 1 addition & 1 deletion src/main/java/com/univocity/parsers/csv/CsvWriter.java
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ private boolean append(int columnIndex, boolean isElementQuoted, boolean allowTr
}

final int length = element.length();
if (start < length && (element.charAt(start) == quoteChar || columnIndex == 0 && element.charAt(0) == comment)) {
if (start < length && (element.charAt(start) == quoteChar || columnIndex == 0 && element.charAt(0) == comment && processComments)) {
isElementQuoted = true;
}

Expand Down
42 changes: 42 additions & 0 deletions src/test/java/com/univocity/parsers/issues/github/Github_505.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.univocity.parsers.issues.github;

import com.univocity.parsers.csv.CsvParser;
import com.univocity.parsers.csv.CsvParserSettings;
import com.univocity.parsers.csv.CsvWriter;
import com.univocity.parsers.csv.CsvWriterSettings;
import org.testng.annotations.Test;

import java.io.StringReader;
import java.io.StringWriter;
import java.util.List;

import static org.testng.Assert.assertEquals;

/**
* From: https://github.com/uniVocity/univocity-parsers/issues/505
*/
public class Github_505 {

@Test
public void testCommentCharWriting() {
StringWriter sw1 = new StringWriter();
{
CsvWriterSettings writerSettings = new CsvWriterSettings();
writerSettings.setCommentProcessingEnabled(false);
CsvWriter writer = new CsvWriter(sw1, writerSettings);
writer.writeRow(new String[]{"#field1", "field2"});
writer.close();
}
assertEquals(sw1.toString(), "#field1,field2\n");

StringWriter sw2 = new StringWriter();
{
CsvWriterSettings writerSettings = new CsvWriterSettings();
writerSettings.setCommentProcessingEnabled(true);
CsvWriter writer = new CsvWriter(sw2, writerSettings);
writer.writeRow(new String[]{"#field1", "field2"});
writer.close();
}
assertEquals(sw2.toString(), "\"#field1\",field2\n");
}
}