2022-07-18 23:36:02 +00:00
|
|
|
|
package utf8
|
2018-07-02 08:27:49 +00:00
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"io"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// ReadRune reads a single UTF-8 encoded Unicode character from an io.Reader,
|
2024-03-31 16:34:54 +00:00
|
|
|
|
// 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.”
|
2018-07-02 08:36:39 +00:00
|
|
|
|
//
|
2023-08-18 12:57:56 +00:00
|
|
|
|
// If ‘reader’ is nil then ReaderRune will return an error that matches utf8.NilReaderError.
|
2022-07-19 05:12:14 +00:00
|
|
|
|
//
|
|
|
|
|
// Example
|
|
|
|
|
//
|
|
|
|
|
// Here is an example usage of ReadRune:
|
|
|
|
|
//
|
|
|
|
|
// r, n, err := utf8.ReadRune(reader)
|
|
|
|
|
// if nil != err {
|
|
|
|
|
//
|
|
|
|
|
// switch err.(type) {
|
2023-08-18 12:57:56 +00:00
|
|
|
|
// case utf8.NilReaderError:
|
2022-07-19 05:12:14 +00:00
|
|
|
|
// //@TODO
|
2023-08-18 12:57:56 +00:00
|
|
|
|
// case utf8.InvalidUTF8Error:
|
2022-07-19 05:12:14 +00:00
|
|
|
|
// //@TODO
|
|
|
|
|
// default:
|
|
|
|
|
// //TODO
|
|
|
|
|
// }
|
|
|
|
|
// }
|
2022-07-19 05:13:30 +00:00
|
|
|
|
// if utf8.RuneError == r {
|
|
|
|
|
// //@TODO
|
|
|
|
|
// }
|
2022-07-19 05:12:14 +00:00
|
|
|
|
//
|
|
|
|
|
// Number Of Bytes
|
|
|
|
|
//
|
2018-07-02 08:36:39 +00:00
|
|
|
|
// 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.
|
2018-07-02 08:27:49 +00:00
|
|
|
|
func ReadRune(reader io.Reader) (rune, int, error) {
|
|
|
|
|
if nil == reader {
|
|
|
|
|
return 0, 0, errNilReader
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var count int
|
|
|
|
|
|
|
|
|
|
var b0 byte
|
|
|
|
|
{
|
|
|
|
|
var buffer [1]byte
|
|
|
|
|
var p []byte = buffer[:]
|
|
|
|
|
|
|
|
|
|
n, err := reader.Read(p)
|
|
|
|
|
count += n
|
|
|
|
|
if nil != err {
|
2024-03-31 16:34:54 +00:00
|
|
|
|
return 0, 0, err
|
2018-07-02 08:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
if 1 != n {
|
2024-03-31 16:34:54 +00:00
|
|
|
|
return 0, 0, errInternalError
|
2018-07-02 08:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
b0 = buffer[0]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if 127 >= b0 {
|
|
|
|
|
return rune(b0), count, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var more int
|
|
|
|
|
{
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
|
|
// 110x,xxxx 110x,xxxx
|
|
|
|
|
// 0b1100,0000 == (0b1110,0000 & b0)
|
|
|
|
|
case 0xC0 == (0xE0 & b0):
|
|
|
|
|
more = 2-1
|
|
|
|
|
|
|
|
|
|
// 1110,xxxx 1110,xxxx
|
|
|
|
|
// 0b1110,0000 == (0b1111,0000 & b0)
|
|
|
|
|
case 0xE0 == (0xF0 & b0):
|
|
|
|
|
more = 3-1
|
|
|
|
|
|
|
|
|
|
// 1111,0xxx 1111,0xxx
|
|
|
|
|
// 0b1111,0000 == (0b1111,1000 & b0)
|
|
|
|
|
case 0xF0 == (0xF8 & b0):
|
|
|
|
|
more = 4-1
|
|
|
|
|
|
|
|
|
|
// 1111,10xx 1111,10xx
|
|
|
|
|
// 0b1111,1000 == (0b1111,1100 & b0)
|
|
|
|
|
case 0xF8 == (0xFC & b0):
|
|
|
|
|
more = 5-1
|
|
|
|
|
|
|
|
|
|
// 1111,110x 1111,110x
|
|
|
|
|
// 0b1111,1100 == (0b1111,1110 & b0)
|
|
|
|
|
case 0xFC == (0xFE & b0):
|
|
|
|
|
more = 6-1
|
|
|
|
|
|
|
|
|
|
// 1111,1111 1111,1111
|
|
|
|
|
// 0b1111,1110 == (0b1111,1111 & b0)
|
|
|
|
|
case 0xFE == (0xFF & b0):
|
|
|
|
|
more = 7-1
|
|
|
|
|
|
|
|
|
|
default:
|
2024-03-31 16:34:54 +00:00
|
|
|
|
return 0, 0, errInternalError
|
2018-07-02 08:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
var bs [6]byte
|
|
|
|
|
{
|
|
|
|
|
p := bs[:more]
|
|
|
|
|
|
|
|
|
|
n, err := reader.Read(p)
|
|
|
|
|
count += n
|
|
|
|
|
if nil != err {
|
2024-03-31 16:34:54 +00:00
|
|
|
|
return 0, 0, err
|
2018-07-02 08:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
if more != n {
|
2024-03-31 16:34:54 +00:00
|
|
|
|
return 0, 0, errInternalError
|
2018-07-02 08:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var r rune
|
|
|
|
|
{
|
|
|
|
|
var b byte
|
|
|
|
|
|
|
|
|
|
switch {
|
|
|
|
|
|
|
|
|
|
// 110x,xxxx 110x,xxxx
|
|
|
|
|
// 0b1100,0000 == (0b1110,0000 & b0)
|
|
|
|
|
case 0xC0 == (0xE0 & b0):
|
|
|
|
|
b = (0xE0^0xFF) & b0
|
|
|
|
|
|
|
|
|
|
// 1110,xxxx 1110,xxxx
|
|
|
|
|
// 0b1110,0000 == (0b1111,0000 & b0)
|
|
|
|
|
case 0xE0 == (0xF0 & b0):
|
|
|
|
|
b = (0xF0^0xFF) & b0
|
|
|
|
|
|
|
|
|
|
// 1111,0xxx 1111,0xxx
|
|
|
|
|
// 0b1111,0000 == (0b1111,1000 & b0)
|
|
|
|
|
case 0xF0 == (0xF8 & b0):
|
|
|
|
|
b = (0xF8^0xFF) & b0
|
|
|
|
|
|
|
|
|
|
// 1111,10xx 1111,10xx
|
|
|
|
|
// 0b1111,1000 == (0b1111,1100 & b0)
|
|
|
|
|
case 0xF8 == (0xFC & b0):
|
|
|
|
|
b = (0xFC^0xFF) & b0
|
|
|
|
|
|
|
|
|
|
// 1111,110x 1111,110x
|
|
|
|
|
// 0b1111,1100 == (0b1111,1110 & b0)
|
|
|
|
|
case 0xFC == (0xFE & b0):
|
|
|
|
|
b = (0xFE^0xFF) & b0
|
|
|
|
|
|
|
|
|
|
// 1111,1111 1111,1111
|
|
|
|
|
// 0b1111,1110 == (0b1111,1111 & b0)
|
|
|
|
|
case 0xFE == (0xFF & b0):
|
|
|
|
|
//b := (0xFF^0xFF) & b0
|
|
|
|
|
|
|
|
|
|
default:
|
2024-03-31 16:34:54 +00:00
|
|
|
|
return 0, 0, errInternalError
|
2018-07-02 08:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
r = rune(b)
|
|
|
|
|
r <<= 6
|
|
|
|
|
|
|
|
|
|
for i:=0; i<more; i++ {
|
|
|
|
|
|
|
|
|
|
bsi := bs[i]
|
|
|
|
|
|
|
|
|
|
// if 0b1000,0000 != (0b0b1100,0000 & bsi) {
|
|
|
|
|
if 0x80 != (0xC0 & bsi) {
|
2024-03-31 16:34:54 +00:00
|
|
|
|
return 0, 0, errInvalidUTF8
|
2018-07-02 08:27:49 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// b := 0b0011,1111 & bsi
|
|
|
|
|
b := 0x3F & bsi
|
|
|
|
|
|
|
|
|
|
r2 := rune(b)
|
|
|
|
|
|
|
|
|
|
r |= r2
|
|
|
|
|
if i < (more-1) {
|
|
|
|
|
r <<= 6
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return r, count, nil
|
|
|
|
|
}
|