Source file src/bytes/buffer.go

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package bytes
     6  
     7  // Simple byte buffer for marshaling data.
     8  
     9  import (
    10  	"errors"
    11  	"io"
    12  	"unicode/utf8"
    13  )
    14  
    15  // smallBufferSize is an initial allocation minimal capacity.
    16  const smallBufferSize = 64
    17  
    18  // A Buffer is a variable-sized buffer of bytes with Read and Write methods.
    19  // The zero value for Buffer is an empty buffer ready to use.
    20  type Buffer struct {
    21  	buf      []byte // contents are the bytes buf[off : len(buf)]
    22  	off      int    // read at &buf[off], write at &buf[len(buf)]
    23  	lastRead readOp // last read operation, so that Unread* can work correctly.
    24  }
    25  
    26  // The readOp constants describe the last action performed on
    27  // the buffer, so that UnreadRune and UnreadByte can check for
    28  // invalid usage. opReadRuneX constants are chosen such that
    29  // converted to int they correspond to the rune size that was read.
    30  type readOp int8
    31  
    32  // Don't use iota for these, as the values need to correspond with the
    33  // names and comments, which is easier to see when being explicit.
    34  const (
    35  	opRead      readOp = -1 // Any other read operation.
    36  	opInvalid   readOp = 0  // Non-read operation.
    37  	opReadRune1 readOp = 1  // Read rune of size 1.
    38  	opReadRune2 readOp = 2  // Read rune of size 2.
    39  	opReadRune3 readOp = 3  // Read rune of size 3.
    40  	opReadRune4 readOp = 4  // Read rune of size 4.
    41  )
    42  
    43  // ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
    44  var ErrTooLarge = errors.New("bytes.Buffer: too large")
    45  var errNegativeRead = errors.New("bytes.Buffer: reader returned negative count from Read")
    46  
    47  const maxInt = int(^uint(0) >> 1)
    48  
    49  // Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
    50  // The slice is valid for use only until the next buffer modification (that is,
    51  // only until the next call to a method like Read, Write, Reset, or Truncate).
    52  // The slice aliases the buffer content at least until the next buffer modification,
    53  // so immediate changes to the slice will affect the result of future reads.
    54  func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
    55  
    56  // String returns the contents of the unread portion of the buffer
    57  // as a string. If the Buffer is a nil pointer, it returns "<nil>".
    58  //
    59  // To build strings more efficiently, see the strings.Builder type.
    60  func (b *Buffer) String() string {
    61  	if b == nil {
    62  		// Special case, useful in debugging.
    63  		return "<nil>"
    64  	}
    65  	return string(b.buf[b.off:])
    66  }
    67  
    68  // empty reports whether the unread portion of the buffer is empty.
    69  func (b *Buffer) empty() bool { return len(b.buf) <= b.off }
    70  
    71  // Len returns the number of bytes of the unread portion of the buffer;
    72  // b.Len() == len(b.Bytes()).
    73  func (b *Buffer) Len() int { return len(b.buf) - b.off }
    74  
    75  // Cap returns the capacity of the buffer's underlying byte slice, that is, the
    76  // total space allocated for the buffer's data.
    77  func (b *Buffer) Cap() int { return cap(b.buf) }
    78  
    79  // Truncate discards all but the first n unread bytes from the buffer
    80  // but continues to use the same allocated storage.
    81  // It panics if n is negative or greater than the length of the buffer.
    82  func (b *Buffer) Truncate(n int) {
    83  	if n == 0 {
    84  		b.Reset()
    85  		return
    86  	}
    87  	b.lastRead = opInvalid
    88  	if n < 0 || n > b.Len() {
    89  		panic("bytes.Buffer: truncation out of range")
    90  	}
    91  	b.buf = b.buf[:b.off+n]
    92  }
    93  
    94  // Reset resets the buffer to be empty,
    95  // but it retains the underlying storage for use by future writes.
    96  // Reset is the same as Truncate(0).
    97  func (b *Buffer) Reset() {
    98  	b.buf = b.buf[:0]
    99  	b.off = 0
   100  	b.lastRead = opInvalid
   101  }
   102  
   103  // tryGrowByReslice is a inlineable version of grow for the fast-case where the
   104  // internal buffer only needs to be resliced.
   105  // It returns the index where bytes should be written and whether it succeeded.
   106  func (b *Buffer) tryGrowByReslice(n int) (int, bool) {
   107  	if l := len(b.buf); n <= cap(b.buf)-l {
   108  		b.buf = b.buf[:l+n]
   109  		return l, true
   110  	}
   111  	return 0, false
   112  }
   113  
   114  // grow grows the buffer to guarantee space for n more bytes.
   115  // It returns the index where bytes should be written.
   116  // If the buffer can't grow it will panic with ErrTooLarge.
   117  func (b *Buffer) grow(n int) int {
   118  	m := b.Len()
   119  	// If buffer is empty, reset to recover space.
   120  	if m == 0 && b.off != 0 {
   121  		b.Reset()
   122  	}
   123  	// Try to grow by means of a reslice.
   124  	if i, ok := b.tryGrowByReslice(n); ok {
   125  		return i
   126  	}
   127  	if b.buf == nil && n <= smallBufferSize {
   128  		b.buf = make([]byte, n, smallBufferSize)
   129  		return 0
   130  	}
   131  	c := cap(b.buf)
   132  	if n <= c/2-m {
   133  		// We can slide things down instead of allocating a new
   134  		// slice. We only need m+n <= c to slide, but
   135  		// we instead let capacity get twice as large so we
   136  		// don't spend all our time copying.
   137  		copy(b.buf, b.buf[b.off:])
   138  	} else if c > maxInt-c-n {
   139  		panic(ErrTooLarge)
   140  	} else {
   141  		// Add b.off to account for b.buf[:b.off] being sliced off the front.
   142  		b.buf = growSlice(b.buf[b.off:], b.off+n)
   143  	}
   144  	// Restore b.off and len(b.buf).
   145  	b.off = 0
   146  	b.buf = b.buf[:m+n]
   147  	return m
   148  }
   149  
   150  // Grow grows the buffer's capacity, if necessary, to guarantee space for
   151  // another n bytes. After Grow(n), at least n bytes can be written to the
   152  // buffer without another allocation.
   153  // If n is negative, Grow will panic.
   154  // If the buffer can't grow it will panic with ErrTooLarge.
   155  func (b *Buffer) Grow(n int) {
   156  	if n < 0 {
   157  		panic("bytes.Buffer.Grow: negative count")
   158  	}
   159  	m := b.grow(n)
   160  	b.buf = b.buf[:m]
   161  }
   162  
   163  // Write appends the contents of p to the buffer, growing the buffer as
   164  // needed. The return value n is the length of p; err is always nil. If the
   165  // buffer becomes too large, Write will panic with ErrTooLarge.
   166  func (b *Buffer) Write(p []byte) (n int, err error) {
   167  	b.lastRead = opInvalid
   168  	m, ok := b.tryGrowByReslice(len(p))
   169  	if !ok {
   170  		m = b.grow(len(p))
   171  	}
   172  	return copy(b.buf[m:], p), nil
   173  }
   174  
   175  // WriteString appends the contents of s to the buffer, growing the buffer as
   176  // needed. The return value n is the length of s; err is always nil. If the
   177  // buffer becomes too large, WriteString will panic with ErrTooLarge.
   178  func (b *Buffer) WriteString(s string) (n int, err error) {
   179  	b.lastRead = opInvalid
   180  	m, ok := b.tryGrowByReslice(len(s))
   181  	if !ok {
   182  		m = b.grow(len(s))
   183  	}
   184  	return copy(b.buf[m:], s), nil
   185  }
   186  
   187  // MinRead is the minimum slice size passed to a Read call by
   188  // Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
   189  // what is required to hold the contents of r, ReadFrom will not grow the
   190  // underlying buffer.
   191  const MinRead = 512
   192  
   193  // ReadFrom reads data from r until EOF and appends it to the buffer, growing
   194  // the buffer as needed. The return value n is the number of bytes read. Any
   195  // error except io.EOF encountered during the read is also returned. If the
   196  // buffer becomes too large, ReadFrom will panic with ErrTooLarge.
   197  func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
   198  	b.lastRead = opInvalid
   199  	for {
   200  		i := b.grow(MinRead)
   201  		b.buf = b.buf[:i]
   202  		m, e := r.Read(b.buf[i:cap(b.buf)])
   203  		if m < 0 {
   204  			panic(errNegativeRead)
   205  		}
   206  
   207  		b.buf = b.buf[:i+m]
   208  		n += int64(m)
   209  		if e == io.EOF {
   210  			return n, nil // e is EOF, so return nil explicitly
   211  		}
   212  		if e != nil {
   213  			return n, e
   214  		}
   215  	}
   216  }
   217  
   218  // growSlice grows b by n, preserving the original content of b.
   219  // If the allocation fails, it panics with ErrTooLarge.
   220  func growSlice(b []byte, n int) []byte {
   221  	defer func() {
   222  		if recover() != nil {
   223  			panic(ErrTooLarge)
   224  		}
   225  	}()
   226  	// TODO(http://golang.org/issue/51462): We should rely on the append-make
   227  	// pattern so that the compiler can call runtime.growslice. For example:
   228  	//	return append(b, make([]byte, n)...)
   229  	// This avoids unnecessary zero-ing of the first len(b) bytes of the
   230  	// allocated slice, but this pattern causes b to escape onto the heap.
   231  	//
   232  	// Instead use the append-make pattern with a nil slice to ensure that
   233  	// we allocate buffers rounded up to the closest size class.
   234  	c := len(b) + n // ensure enough space for n elements
   235  	if c < 2*cap(b) {
   236  		// The growth rate has historically always been 2x. In the future,
   237  		// we could rely purely on append to determine the growth rate.
   238  		c = 2 * cap(b)
   239  	}
   240  	b2 := append([]byte(nil), make([]byte, c)...)
   241  	copy(b2, b)
   242  	return b2[:len(b)]
   243  }
   244  
   245  // WriteTo writes data to w until the buffer is drained or an error occurs.
   246  // The return value n is the number of bytes written; it always fits into an
   247  // int, but it is int64 to match the io.WriterTo interface. Any error
   248  // encountered during the write is also returned.
   249  func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
   250  	b.lastRead = opInvalid
   251  	if nBytes := b.Len(); nBytes > 0 {
   252  		m, e := w.Write(b.buf[b.off:])
   253  		if m > nBytes {
   254  			panic("bytes.Buffer.WriteTo: invalid Write count")
   255  		}
   256  		b.off += m
   257  		n = int64(m)
   258  		if e != nil {
   259  			return n, e
   260  		}
   261  		// all bytes should have been written, by definition of
   262  		// Write method in io.Writer
   263  		if m != nBytes {
   264  			return n, io.ErrShortWrite
   265  		}
   266  	}
   267  	// Buffer is now empty; reset.
   268  	b.Reset()
   269  	return n, nil
   270  }
   271  
   272  // WriteByte appends the byte c to the buffer, growing the buffer as needed.
   273  // The returned error is always nil, but is included to match bufio.Writer's
   274  // WriteByte. If the buffer becomes too large, WriteByte will panic with
   275  // ErrTooLarge.
   276  func (b *Buffer) WriteByte(c byte) error {
   277  	b.lastRead = opInvalid
   278  	m, ok := b.tryGrowByReslice(1)
   279  	if !ok {
   280  		m = b.grow(1)
   281  	}
   282  	b.buf[m] = c
   283  	return nil
   284  }
   285  
   286  // WriteRune appends the UTF-8 encoding of Unicode code point r to the
   287  // buffer, returning its length and an error, which is always nil but is
   288  // included to match bufio.Writer's WriteRune. The buffer is grown as needed;
   289  // if it becomes too large, WriteRune will panic with ErrTooLarge.
   290  func (b *Buffer) WriteRune(r rune) (n int, err error) {
   291  	// Compare as uint32 to correctly handle negative runes.
   292  	if uint32(r) < utf8.RuneSelf {
   293  		b.WriteByte(byte(r))
   294  		return 1, nil
   295  	}
   296  	b.lastRead = opInvalid
   297  	m, ok := b.tryGrowByReslice(utf8.UTFMax)
   298  	if !ok {
   299  		m = b.grow(utf8.UTFMax)
   300  	}
   301  	b.buf = utf8.AppendRune(b.buf[:m], r)
   302  	return len(b.buf) - m, nil
   303  }
   304  
   305  // Read reads the next len(p) bytes from the buffer or until the buffer
   306  // is drained. The return value n is the number of bytes read. If the
   307  // buffer has no data to return, err is io.EOF (unless len(p) is zero);
   308  // otherwise it is nil.
   309  func (b *Buffer) Read(p []byte) (n int, err error) {
   310  	b.lastRead = opInvalid
   311  	if b.empty() {
   312  		// Buffer is empty, reset to recover space.
   313  		b.Reset()
   314  		if len(p) == 0 {
   315  			return 0, nil
   316  		}
   317  		return 0, io.EOF
   318  	}
   319  	n = copy(p, b.buf[b.off:])
   320  	b.off += n
   321  	if n > 0 {
   322  		b.lastRead = opRead
   323  	}
   324  	return n, nil
   325  }
   326  
   327  // Next returns a slice containing the next n bytes from the buffer,
   328  // advancing the buffer as if the bytes had been returned by Read.
   329  // If there are fewer than n bytes in the buffer, Next returns the entire buffer.
   330  // The slice is only valid until the next call to a read or write method.
   331  func (b *Buffer) Next(n int) []byte {
   332  	b.lastRead = opInvalid
   333  	m := b.Len()
   334  	if n > m {
   335  		n = m
   336  	}
   337  	data := b.buf[b.off : b.off+n]
   338  	b.off += n
   339  	if n > 0 {
   340  		b.lastRead = opRead
   341  	}
   342  	return data
   343  }
   344  
   345  // ReadByte reads and returns the next byte from the buffer.
   346  // If no byte is available, it returns error io.EOF.
   347  func (b *Buffer) ReadByte() (byte, error) {
   348  	if b.empty() {
   349  		// Buffer is empty, reset to recover space.
   350  		b.Reset()
   351  		return 0, io.EOF
   352  	}
   353  	c := b.buf[b.off]
   354  	b.off++
   355  	b.lastRead = opRead
   356  	return c, nil
   357  }
   358  
   359  // ReadRune reads and returns the next UTF-8-encoded
   360  // Unicode code point from the buffer.
   361  // If no bytes are available, the error returned is io.EOF.
   362  // If the bytes are an erroneous UTF-8 encoding, it
   363  // consumes one byte and returns U+FFFD, 1.
   364  func (b *Buffer) ReadRune() (r rune, size int, err error) {
   365  	if b.empty() {
   366  		// Buffer is empty, reset to recover space.
   367  		b.Reset()
   368  		return 0, 0, io.EOF
   369  	}
   370  	c := b.buf[b.off]
   371  	if c < utf8.RuneSelf {
   372  		b.off++
   373  		b.lastRead = opReadRune1
   374  		return rune(c), 1, nil
   375  	}
   376  	r, n := utf8.DecodeRune(b.buf[b.off:])
   377  	b.off += n
   378  	b.lastRead = readOp(n)
   379  	return r, n, nil
   380  }
   381  
   382  // UnreadRune unreads the last rune returned by ReadRune.
   383  // If the most recent read or write operation on the buffer was
   384  // not a successful ReadRune, UnreadRune returns an error.  (In this regard
   385  // it is stricter than UnreadByte, which will unread the last byte
   386  // from any read operation.)
   387  func (b *Buffer) UnreadRune() error {
   388  	if b.lastRead <= opInvalid {
   389  		return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
   390  	}
   391  	if b.off >= int(b.lastRead) {
   392  		b.off -= int(b.lastRead)
   393  	}
   394  	b.lastRead = opInvalid
   395  	return nil
   396  }
   397  
   398  var errUnreadByte = errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read")
   399  
   400  // UnreadByte unreads the last byte returned by the most recent successful
   401  // read operation that read at least one byte. If a write has happened since
   402  // the last read, if the last read returned an error, or if the read read zero
   403  // bytes, UnreadByte returns an error.
   404  func (b *Buffer) UnreadByte() error {
   405  	if b.lastRead == opInvalid {
   406  		return errUnreadByte
   407  	}
   408  	b.lastRead = opInvalid
   409  	if b.off > 0 {
   410  		b.off--
   411  	}
   412  	return nil
   413  }
   414  
   415  // ReadBytes reads until the first occurrence of delim in the input,
   416  // returning a slice containing the data up to and including the delimiter.
   417  // If ReadBytes encounters an error before finding a delimiter,
   418  // it returns the data read before the error and the error itself (often io.EOF).
   419  // ReadBytes returns err != nil if and only if the returned data does not end in
   420  // delim.
   421  func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
   422  	slice, err := b.readSlice(delim)
   423  	// return a copy of slice. The buffer's backing array may
   424  	// be overwritten by later calls.
   425  	line = append(line, slice...)
   426  	return line, err
   427  }
   428  
   429  // readSlice is like ReadBytes but returns a reference to internal buffer data.
   430  func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
   431  	i := IndexByte(b.buf[b.off:], delim)
   432  	end := b.off + i + 1
   433  	if i < 0 {
   434  		end = len(b.buf)
   435  		err = io.EOF
   436  	}
   437  	line = b.buf[b.off:end]
   438  	b.off = end
   439  	b.lastRead = opRead
   440  	return line, err
   441  }
   442  
   443  // ReadString reads until the first occurrence of delim in the input,
   444  // returning a string containing the data up to and including the delimiter.
   445  // If ReadString encounters an error before finding a delimiter,
   446  // it returns the data read before the error and the error itself (often io.EOF).
   447  // ReadString returns err != nil if and only if the returned data does not end
   448  // in delim.
   449  func (b *Buffer) ReadString(delim byte) (line string, err error) {
   450  	slice, err := b.readSlice(delim)
   451  	return string(slice), err
   452  }
   453  
   454  // NewBuffer creates and initializes a new Buffer using buf as its
   455  // initial contents. The new Buffer takes ownership of buf, and the
   456  // caller should not use buf after this call. NewBuffer is intended to
   457  // prepare a Buffer to read existing data. It can also be used to set
   458  // the initial size of the internal buffer for writing. To do that,
   459  // buf should have the desired capacity but a length of zero.
   460  //
   461  // In most cases, new(Buffer) (or just declaring a Buffer variable) is
   462  // sufficient to initialize a Buffer.
   463  func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
   464  
   465  // NewBufferString creates and initializes a new Buffer using string s as its
   466  // initial contents. It is intended to prepare a buffer to read an existing
   467  // string.
   468  //
   469  // In most cases, new(Buffer) (or just declaring a Buffer variable) is
   470  // sufficient to initialize a Buffer.
   471  func NewBufferString(s string) *Buffer {
   472  	return &Buffer{buf: []byte(s)}
   473  }
   474  

View as plain text