Commit 0359af4f authored by Rob Pike's avatar Rob Pike

bytes: document that Buffer.Write grows the buffer

Do a little reformatting too.
Fixes #5152.

R=golang-dev, bradfitz, gri
CC=golang-dev
https://golang.org/cl/8157044
parent ecdcec1d
...@@ -119,20 +119,18 @@ func (b *Buffer) Grow(n int) { ...@@ -119,20 +119,18 @@ func (b *Buffer) Grow(n int) {
b.buf = b.buf[0:m] b.buf = b.buf[0:m]
} }
// Write appends the contents of p to the buffer. The return // Write appends the contents of p to the buffer, growing the buffer as
// value n is the length of p; err is always nil. // needed. The return value n is the length of p; err is always nil. If the
// If the buffer becomes too large, Write will panic with // buffer becomes too large, Write will panic with ErrTooLarge.
// ErrTooLarge.
func (b *Buffer) Write(p []byte) (n int, err error) { func (b *Buffer) Write(p []byte) (n int, err error) {
b.lastRead = opInvalid b.lastRead = opInvalid
m := b.grow(len(p)) m := b.grow(len(p))
return copy(b.buf[m:], p), nil return copy(b.buf[m:], p), nil
} }
// WriteString appends the contents of s to the buffer. The return // WriteString appends the contents of s to the buffer, growing the buffer as
// value n is the length of s; err is always nil. // needed. The return value n is the length of s; err is always nil. If the
// If the buffer becomes too large, WriteString will panic with // buffer becomes too large, WriteString will panic with ErrTooLarge.
// ErrTooLarge.
func (b *Buffer) WriteString(s string) (n int, err error) { func (b *Buffer) WriteString(s string) (n int, err error) {
b.lastRead = opInvalid b.lastRead = opInvalid
m := b.grow(len(s)) m := b.grow(len(s))
...@@ -145,12 +143,10 @@ func (b *Buffer) WriteString(s string) (n int, err error) { ...@@ -145,12 +143,10 @@ func (b *Buffer) WriteString(s string) (n int, err error) {
// underlying buffer. // underlying buffer.
const MinRead = 512 const MinRead = 512
// ReadFrom reads data from r until EOF and appends it to the buffer. // ReadFrom reads data from r until EOF and appends it to the buffer, growing
// The return value n is the number of bytes read. // the buffer as needed. The return value n is the number of bytes read. Any
// Any error except io.EOF encountered during the read // error except io.EOF encountered during the read is also returned. If the
// is also returned. // buffer becomes too large, ReadFrom will panic with ErrTooLarge.
// If the buffer becomes too large, ReadFrom will panic with
// ErrTooLarge.
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) { func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
b.lastRead = opInvalid b.lastRead = opInvalid
// If buffer is empty, reset to recover space. // If buffer is empty, reset to recover space.
...@@ -195,10 +191,10 @@ func makeSlice(n int) []byte { ...@@ -195,10 +191,10 @@ func makeSlice(n int) []byte {
return make([]byte, n) return make([]byte, n)
} }
// WriteTo writes data to w until the buffer is drained or an error // WriteTo writes data to w until the buffer is drained or an error occurs.
// occurs. The return value n is the number of bytes written; it always // The return value n is the number of bytes written; it always fits into an
// fits into an int, but it is int64 to match the io.WriterTo interface. // int, but it is int64 to match the io.WriterTo interface. Any error
// Any error encountered during the write is also returned. // encountered during the write is also returned.
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
b.lastRead = opInvalid b.lastRead = opInvalid
if b.off < len(b.buf) { if b.off < len(b.buf) {
...@@ -223,10 +219,9 @@ func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { ...@@ -223,10 +219,9 @@ func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
return return
} }
// WriteByte appends the byte c to the buffer. // WriteByte appends the byte c to the buffer, growing the buffer as needed.
// The returned error is always nil, but is included // The returned error is always nil, but is included to match bufio.Writer's
// to match bufio.Writer's WriteByte. // WriteByte. If the buffer becomes too large, WriteByte will panic with
// If the buffer becomes too large, WriteByte will panic with
// ErrTooLarge. // ErrTooLarge.
func (b *Buffer) WriteByte(c byte) error { func (b *Buffer) WriteByte(c byte) error {
b.lastRead = opInvalid b.lastRead = opInvalid
...@@ -235,12 +230,10 @@ func (b *Buffer) WriteByte(c byte) error { ...@@ -235,12 +230,10 @@ func (b *Buffer) WriteByte(c byte) error {
return nil return nil
} }
// WriteRune appends the UTF-8 encoding of Unicode // WriteRune appends the UTF-8 encoding of Unicode code point r to the
// code point r to the buffer, returning its length and // buffer, returning its length and an error, which is always nil but is
// an error, which is always nil but is included // included to match bufio.Writer's WriteRune. The buffer is grown as needed;
// to match bufio.Writer's WriteRune. // if it becomes too large, WriteRune will panic with ErrTooLarge.
// If the buffer becomes too large, WriteRune will panic with
// ErrTooLarge.
func (b *Buffer) WriteRune(r rune) (n int, err error) { func (b *Buffer) WriteRune(r rune) (n int, err error) {
if r < utf8.RuneSelf { if r < utf8.RuneSelf {
b.WriteByte(byte(r)) b.WriteByte(byte(r))
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment