Skip to content

Commit

Permalink
Return null also when freshly encountering EOL as first char
Browse files Browse the repository at this point in the history
Achieved in part by not always seeking to the start of the next column
  • Loading branch information
NebelNidas committed Apr 12, 2024
1 parent 05b462d commit 6d068cf
Showing 1 changed file with 36 additions and 24 deletions.
60 changes: 36 additions & 24 deletions src/main/java/net/fabricmc/mappingio/format/ColumnFileReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public boolean nextCol(String expected) throws IOException {
/**
* Read and consume a column without unescaping.
*
* @return null if already at eol or eof, otherwise the read string (may be empty).
* @return null if nothing has been read (first char was eol), otherwise the read string (may be empty).
*/
@Nullable
public String nextCol() throws IOException {
Expand All @@ -67,8 +67,7 @@ public String nextCol() throws IOException {
/**
* Read and consume a column, and unescape it if requested.
*
* @return null if already at eol or eof, otherwise the read string.
* Empty if there was no content or eol was encountered while reading.
* @return null if nothing has been read (first char was eol), otherwise the read string (may be empty).
*/
@Nullable
public String nextCol(boolean unescape) throws IOException {
Expand All @@ -79,8 +78,7 @@ public String nextCol(boolean unescape) throws IOException {
* Read a column without consuming, and unescape if requested.
* Since it doesn't consume, it won't (un)mark bof, eol or eof.
*
* @return null if already at eol or eof, otherwise the read string.
* Empty if there was no content or eol was encountered while reading.
* @return null if nothing has been read (first char was eol), otherwise the read string (may be empty).
*/
@Nullable
public String peekCol(boolean unescape) throws IOException {
Expand All @@ -93,8 +91,7 @@ public String peekCol(boolean unescape) throws IOException {
* @param stopAtNextCol Whether to only read one column.
* @param expected If not null, the read string must match this exactly, otherwise we early-exit with {@link #noMatch}. Always consumes if matched.
*
* @return null if already at eol or eof, otherwise the read string.
* Empty if there was no content or eol was encountered while reading.
* @return null if nothing has been read (first char was eol), otherwise the read string (may be empty).
* If {@code expected} is not null, it will be returned if matched, otherwise {@link #noMatch}.
*/
@Nullable
Expand All @@ -111,29 +108,37 @@ private String read(boolean unescape, boolean consume, boolean stopAtNextCol, @N
int start;
int end = this.bufferPos;
int firstEscaped = -1;
int charsRead = 0;
int contentCharsRead = 0;
int modifiedBufferPos = -1;
int startOffset = 0;
boolean readAnything = false;
boolean filled = true;

readLoop: for (;;) {
while (end < bufferLimit) {
char c = buffer[end];
boolean isColumnSeparator = (c == columnSeparator);

if (expected != null) {
if ((charsRead < expectedLength && c != expected.charAt(charsRead))
|| charsRead > expectedLength) {
// skip leading column separator
if (isColumnSeparator && !readAnything) {
startOffset = 1;
contentCharsRead = -1;
}

readAnything = true;

if (expected != null && contentCharsRead > -1) {
if ((contentCharsRead < expectedLength && c != expected.charAt(contentCharsRead))
|| contentCharsRead > expectedLength) {
return noMatch;
}
}

if (c == '\n' || c == '\r' || (stopAtNextCol && c == columnSeparator)) { // stop reading
start = bufferPos;
if (c == '\n' || c == '\r' || (isColumnSeparator && stopAtNextCol && contentCharsRead > -1)) { // stop reading
start = bufferPos + startOffset;
modifiedBufferPos = end;

// seek to the start of the next column
if (c == columnSeparator) {
modifiedBufferPos++;
} else if (consume) {
if (!isColumnSeparator && consume) {
eol = true;
}

Expand All @@ -142,7 +147,7 @@ private String read(boolean unescape, boolean consume, boolean stopAtNextCol, @N
firstEscaped = bufferPos;
}

charsRead++;
contentCharsRead++;
end++;
}

Expand Down Expand Up @@ -173,7 +178,7 @@ private String read(boolean unescape, boolean consume, boolean stopAtNextCol, @N
int len = end - start;

if (len == 0) {
ret = "";
ret = readAnything ? "" : null;
} else if (firstEscaped >= 0) {
ret = Tiny2Util.unescape(String.valueOf(buffer, start, len));
} else {
Expand All @@ -182,7 +187,7 @@ private String read(boolean unescape, boolean consume, boolean stopAtNextCol, @N
}

if (consume) {
if (charsRead > 0) bof = false;
if (readAnything) bof = false;
if (!filled) eof = eol = true;
if (modifiedBufferPos != -1) bufferPos = modifiedBufferPos;

Expand All @@ -201,8 +206,7 @@ private String read(boolean unescape, boolean consume, boolean stopAtNextCol, @N
/**
* Read and consume all columns until eol, and unescape if requested.
*
* @return null if already at eol or eof, otherwise the read string.
* Empty if there was no content or eol was encountered while reading.
* @return null if nothing has been read (first char was eol), otherwise the read string (may be empty).
*/
@Nullable
public String nextCols(boolean unescape) throws IOException {
Expand All @@ -213,8 +217,7 @@ public String nextCols(boolean unescape) throws IOException {
* Read all columns until eol without consuming, and unescape if requested.
* Since it doesn't consume, it won't (un)mark bof, eol or eof.
*
* @return null if already at eol or eof, otherwise the read string.
* Empty if there was no content or eol was encountered while reading.
* @return null if nothing has been read (first char was eol), otherwise the read string (may be empty).
*/
@Nullable
public String peekCols(boolean unescape) throws IOException {
Expand All @@ -223,6 +226,8 @@ public String peekCols(boolean unescape) throws IOException {

/**
* Read and consume a column and convert it to integer.
*
* @return -1 if nothing has been read (first char was eol), otherwise the number present.
*/
public int nextIntCol() throws IOException {
String str = nextCol(false);
Expand Down Expand Up @@ -283,6 +288,13 @@ public int getLineNumber() {
return lineNumber;
}

/**
* Whether or not EOL has been encountered in the current line yet.
*/
public boolean isAtEol() {
return eol;
}

public boolean isAtBof() {
return bof;
}
Expand Down

0 comments on commit 6d068cf

Please sign in to comment.