From 5b2e5f809be63d301b04eaae9c72cae05e7da35c Mon Sep 17 00:00:00 2001 From: Sebastian Bazley Date: Tue, 7 May 2013 09:09:11 +0000 Subject: [PATCH] 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 --- .../org/apache/commons/csv/CSVPrinter.java | 26 ++++++------------- 1 file changed, 8 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVPrinter.java b/src/main/java/org/apache/commons/csv/CSVPrinter.java index b0ce460e..6a71ec60 100644 --- a/src/main/java/org/apache/commons/csv/CSVPrinter.java +++ b/src/main/java/org/apache/commons/csv/CSVPrinter.java @@ -163,35 +163,28 @@ public class CSVPrinter implements Flushable, Closeable { private void print(final Object object, final CharSequence value, final int offset, final int len) throws IOException { + if (!newRecord) { + out.append(format.getDelimiter()); + } if (format.isQuoting()) { // the original object is needed so can check for Number printAndQuote(object, value, offset, len); } else if (format.isEscaping()) { printAndEscape(value, offset, len); } else { - printDelimiter(); out.append(value, offset, offset + len); } - } - - void printDelimiter() throws IOException { - if (newRecord) { - newRecord = false; - } else { - out.append(format.getDelimiter()); - } + newRecord = false; } /* * 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 pos = offset; final int end = offset + len; - printDelimiter(); - final char delim = format.getDelimiter(); 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 */ // 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 boolean first = newRecord; // is this the first value on this line? boolean quote = false; int start = offset; int pos = offset; final int end = offset + len; - printDelimiter(); - final char delimChar = format.getDelimiter(); 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 // line. If it were not quoted in that case, // an empty line has no tokens. - if (first) { + if (newRecord) { quote = true; } } else { char c = value.charAt(pos); // 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; // } else if (c == ' ' || c == '\f' || c == '\t') { } else if (c <= COMMENT) {