Back to

package buffruneio

Latest Go to latest
Published: Jan 3, 2019 | License: MIT | Module:


Package buffruneio provides rune-based buffered input.



const EOF = -1

EOF is a rune value indicating end-of-file.


var ErrNoRuneToUnread = errors.New("no rune to unwind")

ErrNoRuneToUnread is the error returned when UnreadRune is called with nothing to unread.

type Reader

type Reader struct {
	// contains filtered or unexported fields

A Reader implements rune-based input for an underlying byte stream.

func NewReader

func NewReader(input io.Reader) *Reader

NewReader returns a new Reader reading the given input.

func (*Reader) Forget

func (rd *Reader) Forget()

Forget discards buffered runes before the current input position. Calling Forget makes it impossible to UnreadRune earlier than the current input position but is necessary to avoid unbounded buffer growth.

func (*Reader) PeekRunes

func (rd *Reader) PeekRunes(n int) []rune

PeekRunes returns the next n runes in the input, without advancing the current input position.

If the input has fewer than n runes and then returns an io.EOF error, PeekRune returns a slice containing the available runes followed by EOF. On other hand, if the input ends early with a non-io.EOF error, PeekRune returns a slice containing only the available runes, with no terminating EOF.

func (*Reader) ReadRune

func (rd *Reader) ReadRune() (rune, int, error)

ReadRune reads and returns the next rune from the input. The rune is also saved in an internal buffer, in case UnreadRune is called. To avoid unbounded buffer growth, the caller must call Forget at appropriate intervals.

At end of file, ReadRune returns EOF, 0, nil. On read errors other than io.EOF, ReadRune returns EOF, 0, err.

func (*Reader) UnreadRune

func (rd *Reader) UnreadRune() error

UnreadRune rewinds the input by one rune, undoing the effect of a single ReadRune call. UnreadRune may be called multiple times to rewind a sequence of ReadRune calls, up to the last time Forget was called or the beginning of the input.

If there are no ReadRune calls left to undo, UnreadRune returns ErrNoRuneToUnread.

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier