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