ExpectOneOfString returns bool if string was found

This commit is contained in:
milarin 2023-07-14 20:40:01 +02:00
parent 4f269d0f29
commit 25505b0ea2
2 changed files with 5 additions and 6 deletions

View File

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

View File

@ -233,21 +233,21 @@ func (r *Reader) ExpectString(str string) (bool, error) {
// 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.
// If no string matches, ErrNoMatchFound is returned
func (r *Reader) ExpectOneOfString(str ...string) (string, error) {
func (r *Reader) ExpectOneOfString(str ...string) (string, bool, error) {
for _, s := range str {
ok, err := r.ExpectString(s)
if err != nil {
return "", err
return "", false, err
}
if ok {
return s, nil
return s, true, nil
}
}
return "", ErrNoMatchFound.New()
return "", false, nil
}
// Commit clears the internal buffer and therefore removes all data which were already read.