go-utf8/runescanner.go

53 lines
888 B
Go
Raw Normal View History

2018-07-11 03:59:09 +00:00
package utf8s
import (
"io"
)
// A utf8s.RuneScanner implements the io.RuneScanner interface by reading from an io.Reader.
type RuneScanner struct {
reader io.Reader
prevRune rune
prevSize int
prevErr error
peeked bool
}
func NewRuneScanner(reader io.Reader) *RuneScanner {
return &RuneScanner{
reader: reader,
}
}
func (receiver *RuneScanner) ReadRune() (rune, int, error) {
if nil == receiver {
2019-09-10 15:27:52 +00:00
return RuneError, 0, errNilReceiver
2018-07-11 03:59:09 +00:00
}
reader := receiver.reader
if nil == reader {
2019-09-10 15:27:52 +00:00
return RuneError, 0, errNilReader
2018-07-11 03:59:09 +00:00
}
if receiver.peeked {
receiver.peeked = false
} else {
receiver.prevRune, receiver.prevSize, receiver.prevErr = ReadRune(reader)
}
return receiver.prevRune, receiver.prevSize, receiver.prevErr
}
func (receiver *RuneScanner) UnreadRune() error {
if nil == receiver {
return errNilReceiver
}
receiver.peeked = true
return nil
}