# package utf8
`import "unicode/utf8"`
utf8包實現了對utf-8文本的常用函數和常數的支持,包括rune和utf-8編碼byte序列之間互相翻譯的函數。
## Index
* [Constants](#pkg-constants)
* [func ValidRune(r rune) bool](#ValidRune)
* [func RuneLen(r rune) int](#RuneLen)
* [func RuneStart(b byte) bool](#RuneStart)
* [func FullRune(p []byte) bool](#FullRune)
* [func FullRuneInString(s string) bool](#FullRuneInString)
* [func Valid(p []byte) bool](#Valid)
* [func ValidString(s string) bool](#ValidString)
* [func RuneCount(p []byte) int](#RuneCount)
* [func RuneCountInString(s string) int](#RuneCountInString)
* [func EncodeRune(p []byte, r rune) int](#EncodeRune)
* [func DecodeRune(p []byte) (r rune, size int)](#DecodeRune)
* [func DecodeRuneInString(s string) (r rune, size int)](#DecodeRuneInString)
* [func DecodeLastRune(p []byte) (r rune, size int)](#DecodeLastRune)
* [func DecodeLastRuneInString(s string) (r rune, size int)](#DecodeLastRuneInString)
### Examples
* [DecodeLastRune](#example-DecodeLastRune)
* [DecodeLastRuneInString](#example-DecodeLastRuneInString)
* [DecodeRune](#example-DecodeRune)
* [DecodeRuneInString](#example-DecodeRuneInString)
* [EncodeRune](#example-EncodeRune)
* [FullRune](#example-FullRune)
* [FullRuneInString](#example-FullRuneInString)
* [RuneCount](#example-RuneCount)
* [RuneCountInString](#example-RuneCountInString)
* [RuneLen](#example-RuneLen)
* [RuneStart](#example-RuneStart)
* [Valid](#example-Valid)
* [ValidRune](#example-ValidRune)
* [ValidString](#example-ValidString)
## Constants
```
const (
RuneError = '\uFFFD' // 錯誤的Rune或"Unicode replacement character"
RuneSelf = 0x80 // 低于RunSelf的字符用代表單字節的同一值表示
MaxRune = '\U0010FFFF' // 最大的合法unicode碼值
UTFMax = 4 // 最大的utf-8編碼的unicode字符的長度
)
```
編碼的基礎常數。
## func [ValidRune](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L425 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L310 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L384 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L203 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L209 "View Source")
```
func FullRuneInString(s string) bool
```
函數類似FullRune但輸入參數是字符串。
Example
```
str := "世"
fmt.Println(utf8.FullRuneInString(str))
fmt.Println(utf8.FullRuneInString(str[:2]))
```
Output:
```
true
false
```
## func [RuneCount](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L359 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L374 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L387 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L407 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L330 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L219 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L229 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L239 "View Source")
```
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](https://github.com/golang/go/blob/master/src/unicode/utf8/utf8.go#L276 "View Source")
```
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
```
- 庫
- package achive
- package tar
- package zip
- package bufio
- package builtin
- package bytes
- package compress
- package bzip2
- package flate
- package gzip
- package lzw
- package zlib
- package container
- package heap
- package list
- package ring
- package crypto
- package aes
- package cipher
- package des
- package dsa
- package ecdsa
- package elliptic
- package hmac
- package md5
- package rand
- package rc4
- package rsa
- package sha1
- package sha256
- package sha512
- package subtle
- package tls
- package x509
- package pkix
- package database
- package sql
- package driver
- package encoding
- package ascii85
- package asn1
- package base32
- package base64
- package binary
- package csv
- package gob
- package hex
- package json
- package pem
- package xml
- package errors
- package expvar
- package flag
- package fmt
- package go
- package doc
- package format
- package parser
- package printer
- package hash
- package adler32
- package crc32
- package crc64
- package fnv
- package html
- package template
- package image
- package color
- package palette
- package draw
- package gif
- package jpeg
- package png
- package index
- package suffixarray
- package io
- package ioutil
- package log
- package syslog
- package math
- package big
- package cmplx
- package rand
- package mime
- package multipart
- package net
- package http
- package cgi
- package cookiejar
- package fcgi
- package httptest
- package httputil
- package pprof
- package mail
- package rpc
- package jsonrpc
- package smtp
- package textproto
- package url
- package os
- package exec
- package signal
- package user
- package path
- package filepath
- package reflect
- package regexp
- package runtime
- package cgo
- package debug
- package pprof
- package race
- package sort
- package strconv
- package strings
- package sync
- package atomic
- package text
- package scanner
- package tabwriter
- package template
- package time
- package unicode
- package utf16
- package utf8
- package unsafe