const smallBufferSize … type Buffer … type readOp … const opRead … const opInvalid … const opReadRune1 … const opReadRune2 … const opReadRune3 … const opReadRune4 … var ErrTooLarge … var errNegativeRead … const maxInt … // 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 (b *Buffer) Bytes() []byte { … } // 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 (b *Buffer) AvailableBuffer() []byte { … } // 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 (b *Buffer) String() string { … } // empty reports whether the unread portion of the buffer is empty. func (b *Buffer) empty() bool { … } // Len returns the number of bytes of the unread portion of the buffer; // b.Len() == len(b.Bytes()). func (b *Buffer) Len() int { … } // Cap returns the capacity of the buffer's underlying byte slice, that is, the // total space allocated for the buffer's data. func (b *Buffer) Cap() int { … } // Available returns how many bytes are unused in the buffer. func (b *Buffer) Available() int { … } // 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 (b *Buffer) Truncate(n int) { … } // 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 (b *Buffer) Reset() { … } // 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 (b *Buffer) tryGrowByReslice(n int) (int, bool) { … } // 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 (b *Buffer) grow(n int) int { … } // 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 (b *Buffer) Grow(n int) { … } // 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 (b *Buffer) Write(p []byte) (n int, err error) { … } // 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 (b *Buffer) WriteString(s string) (n int, err error) { … } const MinRead … // 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 (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) { … } // growSlice grows b by n, preserving the original content of b. // If the allocation fails, it panics with ErrTooLarge. func growSlice(b []byte, n int) []byte { … } // 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 (b *Buffer) WriteTo(w io.Writer) (n int64, err error) { … } // 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 (b *Buffer) WriteByte(c byte) error { … } // 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 (b *Buffer) WriteRune(r rune) (n int, err error) { … } // 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 (b *Buffer) Read(p []byte) (n int, err error) { … } // 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 (b *Buffer) Next(n int) []byte { … } // ReadByte reads and returns the next byte from the buffer. // If no byte is available, it returns error [io.EOF]. func (b *Buffer) ReadByte() (byte, error) { … } // 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 (b *Buffer) ReadRune() (r rune, size int, err error) { … } // 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 (b *Buffer) UnreadRune() error { … } var errUnreadByte … // 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 (b *Buffer) UnreadByte() error { … } // 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 (b *Buffer) ReadBytes(delim byte) (line []byte, err error) { … } // readSlice is like ReadBytes but returns a reference to internal buffer data. func (b *Buffer) readSlice(delim byte) (line []byte, err error) { … } // 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 (b *Buffer) ReadString(delim byte) (line string, err error) { … } // 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 NewBuffer(buf []byte) *Buffer { … } // 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 NewBufferString(s string) *Buffer { … }