diff --git a/src/bytes/reader.go b/src/bytes/reader.go index 5941ebdab4c..7aa30578b32 100644 --- a/src/bytes/reader.go +++ b/src/bytes/reader.go @@ -146,5 +146,8 @@ func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { return } +// Reset resets the Reader to be reading from b. +func (r *Reader) Reset(b []byte) { *r = Reader{b, 0, -1} } + // NewReader returns a new Reader reading from b. func NewReader(b []byte) *Reader { return &Reader{b, 0, -1} } diff --git a/src/bytes/reader_test.go b/src/bytes/reader_test.go index b929a282609..add985d57eb 100644 --- a/src/bytes/reader_test.go +++ b/src/bytes/reader_test.go @@ -256,3 +256,23 @@ func TestReaderLenSize(t *testing.T) { t.Errorf("Size = %d; want 3", r.Size()) } } + +func TestReaderReset(t *testing.T) { + r := NewReader([]byte("世界")) + if _, _, err := r.ReadRune(); err != nil { + t.Errorf("ReadRune: unexpected error: %v", err) + } + + const want = "abcdef" + r.Reset([]byte(want)) + if err := r.UnreadRune(); err == nil { + t.Errorf("UnreadRune: expected error, got nil") + } + buf, err := ioutil.ReadAll(r) + if err != nil { + t.Errorf("ReadAll: unexpected error: %v", err) + } + if got := string(buf); got != want { + t.Errorf("ReadAll: got %q, want %q", got, want) + } +} diff --git a/src/strings/reader.go b/src/strings/reader.go index 248e55245c1..737873c0992 100644 --- a/src/strings/reader.go +++ b/src/strings/reader.go @@ -145,6 +145,9 @@ func (r *Reader) WriteTo(w io.Writer) (n int64, err error) { return } +// Reset resets the Reader to be reading from s. +func (r *Reader) Reset(s string) { *r = Reader{s, 0, -1} } + // NewReader returns a new Reader reading from s. // It is similar to bytes.NewBufferString but more efficient and read-only. func NewReader(s string) *Reader { return &Reader{s, 0, -1} } diff --git a/src/strings/reader_test.go b/src/strings/reader_test.go index 5003a37be48..7bca2e89a17 100644 --- a/src/strings/reader_test.go +++ b/src/strings/reader_test.go @@ -170,3 +170,23 @@ func TestReaderLenSize(t *testing.T) { t.Errorf("Size = %d; want 3", r.Size()) } } + +func TestReaderReset(t *testing.T) { + r := strings.NewReader("世界") + if _, _, err := r.ReadRune(); err != nil { + t.Errorf("ReadRune: unexpected error: %v", err) + } + + const want = "abcdef" + r.Reset(want) + if err := r.UnreadRune(); err == nil { + t.Errorf("UnreadRune: expected error, got nil") + } + buf, err := ioutil.ReadAll(r) + if err != nil { + t.Errorf("ReadAll: unexpected error: %v", err) + } + if got := string(buf); got != want { + t.Errorf("ReadAll: got %q, want %q", got, want) + } +}