go-utf8/runescanner.go

53 lines
885 B
Go
Raw Normal View History

2022-07-18 23:36:02 +00:00
package utf8
2018-07-11 03:59:09 +00:00
import (
"io"
)
2022-07-18 23:36:02 +00:00
// A utf8.RuneScanner implements the io.RuneScanner interface by reading from an io.Reader.
2018-07-11 03:59:09 +00:00
type RuneScanner struct {
reader io.Reader
prevRune rune
prevSize int
prevErr error
peeked bool
}
2022-07-19 04:04:30 +00:00
func RuneScannerWrap(reader io.Reader) RuneScanner {
return RuneScanner{
2018-07-11 03:59:09 +00:00
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
}