Implemented (but not tested) cursor.RuneReader

This commit is contained in:
Brandon Dyck 2024-09-03 16:10:27 -06:00
parent 9b15090171
commit cfeaaa27e3
3 changed files with 39 additions and 7 deletions

View File

@ -25,6 +25,9 @@ type Cursor[Datum any] interface {
// Pos returns the Cursor's position within the source. // Pos returns the Cursor's position within the source.
Pos() uint64 Pos() uint64
// At returns a new cursor at the position pos.
At(pos uint64) Cursor[Datum]
} }
type SliceCursor[Datum any] struct { type SliceCursor[Datum any] struct {
@ -56,6 +59,11 @@ func (sc SliceCursor[Datum]) Pos() uint64 {
return sc.offset return sc.offset
} }
func (sc SliceCursor[Datum]) At(pos uint64) Cursor[Datum] {
sc.offset = pos
return sc
}
type ReaderAtCursor struct { type ReaderAtCursor struct {
r io.ReaderAt r io.ReaderAt
pos uint64 pos uint64
@ -77,6 +85,11 @@ func (rac ReaderAtCursor) Pos() uint64 {
return rac.pos return rac.pos
} }
func (rac ReaderAtCursor) At(pos uint64) Cursor[byte] {
rac.pos = pos
return rac
}
// StringCursor is identical to SliceCursor[byte], but uses a string as its data source. // StringCursor is identical to SliceCursor[byte], but uses a string as its data source.
// The advantage is that creating a StringCursor does not require copying the source // The advantage is that creating a StringCursor does not require copying the source
// string into a []byte. // string into a []byte.
@ -105,3 +118,8 @@ func (sc StringCursor) Read(dst []byte) (n uint64, next Cursor[byte], err error)
func (sc StringCursor) Pos() uint64 { func (sc StringCursor) Pos() uint64 {
return sc.offset return sc.offset
} }
func (sc StringCursor) At(pos uint64) Cursor[byte] {
sc.offset = pos
return sc
}

View File

@ -73,6 +73,7 @@ func testCursor[C cursor.Cursor[byte]](t *testing.T, makeCursor func([]byte) C)
test.ErrorIs(t, err, io.EOF) test.ErrorIs(t, err, io.EOF)
test.EqOp(t, len(data), int(n)) test.EqOp(t, len(data), int(n))
})) }))
t.Run("At", Todo)
} }
func TestSliceCursor(t *testing.T) { func TestSliceCursor(t *testing.T) {

View File

@ -1,6 +1,11 @@
package cursor package cursor
import "io" import (
"errors"
"fmt"
"io"
"unicode/utf8"
)
// BufferedReaderAt uses a buffer to supplement an io.Reader // BufferedReaderAt uses a buffer to supplement an io.Reader
// with limited backward seeking. // with limited backward seeking.
@ -31,12 +36,20 @@ func NewRuneReader(c Cursor[byte]) *RuneReader {
return &RuneReader{cursor: c} return &RuneReader{cursor: c}
} }
func (r *RuneReader) Read(dst []byte) (int, error) { func (rr *RuneReader) ReadRune() (r rune, size int, err error) {
n, c, err := r.cursor.Read(dst) var b [4]byte
r.cursor = c s := b[:]
return int(n), err n, next, err := rr.cursor.Read(s)
if err != nil && !errors.Is(err, io.EOF) {
rr.cursor = next
return 0, 0, fmt.Errorf("ReadRune: %w", err)
}
s = s[:n]
r, size = utf8.DecodeRune(s)
rr.cursor = rr.cursor.At(rr.cursor.Pos() + uint64(size))
return r, size, err
} }
func (r *RuneReader) Cursor() Cursor[byte] { func (rr *RuneReader) Cursor() Cursor[byte] {
return r.cursor return rr.cursor
} }