From e88f89028a55acf9c8b76b7f6ca284c3f9eb4cbd Mon Sep 17 00:00:00 2001 From: Joe Tsai Date: Thu, 31 Mar 2016 16:05:23 -0700 Subject: 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/strings/reader.go | 3 +++ 1 file changed, 3 insertions(+) (limited to 'src/strings/reader.go') diff --git a/src/strings/reader.go b/src/strings/reader.go index 248e55245c..737873c099 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} } -- cgit v1.3