Internal refactoring

Reduce whitepace
This commit is contained in:
Gary Gregory 2024-03-11 09:29:41 -04:00
parent 1eb95dc4d9
commit 5e5e28c551
6 changed files with 46 additions and 47 deletions

View File

@ -28,6 +28,7 @@ import static org.apache.commons.csv.Constants.LF;
import static org.apache.commons.csv.Constants.PIPE;
import static org.apache.commons.csv.Constants.SP;
import static org.apache.commons.csv.Constants.TAB;
import static org.apache.commons.io.IOUtils.EOF;
import java.io.File;
import java.io.FileOutputStream;
@ -2172,7 +2173,7 @@ public final class CSVFormat implements Serializable {
final char escape = getEscapeCharacter().charValue();
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
int c;
while (-1 != (c = bufferedReader.read())) {
while (EOF != (c = bufferedReader.read())) {
builder.append((char) c);
final boolean isDelimiterStart = isDelimiter((char) c, builder.toString() + new String(bufferedReader.lookAhead(delimLength - 1)), pos, delim,
delimLength);
@ -2321,12 +2322,12 @@ public final class CSVFormat implements Serializable {
printWithEscapes(reader, appendable);
return;
}
int pos = 0;
final char quote = getQuoteCharacter().charValue();
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
append(quote, appendable);
final StringBuilder builder = new StringBuilder(IOUtils.DEFAULT_BUFFER_SIZE);
int c;
while (-1 != (c = reader.read())) {
int pos = 0;
while (EOF != (c = reader.read())) {
builder.append((char) c);
if (c == quote) {
// write out segment up until this char

View File

@ -18,7 +18,7 @@
package org.apache.commons.csv;
/**
* Constants for this package.
* Private constants to this package.
*/
final class Constants {
@ -44,9 +44,6 @@ final class Constants {
static final String[] EMPTY_STRING_ARRAY = {};
/** The end of stream symbol */
static final int END_OF_STREAM = -1;
static final char FF = '\f';
static final char LF = '\n';

View File

@ -18,9 +18,9 @@
package org.apache.commons.csv;
import static org.apache.commons.csv.Constants.CR;
import static org.apache.commons.csv.Constants.END_OF_STREAM;
import static org.apache.commons.csv.Constants.LF;
import static org.apache.commons.csv.Constants.UNDEFINED;
import static org.apache.commons.io.IOUtils.EOF;
import java.io.BufferedReader;
import java.io.IOException;
@ -63,7 +63,7 @@ final class ExtendedBufferedReader extends BufferedReader {
public void close() throws IOException {
// Set ivars before calling super close() in case close() throws an IOException.
closed = true;
lastChar = END_OF_STREAM;
lastChar = EOF;
super.close();
}
@ -74,7 +74,7 @@ final class ExtendedBufferedReader extends BufferedReader {
*/
long getCurrentLineNumber() {
// Check if we are at EOL or EOF or just starting
if (lastChar == CR || lastChar == LF || lastChar == UNDEFINED || lastChar == END_OF_STREAM) {
if (lastChar == CR || lastChar == LF || lastChar == UNDEFINED || lastChar == EOF) {
return eolCounter; // counter is accurate
}
return eolCounter + 1; // Allow for counter being incremented only at EOL
@ -84,7 +84,7 @@ final class ExtendedBufferedReader extends BufferedReader {
* Returns the last character that was read as an integer (0 to 65535). This will be the last character returned by
* any of the read methods. This will not include a character read using the {@link #lookAhead()} method. If no
* character has been read then this will return {@link Constants#UNDEFINED}. If the end of the stream was reached
* on the last read then this will return {@link Constants#END_OF_STREAM}.
* on the last read then this will return {@link Constants#EOF}.
*
* @return the last character that was read
*/
@ -158,7 +158,7 @@ final class ExtendedBufferedReader extends BufferedReader {
public int read() throws IOException {
final int current = super.read();
if (current == CR || current == LF && lastChar != CR ||
current == END_OF_STREAM && lastChar != CR && lastChar != LF && lastChar != END_OF_STREAM) {
current == EOF && lastChar != CR && lastChar != LF && lastChar != EOF) {
eolCounter++;
}
lastChar = current;
@ -189,8 +189,8 @@ final class ExtendedBufferedReader extends BufferedReader {
lastChar = buf[offset + len - 1];
} else if (len == -1) {
lastChar = END_OF_STREAM;
} else if (len == EOF) {
lastChar = EOF;
}
position += len;
@ -204,14 +204,14 @@ final class ExtendedBufferedReader extends BufferedReader {
* Increments {@link #eolCounter} and updates {@link #position}.
* </p>
* <p>
* Sets {@link #lastChar} to {@link Constants#END_OF_STREAM} at EOF, otherwise the last EOL character.
* Sets {@link #lastChar} to {@link Constants#EOF} at EOF, otherwise the last EOL character.
* </p>
*
* @return the line that was read, or null if reached EOF.
*/
@Override
public String readLine() throws IOException {
if (lookAhead() == END_OF_STREAM) {
if (lookAhead() == EOF) {
return null;
}
final StringBuilder buffer = new StringBuilder();
@ -223,7 +223,7 @@ final class ExtendedBufferedReader extends BufferedReader {
read();
}
}
if (current == END_OF_STREAM || current == LF || current == CR) {
if (current == EOF || current == LF || current == CR) {
break;
}
buffer.append((char) current);

View File

@ -19,16 +19,15 @@ package org.apache.commons.csv;
import static org.apache.commons.csv.Constants.BACKSPACE;
import static org.apache.commons.csv.Constants.CR;
import static org.apache.commons.csv.Constants.END_OF_STREAM;
import static org.apache.commons.csv.Constants.FF;
import static org.apache.commons.csv.Constants.LF;
import static org.apache.commons.csv.Constants.TAB;
import static org.apache.commons.csv.Constants.UNDEFINED;
import static org.apache.commons.csv.Token.Type.COMMENT;
import static org.apache.commons.csv.Token.Type.EOF;
import static org.apache.commons.csv.Token.Type.EORECORD;
import static org.apache.commons.csv.Token.Type.INVALID;
import static org.apache.commons.csv.Token.Type.TOKEN;
import static org.apache.commons.io.IOUtils.EOF;
import java.io.Closeable;
import java.io.IOException;
@ -141,7 +140,7 @@ final class Lexer implements Closeable {
}
}
final int count = reader.read(delimiterBuf, 0, delimiterBuf.length);
isLastTokenDelimiter = count != END_OF_STREAM;
isLastTokenDelimiter = count != EOF;
return isLastTokenDelimiter;
}
@ -151,7 +150,7 @@ final class Lexer implements Closeable {
* @return true if the given character indicates the end of the file.
*/
boolean isEndOfFile(final int ch) {
return ch == END_OF_STREAM;
return ch == EOF;
}
/**
@ -182,7 +181,7 @@ final class Lexer implements Closeable {
}
}
final int count = reader.read(escapeDelimiterBuf, 0, escapeDelimiterBuf.length);
return count != END_OF_STREAM;
return count != EOF;
}
private boolean isMetaChar(final int ch) {
@ -240,7 +239,7 @@ final class Lexer implements Closeable {
eol = readEndOfLine(c);
// reached the end of the file without any content (empty line at the end)
if (isEndOfFile(c)) {
token.type = EOF;
token.type = Token.Type.EOF;
// don't set token.isReady here because no content
return token;
}
@ -249,7 +248,7 @@ final class Lexer implements Closeable {
// Did we reach EOF during the last iteration already? EOF
if (isEndOfFile(lastChar) || !isLastTokenDelimiter && isEndOfFile(c)) {
token.type = EOF;
token.type = Token.Type.EOF;
// don't set token.isReady here because no content
return token;
}
@ -257,7 +256,7 @@ final class Lexer implements Closeable {
if (isStartOfLine(lastChar) && isCommentStart(c)) {
final String line = reader.readLine();
if (line == null) {
token.type = EOF;
token.type = Token.Type.EOF;
// don't set token.isReady here because no content
return token;
}
@ -291,7 +290,7 @@ final class Lexer implements Closeable {
} else if (isEndOfFile(c)) {
// end of file return EOF()
// noop: token.content.append("");
token.type = EOF;
token.type = Token.Type.EOF;
token.isReady = true; // there is data at EOF
} else {
// next token must be a simple token
@ -337,7 +336,7 @@ final class Lexer implements Closeable {
token.content.append(delimiter);
} else {
final int unescaped = readEscape();
if (unescaped == END_OF_STREAM) { // unexpected char after escape
if (unescaped == EOF) { // unexpected char after escape
token.content.append((char) c).append((char) reader.getLastChar());
} else {
token.content.append((char) unescaped);
@ -357,7 +356,7 @@ final class Lexer implements Closeable {
return token;
}
if (isEndOfFile(c)) {
token.type = EOF;
token.type = Token.Type.EOF;
token.isReady = true; // There is data at EOF
return token;
}
@ -411,7 +410,7 @@ final class Lexer implements Closeable {
break;
}
if (isEndOfFile(ch)) {
token.type = EOF;
token.type = Token.Type.EOF;
token.isReady = true; // There is data at EOF
break;
}
@ -425,7 +424,7 @@ final class Lexer implements Closeable {
token.content.append(delimiter);
} else {
final int unescaped = readEscape();
if (unescaped == END_OF_STREAM) { // unexpected char after escape
if (unescaped == EOF) { // unexpected char after escape
token.content.append((char) ch).append((char) reader.getLastChar());
} else {
token.content.append((char) unescaped);
@ -478,7 +477,7 @@ final class Lexer implements Closeable {
* On return, the next character is available by calling {@link ExtendedBufferedReader#getLastChar()}
* on the input stream.
*
* @return the unescaped character (as an int) or {@link Constants#END_OF_STREAM} if char following the escape is
* @return the unescaped character (as an int) or {@link Constants#EOF} if char following the escape is
* invalid.
* @throws IOException if there is a problem reading the stream or the end of stream is detected:
* the escape character is not allowed at end of stream
@ -503,7 +502,7 @@ final class Lexer implements Closeable {
case TAB: // TODO is this correct? Do tabs need to be escaped?
case BACKSPACE: // TODO is this correct?
return ch;
case END_OF_STREAM:
case EOF:
throw new IOException("EOF whilst processing escape sequence");
default:
// Now check for meta-characters
@ -511,7 +510,7 @@ final class Lexer implements Closeable {
return ch;
}
// indicate unexpected char - available from in.getLastChar()
return END_OF_STREAM;
return EOF;
}
}

View File

@ -17,7 +17,7 @@
package org.apache.commons.csv;
import static org.apache.commons.csv.Constants.END_OF_STREAM;
import static org.apache.commons.io.IOUtils.EOF;
import static org.apache.commons.csv.Constants.UNDEFINED;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ -39,9 +39,9 @@ public class ExtendedBufferedReaderTest {
@Test
public void testEmptyInput() throws Exception {
try (final ExtendedBufferedReader br = createBufferedReader("")) {
assertEquals(END_OF_STREAM, br.read());
assertEquals(END_OF_STREAM, br.lookAhead());
assertEquals(END_OF_STREAM, br.getLastChar());
assertEquals(EOF, br.read());
assertEquals(EOF, br.lookAhead());
assertEquals(EOF, br.getLastChar());
assertNull(br.readLine());
assertEquals(0, br.read(new char[10], 0, 0));
}
@ -69,7 +69,7 @@ public class ExtendedBufferedReaderTest {
}
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
while (br.read() != -1) {
while (br.read() != EOF) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
@ -77,7 +77,7 @@ public class ExtendedBufferedReaderTest {
try (final ExtendedBufferedReader br = createBufferedReader(test)) {
assertEquals(0, br.getCurrentLineNumber());
final char[] buff = new char[10];
while (br.read(buff, 0, 3) != -1) {
while (br.read(buff, 0, 3) != EOF) {
// consume all
}
assertEquals(EOLeolct, br.getCurrentLineNumber());
@ -185,12 +185,12 @@ public class ExtendedBufferedReaderTest {
assertEquals('\n', br.getLastChar());
assertEquals(3, br.getCurrentLineNumber());
assertEquals(END_OF_STREAM, br.lookAhead());
assertEquals(EOF, br.lookAhead());
assertEquals('\n', br.getLastChar());
assertEquals(END_OF_STREAM, br.read());
assertEquals(END_OF_STREAM, br.getLastChar());
assertEquals(END_OF_STREAM, br.read());
assertEquals(END_OF_STREAM, br.lookAhead());
assertEquals(EOF, br.read());
assertEquals(EOF, br.getLastChar());
assertEquals(EOF, br.read());
assertEquals(EOF, br.lookAhead());
assertEquals(3, br.getCurrentLineNumber());
}

View File

@ -17,6 +17,8 @@
package org.apache.commons.csv;
import static org.apache.commons.io.IOUtils.EOF;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
@ -258,7 +260,7 @@ public class PerformanceTest {
int read;
if (makeString) {
StringBuilder sb = new StringBuilder();
while ((read = in.read()) != -1) {
while ((read = in.read()) != EOF) {
sb.append((char) read);
if (read == ',') { // count delimiters
sb.toString();
@ -271,7 +273,7 @@ public class PerformanceTest {
}
}
} else {
while ((read = in.read()) != -1) {
while ((read = in.read()) != EOF) {
if (read == ',') { // count delimiters
fields++;
} else if (read == '\n') {