1. Home
  2. Docs
  3. golang
  4. 内置库(包-package)
  5. bufio

bufio

Overview

Package bufio implements buffered I/O. It wraps an io.Reader or io.Writer object, creating another object (Reader or Writer) that also implements the interface but provides buffering and some help for textual I/O.

包bufio实现了缓冲的I/O。它包装了一个io.Reader或io.Writer对象,创建了另一个对象(Reader或Writer),它也实现了该接口,但提供了缓冲和一些文本I/O的帮助。

Constants

const (
    // MaxScanTokenSize is the maximum size used to buffer a token
    // unless the user provides an explicit buffer with Scanner.Buffer.
    // The actual maximum token size may be smaller as the buffer
    // may need to include, for instance, a newline.
    MaxScanTokenSize = 64 * 1024
)

Variables

var (
    ErrInvalidUnreadByte = errors.New("bufio: invalid use of UnreadByte")
    ErrInvalidUnreadRune = errors.New("bufio: invalid use of UnreadRune")
    ErrBufferFull        = errors.New("bufio: buffer full")
    ErrNegativeCount     = errors.New("bufio: negative count")
)
var (
    ErrTooLong         = errors.New("bufio.Scanner: token too long")
    ErrNegativeAdvance = errors.New("bufio.Scanner: SplitFunc returns negative advance count")
    ErrAdvanceTooFar   = errors.New("bufio.Scanner: SplitFunc returns advance count beyond input")
    ErrBadReadCount    = errors.New("bufio.Scanner: Read returned impossible count")
)

Errors returned by Scanner.

var ErrFinalToken = errors.New("final token")

Functions

func ScanBytes

func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error)

ScanBytes is a split function for a Scanner that returns each byte as a token.

ScanBytes是Scanner的一个分割函数,它将每个字节作为一个标记返回。

func ScanLines

func ScanLines(data []byte, atEOF bool) (advance int, token []byte, err error)

ScanLines is a split function for a Scanner that returns each line of text, stripped of any trailing end-of-line marker. The returned line may be empty. The end-of-line marker is one optional carriage return followed by one mandatory newline. In regular expression notation, it is \r?\n. The last non-empty line of input will be returned even if it has no newline.

ScanLines是Scanner的一个分割函数,它返回每一行的文本,去掉任何尾部的行尾标记。返回的行可能是空的。行末标记是一个可选的回车键,后面是一个强制性的换行键。在正则表达式中,它是r?\n。最后一行非空的输入将被返回,即使它没有换行。

func ScanRunes

func ScanRunes(data []byte, atEOF bool) (advance int, token []byte, err error)

ScanRunes is a split function for a Scanner that returns each UTF-8-encoded rune as a token. The sequence of runes returned is equivalent to that from a range loop over the input as a string, which means that erroneous UTF-8 encodings translate to U+FFFD = "\xef\xbf\xbd". Because of the Scan interface, this makes it impossible for the client to distinguish correctly encoded replacement runes from encoding errors.

ScanRunes是Scanner的一个分割函数,它将每个UTF-8编码的符文作为一个标记返回。返回的符文序列等同于从输入的范围循环中得到的字符串,这意味着错误的UTF-8编码会转化为U+FFFD = "\xef\xbf\xbd"。由于扫描接口的存在,这使得客户端无法区分正确编码的替换符文和编码错误。

func ScanWords

func ScanWords(data []byte, atEOF bool) (advance int, token []byte, err error)

ScanWords is a split function for a Scanner that returns each space-separated word of text, with surrounding spaces deleted. It will never return an empty string. The definition of space is set by unicode.IsSpace.

ScanWords是Scanner的一个分割函数,它返回每个以空格分隔的文本字,并删除周围的空格。它永远不会返回一个空字符串。空间的定义是由unicode.IsSpace设置的。

Types

type ReadWriter

type ReadWriter struct {
    *Reader
    *Writer
}

ReadWriter stores pointers to a Reader and a Writer. It implements io.ReadWriter.

ReadWriter存储指向一个Reader和一个Writer的指针。它实现了io.ReadWriter

func NewReadWriter

func NewReadWriter(r *Reader, w *Writer) *ReadWriter

NewReadWriter allocates a new ReadWriter that dispatches to r and w.

NewReadWriter分配了一个新的ReadWriter,向r和w派发。

type Reader

type Reader struct {
    // contains filtered or unexported fields
}

Reader implements buffering for an io.Reader object.

读者实现了io.Reader对象的缓冲。

func NewReader

func NewReader(rd io.Reader) *Reader

NewReader returns a new Reader whose buffer has the default size.

NewReader返回一个新的阅读器,其缓冲区具有默认大小。

func NewReaderSize

func NewReaderSize(rd io.Reader, size int) *Reader

NewReaderSize returns a new Reader whose buffer has at least the specified size. If the argument io.Reader is already a Reader with large enough size, it returns the underlying Reader.

NewReaderSize返回一个新的阅读器,其缓冲区至少有指定的大小。如果参数io.Reader已经是一个具有足够大尺寸的Reader,它将返回底层的Reader。

func (*Reader) Buffered

func (b *Reader) Buffered() int

Buffered returns the number of bytes that can be read from the current buffer.

Buffered返回可从当前缓冲区读取的字节数。

func (*Reader) Discard

func (b *Reader) Discard(n int) (discarded int, err error)

Discard skips the next n bytes, returning the number of bytes discarded.
If Discard skips fewer than n bytes, it also returns an error. If 0 <= n <= b.Buffered(), Discard is guaranteed to succeed without reading from the underlying io.Reader.

Discard跳过接下来的n个字节,返回被丢弃的字节数。
如果Discard跳过的字节数少于n,它也会返回一个错误。如果0 <= n <= b.Buffered(),Discard保证成功,不需要从底层的io.Reader中读取。

func (*Reader) Peek

func (b *Reader) Peek(n int) ([]byte, error)

Peek returns the next n bytes without advancing the reader. The bytes stop being valid at the next read call. If Peek returns fewer than n bytes, it also returns an error explaining why the read is short. The error is ErrBufferFull if n is larger than b’s buffer size.

Calling Peek prevents a UnreadByte or UnreadRune call from succeeding until the next read operation.

Peek返回下一个n个字节,而不推进阅读器。这些字节在下一次读取调用时不再有效。如果Peek返回的字节数少于n,它也会返回一个错误,解释为什么读的时间短。如果n大于b的缓冲区大小,这个错误就是ErrBufferFull。

调用Peek可以防止UnreadByte或UnreadRune调用成功,直到下一次读操作。

func (*Reader) Read

func (b *Reader) Read(p []byte) (n int, err error)

Read reads data into p. It returns the number of bytes read into p. The bytes are taken from at most one Read on the underlying Reader, hence n may be less than len(p). To read exactly len(p) bytes, use io.ReadFull(b, p). At EOF, the count will be zero and err will be io.EOF.

读取数据到p中。它返回读到p中的字节数。这些字节最多取自底层阅读器上的一个Read,因此n可能小于len(p)。要准确地读取len(p)字节,请使用io.ReadFull(b, p)。在EOF时,计数将为零,err将为io.EOF。


bufio.NewReader

NewReader returns a new Reader whose buffer has the default size.

NewReader返回一个新的阅读器,其缓冲区具有默认大小。

bufio.NewScanner

func NewScanner(r io.Reader) *Scanner


NewScanner returns a new Scanner to read from r. The split function defaults to ScanLines.

NewScanner返回一个新的Scanner以从r中读取,分割函数默认为ScanLines。

bufio.ScanBytes

func ScanBytes(data []byte, atEOF bool) (advance int, token []byte, err error)


ScanBytes is a split function for a Scanner that returns each byte as a token.

ScanBytes是Scanner的一个分割函数,它将每个字节作为一个标记返回。

func (s *Scanner) Split

func (s *Scanner) Split(split SplitFunc)


Split sets the split function for the Scanner. The default split function is ScanLines.

Split panics if it is called after scanning has started.

分割设置Scanner的分割功能。默认的分割功能是ScanLines。

如果在扫描开始后调用Split,就会出现panic。

func (*Scanner) Scan

func (s *Scanner) Scan() bool


Scan advances the Scanner to the next token, which will then be available through the Bytes or Text method. It returns false when the scan stops, either by reaching the end of the input or an error. After Scan returns false, the Err method will return any error that occurred during scanning, except that if it was io.EOF, Err will return nil. Scan panics if the split function returns too many empty tokens without advancing the input. This is a common error mode for scanners.

扫描将扫描器推进到下一个标记,然后可以通过Bytes或Text方法获得该标记。当扫描停止时,它将返回false,因为它达到了输入的终点或出现了错误。在Scan返回false后,Err方法将返回扫描过程中发生的任何错误,除了如果是io.EOF,Err将返回nil。如果split函数返回了太多的空符号而没有推进输入,Scan就会报panic。这是扫描器的一种常见错误模式。

Was this article helpful to you? Yes No

How can we help?