From 5a9076dd18ef5b3bed58e2fe65400f4f5129cd52 Mon Sep 17 00:00:00 2001 From: Charles Iliya Krempeaux Date: Sun, 13 Nov 2022 20:49:36 -0800 Subject: [PATCH] initial commits --- writerune.go | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 writerune.go diff --git a/writerune.go b/writerune.go new file mode 100644 index 0000000..deaba05 --- /dev/null +++ b/writerune.go @@ -0,0 +1,26 @@ +package netln + +import ( + "github.com/reiver/go-utf8" + + "fmt" + "io" +) + +// writeRune deals with writing a single rune. +// +// It also makes sure that any error condition is represented as a Go error. +// +// It also wraps any errors, and provides a more appropriate error message. +func writeRune(writer io.Writer, r rune, expectedWritten int) error { + + n, err := utf8.WriteRune(writer, r) + if nil != err { + return fmt.Errorf("problem writing UTF-8 character %U: %w", r, err) + } + if expectedWritten != n { + return fmt.Errorf("problem writing UTF-8 character %U — expected to write %d bytes, but actually wrote %b bytes", r, expectedWritten, n) + } + + return nil +}