Skip to content

Commit

Permalink
Add releates notes wrt #270, minor refactoring of a check
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed May 28, 2021
1 parent 3f33e43 commit 8840d1d
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,9 @@ public class CsvEncoder
*/
final protected char[] _cfgNullValue;

final protected boolean _cfgAllowsComments;

final protected int _cfgLineSeparatorLength;

protected int _cfgMaxQuoteCheckChars;
final protected int _cfgMaxQuoteCheckChars;

/**
* Lowest-valued character that is safe to output without using
Expand All @@ -96,6 +94,9 @@ public class CsvEncoder
*/
protected boolean _cfgOptimalQuoting;

// @since 2.13
final protected boolean _cfgAllowsComments;

/**
* @since 2.4
*/
Expand Down Expand Up @@ -534,9 +535,8 @@ protected void appendValue(String value) throws IOException
if (_nextColumnToWrite > 0) {
appendColumnSeparator();
}
/* First: determine if we need quotes; simple heuristics;
* only check for short Strings, stop if something found
*/
// First: determine if we need quotes; simple heuristics;
// only check for short Strings, stop if something found
final int len = value.length();
if (_cfgAlwaysQuoteStrings || _mayNeedQuotes(value, len, _nextColumnToWrite)) {
if (_cfgEscapeCharacter > 0) {
Expand Down Expand Up @@ -1018,10 +1018,16 @@ protected boolean _mayNeedQuotes(String value, int length, int columnIndex)
}
// may skip checks unless we want exact checking
if (_cfgOptimalQuoting) {
// 31-Dec-2014, tatu: Comment lines start with # so quote if starts with #
// 28-May-2021, tatu: As per [dataformats-text#270] only check if first column
if (_cfgAllowsComments && (columnIndex == 0)
&& (length > 0) && (value.charAt(0) == '#')) {
return true;
}
if (_cfgEscapeCharacter > 0) {
return _needsQuotingStrict(value, columnIndex, _cfgEscapeCharacter);
return _needsQuotingStrict(value, _cfgEscapeCharacter);
}
return _needsQuotingStrict(value, columnIndex);
return _needsQuotingStrict(value);
}
if (length > _cfgMaxQuoteCheckChars) {
return true;
Expand All @@ -1036,10 +1042,6 @@ protected boolean _mayNeedQuotes(String value, int length, int columnIndex)
}

/**
*<p>
* NOTE: final since checking is not expected to be changed here; override
* calling method (<code>_mayNeedQuotes</code>) instead, if necessary.
*
* @since 2.4
*/
protected final boolean _needsQuotingLoose(String value)
Expand Down Expand Up @@ -1072,7 +1074,7 @@ protected final boolean _needsQuotingLoose(String value, int esc)
/**
* @since 2.4
*/
protected boolean _needsQuotingStrict(String value, int columnIndex)
protected boolean _needsQuotingStrict(String value)
{
final int minSafe = _cfgMinSafeChar;

Expand All @@ -1087,9 +1089,7 @@ protected boolean _needsQuotingStrict(String value, int columnIndex)
if (c < minSafe) {
if (c == _cfgColumnSeparator || c == _cfgQuoteCharacter
|| (c < escLen && escCodes[c] != 0)
|| (c == lfFirst)
// 31-Dec-2014, tatu: Comment lines start with # so quote if starts with #
|| (columnIndex == 0 && _cfgAllowsComments && c == '#' && i == 0)) {
|| (c == lfFirst)) {
return true;
}
}
Expand All @@ -1100,7 +1100,7 @@ protected boolean _needsQuotingStrict(String value, int columnIndex)
/**
* @since 2.7
*/
protected boolean _needsQuotingStrict(String value, int columnIndex, int esc)
protected boolean _needsQuotingStrict(String value, int esc)
{
final int minSafe = _cfgMinSafeChar;
final int[] escCodes = _outputEscapes;
Expand All @@ -1114,9 +1114,7 @@ protected boolean _needsQuotingStrict(String value, int columnIndex, int esc)
if (c < minSafe) {
if (c == _cfgColumnSeparator || c == _cfgQuoteCharacter
|| (c < escLen && escCodes[c] != 0)
|| (c == lfFirst)
// 31-Dec-2014, tatu: Comment lines start with # so quote if starts with #
|| (columnIndex == 0 && _cfgAllowsComments && c == '#' && i == 0)) {
|| (c == lfFirst)) {
return true;
}
} else if (c == esc) {
Expand Down
4 changes: 4 additions & 0 deletions release-notes/CREDITS-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,7 @@ Jonas Konrad (yawkat@github)
* Contributed #219: Add TOML (https://en.wikipedia.org/wiki/TOML) support
(2.13.0)

Krzysztof Debski (kdebski85@github)
* Contributed #270: Should not quote with strict quoting when line starts with `#` but comments
are disabled
(2.13.0)
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ Active Maintainers:
#240: (csv) Split `CsvMappingException` into `CsvReadException`/`CsvWriteException`
#255: (properties) Ensure that empty String to null/empty works by default
for Properties format
#270: Should not quote with strict quoting when line starts with `#` but comments
are disabled
(contributed by Krzysztof D)

2.12.3 (12-Apr-2021)

Expand Down

0 comments on commit 8840d1d

Please sign in to comment.