[go] bytes, string: add Reset method to Reader

120 views
Skip to first unread message

Joe Tsai (Gerrit)

unread,
Mar 31, 2016, 7:10:45 PM3/31/16
to Ian Lance Taylor, golang-co...@googlegroups.com
Joe Tsai uploaded a change:
https://go-review.googlesource.com/21386

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
---
M src/bytes/reader.go
M src/bytes/reader_test.go
M src/strings/reader.go
M src/strings/reader_test.go
4 files changed, 46 insertions(+), 0 deletions(-)



diff --git a/src/bytes/reader.go b/src/bytes/reader.go
index 5941ebd..7aa3057 100644
--- a/src/bytes/reader.go
+++ b/src/bytes/reader.go
@@ -146,5 +146,8 @@
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 b929a28..add985d 100644
--- a/src/bytes/reader_test.go
+++ b/src/bytes/reader_test.go
@@ -256,3 +256,23 @@
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 248e552..737873c 100644
--- a/src/strings/reader.go
+++ b/src/strings/reader.go
@@ -145,6 +145,9 @@
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 5003a37..7bca2e8 100644
--- a/src/strings/reader_test.go
+++ b/src/strings/reader_test.go
@@ -170,3 +170,23 @@
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)
+ }
+}

--
https://go-review.googlesource.com/21386

Brad Fitzpatrick (Gerrit)

unread,
Mar 31, 2016, 7:16:01 PM3/31/16
to Joe Tsai, Brad Fitzpatrick, golang-co...@googlegroups.com
Brad Fitzpatrick has posted comments on this change.

bytes, string: add Reset method to Reader

Patch Set 1: Run-TryBot+1 Code-Review+2

--
https://go-review.googlesource.com/21386
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-HasComments: No

Gobot Gobot (Gerrit)

unread,
Mar 31, 2016, 7:16:06 PM3/31/16
to Joe Tsai, Brad Fitzpatrick, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

bytes, string: add Reset method to Reader

Patch Set 1:

TryBots beginning. Status page: http://farmer.golang.org/try?commit=99c65e73

--
https://go-review.googlesource.com/21386
Gerrit-Reviewer: Brad Fitzpatrick <brad...@golang.org>
Gerrit-Reviewer: Gobot Gobot <go...@golang.org>
Gerrit-HasComments: No

Gobot Gobot (Gerrit)

unread,
Mar 31, 2016, 7:26:37 PM3/31/16
to Joe Tsai, Brad Fitzpatrick, golang-co...@googlegroups.com
Gobot Gobot has posted comments on this change.

bytes, string: add Reset method to Reader

Patch Set 1: TryBot-Result+1

TryBots are happy.

Brad Fitzpatrick (Gerrit)

unread,
Apr 7, 2016, 2:58:04 PM4/7/16
to Brad Fitzpatrick, Joe Tsai, golang-...@googlegroups.com, Gobot Gobot, golang-co...@googlegroups.com
Brad Fitzpatrick has submitted this change and it was merged.

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 <brad...@golang.org>
Run-TryBot: Brad Fitzpatrick <brad...@golang.org>
TryBot-Result: Gobot Gobot <go...@golang.org>
---
M src/bytes/reader.go
M src/bytes/reader_test.go
M src/strings/reader.go
M src/strings/reader_test.go
4 files changed, 46 insertions(+), 0 deletions(-)

Approvals:
Gobot Gobot: TryBots succeeded
Brad Fitzpatrick: Looks good to me, approved; Run TryBots
Reply all
Reply to author
Forward
0 new messages