fixed StringWhile

This commit is contained in:
Timon Ringwald 2022-05-23 14:04:39 +02:00
parent 40d00d9244
commit ca6cf3d943
2 changed files with 14 additions and 1 deletions

View File

@ -84,8 +84,9 @@ func (r *Reader) UnreadString(str string) error {
func (r *Reader) StringWhile(f func(rn rune) bool) (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 && f(rn); rn, err = r.Rune() {
s.WriteRune(rn)
}

View File

@ -22,3 +22,15 @@ func TestPos(t *testing.T) {
}
}
}
func TestEOF(t *testing.T) {
r := NewReader(strings.NewReader("hello world\nasddsa"))
for line, err := r.StringUntil(isNewline); err == nil; line, err = r.StringUntil(isNewline) {
fmt.Println(line, err)
}
}
func isNewline(rn rune) bool {
return rn == '\n'
}