首页
学习
活动
专区
工具
TVP
发布

unicode/utf8

  • import "unicode/utf8"
  • 概观
  • 索引
  • 示例

概观

打包 utf8 实现函数和常量以支持以 UTF-8 编码的文本。它包含在符文和 UTF-8 字节序列之间转换的函数。

索引

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

示例

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

打包文件

常量

编码的基本数字。

const (
        RuneError = '\uFFFD'     // the "error" Rune or "Unicode replacement character"
        RuneSelf  = 0x80         // characters below Runeself are represented as themselves in a single byte.
        MaxRune   = '\U0010FFFF' // Maximum valid Unicode code point.
        UTFMax    = 4            // maximum number of bytes of a UTF-8 encoded Unicode character.
)

func DecodeLastRuneSource

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

DecodeLastRune 解压 p 中的最后一个 UTF-8 编码,并以字节为单位返回符文及其宽度。如果p为空,则返回(RuneError, 0)。否则,如果编码无效,则返回(RuneError, 1)。对于正确的非空 UTF-8,两者都是不可能的结果。

如果编码不正确,则编码无效 UTF-8,对超出范围的符文进行编码,或者该值不是最短的 UTF-8 编码。不执行其他验证。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	b := []byte("Hello, 世界")

	for len(b) > 0 {
		r, size := utf8.DecodeLastRune(b)
		fmt.Printf("%c %v\n", r, size)

		b = b[:len(b)-size]
	}
}

func DecodeLastRuneInStringSource

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

DecodeLastRuneInString 与 DecodeLastRune 类似,但其输入是一个字符串。如果 s 为空,则返回(RuneError, 0)。否则,如果编码无效,则返回(RuneError, 1)。对于正确的非空 UTF-8,两者都是不可能的结果。

如果编码不正确,则编码无效 UTF-8,对超出范围的符文进行编码,或者该值不是最短的UTF-8 编码。不执行其他验证。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	str := "Hello, 世界"

	for len(str) > 0 {
		r, size := utf8.DecodeLastRuneInString(str)
		fmt.Printf("%c %v\n", r, size)

		str = str[:len(str)-size]
	}
}

func DecodeRuneSource

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

DecodeRune 解压 p 中的第一个 UTF-8 编码,并以字节为单位返回符文及其宽度。如果 p 为空,则返回(RuneError, 0)。否则,如果编码无效,则返回(RuneError, 1)。对于正确的非空 UTF-8 ,两者都是不可能的结果。

如果编码不正确,则编码无效 UTF-8 ,对超出范围的符文进行编码,或者该值不是最短的 UTF-8 编码。不执行其他验证。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	b := []byte("Hello, 世界")

	for len(b) > 0 {
		r, size := utf8.DecodeRune(b)
		fmt.Printf("%c %v\n", r, size)

		b = b[size:]
	}
}

func DecodeRuneInStringSource

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

DecodeRuneInString 与 DecodeRune类似,但其输入是一个字符串。如果 s 为空,则返回(RuneError, 0)。否则,如果编码无效,则返回 (RuneError, 1)。对于正确的非空 UTF-8 ,两者都是不可能的结果。

如果编码不正确,则编码无效 UTF-8,对超出范围的符文进行编码,或者该值不是最短的 UTF-8 编码。不执行其他验证。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	str := "Hello, 世界"

	for len(str) > 0 {
		r, size := utf8.DecodeRuneInString(str)
		fmt.Printf("%c %v\n", r, size)

		str = str[size:]
	}
}

func EncodeRuneSource

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

EncodeRune 写入 p (必须足够大)符文的 UTF-8 编码。它返回写入的字节数。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	r := '世'
	buf := make([]byte, 3)

	n := utf8.EncodeRune(buf, r)

	fmt.Println(buf)
	fmt.Println(n)
}

func FullRuneSource

func FullRune(p []byte) bool

FullRune 报告 p 中的字节是否以完整的符文 UTF-8 编码开始。一个无效的编码被认为是一个完整的符文,因为它将转换为一个宽度为1的错误符文。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

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

func FullRuneInStringSource

func FullRuneInString(s string) bool

FullRuneInString 与 FullRune 类似,但其输入是一个字符串。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	str := "世"
	fmt.Println(utf8.FullRuneInString(str))
	fmt.Println(utf8.FullRuneInString(str[:2]))
}

func RuneCountSource

func RuneCount(p []byte) int

RuneCount 返回 p 中的符文数。错误和短的编码被视为宽度为1个字节的单个符文。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

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

func RuneCountInStringSource

func RuneCountInString(s string) (n int)

RuneCountInString 就像 RuneCount ,但它的输入是一个字符串。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	str := "Hello, 世界"
	fmt.Println("bytes =", len(str))
	fmt.Println("runes =", utf8.RuneCountInString(str))
}

func RuneLenSource

func RuneLen(r rune) int

RuneLen 返回对符文进行编码所需的字节数。如果符文不是以 UTF-8 编码的有效值,则它返回-1。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	fmt.Println(utf8.RuneLen('a'))
	fmt.Println(utf8.RuneLen('界'))
}

func RuneStartSource

func RuneStart(b byte) bool

RuneStart 报告该字节是否可能是编码的,可能无效的符文的第一个字节。第二个和后续字节总是将前两位设置为10。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	buf := []byte("a界")
	fmt.Println(utf8.RuneStart(buf[0]))
	fmt.Println(utf8.RuneStart(buf[1]))
	fmt.Println(utf8.RuneStart(buf[2]))
}

func ValidSource

func Valid(p []byte) bool

有效报告 p 是否完全由有效的 UTF-8 编码符文组成。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	valid := []byte("Hello, 世界")
	invalid := []byte{0xff, 0xfe, 0xfd}

	fmt.Println(utf8.Valid(valid))
	fmt.Println(utf8.Valid(invalid))
}

func ValidRuneSource

func ValidRune(r rune) bool

ValidRune 报告 r 是否可以合法编码为 UTF-8 。超出范围或代理一半的代码点是非法的。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	valid := 'a'
	invalid := rune(0xfffffff)

	fmt.Println(utf8.ValidRune(valid))
	fmt.Println(utf8.ValidRune(invalid))
}

func ValidStringSource

func ValidString(s string) bool

ValidString 报告 s 是否完全由有效的 UTF-8 编码符文组成。

示例

package main

import (
	"fmt"
	"unicode/utf8"
)

func main() {
	valid := "Hello, 世界"
	invalid := string([]byte{0xff, 0xfe, 0xfd})

	fmt.Println(utf8.ValidString(valid))
	fmt.Println(utf8.ValidString(invalid))
}

扫码关注腾讯云开发者

领取腾讯云代金券