diff options
author | Russ Cox <rsc@golang.org> | 2010-04-26 10:02:01 -0700 |
---|---|---|
committer | Russ Cox <rsc@golang.org> | 2010-04-26 10:02:01 -0700 |
commit | 49d97fdf9540482c4e9b49005457aaa2b7f60e48 (patch) | |
tree | fac23ebe52a353308ffb8a8f41811d58652072e7 /src/pkg/bytes/buffer.go | |
parent | a3c4d2f8fd30316e08deb6950b0bbb6c713a55e7 (diff) | |
download | golang-49d97fdf9540482c4e9b49005457aaa2b7f60e48.tar.gz |
bytes: add Next method to Buffer, simplify Read.
R=r
CC=golang-dev
http://codereview.appspot.com/980043
Diffstat (limited to 'src/pkg/bytes/buffer.go')
-rw-r--r-- | src/pkg/bytes/buffer.go | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/src/pkg/bytes/buffer.go b/src/pkg/bytes/buffer.go index faccca3be..7a996c4ca 100644 --- a/src/pkg/bytes/buffer.go +++ b/src/pkg/bytes/buffer.go @@ -196,17 +196,23 @@ func (b *Buffer) Read(p []byte) (n int, err os.Error) { b.Truncate(0) return 0, os.EOF } - m := b.Len() - n = len(p) + n = copy(p, b.buf[b.off:]) + b.off += n + return +} +// Next returns a slice containing the next n bytes from the buffer, +// advancing the buffer as if the bytes had been returned by Read. +// If there are fewer than n bytes in the buffer, Next returns the entire buffer. +// The slice is only valid until the next call to a read or write method. +func (b *Buffer) Next(n int) []byte { + m := b.Len() if n > m { - // more bytes requested than available n = m } - - copy(p, b.buf[b.off:b.off+n]) + data := b.buf[b.off : b.off+n] b.off += n - return n, err + return data } // ReadByte reads and returns the next byte from the buffer. |