wrap, new

master
Charles Iliya Krempeaux 2023-08-18 06:37:04 -07:00
parent 243e249506
commit d38de8eb47
7 changed files with 36 additions and 21 deletions

View File

@ -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)
// ...

View File

@ -4,22 +4,27 @@ import (
"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.
type RuneReader struct {
reader io.Reader
}
func RuneReaderWrap(reader io.Reader) RuneReader {
func WrapRuneReader(reader io.Reader) RuneReader {
return RuneReader{
reader: reader,
}
}
func (receiver *RuneReader) ReadRune() (rune, int, error) {
if nil == receiver {
return 0, 0, errNilReceiver
}
func NewRuneReader(reader io.Reader) *RuneReader {
var runereader RuneReader = WrapRuneReader(reader)
return &runereader
}
func (receiver RuneReader) ReadRune() (rune, int, error) {
reader := receiver.reader
if nil == reader {
return 0, 0, errNilReader

View File

@ -295,7 +295,7 @@ func TestRuneReader(t *testing.T) {
for testNumber, test := range tests {
runeReader := RuneReaderWrap(test.Reader)
runeReader := WrapRuneReader(test.Reader)
actualRune, actualInt, err := runeReader.ReadRune()
if nil != err {
@ -563,7 +563,7 @@ func TestRuneReaders(t *testing.T) {
var runeNumber int
for {
runeReader := RuneReaderWrap(test.Reader)
runeReader := WrapRuneReader(test.Reader)
actualRune, actualInt, err := runeReader.ReadRune()
if nil != err && io.EOF != err {

View File

@ -4,6 +4,8 @@ import (
"io"
)
var _ io.RuneScanner = NewRuneScanner(nil)
// A utf8.RuneScanner implements the io.RuneScanner interface by reading from an io.Reader.
type RuneScanner struct {
reader io.Reader
@ -15,12 +17,18 @@ type RuneScanner struct {
peeked bool
}
func RuneScannerWrap(reader io.Reader) RuneScanner {
func WrapRuneScanner(reader io.Reader) RuneScanner {
return RuneScanner{
reader: reader,
}
}
func NewRuneScanner(reader io.Reader) *RuneScanner {
var runescanner RuneScanner = WrapRuneScanner(reader)
return &runescanner
}
func (receiver *RuneScanner) ReadRune() (rune, int, error) {
if nil == receiver {
return RuneError, 0, errNilReceiver

View File

@ -295,7 +295,7 @@ func TestRuneScanner(t *testing.T) {
for testNumber, test := range tests {
runeReader := RuneScannerWrap(test.Reader)
runeReader := WrapRuneScanner(test.Reader)
actualRune, actualInt, err := runeReader.ReadRune()
if nil != err {
@ -563,7 +563,7 @@ func TestRuneScanners(t *testing.T) {
var runeNumber int
for {
runeReader := RuneScannerWrap(test.Reader)
runeReader := WrapRuneScanner(test.Reader)
actualRune, actualInt, err := runeReader.ReadRune()
if nil != err && io.EOF != err {
@ -616,7 +616,7 @@ func TestRuneScannerUnread(t *testing.T) {
TestLoop: for testNumber, test := range tests {
runeScanner := RuneScannerWrap(test.Reader)
runeScanner := WrapRuneScanner(test.Reader)
var readCount int
for instructionNumber, instruction := range test.Instructions {

View File

@ -4,23 +4,25 @@ import (
"io"
)
type runeWriter interface {
WriteRune(rune) (int, error)
}
var _ runeWriter = WrapRuneWriter(nil)
// RuneWriter writes a single UTF-8 encoded Unicode characters.
type RuneWriter struct {
writer io.Writer
}
// RuneWriterWrap wraps an io.Writer and returns a RuneWriter.
func RuneWriterWrap(writer io.Writer) RuneWriter {
// WrapRuneWriter wraps an io.Writer and returns a RuneWriter.
func WrapRuneWriter(writer io.Writer) RuneWriter {
return RuneWriter{
writer: writer,
}
}
// WriteRune writes a single UTF-8 encoded Unicode character and returns the number of bytes written.
func (receiver *RuneWriter) WriteRune(r rune) (int, error) {
if nil == receiver {
return 0, errNilReceiver
}
func (receiver RuneWriter) WriteRune(r rune) (int, error) {
return WriteRune(receiver.writer, r)
}

View File

@ -58,7 +58,7 @@ func TestRuneWriter(t *testing.T) {
var buffer strings.Builder
var total int
runeWriter := utf8.RuneWriterWrap(&buffer)
runeWriter := utf8.WrapRuneWriter(&buffer)
for runeNumber, r := range test.Runes {