Documentation ¶
Index ¶
Constants ¶
const (
RuneError = '\uFFFD' // Unicode Replacement Character (U+FFFD).
)
Variables ¶
This section is empty.
Functions ¶
func FormatBinary ¶
FormatBinary returns a representation of a rune as a sequence of bytes, given in binary format.
Example
utf8.FormatBinary('۵') // Outputs: // <<0b11011011 ; 0b10110101>>
func ReadRune ¶
ReadRune reads a single UTF-8 encoded Unicode character from an io.Reader, and returns the Unicode character (as a Go rune) and the size of the rune.
Note that it returns the size-of-the-rune rather than the number-of-bytes-read. This is to match what is described in the Go built-in package:
“ReadRune reads a single encoded Unicode character and returns the rune and its size in bytes. If no character is available, err will be set.”
If ‘reader’ is nil then ReaderRune will return an error that matches utf8.NilReaderError.
Example ¶
Here is an example usage of ReadRune:
r, n, err := utf8.ReadRune(reader) if nil != err { switch err.(type) { case utf8.NilReaderError: //@TODO case utf8.InvalidUTF8Error: //@TODO default: //TODO } } if utf8.RuneError == r { //@TODO }
Number Of Bytes ¶
Note that a single UTF-8 encoded Unicode character could be more than one byte.
For example, the Unicode "≡" (IDENTICAL TO) character gets encoded using 3 bytes under UTF-8.
func RuneLength ¶
RuneLength returns the number of bytes in a UTF-8 encoding of this Unicode code point.
Example
length := utf8.RuneLength('A') // length == 1
Example
length := utf8.RuneLength('r') // length == 1
Example
length := utf8.RuneLength('¡') // length == 2
Example
length := utf8.RuneLength('۵') // length == 2
func WriteRune ¶
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 } }
Types ¶
type InvalidUTF8Error ¶
type InvalidUTF8Error interface { error InvalidUTF8Error() }
InvalidUTF8Error is a type of error that could be returned by the utf8.ReadRune() function, by the utf8.RuneReader.ReadRune() method, and by the utf8.RuneScanner.ReadRune() method.
Here is how one might use this type:
r, n, err := utf8.ReadRune(reader) if nil != err { switch { case utf8.InvalidUTF8Error: //@TODO default: //@TODO } }
type NilReaderError ¶
type NilReaderError interface { error NilReaderError() }
type NilWriterError ¶
type NilWriterError interface { error NilWriterError() }
type RuneReader ¶
type RuneReader struct {
// contains filtered or unexported fields
}
A utf8.RuneReader implements the io.RuneReader interface by reading from an io.Reader.
func NewRuneReader ¶
func NewRuneReader(reader io.Reader) *RuneReader
func WrapRuneReader ¶
func WrapRuneReader(reader io.Reader) RuneReader
type RuneScanner ¶
type RuneScanner struct {
// contains filtered or unexported fields
}
A utf8.RuneScanner implements the io.RuneScanner interface by reading from an io.Reader.
func NewRuneScanner ¶
func NewRuneScanner(reader io.Reader) *RuneScanner
func WrapRuneScanner ¶
func WrapRuneScanner(reader io.Reader) RuneScanner
func (*RuneScanner) Buffered ¶
func (receiver *RuneScanner) Buffered() int
Buffered returns the number of bytes the UTF-8 encoding of the current buffered rune takes up, if there is a buffered rune.
A buffered rune would come from someone calleding .UnreadRune().
If there is not buffered rune then .Buffered() returns zero (0).
So, for example, if .UnreadRune() was called for the rune 'A' (U+0041), then .Buffered() would return 1.
Also, for example, if .UnreadRune() was called for the rune '۵' (U+06F5), then .Buffered() would return 2.
And, for example, if .UnreadRune() was called for the rune '≡' (U+2261), then .Buffered() would return 3.
And also, for example, if .UnreadRune() was called for the rune '🙂' (U+1F642), then .Buffered() would return 4.
This method has been made to be semantically the same as bufio.Reader.Buffered()
func (*RuneScanner) UnreadRune ¶
func (receiver *RuneScanner) UnreadRune() error
type RuneWriter ¶
type RuneWriter struct {
// contains filtered or unexported fields
}
RuneWriter writes a single UTF-8 encoded Unicode characters.
func WrapRuneWriter ¶
func WrapRuneWriter(writer io.Writer) RuneWriter
WrapRuneWriter wraps an io.Writer and returns a RuneWriter.