6 Commits

Author SHA1 Message Date
9d4da8ef95 fixed line indexing 2023-03-18 23:08:33 +01:00
31f5aa6e52 unread rune on skip methods 2023-01-21 00:37:14 +01:00
1c9a3962ec new constructors 2023-01-21 00:18:38 +01:00
11b94aba9e RuneFunc implemented 2023-01-20 23:59:36 +01:00
3ca76e7670 UnreadRunes implemented 2023-01-20 22:33:44 +01:00
d60096e5b2 Commit() method implemented 2023-01-20 22:16:46 +01:00
7 changed files with 215 additions and 29 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=

140
reader.go
View File

@ -5,23 +5,27 @@ 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 {
func New(r io.Reader) *Reader {
return &Reader{
buf: new(dstruct.Stack[posRune]),
buf: ds.NewArrayStack[posRune](),
src: bufio.NewReader(r),
pos: &Position{},
pos: &Position{Index: 0, Line: 1, Column: 0},
}
}
func NewFromString(str string) *Reader {
return New(strings.NewReader(str))
}
func (r *Reader) psrn(rn rune) posRune {
return posRune{
Rune: rn,
@ -43,6 +47,37 @@ 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
}
// 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
@ -77,23 +112,100 @@ func (r *Reader) UnreadString(str string) error {
return r.UnreadRune()
}
// StringWhile reads runes and calls f for each one.
// It returns all runes as a string for which f returned true.
// It stops when f returns false or an error occured.
// The rune for which f returned false will not be unread.
func (r *Reader) StringWhile(f func(rn rune) bool) (string, error) {
// 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
}
// StringWhile reads runes and calls all functions for each one.
// It returns all runes as a string for which any function returned true.
// It stops when all functions returned false or an error occured.
// The rune for which that function returned false will not be unread.
func (r *Reader) StringWhile(f ...RuneFunc) (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 && findFirstTrue(rn, f); rn, err = r.Rune() {
s.WriteRune(rn)
}
return s.String(), err
}
// 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) })
// PeekStringWhile acts as StringWhile but does not advance reader position
func (r *Reader) PeekStringWhile(f ...RuneFunc) (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 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
}
// 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()
}

View File

@ -7,7 +7,7 @@ import (
)
func TestPos(t *testing.T) {
r := NewReader(strings.NewReader("hello world\nsecond line"))
r := New(strings.NewReader("hello world\nsecond line"))
unread := false
for rn, err := r.Rune(); err == nil; rn, err = r.Rune() {
@ -24,13 +24,13 @@ func TestPos(t *testing.T) {
}
func TestEOF(t *testing.T) {
r := NewReader(strings.NewReader("hello world\nasddsa"))
r := New(strings.NewReader("hello world\nasddsa"))
for line, err := r.StringUntil(isNewline); err == nil; line, err = r.StringUntil(isNewline) {
var line string
var err error
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'
fmt.Println(line, err)
}

52
runefunc.go Normal file
View File

@ -0,0 +1,52 @@
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 IsWhitespace(rn rune) bool {
return IsSpace(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,3 +13,21 @@ func prependString(str string, r io.Reader) *bufio.Reader {
func prependRune(rn rune, r io.Reader) *bufio.Reader {
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
}