// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package bytes

// Simple byte buffer for marshaling data.

import (
	
	
	
)

// smallBufferSize is an initial allocation minimal capacity.
const smallBufferSize = 64

// A Buffer is a variable-sized buffer of bytes with [Buffer.Read] and [Buffer.Write] methods.
// The zero value for Buffer is an empty buffer ready to use.
type Buffer struct {
	buf      []byte // contents are the bytes buf[off : len(buf)]
	off      int    // read at &buf[off], write at &buf[len(buf)]
	lastRead readOp // last read operation, so that Unread* can work correctly.
}

// The readOp constants describe the last action performed on
// the buffer, so that UnreadRune and UnreadByte can check for
// invalid usage. opReadRuneX constants are chosen such that
// converted to int they correspond to the rune size that was read.
type readOp int8

// Don't use iota for these, as the values need to correspond with the
// names and comments, which is easier to see when being explicit.
const (
	opRead      readOp = -1 // Any other read operation.
	opInvalid   readOp = 0  // Non-read operation.
	opReadRune1 readOp = 1  // Read rune of size 1.
	opReadRune2 readOp = 2  // Read rune of size 2.
	opReadRune3 readOp = 3  // Read rune of size 3.
	opReadRune4 readOp = 4  // Read rune of size 4.
)

// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
var ErrTooLarge = errors.New("bytes.Buffer: too large")
var errNegativeRead = errors.New("bytes.Buffer: reader returned negative count from Read")

const maxInt = int(^uint(0) >> 1)

// Bytes returns a slice of length b.Len() holding the unread portion of the buffer.
// The slice is valid for use only until the next buffer modification (that is,
// only until the next call to a method like [Buffer.Read], [Buffer.Write], [Buffer.Reset], or [Buffer.Truncate]).
// The slice aliases the buffer content at least until the next buffer modification,
// so immediate changes to the slice will affect the result of future reads.
func ( *Buffer) () []byte { return .buf[.off:] }

// AvailableBuffer returns an empty buffer with b.Available() capacity.
// This buffer is intended to be appended to and
// passed to an immediately succeeding [Buffer.Write] call.
// The buffer is only valid until the next write operation on b.
func ( *Buffer) () []byte { return .buf[len(.buf):] }

// String returns the contents of the unread portion of the buffer
// as a string. If the [Buffer] is a nil pointer, it returns "<nil>".
//
// To build strings more efficiently, see the strings.Builder type.
func ( *Buffer) () string {
	if  == nil {
		// Special case, useful in debugging.
		return "<nil>"
	}
	return string(.buf[.off:])
}

// empty reports whether the unread portion of the buffer is empty.
func ( *Buffer) () bool { return len(.buf) <= .off }

// Len returns the number of bytes of the unread portion of the buffer;
// b.Len() == len(b.Bytes()).
func ( *Buffer) () int { return len(.buf) - .off }

// Cap returns the capacity of the buffer's underlying byte slice, that is, the
// total space allocated for the buffer's data.
func ( *Buffer) () int { return cap(.buf) }

// Available returns how many bytes are unused in the buffer.
func ( *Buffer) () int { return cap(.buf) - len(.buf) }

// Truncate discards all but the first n unread bytes from the buffer
// but continues to use the same allocated storage.
// It panics if n is negative or greater than the length of the buffer.
func ( *Buffer) ( int) {
	if  == 0 {
		.Reset()
		return
	}
	.lastRead = opInvalid
	if  < 0 ||  > .Len() {
		panic("bytes.Buffer: truncation out of range")
	}
	.buf = .buf[:.off+]
}

// Reset resets the buffer to be empty,
// but it retains the underlying storage for use by future writes.
// Reset is the same as [Buffer.Truncate](0).
func ( *Buffer) () {
	.buf = .buf[:0]
	.off = 0
	.lastRead = opInvalid
}

// tryGrowByReslice is an inlineable version of grow for the fast-case where the
// internal buffer only needs to be resliced.
// It returns the index where bytes should be written and whether it succeeded.
func ( *Buffer) ( int) (int, bool) {
	if  := len(.buf);  <= cap(.buf)- {
		.buf = .buf[:+]
		return , true
	}
	return 0, false
}

// grow grows the buffer to guarantee space for n more bytes.
// It returns the index where bytes should be written.
// If the buffer can't grow it will panic with ErrTooLarge.
func ( *Buffer) ( int) int {
	 := .Len()
	// If buffer is empty, reset to recover space.
	if  == 0 && .off != 0 {
		.Reset()
	}
	// Try to grow by means of a reslice.
	if ,  := .tryGrowByReslice();  {
		return 
	}
	if .buf == nil &&  <= smallBufferSize {
		.buf = make([]byte, , smallBufferSize)
		return 0
	}
	 := cap(.buf)
	if  <= /2- {
		// We can slide things down instead of allocating a new
		// slice. We only need m+n <= c to slide, but
		// we instead let capacity get twice as large so we
		// don't spend all our time copying.
		copy(.buf, .buf[.off:])
	} else if  > maxInt-- {
		panic(ErrTooLarge)
	} else {
		// Add b.off to account for b.buf[:b.off] being sliced off the front.
		.buf = growSlice(.buf[.off:], .off+)
	}
	// Restore b.off and len(b.buf).
	.off = 0
	.buf = .buf[:+]
	return 
}

// Grow grows the buffer's capacity, if necessary, to guarantee space for
// another n bytes. After Grow(n), at least n bytes can be written to the
// buffer without another allocation.
// If n is negative, Grow will panic.
// If the buffer can't grow it will panic with [ErrTooLarge].
func ( *Buffer) ( int) {
	if  < 0 {
		panic("bytes.Buffer.Grow: negative count")
	}
	 := .grow()
	.buf = .buf[:]
}

// Write appends the contents of p to the buffer, growing the buffer as
// needed. The return value n is the length of p; err is always nil. If the
// buffer becomes too large, Write will panic with [ErrTooLarge].
func ( *Buffer) ( []byte) ( int,  error) {
	.lastRead = opInvalid
	,  := .tryGrowByReslice(len())
	if ! {
		 = .grow(len())
	}
	return copy(.buf[:], ), nil
}

// WriteString appends the contents of s to the buffer, growing the buffer as
// needed. The return value n is the length of s; err is always nil. If the
// buffer becomes too large, WriteString will panic with [ErrTooLarge].
func ( *Buffer) ( string) ( int,  error) {
	.lastRead = opInvalid
	,  := .tryGrowByReslice(len())
	if ! {
		 = .grow(len())
	}
	return copy(.buf[:], ), nil
}

// MinRead is the minimum slice size passed to a Read call by
// [Buffer.ReadFrom]. As long as the [Buffer] has at least MinRead bytes beyond
// what is required to hold the contents of r, ReadFrom will not grow the
// underlying buffer.
const MinRead = 512

// ReadFrom reads data from r until EOF and appends it to the buffer, growing
// the buffer as needed. The return value n is the number of bytes read. Any
// error except io.EOF encountered during the read is also returned. If the
// buffer becomes too large, ReadFrom will panic with [ErrTooLarge].
func ( *Buffer) ( io.Reader) ( int64,  error) {
	.lastRead = opInvalid
	for {
		 := .grow(MinRead)
		.buf = .buf[:]
		,  := .Read(.buf[:cap(.buf)])
		if  < 0 {
			panic(errNegativeRead)
		}

		.buf = .buf[:+]
		 += int64()
		if  == io.EOF {
			return , nil // e is EOF, so return nil explicitly
		}
		if  != nil {
			return , 
		}
	}
}

// growSlice grows b by n, preserving the original content of b.
// If the allocation fails, it panics with ErrTooLarge.
func growSlice( []byte,  int) []byte {
	defer func() {
		if recover() != nil {
			panic(ErrTooLarge)
		}
	}()
	// TODO(http://golang.org/issue/51462): We should rely on the append-make
	// pattern so that the compiler can call runtime.growslice. For example:
	//	return append(b, make([]byte, n)...)
	// This avoids unnecessary zero-ing of the first len(b) bytes of the
	// allocated slice, but this pattern causes b to escape onto the heap.
	//
	// Instead use the append-make pattern with a nil slice to ensure that
	// we allocate buffers rounded up to the closest size class.
	 := len() +  // ensure enough space for n elements
	if  < 2*cap() {
		// The growth rate has historically always been 2x. In the future,
		// we could rely purely on append to determine the growth rate.
		 = 2 * cap()
	}
	 := append([]byte(nil), make([]byte, )...)
	copy(, )
	return [:len()]
}

// WriteTo writes data to w until the buffer is drained or an error occurs.
// The return value n is the number of bytes written; it always fits into an
// int, but it is int64 to match the io.WriterTo interface. Any error
// encountered during the write is also returned.
func ( *Buffer) ( io.Writer) ( int64,  error) {
	.lastRead = opInvalid
	if  := .Len();  > 0 {
		,  := .Write(.buf[.off:])
		if  >  {
			panic("bytes.Buffer.WriteTo: invalid Write count")
		}
		.off += 
		 = int64()
		if  != nil {
			return , 
		}
		// all bytes should have been written, by definition of
		// Write method in io.Writer
		if  !=  {
			return , io.ErrShortWrite
		}
	}
	// Buffer is now empty; reset.
	.Reset()
	return , nil
}

// WriteByte appends the byte c to the buffer, growing the buffer as needed.
// The returned error is always nil, but is included to match [bufio.Writer]'s
// WriteByte. If the buffer becomes too large, WriteByte will panic with
// [ErrTooLarge].
func ( *Buffer) ( byte) error {
	.lastRead = opInvalid
	,  := .tryGrowByReslice(1)
	if ! {
		 = .grow(1)
	}
	.buf[] = 
	return nil
}

// WriteRune appends the UTF-8 encoding of Unicode code point r to the
// buffer, returning its length and an error, which is always nil but is
// included to match [bufio.Writer]'s WriteRune. The buffer is grown as needed;
// if it becomes too large, WriteRune will panic with [ErrTooLarge].
func ( *Buffer) ( rune) ( int,  error) {
	// Compare as uint32 to correctly handle negative runes.
	if uint32() < utf8.RuneSelf {
		.WriteByte(byte())
		return 1, nil
	}
	.lastRead = opInvalid
	,  := .tryGrowByReslice(utf8.UTFMax)
	if ! {
		 = .grow(utf8.UTFMax)
	}
	.buf = utf8.AppendRune(.buf[:], )
	return len(.buf) - , nil
}

// Read reads the next len(p) bytes from the buffer or until the buffer
// is drained. The return value n is the number of bytes read. If the
// buffer has no data to return, err is io.EOF (unless len(p) is zero);
// otherwise it is nil.
func ( *Buffer) ( []byte) ( int,  error) {
	.lastRead = opInvalid
	if .empty() {
		// Buffer is empty, reset to recover space.
		.Reset()
		if len() == 0 {
			return 0, nil
		}
		return 0, io.EOF
	}
	 = copy(, .buf[.off:])
	.off += 
	if  > 0 {
		.lastRead = opRead
	}
	return , nil
}

// Next returns a slice containing the next n bytes from the buffer,
// advancing the buffer as if the bytes had been returned by [Buffer.Read].
// If there are fewer than n bytes in the buffer, Next returns the entire buffer.
// The slice is only valid until the next call to a read or write method.
func ( *Buffer) ( int) []byte {
	.lastRead = opInvalid
	 := .Len()
	if  >  {
		 = 
	}
	 := .buf[.off : .off+]
	.off += 
	if  > 0 {
		.lastRead = opRead
	}
	return 
}

// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error io.EOF.
func ( *Buffer) () (byte, error) {
	if .empty() {
		// Buffer is empty, reset to recover space.
		.Reset()
		return 0, io.EOF
	}
	 := .buf[.off]
	.off++
	.lastRead = opRead
	return , nil
}

// ReadRune reads and returns the next UTF-8-encoded
// Unicode code point from the buffer.
// If no bytes are available, the error returned is io.EOF.
// If the bytes are an erroneous UTF-8 encoding, it
// consumes one byte and returns U+FFFD, 1.
func ( *Buffer) () ( rune,  int,  error) {
	if .empty() {
		// Buffer is empty, reset to recover space.
		.Reset()
		return 0, 0, io.EOF
	}
	 := .buf[.off]
	if  < utf8.RuneSelf {
		.off++
		.lastRead = opReadRune1
		return rune(), 1, nil
	}
	,  := utf8.DecodeRune(.buf[.off:])
	.off += 
	.lastRead = readOp()
	return , , nil
}

// UnreadRune unreads the last rune returned by [Buffer.ReadRune].
// If the most recent read or write operation on the buffer was
// not a successful [Buffer.ReadRune], UnreadRune returns an error.  (In this regard
// it is stricter than [Buffer.UnreadByte], which will unread the last byte
// from any read operation.)
func ( *Buffer) () error {
	if .lastRead <= opInvalid {
		return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
	}
	if .off >= int(.lastRead) {
		.off -= int(.lastRead)
	}
	.lastRead = opInvalid
	return nil
}

var errUnreadByte = errors.New("bytes.Buffer: UnreadByte: previous operation was not a successful read")

// UnreadByte unreads the last byte returned by the most recent successful
// read operation that read at least one byte. If a write has happened since
// the last read, if the last read returned an error, or if the read read zero
// bytes, UnreadByte returns an error.
func ( *Buffer) () error {
	if .lastRead == opInvalid {
		return errUnreadByte
	}
	.lastRead = opInvalid
	if .off > 0 {
		.off--
	}
	return nil
}

// ReadBytes reads until the first occurrence of delim in the input,
// returning a slice containing the data up to and including the delimiter.
// If ReadBytes encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadBytes returns err != nil if and only if the returned data does not end in
// delim.
func ( *Buffer) ( byte) ( []byte,  error) {
	,  := .readSlice()
	// return a copy of slice. The buffer's backing array may
	// be overwritten by later calls.
	 = append(, ...)
	return , 
}

// readSlice is like ReadBytes but returns a reference to internal buffer data.
func ( *Buffer) ( byte) ( []byte,  error) {
	 := IndexByte(.buf[.off:], )
	 := .off +  + 1
	if  < 0 {
		 = len(.buf)
		 = io.EOF
	}
	 = .buf[.off:]
	.off = 
	.lastRead = opRead
	return , 
}

// ReadString reads until the first occurrence of delim in the input,
// returning a string containing the data up to and including the delimiter.
// If ReadString encounters an error before finding a delimiter,
// it returns the data read before the error and the error itself (often io.EOF).
// ReadString returns err != nil if and only if the returned data does not end
// in delim.
func ( *Buffer) ( byte) ( string,  error) {
	,  := .readSlice()
	return string(), 
}

// NewBuffer creates and initializes a new [Buffer] using buf as its
// initial contents. The new [Buffer] takes ownership of buf, and the
// caller should not use buf after this call. NewBuffer is intended to
// prepare a [Buffer] to read existing data. It can also be used to set
// the initial size of the internal buffer for writing. To do that,
// buf should have the desired capacity but a length of zero.
//
// In most cases, new([Buffer]) (or just declaring a [Buffer] variable) is
// sufficient to initialize a [Buffer].
func ( []byte) *Buffer { return &Buffer{buf: } }

// NewBufferString creates and initializes a new [Buffer] using string s as its
// initial contents. It is intended to prepare a buffer to read an existing
// string.
//
// In most cases, new([Buffer]) (or just declaring a [Buffer] variable) is
// sufficient to initialize a [Buffer].
func ( string) *Buffer {
	return &Buffer{buf: []byte()}
}