diff --git a/reader.go b/reader.go index 342b710..9d4172f 100644 --- a/reader.go +++ b/reader.go @@ -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) } diff --git a/reader_test.go b/reader_test.go index 5a2de01..27a95c5 100644 --- a/reader_test.go +++ b/reader_test.go @@ -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' +}