Skip to content

Commit

Permalink
Add isEOF
Browse files Browse the repository at this point in the history
  • Loading branch information
tonsky committed Dec 19, 2024
1 parent 7e0606e commit 0501a4f
Showing 1 changed file with 23 additions and 24 deletions.
47 changes: 23 additions & 24 deletions src/fast_edn/EdnParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public class EdnParser {
public int readPos = 0;
public int readGlobalPos = 0;
public int readLen = 0;
public boolean isEOF = false;
public char[] accumulator;
public int accumulatorLength;
public Object[] arrayMapBuf = new Object[16];
Expand All @@ -48,6 +49,7 @@ public EdnParser withReader(Reader reader) {
this.readPos = 0;
this.readGlobalPos = 0;
this.readLen = 0;
this.isEOF = false;
return this;
}

Expand Down Expand Up @@ -82,13 +84,13 @@ public void updateLineColumn(int until) {
}

public void nextBuffer() {
if (readLen != -1) {
if (!isEOF) {
try {
// for better context() in badly buffered readers (e.g. stdin)
if (readLen <= readBuf.length / 2) {
int readLenNew = reader.read(readBuf, readLen, readBuf.length - readLen);
if (readLenNew == -1) {
readLen = -1;
isEOF = true;
} else {
readLen += readLenNew;
}
Expand All @@ -97,11 +99,13 @@ public void nextBuffer() {
updateLineColumn(readLen);
}
int readLenNew = reader.read(readBuf, 0, readBuf.length);
if (readLenNew >= 0) {
if (readLenNew == -1) {
isEOF = true;
} else {
readPos = 0;
readGlobalPos += readLen;
readLen = readLenNew;
}
readLen = readLenNew;
}
} catch (IOException e) {
Util.sneakyThrow(e);
Expand All @@ -110,24 +114,20 @@ public void nextBuffer() {
}

public int read() {
if (readLen > readPos) {
if (!isEOF && readLen > readPos) {
return readBuf[readPos++];
}
nextBuffer();
return readLen == -1 ? -1 : readBuf[readPos++];
return isEOF ? -1 : readBuf[readPos++];
}

public void unread() {
assert readPos > 0;
assert readPos > 0 : "Expected readPos > 0, got readPos = " + readPos;
readPos -= 1;
}

public boolean eof() {
return readLen == -1;
}

public int skip(IntPredicate pred) {
while (!eof()) {
while (!isEOF) {
char[] buf = readBuf;
int pos = readPos;
int len = readLen;
Expand Down Expand Up @@ -166,8 +166,7 @@ public boolean compareNext(int ch, String s) {
}

public String context() {
int len = readLen == -1 ? readPos : readLen;
if (len <= 0) {
if (readLen <= 0) {
return "";
}

Expand All @@ -181,7 +180,7 @@ public String context() {
}

int end = readPos;
for (; end < Math.min(len, readPos + 100); ++end) {
for (; end < Math.min(readLen, readPos + 100); ++end) {
int ch = readBuf[end];
if (ch == '\n' || ch == '\r') {
end = Math.max(end - 1, readPos);
Expand Down Expand Up @@ -326,7 +325,7 @@ public String readStringComplex(char[] buf, int start, int pos) {
accumulatorAppend(buf, start, pos);

outer:
while (!eof()) {
while (!isEOF) {
buf = readBuf;
start = readPos;
pos = start;
Expand Down Expand Up @@ -460,7 +459,7 @@ public Object readSymbolComplex(char[] buf, int start, int slash, int pos) {
}

outer:
while (!eof()) {
while (!isEOF) {
buf = readBuf;
start = readPos;
pos = start;
Expand Down Expand Up @@ -586,7 +585,7 @@ public Keyword readKeywordComplex(char[] buf, int start, int slash, int pos) {
}

outer:
while (!eof()) {
while (!isEOF) {
buf = readBuf;
start = readPos;
pos = start;
Expand Down Expand Up @@ -674,7 +673,7 @@ public Number readNumberComplex(char[] buf, int start, int pos) {
int radixPos = -1;

outer:
while (!eof()) {
while (!isEOF) {
buf = readBuf;
start = readPos;
pos = start;
Expand Down Expand Up @@ -825,7 +824,7 @@ public Double readSymbolicValue() {
public IPersistentList readList() {
ArrayList acc = new ArrayList();

while (!eof()) {
while (!isEOF) {
int ch = skipWhitespace();

if (ch == ')') {
Expand Down Expand Up @@ -853,7 +852,7 @@ public IPersistentList readList() {
public PersistentVector readVector() {
ITransientCollection acc = PersistentVector.EMPTY.asTransient();

while (!eof()) {
while (!isEOF) {
int ch = skipWhitespace();

if (ch == ']') {
Expand All @@ -878,7 +877,7 @@ public PersistentHashSet readSet() {
ATransientSet acc = (ATransientSet) PersistentHashSet.EMPTY.asTransient();
int count = 0;

while (!eof()) {
while (!isEOF) {
int ch = skipWhitespace();

if (ch == '}') {
Expand Down Expand Up @@ -906,7 +905,7 @@ public PersistentHashSet readSet() {

public IPersistentMap readMap(String ns) {
int len = 0;
while (!eof()) {
while (!isEOF) {
int ch = skipWhitespace();

if (ch == '}') {
Expand Down Expand Up @@ -965,7 +964,7 @@ public IPersistentMap readHashMap(String ns, Object[] init) {
}
}

while (!eof()) {
while (!isEOF) {
int ch = skipWhitespace();

if (ch == '}') {
Expand Down

0 comments on commit 0501a4f

Please sign in to comment.