Removed SliceCursor
This commit is contained in:
parent
c0603b1c30
commit
412707c2b5
3
TODO.txt
3
TODO.txt
@ -1,5 +1,6 @@
|
|||||||
Write Repeat tests
|
Write Repeat tests
|
||||||
Create a ReaderAt[Datum any] interface and combine Cursor with State
|
Clean up cursor tests
|
||||||
|
Combine Cursor with State
|
||||||
Think about not requiring so much Pos() when making messages
|
Think about not requiring so much Pos() when making messages
|
||||||
Rename Seq2 to Seq
|
Rename Seq2 to Seq
|
||||||
Document Seq
|
Document Seq
|
||||||
|
@ -4,6 +4,7 @@ package bytes_test
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.codemonkeysoftware.net/b/gigaparsec"
|
"git.codemonkeysoftware.net/b/gigaparsec"
|
||||||
@ -28,7 +29,7 @@ func TestRegexp(t *testing.T) {
|
|||||||
|
|
||||||
}))
|
}))
|
||||||
t.Run("basically works", func(t *testing.T) {
|
t.Run("basically works", func(t *testing.T) {
|
||||||
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(cursor.NewSlice([]byte("a"))))
|
result, err := pbytes.Regexp("a")(gigaparsec.MakeState(cursor.NewReaderAt(strings.NewReader("a"))))
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
success, value, _ := result.Status()
|
success, value, _ := result.Status()
|
||||||
test.True(t, success, test.Sprint(result.Message()))
|
test.True(t, success, test.Sprint(result.Message()))
|
||||||
@ -38,8 +39,8 @@ func TestRegexp(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestRuneReader(t *testing.T) {
|
func TestRuneReader(t *testing.T) {
|
||||||
var s = []byte("abcdefghijklmnopqrstuvwxyz")
|
var s = "abcdefghijklmnopqrstuvwxyz"
|
||||||
rr := pbytes.NewRuneReader(cursor.NewSlice(s))
|
rr := pbytes.NewRuneReader(cursor.NewReaderAt(strings.NewReader(s)))
|
||||||
for i, b := range s {
|
for i, b := range s {
|
||||||
r, n, err := rr.ReadRune()
|
r, n, err := rr.ReadRune()
|
||||||
test.NoError(t, err)
|
test.NoError(t, err)
|
||||||
@ -67,7 +68,7 @@ func TestMatchString(t *testing.T) {
|
|||||||
notPrefix := func(b []byte) bool { return !bytes.HasPrefix(input, b) }
|
notPrefix := func(b []byte) bool { return !bytes.HasPrefix(input, b) }
|
||||||
s := string(bgen.Filter(notPrefix).Draw(t, "s"))
|
s := string(bgen.Filter(notPrefix).Draw(t, "s"))
|
||||||
|
|
||||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input))))
|
||||||
test.NoError(t, err)
|
test.NoError(t, err)
|
||||||
success, _, _ := result.Status()
|
success, _, _ := result.Status()
|
||||||
test.False(t, success)
|
test.False(t, success)
|
||||||
@ -77,7 +78,7 @@ func TestMatchString(t *testing.T) {
|
|||||||
input := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "input")
|
input := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "input")
|
||||||
slen := rapid.IntRange(0, len(input)).Draw(t, "slen")
|
slen := rapid.IntRange(0, len(input)).Draw(t, "slen")
|
||||||
s := string(input[:slen])
|
s := string(input[:slen])
|
||||||
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewSlice(input)))
|
result, err := pbytes.MatchString(s)(gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input))))
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
success, value, next := result.Status()
|
success, value, next := result.Status()
|
||||||
must.True(t, success)
|
must.True(t, success)
|
||||||
|
@ -53,40 +53,6 @@ func (s SliceReaderAt[T]) ReadAt(dst []T, off int64) (n int, err error) {
|
|||||||
return n, err
|
return n, err
|
||||||
}
|
}
|
||||||
|
|
||||||
type SliceCursor[Datum any] struct {
|
|
||||||
data []Datum
|
|
||||||
offset uint64
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewSlice[Datum any](data []Datum) SliceCursor[Datum] {
|
|
||||||
return SliceCursor[Datum]{
|
|
||||||
data: data,
|
|
||||||
offset: 0,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (sc SliceCursor[Datum]) Read(dst []Datum) (n uint64, next Cursor[Datum], err error) {
|
|
||||||
if sc.offset == uint64(len(sc.data)) {
|
|
||||||
return 0, sc, io.EOF
|
|
||||||
}
|
|
||||||
copied := copy(dst, sc.data[sc.offset:])
|
|
||||||
if copied < len(dst) {
|
|
||||||
err = io.EOF
|
|
||||||
}
|
|
||||||
n = uint64(copied)
|
|
||||||
sc.offset += n
|
|
||||||
return n, sc, err
|
|
||||||
}
|
|
||||||
|
|
||||||
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[T any] struct {
|
type ReaderAtCursor[T any] struct {
|
||||||
r ReaderAt[T]
|
r ReaderAt[T]
|
||||||
pos uint64
|
pos uint64
|
||||||
|
@ -100,10 +100,6 @@ func testCursor[C cursor.Cursor[byte]](t *testing.T, makeCursor func([]byte) C)
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestSliceCursor(t *testing.T) {
|
|
||||||
testCursor(t, cursor.NewSlice[byte])
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestReaderAtCursor(t *testing.T) {
|
func TestReaderAtCursor(t *testing.T) {
|
||||||
testCursor(t, func(b []byte) cursor.ReaderAtCursor[byte] {
|
testCursor(t, func(b []byte) cursor.ReaderAtCursor[byte] {
|
||||||
return cursor.NewReaderAt(bytes.NewReader(b))
|
return cursor.NewReaderAt(bytes.NewReader(b))
|
||||||
|
@ -30,7 +30,7 @@ func hasPrefix(prefix []byte) func([]byte) bool {
|
|||||||
func TestSlice(t *testing.T) {
|
func TestSlice(t *testing.T) {
|
||||||
assertParseFails := func(t rapid.TB, input []byte, p gigaparsec.Parser[byte, []byte]) {
|
assertParseFails := func(t rapid.TB, input []byte, p gigaparsec.Parser[byte, []byte]) {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
start := gigaparsec.MakeState(cursor.NewSlice(input))
|
start := gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input)))
|
||||||
result, err := p(start)
|
result, err := p(start)
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
success, _, _ := result.Status()
|
success, _, _ := result.Status()
|
||||||
@ -66,7 +66,7 @@ func TestSlice(t *testing.T) {
|
|||||||
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")
|
input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input")
|
||||||
sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen")
|
sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen")
|
||||||
s := input[:sLen]
|
s := input[:sLen]
|
||||||
start := gigaparsec.MakeState(cursor.NewSlice(input))
|
start := gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input)))
|
||||||
|
|
||||||
result, err := gigaparsec.MatchSlice(s)(start)
|
result, err := gigaparsec.MatchSlice(s)(start)
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
@ -108,7 +108,7 @@ func TestBind(t *testing.T) {
|
|||||||
p := makeParser(pConsume)
|
p := makeParser(pConsume)
|
||||||
q := func(struct{}) gigaparsec.Parser[byte, struct{}] { return makeParser(qConsume) }
|
q := func(struct{}) gigaparsec.Parser[byte, struct{}] { return makeParser(qConsume) }
|
||||||
|
|
||||||
result, err := gigaparsec.Bind(p, q)(gigaparsec.MakeState(cursor.NewSlice([]byte{})))
|
result, err := gigaparsec.Bind(p, q)(gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(nil))))
|
||||||
must.NoError(t, err)
|
must.NoError(t, err)
|
||||||
must.EqOp(t, pConsume || qConsume, result.Consumed())
|
must.EqOp(t, pConsume || qConsume, result.Consumed())
|
||||||
}))
|
}))
|
||||||
|
Loading…
Reference in New Issue
Block a user