new constructors
This commit is contained in:
parent
11b94aba9e
commit
1c9a3962ec
31
reader.go
31
reader.go
@ -14,7 +14,7 @@ type Reader struct {
|
|||||||
pos *Position
|
pos *Position
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewReader(r io.Reader) *Reader {
|
func New(r io.Reader) *Reader {
|
||||||
return &Reader{
|
return &Reader{
|
||||||
buf: ds.NewArrayStack[posRune](),
|
buf: ds.NewArrayStack[posRune](),
|
||||||
src: bufio.NewReader(r),
|
src: bufio.NewReader(r),
|
||||||
@ -22,6 +22,10 @@ func NewReader(r io.Reader) *Reader {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewFromString(str string) *Reader {
|
||||||
|
return New(strings.NewReader(str))
|
||||||
|
}
|
||||||
|
|
||||||
func (r *Reader) psrn(rn rune) posRune {
|
func (r *Reader) psrn(rn rune) posRune {
|
||||||
return posRune{
|
return posRune{
|
||||||
Rune: rn,
|
Rune: rn,
|
||||||
@ -149,12 +153,6 @@ func (r *Reader) PeekStringWhile(f ...RuneFunc) (string, error) {
|
|||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SkipStringWhile acts as StringWhile but discards the string
|
|
||||||
func (r *Reader) SkipStringWhile(f ...RuneFunc) error {
|
|
||||||
_, err := r.StringWhile(f...)
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
// StringUntil reads runes and calls all functions for each one.
|
// StringUntil reads runes and calls all functions for each one.
|
||||||
// It returns all runes as a string for which all functions returned true.
|
// It returns all runes as a string for which all functions returned true.
|
||||||
// It stops when any function returns false or an error occured.
|
// It stops when any function returns false or an error occured.
|
||||||
@ -177,12 +175,27 @@ func (r *Reader) PeekStringUntil(f ...RuneFunc) (string, error) {
|
|||||||
return str, nil
|
return str, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SkipStringUntil acts as StringUntil but discards the string
|
// SkipUntil acts as StringUntil but discards the string
|
||||||
func (r *Reader) SkipStringUntil(f ...RuneFunc) error {
|
func (r *Reader) SkipUntil(f ...RuneFunc) error {
|
||||||
_, err := r.StringUntil(f...)
|
_, err := r.StringUntil(f...)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// SkipWhile acts as StringWhile but discards the string
|
||||||
|
func (r *Reader) SkipWhile(f ...RuneFunc) error {
|
||||||
|
_, err := r.StringWhile(f...)
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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.
|
// 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.
|
// After calling Commit any unreads will return ErrNothingToUnread until another read occured.
|
||||||
func (r *Reader) Commit() {
|
func (r *Reader) Commit() {
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func TestPos(t *testing.T) {
|
func TestPos(t *testing.T) {
|
||||||
r := NewReader(strings.NewReader("hello world\nsecond line"))
|
r := New(strings.NewReader("hello world\nsecond line"))
|
||||||
|
|
||||||
unread := false
|
unread := false
|
||||||
for rn, err := r.Rune(); err == nil; rn, err = r.Rune() {
|
for rn, err := r.Rune(); err == nil; rn, err = r.Rune() {
|
||||||
@ -24,7 +24,7 @@ func TestPos(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestEOF(t *testing.T) {
|
func TestEOF(t *testing.T) {
|
||||||
r := NewReader(strings.NewReader("hello world\nasddsa"))
|
r := New(strings.NewReader("hello world\nasddsa"))
|
||||||
|
|
||||||
var line string
|
var line string
|
||||||
var err error
|
var err error
|
||||||
|
Loading…
Reference in New Issue
Block a user