From d60096e5b23deb3ca5eb601a2c7343b7b022ea18 Mon Sep 17 00:00:00 2001 From: milarin Date: Fri, 20 Jan 2023 22:16:46 +0100 Subject: [PATCH] Commit() method implemented --- errors.go | 2 +- go.mod | 8 ++++---- go.sum | 12 ++++++++---- reader.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++--- 4 files changed, 65 insertions(+), 12 deletions(-) diff --git a/errors.go b/errors.go index 72bfa8d..7152e3a 100644 --- a/errors.go +++ b/errors.go @@ -1,7 +1,7 @@ package bufr import ( - "git.tordarus.net/Tordarus/adverr" + "git.milar.in/milarin/adverr" ) var ( diff --git a/go.mod b/go.mod index ec51b77..b1bfac8 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,8 @@ -module git.tordarus.net/Tordarus/bufr +module git.milar.in/milarin/bufr -go 1.18 +go 1.19 require ( - git.tordarus.net/Tordarus/adverr v0.2.0 - git.tordarus.net/Tordarus/dstruct v0.0.2 + git.milar.in/milarin/adverr v1.1.0 + git.milar.in/milarin/ds v0.0.2 ) diff --git a/go.sum b/go.sum index 3173da5..010c479 100644 --- a/go.sum +++ b/go.sum @@ -1,4 +1,8 @@ -git.tordarus.net/Tordarus/adverr v0.2.0 h1:kLYjR2/Vb2GHiSAMvAv+WPNaHR9BRphKanf8H/pCZdA= -git.tordarus.net/Tordarus/adverr v0.2.0/go.mod h1:XRf0+7nhOkIEr0gi9DUG4RvV2KaOFB0fYPDaR1KLenw= -git.tordarus.net/Tordarus/dstruct v0.0.2 h1:oFaZO7YXQeHjxL4DmMaZ+Eb4MvP23dX8GAXs0Z6VQKo= -git.tordarus.net/Tordarus/dstruct v0.0.2/go.mod h1:RvLL2G4lUCGzwr8KaBGQRi3qlMG0WTgGhcVN6iyZZuw= +git.milar.in/milarin/adverr v1.1.0 h1:jD9WnOvs40lfMhvqQ7cllOaRJNBMWr1f07/s9jAadp0= +git.milar.in/milarin/adverr v1.1.0/go.mod h1:joU9sBb7ySyNv4SpTXB0Z4o1mjXsArBw4N27wjgzj9E= +git.milar.in/milarin/ds v0.0.0-20230120204927-7dc44b9cd222 h1:r7gcMqltPqk9U2/+LzEN5dooZ5wiQ8C7SI/nK6T0KjA= +git.milar.in/milarin/ds v0.0.0-20230120204927-7dc44b9cd222/go.mod h1:HJK7QERcRvV9j7xzEocrKUtW+1q4JB1Ly4Bj54chfwI= +git.milar.in/milarin/ds v0.0.1 h1:ov4Rp+QiB3xtmV0a4eC+LluxWEOvbykgW+wCchYEp9o= +git.milar.in/milarin/ds v0.0.1/go.mod h1:HJK7QERcRvV9j7xzEocrKUtW+1q4JB1Ly4Bj54chfwI= +git.milar.in/milarin/ds v0.0.2 h1:vCA3mDxZUNfvHpzrdz7SeBUKiPn74NTopo915IUG7I0= +git.milar.in/milarin/ds v0.0.2/go.mod h1:HJK7QERcRvV9j7xzEocrKUtW+1q4JB1Ly4Bj54chfwI= diff --git a/reader.go b/reader.go index 9d4172f..a0e9e57 100644 --- a/reader.go +++ b/reader.go @@ -5,18 +5,18 @@ import ( "io" "strings" - "git.tordarus.net/Tordarus/dstruct" + "git.milar.in/milarin/ds" ) type Reader struct { - buf *dstruct.Stack[posRune] + buf ds.Stack[posRune] src *bufio.Reader pos *Position } func NewReader(r io.Reader) *Reader { return &Reader{ - buf: new(dstruct.Stack[posRune]), + buf: ds.NewArrayStack[posRune](), src: bufio.NewReader(r), pos: &Position{}, } @@ -43,6 +43,21 @@ func (r *Reader) Rune() (rune, error) { return rn, err } +// 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 +} + // UnreadRune unreads the last rune. // The next read will include the unread rune. // It returns ErrNothingToUnread if there wasn't any read yet @@ -93,7 +108,41 @@ func (r *Reader) StringWhile(f func(rn rune) bool) (string, error) { return s.String(), err } +// PeekStringWhile acts as StringWhile but does not advance reader position +func (r *Reader) PeekStringWhile(f func(rn rune) bool) (string, error) { + str, err := r.StringWhile(f) + if err != nil { + return "", err + } + + if err := r.UnreadString(str); err != nil { + return "", err + } + + return str, nil +} + // StringUntil is a shorthand for r.StringWhile(func(rn rune) bool { return !f(rn) }) func (r *Reader) StringUntil(f func(rn rune) bool) (string, error) { return r.StringWhile(func(rn rune) bool { return !f(rn) }) } + +// PeekStringUntil acts as StringUntil but does not advance reader position +func (r *Reader) PeekStringUntil(f func(rn rune) bool) (string, error) { + str, err := r.StringUntil(f) + if err != nil { + return "", err + } + + if err := r.UnreadString(str); err != nil { + return "", err + } + + return str, nil +} + +// Commit clears the internal buffer and therefore removes all data which were already read. +// After calling Commit any unreads will return ErrNothingToUnread until another read occured. +func (r *Reader) Commit() { + r.buf.Clear() +}