From 8840d1de81b06fe41a7ec115ccd716953d3f92d5 Mon Sep 17 00:00:00 2001 From: Tatu Saloranta Date: Fri, 28 May 2021 13:58:13 -0700 Subject: [PATCH] Add releates notes wrt #270, minor refactoring of a check --- .../dataformat/csv/impl/CsvEncoder.java | 38 +++++++++---------- release-notes/CREDITS-2.x | 4 ++ release-notes/VERSION-2.x | 3 ++ 3 files changed, 25 insertions(+), 20 deletions(-) diff --git a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvEncoder.java b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvEncoder.java index ddc48049..f56b6f4d 100644 --- a/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvEncoder.java +++ b/csv/src/main/java/com/fasterxml/jackson/dataformat/csv/impl/CsvEncoder.java @@ -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 @@ -96,6 +94,9 @@ public class CsvEncoder */ protected boolean _cfgOptimalQuoting; + // @since 2.13 + final protected boolean _cfgAllowsComments; + /** * @since 2.4 */ @@ -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) { @@ -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; @@ -1036,10 +1042,6 @@ protected boolean _mayNeedQuotes(String value, int length, int columnIndex) } /** - *

- * NOTE: final since checking is not expected to be changed here; override - * calling method (_mayNeedQuotes) instead, if necessary. - * * @since 2.4 */ protected final boolean _needsQuotingLoose(String value) @@ -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; @@ -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; } } @@ -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; @@ -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) { diff --git a/release-notes/CREDITS-2.x b/release-notes/CREDITS-2.x index f0158614..bfa2efc4 100644 --- a/release-notes/CREDITS-2.x +++ b/release-notes/CREDITS-2.x @@ -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) diff --git a/release-notes/VERSION-2.x b/release-notes/VERSION-2.x index d1005d84..1782357f 100644 --- a/release-notes/VERSION-2.x +++ b/release-notes/VERSION-2.x @@ -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)