Documentation
¶
Overview ¶
Package scanner implements a stream tokenizer iterator.
The package makes use of the standard library bufio.Scanner to buffer and split data read from an io.Reader. Scanner has a set of standard splitters for words, lines and runes and supports custom split functions as well.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ErrTooManyTokens ¶
type ErrTooManyTokens struct {
// contains filtered or unexported fields
}
ErrTooManyTokens is returned in response to a panic in the scanner.Scan() method, the result of too many tokens being returned without the scanner advancing.
func (ErrTooManyTokens) Error ¶
func (e ErrTooManyTokens) Error() string
func (ErrTooManyTokens) Unwrap ¶
func (e ErrTooManyTokens) Unwrap() error
type Iterator ¶
type Iterator struct {
// contains filtered or unexported fields
}
Iterator wraps a bufio.Scanner to traverse over a stream of tokens such as words or lines read from an io.Reader.
Iterator does not support the SizeHint interface.
Example ¶
package main import ( "bufio" "context" "fmt" "os" "github.com/jake-scott/go-functional/iter/scanner" ) func main() { f, err := os.Open("/etc/passwd") if err != nil { panic(err) } s := bufio.NewScanner(f) ctx := context.Background() iter := scanner.New(s) for iter.Next(ctx) { fmt.Printf("Line: <%s>\n", iter.Get()) } if err := iter.Error(); err != nil { panic(err) } }
func New ¶
New returns an implementation of Iterator that uses bufio.Scanner to traverse through tokens such as words or lines from an io.Reader such as a file.
func (*Iterator) Error ¶
Error returns the panic message from the scanner if one occured during a Next() call, or the cancellation message if the context was cancelled. Otherwise, Error calls the Scanner's Err() method, which returns nil if there are no errors or if the end of input is reached, otherwise the first error encounterd by the scanner.
func (*Iterator) Get ¶
Get returns the most recent token returned by the scanner during a call to Next(), as a string.
The context is not used in this iterator implementation.
func (*Iterator) Next ¶
Next advances the iterator to the next element (scanner token) by calling Scanner.Scan(). It returns false if the end of the input is reached or an error is encountered including cancellation of the context. If the scanner panics, Next returns false and Error() will return the message from the scanner.