Source File
scan.go
Belonging Package
bufio
package bufio
import (
)
type Scanner struct {
r io.Reader // The reader provided by the client.
split SplitFunc // The function to split the tokens.
maxTokenSize int // Maximum size of a token; modified by tests.
token []byte // Last token returned by split.
buf []byte // Buffer used as argument to split.
start int // First non-processed byte in buf.
end int // End of data in buf.
err error // Sticky error.
empties int // Count of successive empty tokens.
scanCalled bool // Scan has been called; buffer is in use.
done bool // Scan has finished.
}
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")
)
MaxScanTokenSize = 64 * 1024
startBufSize = 4096 // Size of initial allocation for buffer.
)
func ( io.Reader) *Scanner {
return &Scanner{
r: ,
split: ScanLines,
maxTokenSize: MaxScanTokenSize,
}
}
var ErrFinalToken = errors.New("final token")
.empties++
if .empties > maxConsecutiveEmptyReads {
panic("bufio.Scan: too many empty tokens without progressing")
}
}
return true
}
const = int(^uint(0) >> 1)
if len(.buf) >= .maxTokenSize || len(.buf) > /2 {
.setErr(ErrTooLong)
return false
}
:= len(.buf) * 2
if == 0 {
= startBufSize
}
if > .maxTokenSize {
= .maxTokenSize
}
:= make([]byte, )
copy(, .buf[.start:.end])
.buf =
.end -= .start
.start = 0
func ( *Scanner) ( []byte, int) {
if .scanCalled {
panic("Buffer called after Scan")
}
.buf = [0:cap()]
.maxTokenSize =
}
func ( *Scanner) ( SplitFunc) {
if .scanCalled {
panic("Split called after Scan")
}
.split =
}
, := utf8.DecodeRune()
return , [0:], nil
}
:= 0
for := 0; < len(); += {
var rune
, = utf8.DecodeRune([:])
if !isSpace() {
break
}
![]() |
The pages are generated with Golds v0.3.2-preview. (GOOS=darwin GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @Go100and1 (reachable from the left QR code) to get the latest news of Golds. |