go-utf8/runescanner.go

61 lines
1.0 KiB
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"
)
2023-08-18 13:37:04 +00:00
var _ io.RuneScanner = NewRuneScanner(nil)
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
}
2023-08-18 13:37:04 +00:00
func WrapRuneScanner(reader io.Reader) RuneScanner {
2022-07-19 04:04:30 +00:00
return RuneScanner{
2018-07-11 03:59:09 +00:00
reader: reader,
}
}
2023-08-18 13:37:04 +00:00
func NewRuneScanner(reader io.Reader) *RuneScanner {
var runescanner RuneScanner = WrapRuneScanner(reader)
return &runescanner
}
2018-07-11 03:59:09 +00:00
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
}