summaryrefslogtreecommitdiff
path: root/src/pkg/bytes
diff options
context:
space:
mode:
authorOndřej Surý <ondrej@sury.org>2011-02-18 09:50:58 +0100
committerOndřej Surý <ondrej@sury.org>2011-02-18 09:50:58 +0100
commitc072558b90f1bbedc2022b0f30c8b1ac4712538e (patch)
tree67767591619e4bd8111fb05fac185cde94fb7378 /src/pkg/bytes
parent5859517b767c99749a45651c15d4bae5520ebae8 (diff)
downloadgolang-upstream/2011.02.15.tar.gz
Imported Upstream version 2011.02.15upstream/2011.02.15
Diffstat (limited to 'src/pkg/bytes')
-rw-r--r--src/pkg/bytes/buffer.go12
-rw-r--r--src/pkg/bytes/buffer_test.go35
2 files changed, 31 insertions, 16 deletions
diff --git a/src/pkg/bytes/buffer.go b/src/pkg/bytes/buffer.go
index 4aa74371f..1acd4e05c 100644
--- a/src/pkg/bytes/buffer.go
+++ b/src/pkg/bytes/buffer.go
@@ -154,17 +154,20 @@ func (b *Buffer) ReadFrom(r io.Reader) (n int64, err os.Error) {
}
// WriteTo writes data to w until the buffer is drained or an error
-// occurs. The return value n is the number of bytes written.
+// occurs. The return value n is the number of bytes written; it always
+// fits into an int, but it is int64 to match the io.WriterTo interface.
// Any error encountered during the write is also returned.
func (b *Buffer) WriteTo(w io.Writer) (n int64, err os.Error) {
b.lastRead = opInvalid
- for b.off < len(b.buf) {
+ if b.off < len(b.buf) {
m, e := w.Write(b.buf[b.off:])
- n += int64(m)
b.off += m
+ n = int64(m)
if e != nil {
return n, e
}
+ // otherwise all bytes were written, by definition of
+ // Write method in io.Writer
}
// Buffer is now empty; reset.
b.Truncate(0)
@@ -309,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 2af9ffdef..56a2d9275 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)
}
}
}