Go 语言

Go 语言教程 Go 语言环境安装 Go 语言结构 Go 语言基础语法 Go 语言数据类型 Go 语言变量 Go 语言常量 Go 语言运算符 Go 语言条件语句 Go 语言 if 语句 Go 语言 if...else 语句 Go 语言 if 语句嵌套 Go 语言 switch 语句 Go 语言 select 语句 Go 语言循环语句 Go 语言 for 循环 Go 语言循环嵌套 Go 语言 break 语句 Go 语言 continue 语句 Go 语言 goto 语句 Go 语言函数 Go 语言函数值传递值 Go 语言函数引用传递值 Go 语言函数作为值 Go 语言函数闭包 Go 语言函数方法 Go 语言变量作用域 Go 语言数组 Go 语言多维数组 Go 语言向函数传递数组 Go 语言指针 Go 语言指针数组 Go 语言指向指针的指针 Go 语言指针作为函数参数 Go 语言结构体 Go 语言切片(Slice) Go 语言范围(Range) Go 语言Map(集合) Go 语言递归函数 Go 语言类型转换 Go 语言接口 Go 错误处理 Go 语言开发工具Go 语言标准库

Go 语言标准库


package utf8

import "unicode/utf8"

utf8包实现了对utf-8文本的常用函数和常数的支持,包括rune和utf-8编码byte序列之间互相翻译的函数。

Go语言标准库 >>


  • Constants
  • func ValidRune(r rune) bool
  • func RuneLen(r rune) int
  • func RuneStart(b byte) bool
  • func FullRune(p []byte) bool
  • func FullRuneInString(s string) bool
  • func Valid(p []byte) bool
  • func ValidString(s string) bool
  • func RuneCount(p []byte) int
  • func RuneCountInString(s string) int
  • func EncodeRune(p []byte, r rune) int
  • func DecodeRune(p []byte) (r rune, size int)
  • func DecodeRuneInString(s string) (r rune, size int)
  • func DecodeLastRune(p []byte) (r rune, size int)
  • func DecodeLastRuneInString(s string) (r rune, size int)
  • Examples

    Go语言标准库 >>


  • DecodeLastRune
  • DecodeLastRuneInString
  • DecodeRune
  • DecodeRuneInString
  • EncodeRune
  • FullRune
  • FullRuneInString
  • RuneCount
  • RuneCountInString
  • RuneLen
  • RuneStart
  • Valid
  • ValidRune
  • ValidString
  • Constants

    const (
        RuneError = '\uFFFD'     // 错误的Rune或"Unicode replacement character"
        RuneSelf  = 0x80         // 低于RunSelf的字符用代表单字节的同一值表示
        MaxRune   = '\U0010FFFF' // 最大的合法unicode码值
        UTFMax    = 4            // 最大的utf-8编码的unicode字符的长度
    )

    编码的基础常数。

    func ValidRune

    func ValidRune(r rune) bool

    判断r是否可以编码为合法的utf-8序列。

    Example
    valid := 'a'
    invalid := rune(0xfffffff)
    fmt.Println(utf8.ValidRune(valid))
    fmt.Println(utf8.ValidRune(invalid))

    Output:

    true
    false
    

    func RuneLen

    func RuneLen(r rune) int

    返回r编码后的字节数。如果r不是一个合法的可编码为utf-8序列的值,会返回-1。

    Example
    fmt.Println(utf8.RuneLen('a'))
    fmt.Println(utf8.RuneLen('界'))

    Output:

    1
    3
    

    func RuneStart

    func RuneStart(b byte) bool

    报告字节b是否可以作为某个rune编码后的第一个字节。第二个即之后的字节总是将左端两个字位设为10。

    Example
    buf := []byte("a界")
    fmt.Println(utf8.RuneStart(buf[0]))
    fmt.Println(utf8.RuneStart(buf[1]))
    fmt.Println(utf8.RuneStart(buf[2]))

    Output:

    true
    true
    false
    

    func FullRune

    func FullRune(p []byte) bool

    报告切片p是否以一个码值的完整utf-8编码开始。不合法的编码因为会被转换为宽度1的错误码值而被视为完整的。

    Example
    buf := []byte{228, 184, 150} // 世
    fmt.Println(utf8.FullRune(buf))
    fmt.Println(utf8.FullRune(buf[:2]))

    Output:

    true
    false
    

    func FullRuneInString

    func FullRuneInString(s string) bool

    函数类似FullRune但输入参数是字符串。

    Example
    str := "世"
    fmt.Println(utf8.FullRuneInString(str))
    fmt.Println(utf8.FullRuneInString(str[:2]))

    Output:

    true
    false
    

    func RuneCount

    func RuneCount(p []byte) int

    返回p中的utf-8编码的码值的个数。错误或者不完整的编码会被视为宽度1字节的单个码值。

    Example
    buf := []byte("Hello, 世界")
    fmt.Println("bytes =", len(buf))
    fmt.Println("runes =", utf8.RuneCount(buf))

    Output:

    bytes = 13
    runes = 9
    

    func RuneCountInString

    func RuneCountInString(s string) (n int)

    函数类似RuneCount但输入参数是一个字符串。

    Example
    str := "Hello, 世界"
    fmt.Println("bytes =", len(str))
    fmt.Println("runes =", utf8.RuneCountInString(str))

    Output:

    bytes = 13
    runes = 9
    

    func Valid

    func Valid(p []byte) bool

    返回切片p是否包含完整且合法的utf-8编码序列。

    Example
    valid := []byte("Hello, 世界")
    invalid := []byte{0xff, 0xfe, 0xfd}
    fmt.Println(utf8.Valid(valid))
    fmt.Println(utf8.Valid(invalid))

    Output:

    true
    false
    

    func ValidString

    func ValidString(s string) bool

    报告s是否包含完整且合法的utf-8编码序列。

    Example
    valid := "Hello, 世界"
    invalid := string([]byte{0xff, 0xfe, 0xfd})
    fmt.Println(utf8.ValidString(valid))
    fmt.Println(utf8.ValidString(invalid))

    Output:

    true
    false
    

    func EncodeRune

    func EncodeRune(p []byte, r rune) int

    EncodeRune将r的utf-8编码序列写入p(p必须有足够的长度),并返回写入的字节数。

    Example
    r := '世'
    buf := make([]byte, 3)
    n := utf8.EncodeRune(buf, r)
    fmt.Println(buf)
    fmt.Println(n)

    Output:

    [228 184 150]
    3
    

    func DecodeRune

    func DecodeRune(p []byte) (r rune, size int)

    函数解码p开始位置的第一个utf-8编码的码值,返回该码值和编码的字节数。如果编码不合法,会返回(RuneError, 1)。该返回值在正确的utf-8编码情况下是不可能返回的。

    如果一个utf-8编码序列格式不正确,或者编码的码值超出utf-8合法码值的范围,或者不是该码值的最短编码,该编码序列即是不合法的。函数不会执行其他的验证。

    Example
    b := []byte("Hello, 世界")
    for len(b) > 0 {
        r, size := utf8.DecodeRune(b)
        fmt.Printf("%c %v\n", r, size)
        b = b[size:]
    }

    Output:

    H 1
    e 1
    l 1
    l 1
    o 1
    , 1
      1
    世 3
    界 3
    

    func DecodeRuneInString

    func DecodeRuneInString(s string) (r rune, size int)

    函数类似DecodeRune但输入参数是字符串。

    Example
    str := "Hello, 世界"
    for len(str) > 0 {
        r, size := utf8.DecodeRuneInString(str)
        fmt.Printf("%c %v\n", r, size)
        str = str[size:]
    }

    Output:

    H 1
    e 1
    l 1
    l 1
    o 1
    , 1
      1
    世 3
    界 3
    

    func DecodeLastRune

    func DecodeLastRune(p []byte) (r rune, size int)

    函数解码p中最后一个utf-8编码序列,返回该码值和编码序列的长度。

    Example
    b := []byte("Hello, 世界")
    for len(b) > 0 {
        r, size := utf8.DecodeLastRune(b)
        fmt.Printf("%c %v\n", r, size)
        b = b[:len(b)-size]
    }

    Output:

    界 3
    世 3
      1
    , 1
    o 1
    l 1
    l 1
    e 1
    H 1
    

    func DecodeLastRuneInString

    func DecodeLastRuneInString(s string) (r rune, size int)

    函数类似DecodeLastRune但输入参数是字符串。

    Example
    str := "Hello, 世界"
    for len(str) > 0 {
        r, size := utf8.DecodeLastRuneInString(str)
        fmt.Printf("%c %v\n", r, size)
        str = str[:len(str)-size]
    }

    Output:

    界 3
    世 3
      1
    , 1
    o 1
    l 1
    l 1
    e 1
    H 1