Skip to content

Commit dbeae47

Browse files
committed
conform to Reader interface
1 parent 2d21b06 commit dbeae47

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

io/linereader/linereader.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package linereader
22

33
import (
44
"bytes"
5+
"errors"
56
"io"
67

78
armath "github.com/asymmetric-research/go-commons/math"
@@ -28,9 +29,17 @@ func NewInto(dst *T, reader io.Reader, blockSize uint) {
2829
}
2930
}
3031

31-
// Read reads as much as possible into p, until the next newline or EOF is reached.
32+
func (lr *T) Read(dst []byte) (n int, err error) {
33+
n, discarded, err := lr.ReadExtra(dst)
34+
if discarded != 0 {
35+
return n + discarded, errors.New("line too long")
36+
}
37+
return n, err
38+
}
39+
40+
// ReadExtra reads as much as possible into p, until the next newline or EOF is reached.
3241
// Every new call to read starts on a new line. The remainder of the previous line will be discarted.
33-
func (lr *T) Read(dst []byte) (nread int, ndiscarted int, err error) {
42+
func (lr *T) ReadExtra(dst []byte) (nread int, ndiscarted int, err error) {
3443
// copy as much of read buffer as possible to dst
3544
if len(lr.readbuf) > 0 {
3645
// fast path: can we get a new line from the read buffer?

io/linereader/linereader_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestLineReader(t *testing.T) {
2424
var n int
2525

2626
for err != io.EOF {
27-
n, _, err = r.Read(linesback[:])
27+
n, _, err = r.ReadExtra(linesback[:])
2828
line = linesback[:n]
2929
require.Equal(t, expectedLines[0], string(line))
3030
expectedLines = expectedLines[1:]
@@ -45,7 +45,7 @@ func TestLinesOfReaderTruncation(t *testing.T) {
4545
var n int
4646

4747
for err != io.EOF {
48-
n, _, err = r.Read(linesback[:])
48+
n, _, err = r.ReadExtra(linesback[:])
4949
if err != nil {
5050
break
5151
}
@@ -129,7 +129,7 @@ func runOurs(t require.TestingT, r io.Reader) {
129129

130130
cnt := 0
131131
for err == nil {
132-
_, _, err = rd.Read(lineBacking[:])
132+
_, _, err = rd.ReadExtra(lineBacking[:])
133133
cnt += 1
134134
}
135135
require.Equal(t, 283, cnt)

0 commit comments

Comments
 (0)