added another error check.

master
Charles Iliya Krempeaux 2018-07-02 11:26:04 -07:00
parent fc2455f7ec
commit 50a81feeba
3 changed files with 25 additions and 6 deletions

View File

@ -8,4 +8,5 @@ var (
errInternalError = errors.New("Internal Error") errInternalError = errors.New("Internal Error")
errInvalidUTF8 = internalInvalidUTF8Complainer{} errInvalidUTF8 = internalInvalidUTF8Complainer{}
errNilReader = internalNilReaderComplainer{} errNilReader = internalNilReaderComplainer{}
errNilWriter = internalNilWriterComplainer{}
) )

16
nilwriter.go 100644
View File

@ -0,0 +1,16 @@
package utf8s
type NilWriterComplainer interface {
error
NilWriterComplainer()
}
type internalNilWriterComplainer struct{}
func (complainer internalNilWriterComplainer) Error() string {
return "Nil Writer"
}
func (complainer internalNilWriterComplainer) NilWriterComplainer() {
// Nothing here.
}

View File

@ -5,8 +5,10 @@ import (
) )
// 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 WriteRune(w io.Writer, r rune) (int, error) { func WriteRune(writer io.Writer, r rune) (int, error) {
if nil == writer {
return 0, errNilWriter
}
switch { switch {
case 127 >= r: case 127 >= r:
@ -16,7 +18,7 @@ func WriteRune(w io.Writer, r rune) (int, error) {
p := buffer[:] p := buffer[:]
return w.Write(p) return writer.Write(p)
case 0x7FF >= r: case 0x7FF >= r:
var buffer [2]byte var buffer [2]byte
@ -26,7 +28,7 @@ func WriteRune(w io.Writer, r rune) (int, error) {
p := buffer[:] p := buffer[:]
return w.Write(p) return writer.Write(p)
case 0xFFFF >= r: case 0xFFFF >= r:
var buffer [3]byte var buffer [3]byte
@ -37,7 +39,7 @@ func WriteRune(w io.Writer, r rune) (int, error) {
p := buffer[:] p := buffer[:]
return w.Write(p) return writer.Write(p)
case 0x10FFFF >= r: case 0x10FFFF >= r:
var buffer [4]byte var buffer [4]byte
@ -49,7 +51,7 @@ func WriteRune(w io.Writer, r rune) (int, error) {
p := buffer[:] p := buffer[:]
return w.Write(p) return writer.Write(p)
default: default:
return 0, errInternalError return 0, errInternalError