runeio

package
v2.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 28, 2022 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package runeio implements a mechanism to read a stream of Unicode code points (runes) from an io.Reader, with an internal buffer to push code points back to the front of the stream to allow limited peeking and rewind.

Index

Constants

View Source
const RuneEOF = rune(0xFFFFFF)

Variables

This section is empty.

Functions

func Must

func Must(x rune, err error) rune

Must accepts a (rune, error) pair and always returns a rune value or raises a panic. If the error is nil, returns the input rune as normal. If the error is io.EOF, returns a special RuneEOF value. Otherwise, if the error is not nil, panics, wrapping the error.

func MustFunc

func MustFunc(f func() (rune, error)) func() rune

MustFunc wraps a function that returns a (rune, error) pair with Must and returns a new function that always returns a rune, RuneEOF, or panics.

Types

type Offset

type Offset struct {
	Byte int64 // current steam offset in bytes (0-indexed)
	Rune int64 // current line offset in runes (0-indexed)
	Line int64 // current line (0-indexed)
}

type Reader

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

Reader has a useful zero value, but doesn't have a pushback buffer by default.

func NewReader

func NewReader(rd io.Reader) *Reader

func NewReaderBuffered

func NewReaderBuffered(rd *bufio.Reader) *Reader

func (*Reader) Buffer

func (r *Reader) Buffer(buf []byte, max int)

Buffer sets the initial buffer to use when pushing runes back onto the stack with [Next] and the maximum capacity (in bytes) for that stack. If the maximum is less than or equal to the buffer capacity, then it will never grow. (TODO right now it always grows)

Passing a nil buffer will allocate a new buffer.

Buffer panics if it is called after reading has started.

func (*Reader) Last

func (r *Reader) Last() rune

Last returns the rune most recently returned by [Next]. If [Next] has not yet been called, it panics.

func (*Reader) Next

func (r *Reader) Next() (rune, error)

Next reads a single rune from the input stream or, if runes have been pushed back, reads and removes the most recently pushed rune from the stack. Error may be io.EOF or a read error.

func (*Reader) Offset

func (r *Reader) Offset() Offset

func (*Reader) Peek

func (r *Reader) Peek() (rune, error)

Peek returns what the next call to [Next] would return, without advancing the input stream.

func (*Reader) PeekN

func (r *Reader) PeekN(dest []rune, n int) (int, error)

PeekN stores in dest what the next n calls to [Next] would return, without advancing the input stream. It returns the number of elements read, ending early in the event of EOF. The first n elements of dest are set to the special value RuneEOF unless updated with a successfully peeked value. The pushback buffer must be able to handle at least n elements, in addition to its existing contents.

func (*Reader) Push

func (r *Reader) Push(x rune)

Push adds a rune to an internal stack that runes will be read from before the input stream is read from.

If a maximum buffer capacity has been set, a panic will be raised if pushing a rune would exceed that capacity.

func (*Reader) Skip

func (r *Reader) Skip(n int) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL