From ca6cf3d943989095ede9a9155b45cb03f30dc655 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Mon, 23 May 2022 14:04:39 +0200 Subject: [PATCH] fixed StringWhile --- reader.go | 3 ++- reader_test.go | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) 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' +}