Compare commits

..

No commits in common. "main" and "v0.0.1" have entirely different histories.
main ... v0.0.1

9 changed files with 29 additions and 390 deletions

View File

@ -1,10 +1,9 @@
package bufr package bufr
import ( import (
"git.milar.in/milarin/adverr" "git.tordarus.net/Tordarus/adverr"
) )
var ( var (
ErrNothingToUnread = adverr.NewErrTmpl("ErrNothingToUnread", "Unreading failed because there wasn't any Read yet") ErrNothingToUnread = adverr.NewErrTmpl("ErrNothingToUnread", "Unreading failed because there wasn't any Read yet")
ErrPopFailed = adverr.NewErrTmpl("ErrPopFailed", "stack cannot be popped. It was never pushed")
) )

11
go.mod
View File

@ -1,11 +1,8 @@
module git.milar.in/milarin/bufr module git.tordarus.net/Tordarus/bufr
go 1.19 go 1.18
require ( require (
git.milar.in/milarin/adverr v1.1.0 git.tordarus.net/Tordarus/adverr v0.2.0
git.milar.in/milarin/ds v0.0.2 git.tordarus.net/Tordarus/dstruct v0.0.2
git.milar.in/milarin/slices v0.0.8
) )
require git.milar.in/milarin/gmath v0.0.3 // indirect

12
go.sum
View File

@ -1,8 +1,4 @@
git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0= git.tordarus.net/Tordarus/adverr v0.2.0 h1:kLYjR2/Vb2GHiSAMvAv+WPNaHR9BRphKanf8H/pCZdA=
git.milar.in/milarin/adverr v1.1.0/go.mod h1:joU9sBb7ySyNv4SpTXB0Z4o1mjXsArBw4N27wjgzj9E= git.tordarus.net/Tordarus/adverr v0.2.0/go.mod h1:XRf0+7nhOkIEr0gi9DUG4RvV2KaOFB0fYPDaR1KLenw=
git.milar.in/milarin/ds v0.0.2 h1:vCA3mDxZUNfvHpzrdz7SeBUKiPn74NTopo915IUG7I0= git.tordarus.net/Tordarus/dstruct v0.0.2 h1:oFaZO7YXQeHjxL4DmMaZ+Eb4MvP23dX8GAXs0Z6VQKo=
git.milar.in/milarin/ds v0.0.2/go.mod h1:HJK7QERcRvV9j7xzEocrKUtW+1q4JB1Ly4Bj54chfwI= git.tordarus.net/Tordarus/dstruct v0.0.2/go.mod h1:RvLL2G4lUCGzwr8KaBGQRi3qlMG0WTgGhcVN6iyZZuw=
git.milar.in/milarin/gmath v0.0.3 h1:ii6rKNItS55O/wtIFhD1cTN2BMwDZjTBmiOocKURvxM=
git.milar.in/milarin/gmath v0.0.3/go.mod h1:HDLftG5RLpiNGKiIWh+O2G1PYkNzyLDADO8Cd/1abiE=
git.milar.in/milarin/slices v0.0.8 h1:qN9TE3tkArdTixMKSnwvNPcApwAjxpLVwA5a9k1rm2s=
git.milar.in/milarin/slices v0.0.8/go.mod h1:qMhdtMnfWswc1rHpwgNw33lB84aNEkdBn5BDiYA+G3k=

View File

@ -1,6 +0,0 @@
package bufr
type posRune struct {
Rune rune
Pos Position
}

View File

@ -1,17 +0,0 @@
package bufr
type Position struct {
Index int
Line int
Column int
}
func (p *Position) Advance(rn rune) {
p.Index++
if rn == '\n' {
p.Line++
p.Column = 1
} else {
p.Column++
}
}

260
reader.go
View File

@ -5,89 +5,30 @@ import (
"io" "io"
"strings" "strings"
"git.milar.in/milarin/ds" "git.tordarus.net/Tordarus/dstruct"
"git.milar.in/milarin/slices"
) )
type Reader struct { type Reader struct {
buf ds.Stack[posRune] buf *dstruct.Stack[rune]
indices ds.Stack[uint64] src *bufio.Reader
index uint64
src *bufio.Reader
pos *Position
} }
func New(r io.Reader) *Reader { func NewReader(r io.Reader) *Reader {
return &Reader{ return &Reader{
buf: ds.NewArrayStack[posRune](), buf: new(dstruct.Stack[rune]),
src: bufio.NewReader(r), src: bufio.NewReader(r),
pos: &Position{Index: 0, Line: 1, Column: 1},
index: 0,
indices: ds.NewArrayStack[uint64](),
} }
} }
func NewFromString(str string) *Reader {
return New(strings.NewReader(str))
}
func (r *Reader) psrn(rn rune) posRune {
return posRune{
Rune: rn,
Pos: *r.pos,
}
}
func (r *Reader) Pos() Position {
return *r.pos
}
// Rune returns the next rune in r // Rune returns the next rune in r
func (r *Reader) Rune() (rune, error) { func (r *Reader) Rune() (rune, error) {
rn, _, err := r.src.ReadRune() rn, _, err := r.src.ReadRune()
if err == nil { if err == nil {
r.buf.Push(r.psrn(rn)) r.buf.Push(rn)
r.pos.Advance(rn)
r.index++
} }
return rn, err return rn, err
} }
func (r *Reader) Buffered() int {
return r.src.Buffered()
}
// PeekRune returns the next rune in r without advancing reader position.
// The next read will return the same rune again.
func (r *Reader) PeekRune() (rune, error) {
rn, err := r.Rune()
if err != nil {
return 0, err
}
if err := r.UnreadRune(); err != nil {
return 0, err
}
return rn, nil
}
// String returns the next len runes in r as a string.
// If an error occurs, both the already read string and the error will be returned
func (r *Reader) String(len int) (string, error) {
str := ""
for i := 0; i < len; i++ {
rn, err := r.Rune()
if err != nil {
return str, err
}
str += string(rn)
}
return str, nil
}
// UnreadRune unreads the last rune. // UnreadRune unreads the last rune.
// The next read will include the unread rune. // The next read will include the unread rune.
// It returns ErrNothingToUnread if there wasn't any read yet // It returns ErrNothingToUnread if there wasn't any read yet
@ -97,21 +38,16 @@ func (r *Reader) UnreadRune() error {
} }
if err := r.src.UnreadRune(); err == nil { if err := r.src.UnreadRune(); err == nil {
rn := r.buf.Pop() r.buf.Pop()
*r.pos = rn.Pos
} else { } else {
rn := r.buf.Pop() r.src = prependRune(r.buf.Pop(), r.src)
*r.pos = rn.Pos
r.src = prependRune(rn.Rune, r.src)
} }
r.index--
return nil return nil
} }
// UnreadString calls UnreadRune for each rune in str + one addtional rune for the separator rune // UnreadString calls UnreadRune for each rune in str
// The actual runes in str are irrelevant. // The actual runes are irrelevant.
// Only the rune count of str determines the amount of UnreadRune calls. // Only the rune count of str determines the amount of UnreadRune calls.
// The first error occured will be returned immediately. // The first error occured will be returned immediately.
func (r *Reader) UnreadString(str string) error { func (r *Reader) UnreadString(str string) error {
@ -121,181 +57,25 @@ func (r *Reader) UnreadString(str string) error {
return err return err
} }
} }
return r.UnreadRune()
}
// UnreadRunes calls UnreadRune n times
func (r *Reader) UnreadRunes(n int) error {
for i := 0; i < n; i++ {
err := r.UnreadRune()
if err != nil {
return err
}
}
return nil return nil
} }
// StringWhile reads runes and calls all functions for each one. // StringWhile reads runes and calls f for each one.
// It returns all runes as a string for which any function returned true. // It returns all runes as a string for which f returned true.
// It stops when all functions returned false or an error occured. // It stops when f returns false or an error occured.
// The rune for which that function returned false will not be unread. // The rune for which f returned false will not be unread.
func (r *Reader) StringWhile(f ...RuneFunc) (string, error) { func (r *Reader) StringWhile(f func(rn rune) bool) (string, error) {
s := new(strings.Builder) s := new(strings.Builder)
var rn rune
var err error var err error
for rn, err = r.Rune(); err == nil && findFirstTrue(rn, f); rn, err = r.Rune() { for rn, err := r.Rune(); err == nil && f(rn); rn, err = r.Rune() {
s.WriteRune(rn) s.WriteRune(rn)
} }
return s.String(), err return s.String(), err
} }
// PeekStringWhile acts as StringWhile but does not advance reader position // StringUntil is a shorthand for r.StringWhile(func(rn rune) bool { return !f(rn) })
func (r *Reader) PeekStringWhile(f ...RuneFunc) (string, error) { func (r *Reader) StringUntil(f func(rn rune) bool) (string, error) {
str, err := r.StringWhile(f...) return r.StringWhile(func(rn rune) bool { return !f(rn) })
if err != nil {
return "", err
}
if err := r.UnreadString(str); err != nil {
return "", err
}
return str, nil
}
// StringUntil reads runes and calls all functions for each one.
// It returns all runes as a string for which all functions returned true.
// It stops when any function returns false or an error occured.
// The rune for which that function returned false will not be unread.
func (r *Reader) StringUntil(f ...RuneFunc) (string, error) {
return r.StringWhile(func(rn rune) bool { return !findFirstTrue(rn, f) })
}
// PeekStringUntil acts as StringUntil but does not advance reader position
func (r *Reader) PeekStringUntil(f ...RuneFunc) (string, error) {
str, err := r.StringUntil(f...)
if err != nil {
return "", err
}
if err := r.UnreadString(str); err != nil {
return "", err
}
return str, nil
}
// SkipUntil acts as StringUntil but discards the string
// The rune for which that function returned false will be unread.
func (r *Reader) SkipUntil(f ...RuneFunc) error {
_, err := r.StringUntil(f...)
if err != nil {
return err
}
return r.UnreadRune()
}
// SkipWhile acts as StringWhile but discards the string.
// The rune for which that function returned false will be unread.
func (r *Reader) SkipWhile(f ...RuneFunc) error {
_, err := r.StringWhile(f...)
if err != nil {
return err
}
return r.UnreadRune()
}
// ExpectRune returns true if any function returns true for the next rune read from r
func (r *Reader) ExpectRune(f ...RuneFunc) (bool, error) {
rn, err := r.Rune()
if err != nil {
return false, err
}
return findFirstTrue(rn, f), nil
}
// ExpectString calls ExpectRune for each rune in str successively.
// If the expected string was not found, all read runes will be unread
func (r *Reader) ExpectString(str string) (bool, error) {
read := 0
for _, rn := range str {
ok, err := r.ExpectRune(Is(rn))
if err != nil {
return false, err
}
read++
if !ok {
if err := r.UnreadRunes(read); err != nil {
return false, err
}
return false, nil
}
}
return true, nil
}
// ExpectOneOfString calls ExpectString for each string successively
// and returns the string which first matched.
// The boolean value is true if any string was found.
// The returned string will not be unread.
func (r *Reader) ExpectOneOfString(str ...string) (string, bool, error) {
for _, s := range str {
ok, err := r.ExpectString(s)
if err != nil {
return "", false, err
}
if ok {
return s, true, nil
}
}
return "", false, nil
}
func (r *Reader) Push() {
r.indices.Push(r.index)
}
func (r *Reader) Pop() ([]rune, error) {
if r.indices.Empty() {
return nil, ErrPopFailed.New()
}
lastIndex := r.indices.Pop()
currentIndex := r.index
if lastIndex < currentIndex {
values := make([]rune, 0, int(currentIndex-lastIndex))
for i := 0; i < int(currentIndex-lastIndex); i++ {
err := r.UnreadRune()
if err != nil {
return nil, err
}
value, err := r.PeekRune()
if err != nil {
return nil, err
}
values = append(values, value)
}
return slices.Reverse(values), nil
} else if lastIndex > currentIndex {
values := make([]rune, 0, int(lastIndex-currentIndex))
for i := 0; i < int(lastIndex-currentIndex); i++ {
value, err := r.Rune()
if err != nil {
return nil, err
}
values = append(values, value)
}
return values, nil
}
return []rune{}, nil
} }

View File

@ -1,36 +0,0 @@
package bufr
import (
"fmt"
"strings"
"testing"
)
func TestPos(t *testing.T) {
r := New(strings.NewReader("hello world\nsecond line"))
unread := false
for rn, err := r.Rune(); err == nil; rn, err = r.Rune() {
pos := r.Pos()
fmt.Println(string(rn), pos.Index, pos.Line, pos.Column)
if !unread && rn == '\n' {
for i := 0; i < 5; i++ {
r.UnreadRune()
}
unread = true
}
}
}
func TestEOF(t *testing.T) {
r := New(strings.NewReader("hello world\nasddsa"))
var line string
var err error
for line, err = r.StringUntil(IsNewLine); err == nil; line, err = r.StringUntil(IsNewLine) {
fmt.Println(line, err)
}
fmt.Println(line, err)
}

View File

@ -1,56 +0,0 @@
package bufr
import "git.milar.in/milarin/ds"
type RuneFunc = func(rn rune) bool
func IsNewLine(rn rune) bool {
return rn == '\n'
}
func IsSpace(rn rune) bool {
return rn == ' '
}
func IsTab(rn rune) bool {
return rn == '\t'
}
func IsWhitespace(rn rune) bool {
return IsSpace(rn) || IsTab(rn) || IsNewLine(rn)
}
func And(f ...RuneFunc) RuneFunc {
return func(rn rune) bool {
return findFirstFalse(rn, f)
}
}
func Or(f ...RuneFunc) RuneFunc {
return func(rn rune) bool {
return findFirstTrue(rn, f)
}
}
func Not(f RuneFunc) RuneFunc {
return func(rn rune) bool {
return !f(rn)
}
}
func Is(rn rune) RuneFunc {
return func(r rune) bool {
return rn == r
}
}
func OneOf(runes string) RuneFunc {
m := ds.NewSet[rune]()
for _, rn := range runes {
m.Add(rn)
}
return func(r rune) bool {
return m.Has(r)
}
}

View File

@ -13,21 +13,3 @@ func prependString(str string, r io.Reader) *bufio.Reader {
func prependRune(rn rune, r io.Reader) *bufio.Reader { func prependRune(rn rune, r io.Reader) *bufio.Reader {
return prependString(string(rn), r) return prependString(string(rn), r)
} }
func findFirstTrue(rn rune, functions []RuneFunc) bool {
for _, f := range functions {
if f(rn) {
return true
}
}
return false
}
func findFirstFalse(rn rune, functions []RuneFunc) bool {
for _, f := range functions {
if !f(rn) {
return false
}
}
return true
}