go-utf8/writerune.go

78 lines
1.4 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package utf8
import (
"io"
)
// WriteRune writes a single UTF-8 encoded Unicode character and returns the number of bytes written.
//
// If writer is nil then WriteRune will return an error that matches utf8.NilWriterError.
//
// Example
//
// Here is an example usage of WriteRune:
//
// n, err := utf8.WriteRune(writer, r)
// if nil != err {
//
// switch err.(type) {
// case utf8.NilWriterError:
// //@TODO
// default:
// //TODO
// }
//
// }
func WriteRune(writer io.Writer, r rune) (int, error) {
if nil == writer {
return 0, errNilWriter
}
switch {
case 127 >= r:
var buffer [1]byte
buffer[0] = byte(r)
p := buffer[:]
return writer.Write(p)
case 0x7FF >= r:
var buffer [2]byte
buffer[0] = 0xC0 | byte((0x000007C0 & r) >> 6)
buffer[1] = 0x80 | byte( 0x0000003F & r)
p := buffer[:]
return writer.Write(p)
case 0xFFFF >= r:
var buffer [3]byte
buffer[0] = 0xE0 | byte((0x0000F000 & r) >> 12)
buffer[1] = 0x80 | byte((0x00000FC0 & r) >> 6)
buffer[2] = 0x80 | byte( 0x0000003F & r)
p := buffer[:]
return writer.Write(p)
case 0x10FFFF >= r:
var buffer [4]byte
buffer[0] = 0xF0 | byte((0x001C0000 & r) >> 18)
buffer[1] = 0x80 | byte((0x0003F000 & r) >> 12)
buffer[2] = 0x80 | byte((0x00000FC0 & r) >> 6)
buffer[3] = 0x80 | byte( 0x0000003F & r)
p := buffer[:]
return writer.Write(p)
default:
return 0, errInternalError
}
}