CSV-267 - Minor improvement (#126)

This commit is contained in:
Arturo Bernal 2020-12-12 20:04:59 +01:00 committed by GitHub
parent e5fbba3eb3
commit 64bae17317
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 16 additions and 15 deletions

View File

@ -1051,7 +1051,7 @@ public final class CSVFormat implements Serializable {
/** /**
* Returns whether to trim leading and trailing blanks. * Returns whether to trim leading and trailing blanks.
* This is used by {@link #print(Object, Appendable, boolean)} * This is used by {@link #print(Object, Appendable, boolean)}
* Also by {@link CSVParser#addRecordValue(boolean)} * Also by {CSVParser#addRecordValue(boolean)}
* *
* @return whether to trim leading and trailing blanks. * @return whether to trim leading and trailing blanks.
*/ */
@ -1322,8 +1322,7 @@ public final class CSVFormat implements Serializable {
private void printWithEscapes(final CharSequence value, final Appendable out) throws IOException { private void printWithEscapes(final CharSequence value, final Appendable out) throws IOException {
int start = 0; int start = 0;
int pos = 0; int pos = 0;
final int len = value.length(); final int end = value.length();
final int end = len;
final char delim = getDelimiter(); final char delim = getDelimiter();
final char escape = getEscapeCharacter().charValue(); final char escape = getEscapeCharacter().charValue();

View File

@ -179,7 +179,7 @@ public class CSVPrinterTest {
final char[] buf = new char[sz]; final char[] buf = new char[sz];
for (int i = 0; i < sz; i++) { for (int i = 0; i < sz; i++) {
// stick in special chars with greater frequency // stick in special chars with greater frequency
char ch; final char ch;
final int what = r.nextInt(20); final int what = r.nextInt(20);
switch (what) { switch (what) {
case 0: case 0:
@ -1342,7 +1342,7 @@ public class CSVPrinterTest {
final String rowData = StringUtils.join(values, ','); final String rowData = StringUtils.join(values, ',');
final CharArrayWriter charArrayWriter = new CharArrayWriter(0); final CharArrayWriter charArrayWriter = new CharArrayWriter(0);
try (final CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(rowData)); try (final CSVParser parser = CSVFormat.DEFAULT.parse(new StringReader(rowData));
CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) { final CSVPrinter csvPrinter = CSVFormat.INFORMIX_UNLOAD.print(charArrayWriter)) {
for (final CSVRecord record : parser) { for (final CSVRecord record : parser) {
csvPrinter.printRecord(record); csvPrinter.printRecord(record);
} }

View File

@ -31,7 +31,6 @@ import java.io.ObjectOutputStream;
import java.io.StringReader; import java.io.StringReader;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
@ -213,7 +212,7 @@ public class CSVRecordTest {
@Test @Test
public void testSerialization() throws IOException, ClassNotFoundException { public void testSerialization() throws IOException, ClassNotFoundException {
CSVRecord shortRec; final CSVRecord shortRec;
try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) { try (final CSVParser parser = CSVParser.parse("A,B\n#my comment\nOne,Two", CSVFormat.DEFAULT.withHeader().withCommentMarker('#'))) {
shortRec = parser.iterator().next(); shortRec = parser.iterator().next();
} }

View File

@ -88,7 +88,7 @@ public class PerformanceTest {
max = Integer.parseInt(args[0]); max = Integer.parseInt(args[0]);
} }
String tests[]; final String[] tests;
if (argc > 1) { if (argc > 1) {
tests = new String[argc - 1]; tests = new String[argc - 1];
for (int i = 1; i < argc; i++) { for (int i = 1; i < argc; i++) {

View File

@ -39,8 +39,9 @@ final class Utils {
* @param actual the List of {@link CSVRecord} entries, each containing an array of values * @param actual the List of {@link CSVRecord} entries, each containing an array of values
*/ */
public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual) { public static void compare(final String message, final String[][] expected, final List<CSVRecord> actual) {
assertEquals(expected.length, actual.size(), message + " - outer array size"); final int expectedLength = expected.length;
for (int i = 0; i < expected.length; i++) { assertEquals(expectedLength, actual.size(), message + " - outer array size");
for (int i = 0; i < expectedLength; i++) {
assertArrayEquals(expected[i], actual.get(i).values(), message + " (entry " + i + ")"); assertArrayEquals(expected[i], actual.get(i).values(), message + " (entry " + i + ")");
} }
} }

View File

@ -22,6 +22,7 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import org.apache.commons.csv.CSVFormat; import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser; import org.apache.commons.csv.CSVParser;
@ -37,7 +38,7 @@ public class JiraCsv198Test {
final InputStream pointsOfReference = getClass().getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv"); final InputStream pointsOfReference = getClass().getResourceAsStream("/org/apache/commons/csv/CSV-198/optd_por_public.csv");
assertNotNull(pointsOfReference); assertNotNull(pointsOfReference);
try (@SuppressWarnings("resource") try (@SuppressWarnings("resource")
CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, "UTF-8"))) { CSVParser parser = CSV_FORMAT.parse(new InputStreamReader(pointsOfReference, StandardCharsets.UTF_8))) {
for (final CSVRecord record : parser) { for (final CSVRecord record : parser) {
final String locationType = record.get("location_type"); final String locationType = record.get("location_type");
assertNotNull(locationType); assertNotNull(locationType);

View File

@ -46,7 +46,7 @@ public class JiraCsv248Test {
// CSVRecord rec = parser.iterator().next(); // CSVRecord rec = parser.iterator().next();
//} //}
try (InputStream in = getTestInput(); try (InputStream in = getTestInput();
ObjectInputStream ois = new ObjectInputStream(in)) { final ObjectInputStream ois = new ObjectInputStream(in)) {
final Object object = ois.readObject(); final Object object = ois.readObject();
assertTrue(object instanceof CSVRecord); assertTrue(object instanceof CSVRecord);
final CSVRecord rec = (CSVRecord) object; final CSVRecord rec = (CSVRecord) object;

View File

@ -40,7 +40,7 @@ public class JiraCsv249Test {
printer.printRecord("foo \\", "bar"); printer.printRecord("foo \\", "bar");
} }
final StringReader stringReader = new StringReader(stringWriter.toString()); final StringReader stringReader = new StringReader(stringWriter.toString());
List<CSVRecord> records; final List<CSVRecord> records;
try (CSVParser parser = new CSVParser(stringReader, csvFormat)) { try (CSVParser parser = new CSVParser(stringReader, csvFormat)) {
records = parser.getRecords(); records = parser.getRecords();
} }

View File

@ -203,11 +203,12 @@ public final class CSVPrinter implements Flushable, Closeable {
} }
out.append(format.getCommentMarker().charValue()); out.append(format.getCommentMarker().charValue());
out.append(SP); out.append(SP);
for (int i = 0; i < comment.length(); i++) { final int commentLength = comment.length();
for (int i = 0; i < commentLength; i++) {
final char c = comment.charAt(i); final char c = comment.charAt(i);
switch (c) { switch (c) {
case CR: case CR:
if (i + 1 < comment.length() && comment.charAt(i + 1) == LF) { if (i + 1 < commentLength && comment.charAt(i + 1) == LF) {
i++; i++;
} }
//$FALL-THROUGH$ break intentionally excluded. //$FALL-THROUGH$ break intentionally excluded.