From bbfad5f1cc5b8480ac41a3df7c98b5267c1a3327 Mon Sep 17 00:00:00 2001 From: Evan Shaw Date: Fri, 11 Feb 2011 12:39:18 -0500 Subject: [PATCH] bytes: fix bugs in buffer.ReadBytes Fixes #1498. R=golang-dev, mattn, r, rsc CC=golang-dev https://golang.org/cl/4140041 --- src/pkg/bytes/buffer.go | 3 ++- src/pkg/bytes/buffer_test.go | 35 +++++++++++++++++++++++------------ 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/src/pkg/bytes/buffer.go b/src/pkg/bytes/buffer.go index 3da8008be4..1acd4e05ca 100644 --- a/src/pkg/bytes/buffer.go +++ b/src/pkg/bytes/buffer.go @@ -312,13 +312,14 @@ func (b *Buffer) UnreadByte() os.Error { // delim. func (b *Buffer) ReadBytes(delim byte) (line []byte, err os.Error) { i := IndexByte(b.buf[b.off:], delim) - size := i + 1 - b.off + size := i + 1 if i < 0 { size = len(b.buf) - b.off err = os.EOF } line = make([]byte, size) copy(line, b.buf[b.off:]) + b.off += size return } diff --git a/src/pkg/bytes/buffer_test.go b/src/pkg/bytes/buffer_test.go index 2af9ffdefa..56a2d92753 100644 --- a/src/pkg/bytes/buffer_test.go +++ b/src/pkg/bytes/buffer_test.go @@ -350,25 +350,36 @@ func TestNext(t *testing.T) { } var readBytesTests = []struct { - buffer []byte + buffer string delim byte - expected []byte + expected []string err os.Error }{ - {err: os.EOF}, - {[]byte{}, 0, []byte{}, os.EOF}, - {[]byte("a\x00"), 0, []byte("a\x00"), nil}, - {[]byte("hello\x01world"), 1, []byte("hello\x01"), nil}, - {[]byte("foo\nbar"), 0, []byte("foo\nbar"), os.EOF}, - {[]byte("alpha beta gamma"), ' ', []byte("alpha "), nil}, + {"", 0, []string{""}, os.EOF}, + {"a\x00", 0, []string{"a\x00"}, nil}, + {"abbbaaaba", 'b', []string{"ab", "b", "b", "aaab"}, nil}, + {"hello\x01world", 1, []string{"hello\x01"}, nil}, + {"foo\nbar", 0, []string{"foo\nbar"}, os.EOF}, + {"alpha\nbeta\ngamma\n", '\n', []string{"alpha\n", "beta\n", "gamma\n"}, nil}, + {"alpha\nbeta\ngamma", '\n', []string{"alpha\n", "beta\n", "gamma"}, os.EOF}, } func TestReadBytes(t *testing.T) { for _, test := range readBytesTests { - buf := NewBuffer(test.buffer) - bytes, err := buf.ReadBytes(test.delim) - if !Equal(bytes, test.expected) || err != test.err { - t.Errorf("expected %q, %v got %q, %v", test.expected, test.err, bytes, err) + buf := NewBufferString(test.buffer) + var err os.Error + for _, expected := range test.expected { + var bytes []byte + bytes, err = buf.ReadBytes(test.delim) + if string(bytes) != expected { + t.Errorf("expected %q, got %q", expected, bytes) + } + if err != nil { + break + } + } + if err != test.err { + t.Errorf("expected error %v, got %v", test.err, err) } } }