UnreadRunes implemented

This commit is contained in:
milarin 2023-01-20 22:33:44 +01:00
parent d60096e5b2
commit 3ca76e7670

View File

@ -58,6 +58,22 @@ func (r *Reader) PeekRune() (rune, error) {
return rn, nil return rn, nil
} }
// String returns the next len runes in r as a string.
// If an error occurs, both the already read string and the error will be returned
func (r *Reader) String(len int) (string, error) {
str := ""
for i := 0; i < len; i++ {
rn, err := r.Rune()
if err != nil {
return str, err
}
str += string(rn)
}
return str, nil
}
// UnreadRune unreads the last rune. // UnreadRune unreads the last rune.
// The next read will include the unread rune. // The next read will include the unread rune.
// It returns ErrNothingToUnread if there wasn't any read yet // It returns ErrNothingToUnread if there wasn't any read yet
@ -92,6 +108,17 @@ func (r *Reader) UnreadString(str string) error {
return r.UnreadRune() return r.UnreadRune()
} }
// UnreadRunes calls UnreadRune n times
func (r *Reader) UnreadRunes(n int) error {
for i := 0; i < n; i++ {
err := r.UnreadRune()
if err != nil {
return err
}
}
return nil
}
// StringWhile reads runes and calls f for each one. // StringWhile reads runes and calls f for each one.
// It returns all runes as a string for which f returned true. // It returns all runes as a string for which f returned true.
// It stops when f returns false or an error occured. // It stops when f returns false or an error occured.