Simplify code so delimiter only printed in one place
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1479823 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
978e5796e2
commit
5b2e5f809b
|
@ -163,35 +163,28 @@ public class CSVPrinter implements Flushable, Closeable {
|
||||||
|
|
||||||
private void print(final Object object, final CharSequence value,
|
private void print(final Object object, final CharSequence value,
|
||||||
final int offset, final int len) throws IOException {
|
final int offset, final int len) throws IOException {
|
||||||
|
if (!newRecord) {
|
||||||
|
out.append(format.getDelimiter());
|
||||||
|
}
|
||||||
if (format.isQuoting()) {
|
if (format.isQuoting()) {
|
||||||
// the original object is needed so can check for Number
|
// the original object is needed so can check for Number
|
||||||
printAndQuote(object, value, offset, len);
|
printAndQuote(object, value, offset, len);
|
||||||
} else if (format.isEscaping()) {
|
} else if (format.isEscaping()) {
|
||||||
printAndEscape(value, offset, len);
|
printAndEscape(value, offset, len);
|
||||||
} else {
|
} else {
|
||||||
printDelimiter();
|
|
||||||
out.append(value, offset, offset + len);
|
out.append(value, offset, offset + len);
|
||||||
}
|
}
|
||||||
}
|
newRecord = false;
|
||||||
|
|
||||||
void printDelimiter() throws IOException {
|
|
||||||
if (newRecord) {
|
|
||||||
newRecord = false;
|
|
||||||
} else {
|
|
||||||
out.append(format.getDelimiter());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Note: must only be called if escaping is enabled, otherwise will generate NPE
|
* Note: must only be called if escaping is enabled, otherwise will generate NPE
|
||||||
*/
|
*/
|
||||||
void printAndEscape(final CharSequence value, final int offset, final int len) throws IOException {
|
private void printAndEscape(final CharSequence value, final int offset, final int len) throws IOException {
|
||||||
int start = offset;
|
int start = offset;
|
||||||
int pos = offset;
|
int pos = offset;
|
||||||
final int end = offset + len;
|
final int end = offset + len;
|
||||||
|
|
||||||
printDelimiter();
|
|
||||||
|
|
||||||
final char delim = format.getDelimiter();
|
final char delim = format.getDelimiter();
|
||||||
final char escape = format.getEscape().charValue();
|
final char escape = format.getEscape().charValue();
|
||||||
|
|
||||||
|
@ -227,16 +220,13 @@ public class CSVPrinter implements Flushable, Closeable {
|
||||||
* Note: must only be called if quoting is enabled, otherwise will generate NPE
|
* Note: must only be called if quoting is enabled, otherwise will generate NPE
|
||||||
*/
|
*/
|
||||||
// the original object is needed so can check for Number
|
// the original object is needed so can check for Number
|
||||||
void printAndQuote(final Object object, final CharSequence value,
|
private void printAndQuote(final Object object, final CharSequence value,
|
||||||
final int offset, final int len) throws IOException {
|
final int offset, final int len) throws IOException {
|
||||||
final boolean first = newRecord; // is this the first value on this line?
|
|
||||||
boolean quote = false;
|
boolean quote = false;
|
||||||
int start = offset;
|
int start = offset;
|
||||||
int pos = offset;
|
int pos = offset;
|
||||||
final int end = offset + len;
|
final int end = offset + len;
|
||||||
|
|
||||||
printDelimiter();
|
|
||||||
|
|
||||||
final char delimChar = format.getDelimiter();
|
final char delimChar = format.getDelimiter();
|
||||||
final char quoteChar = format.getQuoteChar().charValue();
|
final char quoteChar = format.getQuoteChar().charValue();
|
||||||
|
|
||||||
|
@ -259,14 +249,14 @@ public class CSVPrinter implements Flushable, Closeable {
|
||||||
// on the line, as it may be the only thing on the
|
// on the line, as it may be the only thing on the
|
||||||
// line. If it were not quoted in that case,
|
// line. If it were not quoted in that case,
|
||||||
// an empty line has no tokens.
|
// an empty line has no tokens.
|
||||||
if (first) {
|
if (newRecord) {
|
||||||
quote = true;
|
quote = true;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
char c = value.charAt(pos);
|
char c = value.charAt(pos);
|
||||||
|
|
||||||
// Hmmm, where did this rule come from?
|
// Hmmm, where did this rule come from?
|
||||||
if (first && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
|
if (newRecord && (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || (c > 'z'))) {
|
||||||
quote = true;
|
quote = true;
|
||||||
// } else if (c == ' ' || c == '\f' || c == '\t') {
|
// } else if (c == ' ' || c == '\f' || c == '\t') {
|
||||||
} else if (c <= COMMENT) {
|
} else if (c <= COMMENT) {
|
||||||
|
|
Loading…
Reference in New Issue