github.com/mdempsky/go@v0.0.0-20151201204031-5dd372bd1e70/src/bytes/buffer.go (about)

     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  // A Buffer is a variable-sized buffer of bytes with Read and Write methods.
    16  // The zero value for Buffer is an empty buffer ready to use.
    17  type Buffer struct {
    18  	buf       []byte            // contents are the bytes buf[off : len(buf)]
    19  	off       int               // read at &buf[off], write at &buf[len(buf)]
    20  	runeBytes [utf8.UTFMax]byte // avoid allocation of slice on each WriteByte or Rune
    21  	bootstrap [64]byte          // memory to hold first slice; helps small buffers (Printf) avoid allocation.
    22  	lastRead  readOp            // last read operation, so that Unread* can work correctly.
    23  }
    24  
    25  // The readOp constants describe the last action performed on
    26  // the buffer, so that UnreadRune and UnreadByte can
    27  // check for invalid usage.
    28  type readOp int
    29  
    30  const (
    31  	opInvalid  readOp = iota // Non-read operation.
    32  	opReadRune               // Read rune.
    33  	opRead                   // Any other read operation.
    34  )
    35  
    36  // ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
    37  var ErrTooLarge = errors.New("bytes.Buffer: too large")
    38  
    39  // Bytes returns a slice of the contents of the unread portion of the buffer;
    40  // len(b.Bytes()) == b.Len().  If the caller changes the contents of the
    41  // returned slice, the contents of the buffer will change provided there
    42  // are no intervening method calls on the Buffer.
    43  func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }
    44  
    45  // String returns the contents of the unread portion of the buffer
    46  // as a string.  If the Buffer is a nil pointer, it returns "<nil>".
    47  func (b *Buffer) String() string {
    48  	if b == nil {
    49  		// Special case, useful in debugging.
    50  		return "<nil>"
    51  	}
    52  	return string(b.buf[b.off:])
    53  }
    54  
    55  // Len returns the number of bytes of the unread portion of the buffer;
    56  // b.Len() == len(b.Bytes()).
    57  func (b *Buffer) Len() int { return len(b.buf) - b.off }
    58  
    59  // Cap returns the capacity of the buffer's underlying byte slice, that is, the
    60  // total space allocated for the buffer's data.
    61  func (b *Buffer) Cap() int { return cap(b.buf) }
    62  
    63  // Truncate discards all but the first n unread bytes from the buffer.
    64  // It panics if n is negative or greater than the length of the buffer.
    65  func (b *Buffer) Truncate(n int) {
    66  	b.lastRead = opInvalid
    67  	switch {
    68  	case n < 0 || n > b.Len():
    69  		panic("bytes.Buffer: truncation out of range")
    70  	case n == 0:
    71  		// Reuse buffer space.
    72  		b.off = 0
    73  	}
    74  	b.buf = b.buf[0 : b.off+n]
    75  }
    76  
    77  // Reset resets the buffer so it has no content.
    78  // b.Reset() is the same as b.Truncate(0).
    79  func (b *Buffer) Reset() { b.Truncate(0) }
    80  
    81  // grow grows the buffer to guarantee space for n more bytes.
    82  // It returns the index where bytes should be written.
    83  // If the buffer can't grow it will panic with ErrTooLarge.
    84  func (b *Buffer) grow(n int) int {
    85  	m := b.Len()
    86  	// If buffer is empty, reset to recover space.
    87  	if m == 0 && b.off != 0 {
    88  		b.Truncate(0)
    89  	}
    90  	if len(b.buf)+n > cap(b.buf) {
    91  		var buf []byte
    92  		if b.buf == nil && n <= len(b.bootstrap) {
    93  			buf = b.bootstrap[0:]
    94  		} else if m+n <= cap(b.buf)/2 {
    95  			// We can slide things down instead of allocating a new
    96  			// slice. We only need m+n <= cap(b.buf) to slide, but
    97  			// we instead let capacity get twice as large so we
    98  			// don't spend all our time copying.
    99  			copy(b.buf[:], b.buf[b.off:])
   100  			buf = b.buf[:m]
   101  		} else {
   102  			// not enough space anywhere
   103  			buf = makeSlice(2*cap(b.buf) + n)
   104  			copy(buf, b.buf[b.off:])
   105  		}
   106  		b.buf = buf
   107  		b.off = 0
   108  	}
   109  	b.buf = b.buf[0 : b.off+m+n]
   110  	return b.off + m
   111  }
   112  
   113  // Grow grows the buffer's capacity, if necessary, to guarantee space for
   114  // another n bytes. After Grow(n), at least n bytes can be written to the
   115  // buffer without another allocation.
   116  // If n is negative, Grow will panic.
   117  // If the buffer can't grow it will panic with ErrTooLarge.
   118  func (b *Buffer) Grow(n int) {
   119  	if n < 0 {
   120  		panic("bytes.Buffer.Grow: negative count")
   121  	}
   122  	m := b.grow(n)
   123  	b.buf = b.buf[0:m]
   124  }
   125  
   126  // Write appends the contents of p to the buffer, growing the buffer as
   127  // needed. The return value n is the length of p; err is always nil. If the
   128  // buffer becomes too large, Write will panic with ErrTooLarge.
   129  func (b *Buffer) Write(p []byte) (n int, err error) {
   130  	b.lastRead = opInvalid
   131  	m := b.grow(len(p))
   132  	return copy(b.buf[m:], p), nil
   133  }
   134  
   135  // WriteString appends the contents of s to the buffer, growing the buffer as
   136  // needed. The return value n is the length of s; err is always nil. If the
   137  // buffer becomes too large, WriteString will panic with ErrTooLarge.
   138  func (b *Buffer) WriteString(s string) (n int, err error) {
   139  	b.lastRead = opInvalid
   140  	m := b.grow(len(s))
   141  	return copy(b.buf[m:], s), nil
   142  }
   143  
   144  // MinRead is the minimum slice size passed to a Read call by
   145  // Buffer.ReadFrom.  As long as the Buffer has at least MinRead bytes beyond
   146  // what is required to hold the contents of r, ReadFrom will not grow the
   147  // underlying buffer.
   148  const MinRead = 512
   149  
   150  // ReadFrom reads data from r until EOF and appends it to the buffer, growing
   151  // the buffer as needed. The return value n is the number of bytes read. Any
   152  // error except io.EOF encountered during the read is also returned. If the
   153  // buffer becomes too large, ReadFrom will panic with ErrTooLarge.
   154  func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
   155  	b.lastRead = opInvalid
   156  	// If buffer is empty, reset to recover space.
   157  	if b.off >= len(b.buf) {
   158  		b.Truncate(0)
   159  	}
   160  	for {
   161  		if free := cap(b.buf) - len(b.buf); free < MinRead {
   162  			// not enough space at end
   163  			newBuf := b.buf
   164  			if b.off+free < MinRead {
   165  				// not enough space using beginning of buffer;
   166  				// double buffer capacity
   167  				newBuf = makeSlice(2*cap(b.buf) + MinRead)
   168  			}
   169  			copy(newBuf, b.buf[b.off:])
   170  			b.buf = newBuf[:len(b.buf)-b.off]
   171  			b.off = 0
   172  		}
   173  		m, e := r.Read(b.buf[len(b.buf):cap(b.buf)])
   174  		b.buf = b.buf[0 : len(b.buf)+m]
   175  		n += int64(m)
   176  		if e == io.EOF {
   177  			break
   178  		}
   179  		if e != nil {
   180  			return n, e
   181  		}
   182  	}
   183  	return n, nil // err is EOF, so return nil explicitly
   184  }
   185  
   186  // makeSlice allocates a slice of size n. If the allocation fails, it panics
   187  // with ErrTooLarge.
   188  func makeSlice(n int) []byte {
   189  	// If the make fails, give a known error.
   190  	defer func() {
   191  		if recover() != nil {
   192  			panic(ErrTooLarge)
   193  		}
   194  	}()
   195  	return make([]byte, n)
   196  }
   197  
   198  // WriteTo writes data to w until the buffer is drained or an error occurs.
   199  // The return value n is the number of bytes written; it always fits into an
   200  // int, but it is int64 to match the io.WriterTo interface. Any error
   201  // encountered during the write is also returned.
   202  func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
   203  	b.lastRead = opInvalid
   204  	if b.off < len(b.buf) {
   205  		nBytes := b.Len()
   206  		m, e := w.Write(b.buf[b.off:])
   207  		if m > nBytes {
   208  			panic("bytes.Buffer.WriteTo: invalid Write count")
   209  		}
   210  		b.off += m
   211  		n = int64(m)
   212  		if e != nil {
   213  			return n, e
   214  		}
   215  		// all bytes should have been written, by definition of
   216  		// Write method in io.Writer
   217  		if m != nBytes {
   218  			return n, io.ErrShortWrite
   219  		}
   220  	}
   221  	// Buffer is now empty; reset.
   222  	b.Truncate(0)
   223  	return
   224  }
   225  
   226  // WriteByte appends the byte c to the buffer, growing the buffer as needed.
   227  // The returned error is always nil, but is included to match bufio.Writer's
   228  // WriteByte. If the buffer becomes too large, WriteByte will panic with
   229  // ErrTooLarge.
   230  func (b *Buffer) WriteByte(c byte) error {
   231  	b.lastRead = opInvalid
   232  	m := b.grow(1)
   233  	b.buf[m] = c
   234  	return nil
   235  }
   236  
   237  // WriteRune appends the UTF-8 encoding of Unicode code point r to the
   238  // buffer, returning its length and an error, which is always nil but is
   239  // included to match bufio.Writer's WriteRune. The buffer is grown as needed;
   240  // if it becomes too large, WriteRune will panic with ErrTooLarge.
   241  func (b *Buffer) WriteRune(r rune) (n int, err error) {
   242  	if r < utf8.RuneSelf {
   243  		b.WriteByte(byte(r))
   244  		return 1, nil
   245  	}
   246  	n = utf8.EncodeRune(b.runeBytes[0:], r)
   247  	b.Write(b.runeBytes[0:n])
   248  	return n, nil
   249  }
   250  
   251  // Read reads the next len(p) bytes from the buffer or until the buffer
   252  // is drained.  The return value n is the number of bytes read.  If the
   253  // buffer has no data to return, err is io.EOF (unless len(p) is zero);
   254  // otherwise it is nil.
   255  func (b *Buffer) Read(p []byte) (n int, err error) {
   256  	b.lastRead = opInvalid
   257  	if b.off >= len(b.buf) {
   258  		// Buffer is empty, reset to recover space.
   259  		b.Truncate(0)
   260  		if len(p) == 0 {
   261  			return
   262  		}
   263  		return 0, io.EOF
   264  	}
   265  	n = copy(p, b.buf[b.off:])
   266  	b.off += n
   267  	if n > 0 {
   268  		b.lastRead = opRead
   269  	}
   270  	return
   271  }
   272  
   273  // Next returns a slice containing the next n bytes from the buffer,
   274  // advancing the buffer as if the bytes had been returned by Read.
   275  // If there are fewer than n bytes in the buffer, Next returns the entire buffer.
   276  // The slice is only valid until the next call to a read or write method.
   277  func (b *Buffer) Next(n int) []byte {
   278  	b.lastRead = opInvalid
   279  	m := b.Len()
   280  	if n > m {
   281  		n = m
   282  	}
   283  	data := b.buf[b.off : b.off+n]
   284  	b.off += n
   285  	if n > 0 {
   286  		b.lastRead = opRead
   287  	}
   288  	return data
   289  }
   290  
   291  // ReadByte reads and returns the next byte from the buffer.
   292  // If no byte is available, it returns error io.EOF.
   293  func (b *Buffer) ReadByte() (c byte, err error) {
   294  	b.lastRead = opInvalid
   295  	if b.off >= len(b.buf) {
   296  		// Buffer is empty, reset to recover space.
   297  		b.Truncate(0)
   298  		return 0, io.EOF
   299  	}
   300  	c = b.buf[b.off]
   301  	b.off++
   302  	b.lastRead = opRead
   303  	return c, nil
   304  }
   305  
   306  // ReadRune reads and returns the next UTF-8-encoded
   307  // Unicode code point from the buffer.
   308  // If no bytes are available, the error returned is io.EOF.
   309  // If the bytes are an erroneous UTF-8 encoding, it
   310  // consumes one byte and returns U+FFFD, 1.
   311  func (b *Buffer) ReadRune() (r rune, size int, err error) {
   312  	b.lastRead = opInvalid
   313  	if b.off >= len(b.buf) {
   314  		// Buffer is empty, reset to recover space.
   315  		b.Truncate(0)
   316  		return 0, 0, io.EOF
   317  	}
   318  	b.lastRead = opReadRune
   319  	c := b.buf[b.off]
   320  	if c < utf8.RuneSelf {
   321  		b.off++
   322  		return rune(c), 1, nil
   323  	}
   324  	r, n := utf8.DecodeRune(b.buf[b.off:])
   325  	b.off += n
   326  	return r, n, nil
   327  }
   328  
   329  // UnreadRune unreads the last rune returned by ReadRune.
   330  // If the most recent read or write operation on the buffer was
   331  // not a ReadRune, UnreadRune returns an error.  (In this regard
   332  // it is stricter than UnreadByte, which will unread the last byte
   333  // from any read operation.)
   334  func (b *Buffer) UnreadRune() error {
   335  	if b.lastRead != opReadRune {
   336  		return errors.New("bytes.Buffer: UnreadRune: previous operation was not ReadRune")
   337  	}
   338  	b.lastRead = opInvalid
   339  	if b.off > 0 {
   340  		_, n := utf8.DecodeLastRune(b.buf[0:b.off])
   341  		b.off -= n
   342  	}
   343  	return nil
   344  }
   345  
   346  // UnreadByte unreads the last byte returned by the most recent
   347  // read operation.  If write has happened since the last read, UnreadByte
   348  // returns an error.
   349  func (b *Buffer) UnreadByte() error {
   350  	if b.lastRead != opReadRune && b.lastRead != opRead {
   351  		return errors.New("bytes.Buffer: UnreadByte: previous operation was not a read")
   352  	}
   353  	b.lastRead = opInvalid
   354  	if b.off > 0 {
   355  		b.off--
   356  	}
   357  	return nil
   358  }
   359  
   360  // ReadBytes reads until the first occurrence of delim in the input,
   361  // returning a slice containing the data up to and including the delimiter.
   362  // If ReadBytes encounters an error before finding a delimiter,
   363  // it returns the data read before the error and the error itself (often io.EOF).
   364  // ReadBytes returns err != nil if and only if the returned data does not end in
   365  // delim.
   366  func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
   367  	slice, err := b.readSlice(delim)
   368  	// return a copy of slice. The buffer's backing array may
   369  	// be overwritten by later calls.
   370  	line = append(line, slice...)
   371  	return
   372  }
   373  
   374  // readSlice is like ReadBytes but returns a reference to internal buffer data.
   375  func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
   376  	i := IndexByte(b.buf[b.off:], delim)
   377  	end := b.off + i + 1
   378  	if i < 0 {
   379  		end = len(b.buf)
   380  		err = io.EOF
   381  	}
   382  	line = b.buf[b.off:end]
   383  	b.off = end
   384  	b.lastRead = opRead
   385  	return line, err
   386  }
   387  
   388  // ReadString reads until the first occurrence of delim in the input,
   389  // returning a string containing the data up to and including the delimiter.
   390  // If ReadString encounters an error before finding a delimiter,
   391  // it returns the data read before the error and the error itself (often io.EOF).
   392  // ReadString returns err != nil if and only if the returned data does not end
   393  // in delim.
   394  func (b *Buffer) ReadString(delim byte) (line string, err error) {
   395  	slice, err := b.readSlice(delim)
   396  	return string(slice), err
   397  }
   398  
   399  // NewBuffer creates and initializes a new Buffer using buf as its initial
   400  // contents.  It is intended to prepare a Buffer to read existing data.  It
   401  // can also be used to size the internal buffer for writing. To do that,
   402  // buf should have the desired capacity but a length of zero.
   403  //
   404  // In most cases, new(Buffer) (or just declaring a Buffer variable) is
   405  // sufficient to initialize a Buffer.
   406  func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
   407  
   408  // NewBufferString creates and initializes a new Buffer using string s as its
   409  // initial contents. It is intended to prepare a buffer to read an existing
   410  // string.
   411  //
   412  // In most cases, new(Buffer) (or just declaring a Buffer variable) is
   413  // sufficient to initialize a Buffer.
   414  func NewBufferString(s string) *Buffer {
   415  	return &Buffer{buf: []byte(s)}
   416  }