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
|
|
|
|
|
}
|
|
|
|
|
|
2023-11-30 15:22:18 +00:00
|
|
|
|
// Buffered returns the number of bytes the UTF-8 encoding of the current buffered rune takes up, if there is a buffered rune.
|
|
|
|
|
//
|
|
|
|
|
// A buffered rune would come from someone calleding .UnreadRune().
|
|
|
|
|
//
|
|
|
|
|
// If there is not buffered rune then .Buffered() returns zero (0).
|
|
|
|
|
//
|
|
|
|
|
// So, for example, if .UnreadRune() was called for the rune 'A' (U+0041), then .Buffered() would return 1.
|
|
|
|
|
//
|
2023-11-30 15:24:56 +00:00
|
|
|
|
// Also, for example, if .UnreadRune() was called for the rune '۵' (U+06F5), then .Buffered() would return 2.
|
|
|
|
|
//
|
|
|
|
|
// And, for example, if .UnreadRune() was called for the rune '≡' (U+2261), then .Buffered() would return 3.
|
|
|
|
|
//
|
|
|
|
|
// And also, for example, if .UnreadRune() was called for the rune '🙂' (U+1F642), then .Buffered() would return 4.
|
2023-11-30 15:22:18 +00:00
|
|
|
|
//
|
|
|
|
|
// This method has been made to be semantically the same as bufio.Reader.Buffered()
|
|
|
|
|
func (receiver *RuneScanner) Buffered() int {
|
|
|
|
|
if !receiver.peeked {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return receiver.prevSize
|
|
|
|
|
}
|
|
|
|
|
|
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
|
|
|
|
|
}
|