Skip to content

Commit

Permalink
conform to Reader interface
Browse files Browse the repository at this point in the history
  • Loading branch information
nlgripto committed Sep 10, 2024
1 parent 2d21b06 commit dbeae47
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
13 changes: 11 additions & 2 deletions io/linereader/linereader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package linereader

import (
"bytes"
"errors"
"io"

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

// Read reads as much as possible into p, until the next newline or EOF is reached.
func (lr *T) Read(dst []byte) (n int, err error) {
n, discarded, err := lr.ReadExtra(dst)
if discarded != 0 {
return n + discarded, errors.New("line too long")
}
return n, err
}

// ReadExtra reads as much as possible into p, until the next newline or EOF is reached.
// Every new call to read starts on a new line. The remainder of the previous line will be discarted.
func (lr *T) Read(dst []byte) (nread int, ndiscarted int, err error) {
func (lr *T) ReadExtra(dst []byte) (nread int, ndiscarted int, err error) {
// copy as much of read buffer as possible to dst
if len(lr.readbuf) > 0 {
// fast path: can we get a new line from the read buffer?
Expand Down
6 changes: 3 additions & 3 deletions io/linereader/linereader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestLineReader(t *testing.T) {
var n int

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

for err != io.EOF {
n, _, err = r.Read(linesback[:])
n, _, err = r.ReadExtra(linesback[:])
if err != nil {
break
}
Expand Down Expand Up @@ -129,7 +129,7 @@ func runOurs(t require.TestingT, r io.Reader) {

cnt := 0
for err == nil {
_, _, err = rd.Read(lineBacking[:])
_, _, err = rd.ReadExtra(lineBacking[:])
cnt += 1
}
require.Equal(t, 283, cnt)
Expand Down

0 comments on commit dbeae47

Please sign in to comment.