Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions src/main/java/org/nsvformat/Nsv.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,15 @@ public static List<List<String>> decode(String s) {
List<String> row = new ArrayList<>();
int start = 0;

for (int pos = 0; pos < s.length(); pos++) {
char c = s.charAt(pos);
if (c == '\n') {
if (pos - start >= 1) {
row.add(unescape(s.substring(start, pos)));
} else {
data.add(row);
row = new ArrayList<>();
}
start = pos + 1;
int pos;
while ((pos = s.indexOf('\n', start)) >= 0) {
if (pos > start) {
row.add(unescape(s.substring(start, pos)));
} else {
data.add(row);
row = new ArrayList<>();
}
start = pos + 1;
}

if (start < s.length()) {
Expand Down
30 changes: 20 additions & 10 deletions src/main/java/org/nsvformat/Reader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,35 @@ public class Reader implements Iterator<List<String>> {
private final List<String> rowBuffer = new ArrayList<>();
private List<String> cachedRow = null;

private char[] buf = new char[8192];
private int bufPos = 0;
private int bufLen = 0;

public Reader(java.io.Reader reader) {
this.reader = reader;
}

private String tryReadLine() throws IOException {
while (true) {
int c = reader.read();
if (c == -1) {
for (int i = bufPos; i < bufLen; i++) {
if (buf[i] == '\n') {
// Line complete, return
lineBuffer.append(buf, bufPos, i - bufPos);
bufPos = i + 1;
String line = lineBuffer.toString();
lineBuffer.setLength(0);
return line;
}
}
// Keep reading
lineBuffer.append(buf, bufPos, bufLen - bufPos);
bufLen = reader.read(buf, 0, buf.length);
bufPos = 0;
if (bufLen == -1) {
// Incomplete line at EOF, preserve lineBuffer for next call
bufLen = 0;
return null;
}
if (c == '\n') {
// Line complete, return
String line = lineBuffer.toString();
lineBuffer.setLength(0);
return line;
}
// Keep reading
lineBuffer.append((char) c);
}
}

Expand Down
Loading