Implemented (but not tested) cursor.RuneReader

This commit is contained in:
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() uint64
// At returns a new cursor at the position pos.
At(pos uint64) Cursor[Datum]
}
type SliceCursor[Datum any] struct {
@ -56,6 +59,11 @@ func (sc SliceCursor[Datum]) Pos() uint64 {
return sc.offset
}
func (sc SliceCursor[Datum]) At(pos uint64) Cursor[Datum] {
sc.offset = pos
return sc
}
type ReaderAtCursor struct {
r io.ReaderAt
pos uint64
@ -77,6 +85,11 @@ func (rac ReaderAtCursor) Pos() uint64 {
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.
// The advantage is that creating a StringCursor does not require copying the source
// 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 {
return sc.offset
}
func (sc StringCursor) At(pos uint64) Cursor[byte] {
sc.offset = pos
return sc
}