Skip to content

Commit 66a56ee

Browse files
committed
Refactor magic strings into constants.
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1397534 13f79535-47bb-0310-9956-ffa450edef68
1 parent a5cd9dc commit 66a56ee

1 file changed

Lines changed: 18 additions & 12 deletions

File tree

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
*/
2626
public class CSVPrinter {
2727

28+
private static final char COMMENT = '#';
29+
private static final String EMPTY = "";
30+
private static final char SP = ' ';
31+
private static final char CR = '\r';
32+
private static final char LF = '\n';
33+
2834
/** The place that the values get written. */
2935
private final Appendable out;
3036
private final CSVFormat format;
@@ -106,19 +112,19 @@ public void printComment(final String comment) throws IOException {
106112
println();
107113
}
108114
out.append(format.getCommentStart());
109-
out.append(' ');
115+
out.append(SP);
110116
for (int i = 0; i < comment.length(); i++) {
111117
final char c = comment.charAt(i);
112118
switch (c) {
113-
case '\r':
114-
if (i + 1 < comment.length() && comment.charAt(i + 1) == '\n') {
119+
case CR:
120+
if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) {
115121
i++;
116122
}
117123
//$FALL-THROUGH$ break intentionally excluded.
118-
case '\n':
124+
case LF:
119125
println();
120126
out.append(format.getCommentStart());
121-
out.append(' ');
127+
out.append(SP);
122128
break;
123129
default:
124130
out.append(c);
@@ -159,14 +165,14 @@ void printAndEscape(final CharSequence value, final int offset, final int len) t
159165

160166
while (pos < end) {
161167
char c = value.charAt(pos);
162-
if (c == '\r' || c == '\n' || c == delim || c == escape) {
168+
if (c == CR || c == LF || c == delim || c == escape) {
163169
// write out segment up until this char
164170
if (pos > start) {
165171
out.append(value, start, pos);
166172
}
167-
if (c == '\n') {
173+
if (c == LF) {
168174
c = 'n';
169-
} else if (c == '\r') {
175+
} else if (c == CR) {
170176
c = 'r';
171177
}
172178

@@ -212,15 +218,15 @@ void printAndEncapsulate(final CharSequence value, final int offset, final int l
212218
if (first && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
213219
quote = true;
214220
// } else if (c == ' ' || c == '\f' || c == '\t') {
215-
} else if (c <= '#') {
221+
} else if (c <= COMMENT) {
216222
// Some other chars at the start of a value caused the parser to fail, so for now
217223
// encapsulate if we start in anything less than '#'. We are being conservative
218224
// by including the default comment char too.
219225
quote = true;
220226
} else {
221227
while (pos < end) {
222228
c = value.charAt(pos);
223-
if (c == '\n' || c == '\r' || c == encapsulator || c == delim) {
229+
if (c == LF || c == CR || c == encapsulator || c == delim) {
224230
quote = true;
225231
break;
226232
}
@@ -233,7 +239,7 @@ void printAndEncapsulate(final CharSequence value, final int offset, final int l
233239
// if (c == ' ' || c == '\f' || c == '\t') {
234240
// Some other chars at the end caused the parser to fail, so for now
235241
// encapsulate if we end in anything less than ' '
236-
if (c <= ' ') {
242+
if (c <= SP) {
237243
quote = true;
238244
}
239245
}
@@ -280,7 +286,7 @@ void printAndEncapsulate(final CharSequence value, final int offset, final int l
280286
public void print(String value, final boolean checkForEscape) throws IOException {
281287
if (value == null) {
282288
// null values are considered empty
283-
value = "";
289+
value = EMPTY;
284290
}
285291

286292
if (!checkForEscape) {

0 commit comments

Comments
 (0)