Don't allocate an extra String in CSVFormat.printWithEscapes(Reader,

Appendable)

Internal refactoring
This commit is contained in:
Gary Gregory 2024-03-11 16:28:42 -04:00
parent ac031dc386
commit 6f758dd01a
1 changed files with 17 additions and 15 deletions

View File

@ -1581,6 +1581,11 @@ public final class CSVFormat implements Serializable {
skipHeaderRecord == other.skipHeaderRecord && trailingDelimiter == other.trailingDelimiter && trim == other.trim; skipHeaderRecord == other.skipHeaderRecord && trailingDelimiter == other.trailingDelimiter && trim == other.trim;
} }
private void escape(char c, final Appendable appendable) throws IOException {
append(escapeCharacter.charValue(), appendable);
append(c, appendable);
}
/** /**
* Formats the specified values. * Formats the specified values.
* *
@ -1711,19 +1716,19 @@ public final class CSVFormat implements Serializable {
/** /**
* Gets the escape character. * Gets the escape character.
* *
* @return the escape character, may be {@code null} * @return the escape character, may be {@code 0}
*/ */
public Character getEscapeCharacter() { char getEscapeChar() {
return escapeCharacter; return escapeCharacter != null ? escapeCharacter.charValue() : 0;
} }
/** /**
* Gets the escape character. * Gets the escape character.
* *
* @return the escape character, may be {@code 0} * @return the escape character, may be {@code null}
*/ */
char getEscapeChar() { public Character getEscapeCharacter() {
return escapeCharacter != null ? escapeCharacter.charValue() : 0; return escapeCharacter;
} }
/** /**
@ -2162,14 +2167,13 @@ public final class CSVFormat implements Serializable {
} else if (isCr) { } else if (isCr) {
c = 'r'; c = 'r';
} }
appendable.append(escape); escape(c, appendable);
appendable.append(c);
if (isDelimiterStart) { if (isDelimiterStart) {
for (int i = 1; i < delimLength; i++) { for (int i = 1; i < delimLength; i++) {
pos++; pos++;
c = charSeq.charAt(pos); c = charSeq.charAt(pos);
appendable.append(escape); escape(c, appendable);
appendable.append(c);
} }
} }
start = pos + 1; // start on the current char after this one start = pos + 1; // start on the current char after this one
@ -2216,13 +2220,11 @@ public final class CSVFormat implements Serializable {
} else if (isCr) { } else if (isCr) {
c = 'r'; c = 'r';
} }
append(escape, appendable); escape((char) c, appendable);
append((char) c, appendable);
if (isDelimiterStart) { if (isDelimiterStart) {
for (int i = 1; i < delimLength; i++) { for (int i = 1; i < delimLength; i++) {
c = bufferedReader.read(); c = bufferedReader.read();
append(escape, appendable); escape((char) c, appendable);
append((char) c, appendable);
} }
} }
start = pos + 1; // start on the current char after this one start = pos + 1; // start on the current char after this one
@ -2231,7 +2233,7 @@ public final class CSVFormat implements Serializable {
} }
// write last segment // write last segment
if (pos > start) { if (pos > start) {
append(builder.substring(start, pos), appendable); appendable.append(builder, start, pos);
} }
} }