diff options
| author | Joe Tsai <joetsai@digital-static.net> | 2016-03-31 16:05:23 -0700 |
|---|---|---|
| committer | Brad Fitzpatrick <bradfitz@golang.org> | 2016-04-07 18:58:01 +0000 |
| commit | e88f89028a55acf9c8b76b7f6ca284c3f9eb4cbd (patch) | |
| tree | f33797b9e2ff352667f489fa04e04fad7f2b0d05 /src/strings/reader_test.go | |
| parent | e6f36f0cd5b45b9ce7809a34c45aeb66a5ca64a4 (diff) | |
| download | go-e88f89028a55acf9c8b76b7f6ca284c3f9eb4cbd.tar.xz | |
bytes, string: add Reset method to Reader
Currently, there is no easy allocation-free way to turn a
[]byte or string into an io.Reader. Thus, we add a Reset method
to bytes.Reader and strings.Reader to allow the reuse of these
Readers with another []byte or string.
This is consistent with the fact that many standard library io.Readers
already support a Reset method of some type:
bufio.Reader
flate.Reader
gzip.Reader
zlib.Reader
debug/dwarf.LineReader
bytes.Buffer
crypto/rc4.Cipher
Fixes #15033
Change-Id: I456fd1af77af6ef0b4ac6228b058ac1458ff3d19
Reviewed-on: https://go-review.googlesource.com/21386
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Diffstat (limited to 'src/strings/reader_test.go')
| -rw-r--r-- | src/strings/reader_test.go | 20 |
1 files changed, 20 insertions, 0 deletions
diff --git a/src/strings/reader_test.go b/src/strings/reader_test.go index 5003a37be4..7bca2e89a1 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) + } +} |
