diff --git a/TODO.txt b/TODO.txt index 2667863..c7a1a0f 100644 --- a/TODO.txt +++ b/TODO.txt @@ -1,5 +1,6 @@ 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 Rename Seq2 to Seq Document Seq diff --git a/bytes/regexp_test.go b/bytes/regexp_test.go index e92d04a..8ce81fc 100644 --- a/bytes/regexp_test.go +++ b/bytes/regexp_test.go @@ -4,6 +4,7 @@ package bytes_test import ( "bytes" + "strings" "testing" "git.codemonkeysoftware.net/b/gigaparsec" @@ -28,7 +29,7 @@ func TestRegexp(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) success, value, _ := result.Status() test.True(t, success, test.Sprint(result.Message())) @@ -38,8 +39,8 @@ func TestRegexp(t *testing.T) { } func TestRuneReader(t *testing.T) { - var s = []byte("abcdefghijklmnopqrstuvwxyz") - rr := pbytes.NewRuneReader(cursor.NewSlice(s)) + var s = "abcdefghijklmnopqrstuvwxyz" + rr := pbytes.NewRuneReader(cursor.NewReaderAt(strings.NewReader(s))) for i, b := range s { r, n, err := rr.ReadRune() test.NoError(t, err) @@ -67,7 +68,7 @@ func TestMatchString(t *testing.T) { notPrefix := func(b []byte) bool { return !bytes.HasPrefix(input, b) } 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) success, _, _ := result.Status() test.False(t, success) @@ -77,7 +78,7 @@ func TestMatchString(t *testing.T) { input := rapid.SliceOfN(rapid.Byte(), 1, 100).Draw(t, "input") slen := rapid.IntRange(0, len(input)).Draw(t, "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) success, value, next := result.Status() must.True(t, success) diff --git a/cursor/cursor.go b/cursor/cursor.go index f682216..c669a4d 100644 --- a/cursor/cursor.go +++ b/cursor/cursor.go @@ -53,40 +53,6 @@ func (s SliceReaderAt[T]) ReadAt(dst []T, off int64) (n int, err error) { 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 { r ReaderAt[T] pos uint64 diff --git a/cursor/cursor_test.go b/cursor/cursor_test.go index 08f0e2b..b7d39a3 100644 --- a/cursor/cursor_test.go +++ b/cursor/cursor_test.go @@ -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) { testCursor(t, func(b []byte) cursor.ReaderAtCursor[byte] { return cursor.NewReaderAt(bytes.NewReader(b)) diff --git a/parser_test.go b/parser_test.go index b7de8bf..cf20ad9 100644 --- a/parser_test.go +++ b/parser_test.go @@ -30,7 +30,7 @@ func hasPrefix(prefix []byte) func([]byte) bool { func TestSlice(t *testing.T) { assertParseFails := func(t rapid.TB, input []byte, p gigaparsec.Parser[byte, []byte]) { t.Helper() - start := gigaparsec.MakeState(cursor.NewSlice(input)) + start := gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input))) result, err := p(start) must.NoError(t, err) success, _, _ := result.Status() @@ -66,7 +66,7 @@ func TestSlice(t *testing.T) { input := rapid.SliceOfN(rapid.Byte(), 1, -1).Draw(t, "input") sLen := rapid.IntRange(0, len(input)).Draw(t, "sLen") s := input[:sLen] - start := gigaparsec.MakeState(cursor.NewSlice(input)) + start := gigaparsec.MakeState(cursor.NewReaderAt(bytes.NewReader(input))) result, err := gigaparsec.MatchSlice(s)(start) must.NoError(t, err) @@ -108,7 +108,7 @@ func TestBind(t *testing.T) { p := makeParser(pConsume) 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.EqOp(t, pConsume || qConsume, result.Consumed()) }))