From d38de8eb477f2798a57daafa564fc9b383010b0a Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Fri, 18 Aug 2023 06:37:04 -0700 Subject: [PATCH] wrap, new --- README.md | 4 ++-- runereader.go | 15 ++++++++++----- runereader_test.go | 4 ++-- runescanner.go | 10 +++++++++- runescanner_test.go | 6 +++--- runewriter.go | 16 +++++++++------- runewriter_test.go | 2 +- 7 files changed, 36 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index d8cec5b..7ce105b 100644 --- a/README.md +++ b/README.md @@ -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) // ... diff --git a/runereader.go b/runereader.go index 0e536d1..e8cf707 100644 --- a/runereader.go +++ b/runereader.go @@ -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 diff --git a/runereader_test.go b/runereader_test.go index bc2e983..5ef75f3 100644 --- a/runereader_test.go +++ b/runereader_test.go @@ -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 { diff --git a/runescanner.go b/runescanner.go index c4fe3d2..d867c54 100644 --- a/runescanner.go +++ b/runescanner.go @@ -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 diff --git a/runescanner_test.go b/runescanner_test.go index 73b3de3..3b5c360 100644 --- a/runescanner_test.go +++ b/runescanner_test.go @@ -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 { diff --git a/runewriter.go b/runewriter.go index 4ea6fb3..f24ef80 100644 --- a/runewriter.go +++ b/runewriter.go @@ -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) } diff --git a/runewriter_test.go b/runewriter_test.go index af93e7b..18df7f8 100644 --- a/runewriter_test.go +++ b/runewriter_test.go @@ -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 {