5 Commits

Author SHA1 Message Date
3ca76e7670 UnreadRunes implemented 2023-01-20 22:33:44 +01:00
d60096e5b2 Commit() method implemented 2023-01-20 22:16:46 +01:00
ca6cf3d943 fixed StringWhile 2022-05-23 14:04:39 +02:00
40d00d9244 positions introduced 2022-05-23 12:59:35 +02:00
9c3d038fcd improved UnreadString 2022-04-19 19:58:38 +02:00
7 changed files with 175 additions and 18 deletions

View File

@ -1,7 +1,7 @@
package bufr
import (
"git.tordarus.net/Tordarus/adverr"
"git.milar.in/milarin/adverr"
)
var (

8
go.mod
View File

@ -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
)

12
go.sum
View File

@ -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=

6
pos_rune.go Normal file
View File

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

17
position.go Normal file
View File

@ -0,0 +1,17 @@
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 = 0
} else {
p.Column++
}
}

112
reader.go
View File

@ -5,30 +5,75 @@ import (
"io"
"strings"
"git.tordarus.net/Tordarus/dstruct"
"git.milar.in/milarin/ds"
)
type Reader struct {
buf *dstruct.Stack[rune]
buf ds.Stack[posRune]
src *bufio.Reader
pos *Position
}
func NewReader(r io.Reader) *Reader {
return &Reader{
buf: new(dstruct.Stack[rune]),
buf: ds.NewArrayStack[posRune](),
src: bufio.NewReader(r),
pos: &Position{},
}
}
func (r *Reader) psrn(rn rune) posRune {
return posRune{
Rune: rn,
Pos: *r.pos,
}
}
func (r *Reader) Pos() (index, line, column int) {
return r.pos.Index, r.pos.Line, r.pos.Column
}
// Rune returns the next rune in r
func (r *Reader) Rune() (rune, error) {
rn, _, err := r.src.ReadRune()
if err == nil {
r.buf.Push(rn)
r.buf.Push(r.psrn(rn))
r.pos.Advance(rn)
}
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
}
// 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.
// The next read will include the unread rune.
// It returns ErrNothingToUnread if there wasn't any read yet
@ -38,16 +83,19 @@ func (r *Reader) UnreadRune() error {
}
if err := r.src.UnreadRune(); err == nil {
r.buf.Pop()
rn := r.buf.Pop()
*r.pos = rn.Pos
} else {
r.src = prependRune(r.buf.Pop(), r.src)
rn := r.buf.Pop()
*r.pos = rn.Pos
r.src = prependRune(rn.Rune, r.src)
}
return nil
}
// UnreadString calls UnreadRune for each rune in str
// The actual runes are irrelevant.
// UnreadString calls UnreadRune for each rune in str + one addtional rune for the separator rune
// The actual runes in str are irrelevant.
// Only the rune count of str determines the amount of UnreadRune calls.
// The first error occured will be returned immediately.
func (r *Reader) UnreadString(str string) error {
@ -57,6 +105,17 @@ func (r *Reader) UnreadString(str string) error {
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
}
@ -67,15 +126,50 @@ func (r *Reader) UnreadString(str string) error {
func (r *Reader) StringWhile(f func(rn rune) bool) (string, error) {
s := new(strings.Builder)
var rn rune
var err error
for rn, err := r.Rune(); err == nil && f(rn); rn, err = r.Rune() {
for rn, err = r.Rune(); err == nil && f(rn); rn, err = r.Rune() {
s.WriteRune(rn)
}
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()
}

36
reader_test.go Normal file
View File

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