Skip to content

Commit 6f758dd

Browse files
committed
Don't allocate an extra String in CSVFormat.printWithEscapes(Reader,
Appendable) Internal refactoring
1 parent ac031dc commit 6f758dd

1 file changed

Lines changed: 17 additions & 15 deletions

File tree

src/main/java/org/apache/commons/csv/CSVFormat.java

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1581,6 +1581,11 @@ public boolean equals(final Object obj) {
15811581
skipHeaderRecord == other.skipHeaderRecord && trailingDelimiter == other.trailingDelimiter && trim == other.trim;
15821582
}
15831583

1584+
private void escape(char c, final Appendable appendable) throws IOException {
1585+
append(escapeCharacter.charValue(), appendable);
1586+
append(c, appendable);
1587+
}
1588+
15841589
/**
15851590
* Formats the specified values.
15861591
*
@@ -1711,19 +1716,19 @@ public DuplicateHeaderMode getDuplicateHeaderMode() {
17111716
/**
17121717
* Gets the escape character.
17131718
*
1714-
* @return the escape character, may be {@code null}
1719+
* @return the escape character, may be {@code 0}
17151720
*/
1716-
public Character getEscapeCharacter() {
1717-
return escapeCharacter;
1721+
char getEscapeChar() {
1722+
return escapeCharacter != null ? escapeCharacter.charValue() : 0;
17181723
}
17191724

17201725
/**
17211726
* Gets the escape character.
17221727
*
1723-
* @return the escape character, may be {@code 0}
1728+
* @return the escape character, may be {@code null}
17241729
*/
1725-
char getEscapeChar() {
1726-
return escapeCharacter != null ? escapeCharacter.charValue() : 0;
1730+
public Character getEscapeCharacter() {
1731+
return escapeCharacter;
17271732
}
17281733

17291734
/**
@@ -2162,14 +2167,13 @@ private void printWithEscapes(final CharSequence charSeq, final Appendable appen
21622167
} else if (isCr) {
21632168
c = 'r';
21642169
}
2165-
appendable.append(escape);
2166-
appendable.append(c);
2170+
escape(c, appendable);
21672171
if (isDelimiterStart) {
21682172
for (int i = 1; i < delimLength; i++) {
21692173
pos++;
21702174
c = charSeq.charAt(pos);
2171-
appendable.append(escape);
2172-
appendable.append(c);
2175+
escape(c, appendable);
2176+
21732177
}
21742178
}
21752179
start = pos + 1; // start on the current char after this one
@@ -2216,13 +2220,11 @@ private void printWithEscapes(final Reader reader, final Appendable appendable)
22162220
} else if (isCr) {
22172221
c = 'r';
22182222
}
2219-
append(escape, appendable);
2220-
append((char) c, appendable);
2223+
escape((char) c, appendable);
22212224
if (isDelimiterStart) {
22222225
for (int i = 1; i < delimLength; i++) {
22232226
c = bufferedReader.read();
2224-
append(escape, appendable);
2225-
append((char) c, appendable);
2227+
escape((char) c, appendable);
22262228
}
22272229
}
22282230
start = pos + 1; // start on the current char after this one
@@ -2231,7 +2233,7 @@ private void printWithEscapes(final Reader reader, final Appendable appendable)
22312233
}
22322234
// write last segment
22332235
if (pos > start) {
2234-
append(builder.substring(start, pos), appendable);
2236+
appendable.append(builder, start, pos);
22352237
}
22362238
}
22372239

0 commit comments

Comments
 (0)