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

View as plain text