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
10 changes: 6 additions & 4 deletions core/src/main/java/com/github/gumtreediff/io/LineReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,12 @@ public LineReader(Reader parent) {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int r = reader.read(cbuf, off, len);
for (int i = 0; i < len; i++)
if (cbuf[off + i] == '\n')
lines.add(currentPos + i);
currentPos += len;
if (r > 0) {
for (int i = 0; i < r; i++)
if (cbuf[off + i] == '\n')
lines.add(currentPos + i);
currentPos += r;
}
return r;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,29 @@ public void testLineReader() throws IOException {
assertArrayEquals(new int[] { 3, 2 }, lr.positionFor(9));
}

@Test
public void testLineReaderWithPartialReads() throws IOException {
StringReader reader = new StringReader("foo\nbar\nbaz\n") {
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
return super.read(cbuf, off, Math.min(len, 2));
}
};
LineReader lr = new LineReader(reader);
char[] buffer = new char[8];
StringBuilder contents = new StringBuilder();
int count;

while ((count = lr.read(buffer, 0, buffer.length)) != -1)
contents.append(buffer, 0, count);

assertEquals("foo\nbar\nbaz\n", contents.toString());
assertEquals(4, lr.positionFor(2, 1));
assertEquals(8, lr.positionFor(3, 1));
assertArrayEquals(new int[] { 2, 1 }, lr.positionFor(4));
assertArrayEquals(new int[] { 3, 1 }, lr.positionFor(8));
}

@Test
public void testPrintTextTree() throws Exception {
TreeContext tc = getTreeContext();
Expand Down