【Golang1.20源码阅读】bytes/buffer.go

// 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 (
	"errors"
	"io"
	"unicode/utf8"
)

// smallBufferSize is an initial allocation minimal capacity.
// smallBufferSize是初始分配的最小容量。
const smallBufferSize = 64

// A Buffer is a variable-sized buffer of bytes with Read and Write methods.
// The zero value for Buffer is an empty buffer ready to use.
// Buffer是一个大小可变的字节缓冲区,具有Read和Write方法。
// Buffer的零值是一个可供使用的空缓冲区
type Buffer struct {
	buf      []byte // contents are the bytes buf[off : len(buf)] 内容是字节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. 最后一次读取操作,以便Unread*可以正常工作。
}

// 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.
// readOp常量描述了对缓冲区执行的最后一个操作,因此UnreadRun和UnreadByte可以检查是否存在无效使用。
// 选择opReadRuneX常量,使其转换为int时与读取的符文大小相对应。
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.
// 不要使用iota,因为值需要与名称和注释相对应,这在显式时更容易看到。
const (
	opRead      readOp = -1 // Any other read operation. 任意读操作
	opInvalid   readOp = 0  // Non-read operation. 非读操作
	opReadRune1 readOp = 1  // Read rune of size 1. 长度为 1 的 rune
	opReadRune2 readOp = 2  // Read rune of size 2. 长度为 2 的 rune
	opReadRune3 readOp = 3  // Read rune of size 3. 长度为 3 的 rune
	opReadRune4 readOp = 4  // Read rune of size 4. 长度为 4 的 rune
)

// ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.
// 如果无法分配内存在缓冲区中存储数据,则ErrTooLarge将被传递到panic。
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 Read, Write, Reset, or 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.
// Bytes返回一个长度为b.Len()的片段,其中包含缓冲区的未读部分。
// 该切片仅在下一次缓冲区修改之前有效(即仅在对Read、Write、Reset或Truncate等方法的下一次调用之前有效)。
// 切片对缓冲区内容进行别名,至少直到下一次缓冲区修改, 因此切片的即时更改将影响将来读取的结果。
func (b *Buffer) Bytes() []byte { return b.buf[b.off:] }

// 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.
// 字符串以字符串形式返回缓冲区中未读部分的内容。如果Buffer是一个nil指针,它将返回“<nil>”。若要更有效地生成字符串,请参阅strings.Builder类型。
func (b *Buffer) String() string {
	if b == nil {
		// Special case, useful in debugging.
		return "<nil>"
	}
	return string(b.buf[b.off:])
}

// empty reports whether the unread portion of the buffer is empty.
// empty报告缓冲区的未读部分是否为空。
func (b *Buffer) empty() bool { return len(b.buf) <= b.off }

// Len returns the number of bytes of the unread portion of the buffer;
// b.Len() == len(b.Bytes()).
// Len返回缓冲区中未读部分的字节数;b.Len()==Len(b.Bytes())。
func (b *Buffer) Len() int { return len(b.buf) - b.off }

// Cap returns the capacity of the buffer's underlying byte slice, that is, the
// total space allocated for the buffer's data.
// Cap返回缓冲区底层字节片的容量,即为缓冲区数据分配的总空间。
func (b *Buffer) Cap() int { return cap(b.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.
// Truncate将丢弃缓冲区中除前n个未读字节外的所有字节,但继续使用相同的已分配存储。如果n为负数或大于缓冲区的长度,它会panic。
func (b *Buffer) Truncate(n int) {
	if n == 0 {
		b.Reset()
		return
	}
	b.lastRead = opInvalid
	if n < 0 || n > b.Len() {
		panic("bytes.Buffer: truncation out of range")
	}
	b.buf = b.buf[:b.off+n]
}

// Reset resets the buffer to be empty,
// but it retains the underlying storage for use by future writes.
// Reset is the same as Truncate(0).
//Reset会将缓冲区重置为空,但它会保留底层存储以供将来写入使用。重置与截断(0)相同。
func (b *Buffer) Reset() {
	b.buf = b.buf[:0]
	b.off = 0
	b.lastRead = opInvalid
}

// tryGrowByReslice is a 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.
// tryGrowByReslice是grow的一个可内联版本,适用于只需要重新slice内部缓冲区的快速情况。它返回应该写入字节的索引以及是否成功。
func (b *Buffer) tryGrowByReslice(n int) (int, bool) {
	if l := len(b.buf); n <= cap(b.buf)-l {
		b.buf = b.buf[:l+n]
		return l, 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.
//grow增加缓冲区以保证n个字节的空间。它返回应该写入字节的索引。如果缓冲区无法增长,它将因ErrTooLarge而panic。
func (b *Buffer) grow(n int) int {
	m := b.Len()
	// If buffer is empty, reset to recover space. 如果缓冲区为空,则重置以恢复空间。
	if m == 0 && b.off != 0 {
		b.Reset()
	}
	// Try to grow by means of a reslice. 试着通过重新播种来生长。
	if i, ok := b.tryGrowByReslice(n); ok {
		return i
	}
	if b.buf == nil && n <= smallBufferSize {
		b.buf = make([]byte, n, smallBufferSize)
		return 0
	}
	c := cap(b.buf)
	if n <= c/2-m {
		// 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.
		//我们可以向下滑动,而不是分配一个新的切片。我们只需要m+n<=c来滑动,但我们让容量增加了一倍,这样我们就不会把所有的时间都花在复制上。
		copy(b.buf, b.buf[b.off:])
	} else if c > maxInt-c-n {
		panic(ErrTooLarge)
	} else {
		// Add b.off to account for b.buf[:b.off] being sliced off the front. 加上b.off来说明b.buf[:b.off]的正面被切掉了。
		b.buf = growSlice(b.buf[b.off:], b.off+n)
	}
	// Restore b.off and len(b.buf). 恢复b.off和len(b.buf)。
	b.off = 0
	b.buf = b.buf[:m+n]
	return m
}

// 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.
// Grow在必要时增加缓冲区的容量,以保证另外n个字节的空间。Grow(n)之后,在没有其他分配的情况下,至少可以向缓冲区写入n个字节。
// 如果n为负数,Grow会恐慌。如果缓冲区无法增长,它将因ErrTooLarge而panic。
func (b *Buffer) Grow(n int) {
	if n < 0 {
		panic("bytes.Buffer.Grow: negative count")
	}
	m := b.grow(n)
	b.buf = b.buf[:m]
}

// 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.
// Write将p的内容附加到缓冲区,根据需要增加缓冲区。返回值n是p的长度;err总是零。如果缓冲区变得太大,Write将因ErrTooLarge而panic。
func (b *Buffer) Write(p []byte) (n int, err error) {
	b.lastRead = opInvalid
	m, ok := b.tryGrowByReslice(len(p))
	if !ok {
		m = b.grow(len(p))
	}
	return copy(b.buf[m:], p), 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.
// WriteString将s的内容附加到缓冲区,根据需要增加缓冲区。返回值n是s的长度;err总是零。如果缓冲区过大,WriteString将因ErrTooLarge而死机。
func (b *Buffer) WriteString(s string) (n int, err error) {
	b.lastRead = opInvalid
	m, ok := b.tryGrowByReslice(len(s))
	if !ok {
		m = b.grow(len(s))
	}
	return copy(b.buf[m:], s), 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.
// MinRead是Buffer.ReadFrom传递给Read调用的最小切片大小。只要Buffer的MinRead字节数至少超过了容纳r内容所需的字节数,ReadFrom就不会增加底层缓冲区。
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.
// ReadFrom从r读取数据直到EOF,并将其附加到缓冲区,根据需要增加缓冲区。返回值n是读取的字节数。
// 读取期间遇到的除io.EOF以外的任何错误也会返回。如果缓冲区过大,ReadFrom将因ErrTooLarge而死机。
func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error) {
	b.lastRead = opInvalid
	for {
		i := b.grow(MinRead)
		b.buf = b.buf[:i]
		m, e := r.Read(b.buf[i:cap(b.buf)])
		if m < 0 {
			panic(errNegativeRead)
		}

		b.buf = b.buf[:i+m]
		n += int64(m)
		if e == io.EOF {
			return n, nil // e is EOF, so return nil explicitly e是EOF,因此显式返回nil
		}
		if e != nil {
			return n, e
		}
	}
}

// growSlice grows b by n, preserving the original content of b.
// If the allocation fails, it panics with ErrTooLarge.
// growtSlice将b增长n,保留b的原始内容。如果分配失败,它将使用ErrTooLarge。
func growSlice(b []byte, n 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.
	c := len(b) + n // ensure enough space for n elements
	// TODO(http://golang.org/issue/51462):我们应该依赖append-make模式,这样编译器就可以调用runtime.growslice。
	// 例如:return append(b,make([]byte,n)…)这避免了对分配的切片的第一个len(b)字节进行不必要的清零,但这种模式会导致b逃逸到堆中。
	// 相反,使用带有nil切片的append-make模式来确保我们将缓冲区四舍五入分配到最接近大小的类。c:=len(b)+n//确保n个元素有足够的空间
	if c < 2*cap(b) {
		// The growth rate has historically always been 2x. In the future,
		// we could rely purely on append to determine the growth rate.
		// 历史上,增长率一直是2倍。在未来,我们可以完全依靠追加来确定增长率。
		c = 2 * cap(b)
	}
	b2 := append([]byte(nil), make([]byte, c)...)
	copy(b2, b)
	return b2[:len(b)]
}

// 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.
// WriteTo将数据写入w,直到缓冲区耗尽或出现错误。返回值n是写入的字节数;它总是适合int,但它是int64以匹配io.WriterTo接口。
// 写入过程中遇到的任何错误也会返回。
func (b *Buffer) WriteTo(w io.Writer) (n int64, err error) {
	b.lastRead = opInvalid
	if nBytes := b.Len(); nBytes > 0 {
		m, e := w.Write(b.buf[b.off:])
		if m > nBytes {
			panic("bytes.Buffer.WriteTo: invalid Write count")
		}
		b.off += m
		n = int64(m)
		if e != nil {
			return n, e
		}
		// all bytes should have been written, by definition of
		// Write method in io.Writer
		// 根据io.Writer中Write方法的定义,所有字节都应该已经写入
		if m != nBytes {
			return n, io.ErrShortWrite
		}
	}
	// Buffer is now empty; reset.
	b.Reset()
	return n, 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.
// WriteByte将字节c附加到缓冲区,根据需要增加缓冲区。返回的错误始终为零,但已包含以匹配bufio.Writer的WriteByte。
// 如果缓冲区过大,WriteByte将因ErrTooLarge而panic
func (b *Buffer) WriteByte(c byte) error {
	b.lastRead = opInvalid
	m, ok := b.tryGrowByReslice(1)
	if !ok {
		m = b.grow(1)
	}
	b.buf[m] = c
	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.
// WriteRune将Unicode代码点r的UTF-8编码附加到缓冲区,返回其长度和一个错误,该错误始终为零,但为了匹配bufio.Writer的WriteRune而包含该错误。
// 缓冲区根据需要进行增长;如果它变得太大,WriteRune将因ErrTooLarge而死机。
func (b *Buffer) WriteRune(r rune) (n int, err error) {
	// Compare as uint32 to correctly handle negative runes. 作为uint32进行比较以正确处理负符文。
	if uint32(r) < utf8.RuneSelf {
		b.WriteByte(byte(r))
		return 1, nil
	}
	b.lastRead = opInvalid
	m, ok := b.tryGrowByReslice(utf8.UTFMax)
	if !ok {
		m = b.grow(utf8.UTFMax)
	}
	b.buf = utf8.AppendRune(b.buf[:m], r)
	return len(b.buf) - m, 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.
// Read从缓冲区读取下一个len(p)字节,或者直到缓冲区耗尽为止。返回值n是读取的字节数。
// 如果缓冲区没有要返回的数据,则err为io.EOF(除非len(p)为零);否则为零。
func (b *Buffer) Read(p []byte) (n int, err error) {
	b.lastRead = opInvalid
	if b.empty() {
		// Buffer is empty, reset to recover space. 缓冲区为空,请重置以恢复空间。
		b.Reset()
		if len(p) == 0 {
			return 0, nil
		}
		return 0, io.EOF
	}
	n = copy(p, b.buf[b.off:])
	b.off += n
	if n > 0 {
		b.lastRead = opRead
	}
	return n, nil
}

// Next returns a slice containing the next n bytes from the buffer,
// advancing the buffer as if the bytes had been returned by 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.
// Next从缓冲区返回一个包含下一个n字节的切片,使缓冲区前进,就好像这些字节是由Read返回的一样。
// 如果缓冲区中的字节数少于n,Next将返回整个缓冲区。切片只有在下一次调用读或写方法之前才有效。
func (b *Buffer) Next(n int) []byte {
	b.lastRead = opInvalid
	m := b.Len()
	if n > m {
		n = m
	}
	data := b.buf[b.off : b.off+n]
	b.off += n
	if n > 0 {
		b.lastRead = opRead
	}
	return data
}

// ReadByte reads and returns the next byte from the buffer.
// If no byte is available, it returns error io.EOF.
// ReadByte从缓冲区读取并返回下一个字节。如果没有可用的字节,则返回错误io.EOF。
func (b *Buffer) ReadByte() (byte, error) {
	if b.empty() {
		// Buffer is empty, reset to recover space. 缓冲区为空,请重置以恢复空间。
		b.Reset()
		return 0, io.EOF
	}
	c := b.buf[b.off]
	b.off++
	b.lastRead = opRead
	return c, 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.
// ReadRune从缓冲区读取并返回下一个UTF-8编码的Unicode代码点。如果没有可用的字节,则返回的错误为io.EOF。
// 如果字节是错误的UTF-8编码,则消耗一个字节并返回U+FFFD,1。
func (b *Buffer) ReadRune() (r rune, size int, err error) {
	if b.empty() {
		// Buffer is empty, reset to recover space. 缓冲区为空,请重置以恢复空间。
		b.Reset()
		return 0, 0, io.EOF
	}
	c := b.buf[b.off]
	if c < utf8.RuneSelf {
		b.off++
		b.lastRead = opReadRune1
		return rune(c), 1, nil
	}
	r, n := utf8.DecodeRune(b.buf[b.off:])
	b.off += n
	b.lastRead = readOp(n)
	return r, n, nil
}

// UnreadRune unreads the last rune returned by ReadRune.
// If the most recent read or write operation on the buffer was
// not a successful ReadRune, UnreadRune returns an error.  (In this regard
// it is stricter than UnreadByte, which will unread the last byte
// from any read operation.)
// UnreadRun读取ReadRune返回的最后一个符文。如果缓冲区上最近的读取或写入操作不是成功的ReadRune,则UnreadRun将返回错误。
// (在这方面,它比UnreadByte更严格,后者将从任何读取操作中读取最后一个字节。)
func (b *Buffer) UnreadRune() error {
	if b.lastRead <= opInvalid {
		return errors.New("bytes.Buffer: UnreadRune: previous operation was not a successful ReadRune")
	}
	if b.off >= int(b.lastRead) {
		b.off -= int(b.lastRead)
	}
	b.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.
// UnreadByte读取最近一次成功读取至少一个字节的读取操作返回的最后一个字节。如果自上次读取后发生写入,如果上次读取返回错误,
// 或者如果读取读取的字节为零,则UnreadByte返回错误。
func (b *Buffer) UnreadByte() error {
	if b.lastRead == opInvalid {
		return errUnreadByte
	}
	b.lastRead = opInvalid
	if b.off > 0 {
		b.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.
// ReadBytes读取直到输入中第一次出现delim,返回一个包含分隔符之前的数据的切片。
// 如果ReadBytes在查找分隔符之前遇到错误,它会返回错误之前读取的数据和错误本身(通常为io.EOF)。
// ReadBytes返回err!=nil当且仅当返回的数据不以delim结尾时。
func (b *Buffer) ReadBytes(delim byte) (line []byte, err error) {
	slice, err := b.readSlice(delim)
	// return a copy of slice. The buffer's backing array may
	// be overwritten by later calls.
	// 返回切片的副本。缓冲区的后备数组可能会被以后的调用覆盖。
	line = append(line, slice...)
	return line, err
}

// readSlice is like ReadBytes but returns a reference to internal buffer data.
//readSlice类似于ReadBytes,但返回对内部缓冲区数据的引用。
func (b *Buffer) readSlice(delim byte) (line []byte, err error) {
	i := IndexByte(b.buf[b.off:], delim)
	end := b.off + i + 1
	if i < 0 {
		end = len(b.buf)
		err = io.EOF
	}
	line = b.buf[b.off:end]
	b.off = end
	b.lastRead = opRead
	return line, err
}

// 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.
// ReadString读取直到输入中第一次出现delim,返回一个字符串,该字符串包含直到并包括分隔符的数据。
// 如果ReadString在查找分隔符之前遇到错误,它会返回错误之前读取的数据和错误本身(通常为io.EOF)。
// ReadString返回err!=nil当且仅当返回的数据不以delim结尾时。
func (b *Buffer) ReadString(delim byte) (line string, err error) {
	slice, err := b.readSlice(delim)
	return string(slice), err
}

// 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.

// NewBuffer使用buf作为其初始内容创建并初始化新的Buffer。新的Buffer拥有buf的所有权,调用方不应该在该调用之后使用buf。
// NewBuffer旨在准备一个Buffer来读取现有数据。它还可以用于设置用于写入的内部缓冲区的初始大小。
// 要做到这一点,buf应该具有所需的容量,但长度为零。在大多数情况下,new(Buffer)(或仅声明Buffer变量)就足以初始化Buffer。
func NewBuffer(buf []byte) *Buffer { return &Buffer{buf: 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.

// NewBufferString使用字符串s作为其初始内容创建并初始化新的Buffer。它旨在准备一个缓冲区来读取现有字符串。
// 在大多数情况下,new(Buffer)(或仅声明Buffer变量)就足以初始化Buffer。
func NewBufferString(s string) *Buffer {
	return &Buffer{buf: []byte(s)}
}

猜你喜欢

转载自blog.csdn.net/qq2942713658/article/details/132466832
今日推荐