3 Commits

Author SHA1 Message Date
25505b0ea2 ExpectOneOfString returns bool if string was found 2023-07-14 20:40:01 +02:00
4f269d0f29 fixed error template as template 2023-07-14 20:35:11 +02:00
e344b5973a implemented Buffered method 2023-07-14 18:36:14 +02:00
2 changed files with 9 additions and 6 deletions

View File

@ -6,5 +6,4 @@ import (
var ( var (
ErrNothingToUnread = adverr.NewErrTmpl("ErrNothingToUnread", "Unreading failed because there wasn't any Read yet") ErrNothingToUnread = adverr.NewErrTmpl("ErrNothingToUnread", "Unreading failed because there wasn't any Read yet")
ErrNoMatchFound = adverr.NewErrTmpl("ErrNoMatchFound", "no match found")
) )

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) {
@ -229,21 +233,21 @@ func (r *Reader) ExpectString(str string) (bool, error) {
// ExpectOneOfString calls ExpectString for each string successively // ExpectOneOfString calls ExpectString for each string successively
// and returns the string which first matched. // and returns the string which first matched.
// The boolean value is true if any string was found.
// The returned string will not be unread. // The returned string will not be unread.
// If no string matches, ErrNoMatchFound is returned func (r *Reader) ExpectOneOfString(str ...string) (string, bool, error) {
func (r *Reader) ExpectOneOfString(str ...string) (string, error) {
for _, s := range str { for _, s := range str {
ok, err := r.ExpectString(s) ok, err := r.ExpectString(s)
if err != nil { if err != nil {
return "", err return "", false, err
} }
if ok { if ok {
return s, nil return s, true, nil
} }
} }
return "", ErrNoMatchFound 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.