RuneFunc implemented
This commit is contained in:
41
reader.go
41
reader.go
@ -119,16 +119,16 @@ func (r *Reader) UnreadRunes(n int) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
// 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)
|
||||
}
|
||||
|
||||
@ -136,8 +136,8 @@ func (r *Reader) StringWhile(f func(rn rune) bool) (string, error) {
|
||||
}
|
||||
|
||||
// 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)
|
||||
func (r *Reader) PeekStringWhile(f ...RuneFunc) (string, error) {
|
||||
str, err := r.StringWhile(f...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -149,14 +149,23 @@ func (r *Reader) PeekStringWhile(f func(rn rune) bool) (string, error) {
|
||||
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) })
|
||||
// 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.
|
||||
// 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 func(rn rune) bool) (string, error) {
|
||||
str, err := r.StringUntil(f)
|
||||
func (r *Reader) PeekStringUntil(f ...RuneFunc) (string, error) {
|
||||
str, err := r.StringUntil(f...)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
@ -168,6 +177,12 @@ func (r *Reader) PeekStringUntil(f func(rn rune) bool) (string, error) {
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// SkipStringUntil acts as StringUntil but discards the string
|
||||
func (r *Reader) SkipStringUntil(f ...RuneFunc) error {
|
||||
_, err := r.StringUntil(f...)
|
||||
return err
|
||||
}
|
||||
|
||||
// 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() {
|
||||
|
Reference in New Issue
Block a user