...

Source file src/pkg/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			// Not enough space anywhere, we need to allocate.
   142			buf := makeSlice(2*c + n)
   143			copy(buf, b.buf[b.off:])
   144			b.buf = buf
   145		}
   146		// Restore b.off and len(b.buf).
   147		b.off = 0
   148		b.buf = b.buf[:m+n]
   149		return m
   150	}
   151	
   152	// Grow grows the buffer's capacity, if necessary, to guarantee space for
   153	// another n bytes. After Grow(n), at least n bytes can be written to the
   154	// buffer without another allocation.
   155	// If n is negative, Grow will panic.
   156	// If the buffer can't grow it will panic with ErrTooLarge.
   157	func (b *Buffer) Grow(n int) {
   158		if n < 0 {
   159			panic("bytes.Buffer.Grow: negative count")
   160		}
   161		m := b.grow(n)
   162		b.buf = b.buf[:m]
   163	}
   164	
   165	// Write appends the contents of p to the buffer, growing the buffer as
   166	// needed. The return value n is the length of p; err is always nil. If the
   167	// buffer becomes too large, Write will panic with ErrTooLarge.
   168	func (b *Buffer) Write(p []byte) (n int, err error) {
   169		b.lastRead = opInvalid
   170		m, ok := b.tryGrowByReslice(len(p))
   171		if !ok {
   172			m = b.grow(len(p))
   173		}
   174		return copy(b.buf[m:], p), nil
   175	}
   176	
   177	// WriteString appends the contents of s to the buffer, growing the buffer as
   178	// needed. The return value n is the length of s; err is always nil. If the
   179	// buffer becomes too large, WriteString will panic with ErrTooLarge.
   180	func (b *Buffer) WriteString(s string) (n int, err error) {
   181		b.lastRead = opInvalid
   182		m, ok := b.tryGrowByReslice(len(s))
   183		if !ok {
   184			m = b.grow(len(s))
   185		}
   186		return copy(b.buf[m:], s), nil
   187	}
   188	
   189	// MinRead is the minimum slice size passed to a Read call by
   190	// Buffer.ReadFrom. As long as the Buffer has at least MinRead bytes beyond
   191	// what is required to hold the contents of r, ReadFrom will not grow the
   192	// underlying buffer.
   193	const MinRead = 512
   194	
   195	// ReadFrom reads data from r until EOF and appends it to the buffer, growing
   196	// the buffer as needed. The return value n is the number of bytes read. Any
   197	// error except io.EOF encountered during the read is also returned. If the
   198	// buffer becomes too large, ReadFrom will panic with ErrTooLarge.
   199	func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
   200		b.lastRead = opInvalid
   201		for {
   202			i := b.grow(MinRead)
   203			b.buf = b.buf[:i]
   204			m, e := r.Read(b.buf[i:cap(b.buf)])
   205			if m < 0 {
   206				panic(errNegativeRead)
   207			}
   208	
   209			b.buf = b.buf[:i+m]
   210			n += int64(m)
   211			if e == io.EOF {
   212				return n, nil // e is EOF, so return nil explicitly
   213			}
   214			if e != nil {
   215				return n, e
   216			}
   217		}
   218	}
   219	
   220	// makeSlice allocates a slice of size n. If the allocation fails, it panics
   221	// with ErrTooLarge.
   222	func makeSlice(n int) []byte {
   223		// If the make fails, give a known error.
   224		defer func() {
   225			if recover() != nil {
   226				panic(ErrTooLarge)
   227			}
   228		}()
   229		return make([]byte, n)
   230	}
   231	
   232	// WriteTo writes data to w until the buffer is drained or an error occurs.
   233	// The return value n is the number of bytes written; it always fits into an
   234	// int, but it is int64 to match the io.WriterTo interface. Any error
   235	// encountered during the write is also returned.
   236	func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
   237		b.lastRead = opInvalid
   238		if nBytes := b.Len(); nBytes > 0 {
   239			m, e := w.Write(b.buf[b.off:])
   240			if m > nBytes {
   241				panic("bytes.Buffer.WriteTo: invalid Write count")
   242			}
   243			b.off += m
   244			n = int64(m)
   245			if e != nil {
   246				return n, e
   247			}
   248			// all bytes should have been written, by definition of
   249			// Write method in io.Writer
   250			if m != nBytes {
   251				return n, io.ErrShortWrite
   252			}
   253		}
   254		// Buffer is now empty; reset.
   255		b.Reset()
   256		return n, nil
   257	}
   258	
   259	// WriteByte appends the byte c to the buffer, growing the buffer as needed.
   260	// The returned error is always nil, but is included to match bufio.Writer's
   261	// WriteByte. If the buffer becomes too large, WriteByte will panic with
   262	// ErrTooLarge.
   263	func (b *Buffer) WriteByte(c byte) error {
   264		b.lastRead = opInvalid
   265		m, ok := b.tryGrowByReslice(1)
   266		if !ok {
   267			m = b.grow(1)
   268		}
   269		b.buf[m] = c
   270		return nil
   271	}
   272	
   273	// WriteRune appends the UTF-8 encoding of Unicode code point r to the
   274	// buffer, returning its length and an error, which is always nil but is
   275	// included to match bufio.Writer's WriteRune. The buffer is grown as needed;
   276	// if it becomes too large, WriteRune will panic with ErrTooLarge.
   277	func (b *Buffer) WriteRune(r rune) (n int, err error) {
   278		if r < utf8.RuneSelf {
   279			b.WriteByte(byte(r))
   280			return 1, nil
   281		}
   282		b.lastRead = opInvalid
   283		m, ok := b.tryGrowByReslice(utf8.UTFMax)
   284		if !ok {
   285			m = b.grow(utf8.UTFMax)
   286		}
   287		n = utf8.EncodeRune(b.buf[m:m+utf8.UTFMax], r)
   288		b.buf = b.buf[:m+n]
   289		return n, nil
   290	}
   291	
   292	// Read reads the next len(p) bytes from the buffer or until the buffer
   293	// is drained. The return value n is the number of bytes read. If the
   294	// buffer has no data to return, err is io.EOF (unless len(p) is zero);
   295	// otherwise it is nil.
   296	func (b *Buffer) Read(p []byte) (n int, err error) {
   297		b.lastRead = opInvalid
   298		if b.empty() {
   299			// Buffer is empty, reset to recover space.
   300			b.Reset()
   301			if len(p) == 0 {
   302				return 0, nil
   303			}
   304			return 0, io.EOF
   305		}
   306		n = copy(p, b.buf[b.off:])
   307		b.off += n
   308		if n > 0 {
   309			b.lastRead = opRead
   310		}
   311		return n, nil
   312	}
   313	
   314	// Next returns a slice containing the next n bytes from the buffer,
   315	// advancing the buffer as if the bytes had been returned by Read.
   316	// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
   317	// The slice is only valid until the next call to a read or write method.
   318	func (b *Buffer) Next(n int) []byte {
   319		b.lastRead = opInvalid
   320		m := b.Len()
   321		if n > m {
   322			n = m
   323		}
   324		data := b.buf[b.off : b.off+n]
   325		b.off += n
   326		if n > 0 {
   327			b.lastRead = opRead
   328		}
   329		return data
   330	}
   331	
   332	// ReadByte reads and returns the next byte from the buffer.
   333	// If no byte is available, it returns error io.EOF.
   334	func (b *Buffer) ReadByte() (byte, error) {
   335		if b.empty() {
   336			// Buffer is empty, reset to recover space.
   337			b.Reset()
   338			return 0, io.EOF
   339		}
   340		c := b.buf[b.off]
   341		b.off++
   342		b.lastRead = opRead
   343		return c, nil
   344	}
   345	
   346	// ReadRune reads and returns the next UTF-8-encoded
   347	// Unicode code point from the buffer.
   348	// If no bytes are available, the error returned is io.EOF.
   349	// If the bytes are an erroneous UTF-8 encoding, it
   350	// consumes one byte and returns U+FFFD, 1.
   351	func (b *Buffer) ReadRune() (r rune, size int, err error) {
   352		if b.empty() {
   353			// Buffer is empty, reset to recover space.
   354			b.Reset()
   355			return 0, 0, io.EOF
   356		}
   357		c := b.buf[b.off]
   358		if c < utf8.RuneSelf {
   359			b.off++
   360			b.lastRead = opReadRune1
   361			return rune(c), 1, nil
   362		}
   363		r, n := utf8.DecodeRune(b.buf[b.off:])
   364		b.off += n
   365		b.lastRead = readOp(n)
   366		return r, n, nil
   367	}
   368	
   369	// UnreadRune unreads the last rune returned by ReadRune.
   370	// If the most recent read or write operation on the buffer was
   371	// not a successful ReadRune, UnreadRune returns an error.  (In this regard
   372	// it is stricter than UnreadByte, which will unread the last byte
   373	// from any read operation.)
   374	func (b *Buffer) UnreadRune() error {
   375		if b.lastRead <= opInvalid {
   376			return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
   377		}
   378		if b.off >= int(b.lastRead) {
   379			b.off -= int(b.lastRead)
   380		}
   381		b.lastRead = opInvalid
   382		return nil
   383	}
   384	
   385	var errUnreadByte = errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read")
   386	
   387	// UnreadByte unreads the last byte returned by the most recent successful
   388	// read operation that read at least one byte. If a write has happened since
   389	// the last read, if the last read returned an error, or if the read read zero
   390	// bytes, UnreadByte returns an error.
   391	func (b *Buffer) UnreadByte() error {
   392		if b.lastRead == opInvalid {
   393			return errUnreadByte
   394		}
   395		b.lastRead = opInvalid
   396		if b.off > 0 {
   397			b.off--
   398		}
   399		return nil
   400	}
   401	
   402	// ReadBytes reads until the first occurrence of delim in the input,
   403	// returning a slice containing the data up to and including the delimiter.
   404	// If ReadBytes encounters an error before finding a delimiter,
   405	// it returns the data read before the error and the error itself (often io.EOF).
   406	// ReadBytes returns err != nil if and only if the returned data does not end in
   407	// delim.
   408	func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
   409		slice, err := b.readSlice(delim)
   410		// return a copy of slice. The buffer's backing array may
   411		// be overwritten by later calls.
   412		line = append(line, slice...)
   413		return line, err
   414	}
   415	
   416	// readSlice is like ReadBytes but returns a reference to internal buffer data.
   417	func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
   418		i := IndexByte(b.buf[b.off:], delim)
   419		end := b.off + i + 1
   420		if i < 0 {
   421			end = len(b.buf)
   422			err = io.EOF
   423		}
   424		line = b.buf[b.off:end]
   425		b.off = end
   426		b.lastRead = opRead
   427		return line, err
   428	}
   429	
   430	// ReadString reads until the first occurrence of delim in the input,
   431	// returning a string containing the data up to and including the delimiter.
   432	// If ReadString encounters an error before finding a delimiter,
   433	// it returns the data read before the error and the error itself (often io.EOF).
   434	// ReadString returns err != nil if and only if the returned data does not end
   435	// in delim.
   436	func (b *Buffer) ReadString(delim byte) (line string, err error) {
   437		slice, err := b.readSlice(delim)
   438		return string(slice), err
   439	}
   440	
   441	// NewBuffer creates and initializes a new Buffer using buf as its
   442	// initial contents. The new Buffer takes ownership of buf, and the
   443	// caller should not use buf after this call. NewBuffer is intended to
   444	// prepare a Buffer to read existing data. It can also be used to set
   445	// the initial size of the internal buffer for writing. To do that,
   446	// buf should have the desired capacity but a length of zero.
   447	//
   448	// In most cases, new(Buffer) (or just declaring a Buffer variable) is
   449	// sufficient to initialize a Buffer.
   450	func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: buf} }
   451	
   452	// NewBufferString creates and initializes a new Buffer using string s as its
   453	// initial contents. It is intended to prepare a buffer to read an existing
   454	// string.
   455	//
   456	// In most cases, new(Buffer) (or just declaring a Buffer variable) is
   457	// sufficient to initialize a Buffer.
   458	func NewBufferString(s string) *Buffer {
   459		return &Buffer{buf: []byte(s)}
   460	}
   461	

View as plain text