5 Commits

2 changed files with 51 additions and 1 deletions

View File

@ -47,6 +47,10 @@ func (r *Reader) Rune() (rune, error) {
return rn, err return rn, err
} }
func (r *Reader) Buffered() int {
return r.src.Buffered()
}
// PeekRune returns the next rune in r without advancing reader position. // PeekRune returns the next rune in r without advancing reader position.
// The next read will return the same rune again. // The next read will return the same rune again.
func (r *Reader) PeekRune() (rune, error) { func (r *Reader) PeekRune() (rune, error) {
@ -204,6 +208,48 @@ func (r *Reader) ExpectRune(f ...RuneFunc) (bool, error) {
return findFirstTrue(rn, f), nil return findFirstTrue(rn, f), nil
} }
// ExpectString calls ExpectRune for each rune in str successively.
// If the expected string was not found, all read runes will be unread
func (r *Reader) ExpectString(str string) (bool, error) {
read := 0
for _, rn := range str {
ok, err := r.ExpectRune(Is(rn))
if err != nil {
return false, err
}
read++
if !ok {
if err := r.UnreadRunes(read); err != nil {
return false, err
}
return false, nil
}
}
return true, nil
}
// ExpectOneOfString calls ExpectString for each string successively
// and returns the string which first matched.
// The boolean value is true if any string was found.
// The returned string will not be unread.
func (r *Reader) ExpectOneOfString(str ...string) (string, bool, error) {
for _, s := range str {
ok, err := r.ExpectString(s)
if err != nil {
return "", false, err
}
if ok {
return s, true, nil
}
}
return "", false, 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() {

View File

@ -12,8 +12,12 @@ func IsSpace(rn rune) bool {
return rn == ' ' return rn == ' '
} }
func IsTab(rn rune) bool {
return rn == '\t'
}
func IsWhitespace(rn rune) bool { func IsWhitespace(rn rune) bool {
return IsSpace(rn) || IsNewLine(rn) return IsSpace(rn) || IsTab(rn) || IsNewLine(rn)
} }
func And(f ...RuneFunc) RuneFunc { func And(f ...RuneFunc) RuneFunc {