FAQ
Reviewers: golang-dev_googlegroups.com,

Message:
Hello golang-dev@googlegroups.com,

I'd like you to review this change to
https://go.googlecode.com/hg/


Description:
bytes, strings: fix Reader WriteTo return value on 0 bytes copied

Fixes issue 4421

Please review this at http://codereview.appspot.com/6855083/

Affected files:
M src/pkg/bytes/reader.go
M src/pkg/bytes/reader_test.go
M src/pkg/strings/reader.go
M src/pkg/strings/reader_test.go


Index: src/pkg/bytes/reader.go
===================================================================
--- a/src/pkg/bytes/reader.go
+++ b/src/pkg/bytes/reader.go
@@ -125,7 +125,7 @@
func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
r.prevRune = -1
if r.i >= len(r.s) {
- return 0, io.EOF
+ return 0, nil
}
b := r.s[r.i:]
m, err := w.Write(b)
Index: src/pkg/bytes/reader_test.go
===================================================================
--- a/src/pkg/bytes/reader_test.go
+++ b/src/pkg/bytes/reader_test.go
@@ -88,16 +88,20 @@
}

func TestReaderWriteTo(t *testing.T) {
- for i := 3; i < 30; i += 3 {
- s := data[:len(data)/i]
- r := NewReader(testBytes[:len(testBytes)/i])
+ for i := 0; i < 30; i += 3 {
+ var l int
+ if i > 0 {
+ l = len(data) / i
+ }
+ s := data[:l]
+ r := NewReader(testBytes[:l])
var b Buffer
n, err := r.WriteTo(&b)
if expect := int64(len(s)); n != expect {
t.Errorf("got %v; want %v", n, expect)
}
if err != nil {
- t.Errorf("got error = %v; want nil", err)
+ t.Errorf("for length %d: got error = %v; want nil", l, err)
}
if b.String() != s {
t.Errorf("got string %q; want %q", b.String(), s)
Index: src/pkg/strings/reader.go
===================================================================
--- a/src/pkg/strings/reader.go
+++ b/src/pkg/strings/reader.go
@@ -124,7 +124,7 @@
func (r *Reader) WriteTo(w io.Writer) (n int64, err error) {
r.prevRune = -1
if r.i >= len(r.s) {
- return 0, io.EOF
+ return 0, nil
}
s := r.s[r.i:]
m, err := io.WriteString(w, s)
Index: src/pkg/strings/reader_test.go
===================================================================
--- a/src/pkg/strings/reader_test.go
+++ b/src/pkg/strings/reader_test.go
@@ -90,7 +90,7 @@

func TestWriteTo(t *testing.T) {
const str = "0123456789"
- for i := 0; i < len(str); i++ {
+ for i := 0; i <= len(str); i++ {
s := str[i:]
r := strings.NewReader(s)
var b bytes.Buffer
@@ -99,7 +99,7 @@
t.Errorf("got %v; want %v", n, expect)
}
if err != nil {
- t.Errorf("got error = %v; want nil", err)
+ t.Errorf("for length %d: got error = %v; want nil", len(s), err)
}
if b.String() != s {
t.Errorf("got string %q; want %q", b.String(), s)

Search Discussions

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-dev @
categoriesgo
postedNov 24, '12 at 7:15a
activeNov 25, '12 at 5:04p
posts7
users4
websitegolang.org

4 users in discussion

Bradfitz: 4 posts Dave: 1 post Rsc: 1 post Mchaten: 1 post

People

Translate

site design / logo © 2022 Grokbase