aboutsummaryrefslogtreecommitdiff
path: root/libgo/go/bytes/buffer.go
diff options
context:
space:
mode:
Diffstat (limited to 'libgo/go/bytes/buffer.go')
-rw-r--r--libgo/go/bytes/buffer.go94
1 files changed, 45 insertions, 49 deletions
diff --git a/libgo/go/bytes/buffer.go b/libgo/go/bytes/buffer.go
index 20e42bb..dc9d5e9 100644
--- a/libgo/go/bytes/buffer.go
+++ b/libgo/go/bytes/buffer.go
@@ -15,34 +15,37 @@ import (
// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
// The zero value for Buffer is an empty buffer ready to use.
type Buffer struct {
- buf []byte // contents are the bytes buf[off : len(buf)]
- off int // read at &buf[off], write at &buf[len(buf)]
- lastRead readOp // last read operation, so that Unread* can work correctly.
- // FIXME: lastRead can fit in a single byte
+ buf []byte // contents are the bytes buf[off : len(buf)]
+ off int // read at &buf[off], write at &buf[len(buf)]
+ bootstrap [64]byte // memory to hold first slice; helps small buffers avoid allocation.
+ lastRead readOp // last read operation, so that Unread* can work correctly.
- // memory to hold first slice; helps small buffers avoid allocation.
// FIXME: it would be advisable to align Buffer to cachelines to avoid false
// sharing.
- bootstrap [64]byte
}
// The readOp constants describe the last action performed on
// the buffer, so that UnreadRune and UnreadByte can check for
// invalid usage. opReadRuneX constants are chosen such that
// converted to int they correspond to the rune size that was read.
-type readOp int
+type readOp int8
+// Don't use iota for these, as the values need to correspond with the
+// names and comments, which is easier to see when being explicit.
const (
opRead readOp = -1 // Any other read operation.
- opInvalid = 0 // Non-read operation.
- opReadRune1 = 1 // Read rune of size 1.
- opReadRune2 = 2 // Read rune of size 2.
- opReadRune3 = 3 // Read rune of size 3.
- opReadRune4 = 4 // Read rune of size 4.
+ opInvalid readOp = 0 // Non-read operation.
+ opReadRune1 readOp = 1 // Read rune of size 1.
+ opReadRune2 readOp = 2 // Read rune of size 2.
+ opReadRune3 readOp = 3 // Read rune of size 3.
+ opReadRune4 readOp = 4 // Read rune of size 4.
)
// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
var ErrTooLarge = errors.New("bytes.Buffer: too large")
+var errNegativeRead = errors.New("bytes.Buffer: reader returned negative count from Read")
+
+const maxInt = int(^uint(0) >> 1)
// Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
// The slice is valid for use only until the next buffer modification (that is,
@@ -53,6 +56,8 @@ func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
// String returns the contents of the unread portion of the buffer
// as a string. If the Buffer is a nil pointer, it returns "<nil>".
+//
+// To build strings more efficiently, see the strings.Builder type.
func (b *Buffer) String() string {
if b == nil {
// Special case, useful in debugging.
@@ -61,6 +66,9 @@ func (b *Buffer) String() string {
return string(b.buf[b.off:])
}
+// empty returns whether the unread portion of the buffer is empty.
+func (b *Buffer) empty() bool { return len(b.buf) <= b.off }
+
// Len returns the number of bytes of the unread portion of the buffer;
// b.Len() == len(b.Bytes()).
func (b *Buffer) Len() int { return len(b.buf) - b.off }
@@ -81,7 +89,7 @@ func (b *Buffer) Truncate(n int) {
if n < 0 || n > b.Len() {
panic("bytes.Buffer: truncation out of range")
}
- b.buf = b.buf[0 : b.off+n]
+ b.buf = b.buf[:b.off+n]
}
// Reset resets the buffer to be empty,
@@ -97,7 +105,7 @@ func (b *Buffer) Reset() {
// internal buffer only needs to be resliced.
// It returns the index where bytes should be written and whether it succeeded.
func (b *Buffer) tryGrowByReslice(n int) (int, bool) {
- if l := len(b.buf); l+n <= cap(b.buf) {
+ if l := len(b.buf); n <= cap(b.buf)-l {
b.buf = b.buf[:l+n]
return l, true
}
@@ -122,15 +130,18 @@ func (b *Buffer) grow(n int) int {
b.buf = b.bootstrap[:n]
return 0
}
- if m+n <= cap(b.buf)/2 {
+ c := cap(b.buf)
+ if n <= c/2-m {
// We can slide things down instead of allocating a new
- // slice. We only need m+n <= cap(b.buf) to slide, but
+ // slice. We only need m+n <= c to slide, but
// we instead let capacity get twice as large so we
// don't spend all our time copying.
- copy(b.buf[:], b.buf[b.off:])
+ copy(b.buf, b.buf[b.off:])
+ } else if c > maxInt-c-n {
+ panic(ErrTooLarge)
} else {
// Not enough space anywhere, we need to allocate.
- buf := makeSlice(2*cap(b.buf) + n)
+ buf := makeSlice(2*c + n)
copy(buf, b.buf[b.off:])
b.buf = buf
}
@@ -150,7 +161,7 @@ func (b *Buffer) Grow(n int) {
panic("bytes.Buffer.Grow: negative count")
}
m := b.grow(n)
- b.buf = b.buf[0:m]
+ b.buf = b.buf[:m]
}
// Write appends the contents of p to the buffer, growing the buffer as
@@ -189,34 +200,22 @@ const MinRead = 512
// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
b.lastRead = opInvalid
- // If buffer is empty, reset to recover space.
- if b.off >= len(b.buf) {
- b.Reset()
- }
for {
- if free := cap(b.buf) - len(b.buf); free < MinRead {
- // not enough space at end
- newBuf := b.buf
- if b.off+free < MinRead {
- // not enough space using beginning of buffer;
- // double buffer capacity
- newBuf = makeSlice(2*cap(b.buf) + MinRead)
- }
- copy(newBuf, b.buf[b.off:])
- b.buf = newBuf[:len(b.buf)-b.off]
- b.off = 0
+ i := b.grow(MinRead)
+ m, e := r.Read(b.buf[i:cap(b.buf)])
+ if m < 0 {
+ panic(errNegativeRead)
}
- m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])
- b.buf = b.buf[0 : len(b.buf)+m]
+
+ b.buf = b.buf[:i+m]
n += int64(m)
if e == io.EOF {
- break
+ return n, nil // e is EOF, so return nil explicitly
}
if e != nil {
return n, e
}
}
- return n, nil // err is EOF, so return nil explicitly
}
// makeSlice allocates a slice of size n. If the allocation fails, it panics
@@ -237,8 +236,7 @@ func makeSlice(n int) []byte {
// encountered during the write is also returned.
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
b.lastRead = opInvalid
- if b.off < len(b.buf) {
- nBytes := b.Len()
+ if nBytes := b.Len(); nBytes > 0 {
m, e := w.Write(b.buf[b.off:])
if m > nBytes {
panic("bytes.Buffer.WriteTo: invalid Write count")
@@ -256,7 +254,7 @@ func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
}
// Buffer is now empty; reset.
b.Reset()
- return
+ return n, nil
}
// WriteByte appends the byte c to the buffer, growing the buffer as needed.
@@ -298,11 +296,11 @@ func (b *Buffer) WriteRune(r rune) (n int, err error) {
// otherwise it is nil.
func (b *Buffer) Read(p []byte) (n int, err error) {
b.lastRead = opInvalid
- if b.off >= len(b.buf) {
+ if b.empty() {
// Buffer is empty, reset to recover space.
b.Reset()
if len(p) == 0 {
- return
+ return 0, nil
}
return 0, io.EOF
}
@@ -311,7 +309,7 @@ func (b *Buffer) Read(p []byte) (n int, err error) {
if n > 0 {
b.lastRead = opRead
}
- return
+ return n, nil
}
// Next returns a slice containing the next n bytes from the buffer,
@@ -335,8 +333,7 @@ func (b *Buffer) Next(n int) []byte {
// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error io.EOF.
func (b *Buffer) ReadByte() (byte, error) {
- b.lastRead = opInvalid
- if b.off >= len(b.buf) {
+ if b.empty() {
// Buffer is empty, reset to recover space.
b.Reset()
return 0, io.EOF
@@ -353,8 +350,7 @@ func (b *Buffer) ReadByte() (byte, error) {
// If the bytes are an erroneous UTF-8 encoding, it
// consumes one byte and returns U+FFFD, 1.
func (b *Buffer) ReadRune() (r rune, size int, err error) {
- b.lastRead = opInvalid
- if b.off >= len(b.buf) {
+ if b.empty() {
// Buffer is empty, reset to recover space.
b.Reset()
return 0, 0, io.EOF
@@ -413,7 +409,7 @@ func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
// return a copy of slice. The buffer's backing array may
// be overwritten by later calls.
line = append(line, slice...)
- return
+ return line, err
}
// readSlice is like ReadBytes but returns a reference to internal buffer data.