wrap, new
parent
243e249506
commit
d38de8eb47
|
@ -45,7 +45,7 @@ var reader io.Reader
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
var runeReader io.RuneReader = utf8.RuneReaderWrap(reader)
|
var runeReader io.RuneReader = utf8.NewRuneReader(reader)
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
|
@ -60,7 +60,7 @@ var reader io.Reader
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
var runeScanner io.RuneScanner := utf8.RuneScannerWrap(reader)
|
var runeScanner io.RuneScanner := utf8.NewRuneScanner(reader)
|
||||||
|
|
||||||
// ...
|
// ...
|
||||||
|
|
||||||
|
|
|
@ -4,22 +4,27 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ io.RuneReader = WrapRuneReader(nil)
|
||||||
|
var _ io.RuneReader = NewRuneReader(nil)
|
||||||
|
|
||||||
// A utf8.RuneReader implements the io.RuneReader interface by reading from an io.Reader.
|
// A utf8.RuneReader implements the io.RuneReader interface by reading from an io.Reader.
|
||||||
type RuneReader struct {
|
type RuneReader struct {
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
}
|
}
|
||||||
|
|
||||||
func RuneReaderWrap(reader io.Reader) RuneReader {
|
func WrapRuneReader(reader io.Reader) RuneReader {
|
||||||
return RuneReader{
|
return RuneReader{
|
||||||
reader: reader,
|
reader: reader,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (receiver *RuneReader) ReadRune() (rune, int, error) {
|
func NewRuneReader(reader io.Reader) *RuneReader {
|
||||||
if nil == receiver {
|
var runereader RuneReader = WrapRuneReader(reader)
|
||||||
return 0, 0, errNilReceiver
|
|
||||||
}
|
|
||||||
|
|
||||||
|
return &runereader
|
||||||
|
}
|
||||||
|
|
||||||
|
func (receiver RuneReader) ReadRune() (rune, int, error) {
|
||||||
reader := receiver.reader
|
reader := receiver.reader
|
||||||
if nil == reader {
|
if nil == reader {
|
||||||
return 0, 0, errNilReader
|
return 0, 0, errNilReader
|
||||||
|
|
|
@ -295,7 +295,7 @@ func TestRuneReader(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
for testNumber, test := range tests {
|
for testNumber, test := range tests {
|
||||||
runeReader := RuneReaderWrap(test.Reader)
|
runeReader := WrapRuneReader(test.Reader)
|
||||||
|
|
||||||
actualRune, actualInt, err := runeReader.ReadRune()
|
actualRune, actualInt, err := runeReader.ReadRune()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -563,7 +563,7 @@ func TestRuneReaders(t *testing.T) {
|
||||||
|
|
||||||
var runeNumber int
|
var runeNumber int
|
||||||
for {
|
for {
|
||||||
runeReader := RuneReaderWrap(test.Reader)
|
runeReader := WrapRuneReader(test.Reader)
|
||||||
|
|
||||||
actualRune, actualInt, err := runeReader.ReadRune()
|
actualRune, actualInt, err := runeReader.ReadRune()
|
||||||
if nil != err && io.EOF != err {
|
if nil != err && io.EOF != err {
|
||||||
|
|
|
@ -4,6 +4,8 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var _ io.RuneScanner = NewRuneScanner(nil)
|
||||||
|
|
||||||
// A utf8.RuneScanner implements the io.RuneScanner interface by reading from an io.Reader.
|
// A utf8.RuneScanner implements the io.RuneScanner interface by reading from an io.Reader.
|
||||||
type RuneScanner struct {
|
type RuneScanner struct {
|
||||||
reader io.Reader
|
reader io.Reader
|
||||||
|
@ -15,12 +17,18 @@ type RuneScanner struct {
|
||||||
peeked bool
|
peeked bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func RuneScannerWrap(reader io.Reader) RuneScanner {
|
func WrapRuneScanner(reader io.Reader) RuneScanner {
|
||||||
return RuneScanner{
|
return RuneScanner{
|
||||||
reader: reader,
|
reader: reader,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewRuneScanner(reader io.Reader) *RuneScanner {
|
||||||
|
var runescanner RuneScanner = WrapRuneScanner(reader)
|
||||||
|
|
||||||
|
return &runescanner
|
||||||
|
}
|
||||||
|
|
||||||
func (receiver *RuneScanner) ReadRune() (rune, int, error) {
|
func (receiver *RuneScanner) ReadRune() (rune, int, error) {
|
||||||
if nil == receiver {
|
if nil == receiver {
|
||||||
return RuneError, 0, errNilReceiver
|
return RuneError, 0, errNilReceiver
|
||||||
|
|
|
@ -295,7 +295,7 @@ func TestRuneScanner(t *testing.T) {
|
||||||
|
|
||||||
|
|
||||||
for testNumber, test := range tests {
|
for testNumber, test := range tests {
|
||||||
runeReader := RuneScannerWrap(test.Reader)
|
runeReader := WrapRuneScanner(test.Reader)
|
||||||
|
|
||||||
actualRune, actualInt, err := runeReader.ReadRune()
|
actualRune, actualInt, err := runeReader.ReadRune()
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -563,7 +563,7 @@ func TestRuneScanners(t *testing.T) {
|
||||||
|
|
||||||
var runeNumber int
|
var runeNumber int
|
||||||
for {
|
for {
|
||||||
runeReader := RuneScannerWrap(test.Reader)
|
runeReader := WrapRuneScanner(test.Reader)
|
||||||
|
|
||||||
actualRune, actualInt, err := runeReader.ReadRune()
|
actualRune, actualInt, err := runeReader.ReadRune()
|
||||||
if nil != err && io.EOF != err {
|
if nil != err && io.EOF != err {
|
||||||
|
@ -616,7 +616,7 @@ func TestRuneScannerUnread(t *testing.T) {
|
||||||
|
|
||||||
TestLoop: for testNumber, test := range tests {
|
TestLoop: for testNumber, test := range tests {
|
||||||
|
|
||||||
runeScanner := RuneScannerWrap(test.Reader)
|
runeScanner := WrapRuneScanner(test.Reader)
|
||||||
|
|
||||||
var readCount int
|
var readCount int
|
||||||
for instructionNumber, instruction := range test.Instructions {
|
for instructionNumber, instruction := range test.Instructions {
|
||||||
|
|
|
@ -4,23 +4,25 @@ import (
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type runeWriter interface {
|
||||||
|
WriteRune(rune) (int, error)
|
||||||
|
}
|
||||||
|
|
||||||
|
var _ runeWriter = WrapRuneWriter(nil)
|
||||||
|
|
||||||
// RuneWriter writes a single UTF-8 encoded Unicode characters.
|
// RuneWriter writes a single UTF-8 encoded Unicode characters.
|
||||||
type RuneWriter struct {
|
type RuneWriter struct {
|
||||||
writer io.Writer
|
writer io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
// RuneWriterWrap wraps an io.Writer and returns a RuneWriter.
|
// WrapRuneWriter wraps an io.Writer and returns a RuneWriter.
|
||||||
func RuneWriterWrap(writer io.Writer) RuneWriter {
|
func WrapRuneWriter(writer io.Writer) RuneWriter {
|
||||||
return RuneWriter{
|
return RuneWriter{
|
||||||
writer: writer,
|
writer: writer,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteRune writes a single UTF-8 encoded Unicode character and returns the number of bytes written.
|
// WriteRune writes a single UTF-8 encoded Unicode character and returns the number of bytes written.
|
||||||
func (receiver *RuneWriter) WriteRune(r rune) (int, error) {
|
func (receiver RuneWriter) WriteRune(r rune) (int, error) {
|
||||||
if nil == receiver {
|
|
||||||
return 0, errNilReceiver
|
|
||||||
}
|
|
||||||
|
|
||||||
return WriteRune(receiver.writer, r)
|
return WriteRune(receiver.writer, r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -58,7 +58,7 @@ func TestRuneWriter(t *testing.T) {
|
||||||
var buffer strings.Builder
|
var buffer strings.Builder
|
||||||
var total int
|
var total int
|
||||||
|
|
||||||
runeWriter := utf8.RuneWriterWrap(&buffer)
|
runeWriter := utf8.WrapRuneWriter(&buffer)
|
||||||
|
|
||||||
for runeNumber, r := range test.Runes {
|
for runeNumber, r := range test.Runes {
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue