golang unicode/utf8源码分析

简介

// Package utf8 implements functions and constants to support text encoded in
// UTF-8. It includes functions to translate between runes and UTF-8 byte sequences.
package utf8

utf-8实现的功能和常量用于文章utf8编码,包含runes和utf8字节序列的转换功能.在unicode中,一个中文占两个字节,utf-8中一个中文占三个字节,golang默认的编码是utf-8编码,因此默认一个中文占三个字节,但是golang中的字符串底层实际上是一个byte数组.

package main

import (
    "fmt"
    "reflect"
    "unicode/utf8"
)

// Numbers fundamental to the encoding.
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.
)

const (
    t1 = 0x00 // 0000 0000
    tx = 0x80 // 1000 0000
    t2 = 0xC0 // 1100 0000
    t3 = 0xE0 // 1110 0000
    t4 = 0xF0 // 1111 0000
    t5 = 0xF8 // 1111 1000

    maskx = 0x3F // 0011 1111
    mask2 = 0x1F // 0001 1111
    mask3 = 0x0F // 0000 1111
    mask4 = 0x07 // 0000 0111

    rune1Max = 1<<7 - 1
    rune2Max = 1<<11 - 1
    rune3Max = 1<<16 - 1

    // The default lowest and highest continuation byte.
    locb = 0x80 // 1000 0000
    hicb = 0xBF // 1011 1111

    // These names of these constants are chosen to give nice alignment in the
    // table below. The first nibble is an index into acceptRanges or F for
    // special one-byte cases. The second nibble is the Rune length or the
    // Status for the special one-byte case.
    xx = 0xF1 // invalid: size 1
    as = 0xF0 // ASCII: size 1
    s1 = 0x02 // accept 0, size 2
    s2 = 0x13 // accept 1, size 3
    s3 = 0x03 // accept 0, size 3
    s4 = 0x23 // accept 2, size 3
    s5 = 0x34 // accept 3, size 4
    s6 = 0x04 // accept 0, size 4
    s7 = 0x44 // accept 4, size 4
)

type acceptRange struct {
    lo uint8 // lowest value for second byte.
    hi uint8 // highest value for second byte.
}

var acceptRanges = [...]acceptRange{
    0: {locb, hicb},
    1: {0xA0, hicb},
    2: {locb, 0x9F},
    3: {0x90, hicb},
    4: {locb, 0x8F},
}

// first is information about the first byte in a UTF-8 sequence.
var first = [256]uint8{
    //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F
    //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF
    xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF
    s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF
    s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF
    s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
}


// RuneCountInString is like RuneCount but its input is a string.
func RuneCountInString(s string) (n int) {
    ns := len(s) 
    fmt.Println(ns)
    for i := 0; i < ns; n++ {
        c := s[i]
        if c < RuneSelf {
            // ASCII fast path
            i++
            continue
        }
        fmt.Println("c=", c)
        x := first[c]
        fmt.Println("x=", x)
        if x == xx {
            i++ // invalid.
            continue
        }
        size := int(x & 7)
        fmt.Println("size=", size)
        if i+size > ns {
            i++ // Short or invalid.
            continue
        }
        accept := acceptRanges[x>>4]
        fmt.Println("accept: ", accept)
        if c := s[i+1]; c < accept.lo || accept.hi < c {
            size = 1
        } else if size == 2 {
        } else if c := s[i+2]; c < locb || hicb < c {
            size = 1
        } else if size == 3 {
        } else if c := s[i+3]; c < locb || hicb < c {
            size = 1
        }
        i += size
    }
    return n
}


func FullRune(p []byte) bool {
    n := len(p)
    if n == 0 {
        return false
    }
    fmt.Println("po=", p[0])
    x := first[p[0]]
    if n >= int(x&7) {
        return true // ASCII, invalid or valid.
    }
    // Must be short or invalid.
    accept := acceptRanges[x>>4]
    if n > 1 && (p[1] < accept.lo || accept.hi < p[1]) {
        return true
    } else if n > 2 && (p[2] < locb || hicb < p[2]) {
        return true
    }
    return false
}


// FullRuneInString is like FullRune but its input is a string.
func FullRuneInString(s string) bool {
    n := len(s)
    if n == 0 {
        return false
    }
    x := first[s[0]]
    fmt.Println("xxx= ", x)
    fmt.Println("x&7= ", x&7)
    if n >= int(x&7) {
        fmt.Println("--------")
        return true // ASCII, invalid, or valid.
    }
    // Must be short or invalid.
    accept := acceptRanges[x>>4]
    if n > 1 && (s[1] < accept.lo || accept.hi < s[1]) {
        fmt.Println("xxxxxx")
        return true
    } else if n > 2 && (s[2] < locb || hicb < s[2]) {
        fmt.Println("eeeee")
        return true
    }
    return false
}

func main(){
    fmt.Println(reflect.TypeOf(acceptRanges))
    str := "Hello, 钢铁侠"
    fmt.Println(FullRuneInString(`\ubbbbbbb`))
    fmt.Println(FullRune([]byte(str)))
    fmt.Println(utf8.RuneCount([]byte(str)))
    fmt.Println(str)
    for i:=0;i<len(str);i++ {
        fmt.Println(str[i])
    }
    fmt.Println([]byte(str))
    for _, s := range str {
        fmt.Println(s)
    }
    fmt.Println(reflect.TypeOf([]rune(str)[4]))
    fmt.Println([]rune(str))
    fmt.Println([]int32(str))
    fmt.Println(utf8.RuneCountInString(str))
    //fmt.Println(first[uint8(str[6])])
    //accept := acceptRanges[4]
    fmt.Println(RuneCountInString(str))
    fmt.Println(utf8.ValidString(str))
}

Output:

[5]main.acceptRange
xxx=  240
x&7=  0
--------
true
po= 72
true
10
Hello, 钢铁侠
72
101
108
108
111
44
32
233
146
162
233
147
129
228
190
160
[72 101 108 108 111 44 32 233 146 162 233 147 129 228 190 160]
72
101
108
108
111
44
32
38050
38081
20384
int32
[72 101 108 108 111 44 32 38050 38081 20384]
[72 101 108 108 111 44 32 38050 38081 20384]
10
16
c= 233
x= 3
size= 3
accept:  {128 191}
c= 233
x= 3
size= 3
accept:  {128 191}
c= 228
x= 3
size= 3
accept:  {128 191}
10
true

常量定义

RuneSelf该值的字节码值为128,在判断是否是常规的ascii码是使用。hicb字节码值为191.FF的对应的字节码为255。

// The conditions RuneError==unicode.ReplacementChar and
// MaxRune==unicode.MaxRune are verified in the tests.
// Defining them locally avoids this package depending on package unicode.

// Numbers fundamental to the encoding.
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.
)

// Code points in the surrogate range are not valid for UTF-8.
const (
    surrogateMin = 0xD800
    surrogateMax = 0xDFFF
)

const (
    t1 = 0x00 // 0000 0000
    tx = 0x80 // 1000 0000
    t2 = 0xC0 // 1100 0000
    t3 = 0xE0 // 1110 0000
    t4 = 0xF0 // 1111 0000
    t5 = 0xF8 // 1111 1000

    maskx = 0x3F // 0011 1111
    mask2 = 0x1F // 0001 1111
    mask3 = 0x0F // 0000 1111
    mask4 = 0x07 // 0000 0111

    rune1Max = 1<<7 - 1
    rune2Max = 1<<11 - 1
    rune3Max = 1<<16 - 1

    // The default lowest and highest continuation byte.
    locb = 0x80 // 1000 0000
    hicb = 0xBF // 1011 1111

    // These names of these constants are chosen to give nice alignment in the
    // table below. The first nibble is an index into acceptRanges or F for
    // special one-byte cases. The second nibble is the Rune length or the
    // Status for the special one-byte case.
    xx = 0xF1 // invalid: size 1
    as = 0xF0 // ASCII: size 1
    s1 = 0x02 // accept 0, size 2
    s2 = 0x13 // accept 1, size 3
    s3 = 0x03 // accept 0, size 3
    s4 = 0x23 // accept 2, size 3
    s5 = 0x34 // accept 3, size 4
    s6 = 0x04 // accept 0, size 4
    s7 = 0x44 // accept 4, size 4
)

// first is information about the first byte in a UTF-8 sequence.
var first = [256]uint8{
    //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x00-0x0F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x10-0x1F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x20-0x2F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x30-0x3F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x40-0x4F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x50-0x5F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x60-0x6F
    as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, as, // 0x70-0x7F
    //   1   2   3   4   5   6   7   8   9   A   B   C   D   E   F
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x80-0x8F
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0x90-0x9F
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xA0-0xAF
    xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xB0-0xBF
    xx, xx, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xC0-0xCF
    s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, s1, // 0xD0-0xDF
    s2, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s3, s4, s3, s3, // 0xE0-0xEF
    s5, s6, s6, s6, s7, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, // 0xF0-0xFF
}

// acceptRange gives the range of valid values for the second byte in a UTF-8 sequence.
// acceptRange给出了一个utf8序列中第二个字节的有效范围
type acceptRange struct {
    lo uint8 // lowest value for second byte.
    hi uint8 // highest value for second byte.
}

var acceptRanges = [...]acceptRange{
    0: {locb, hicb},
    1: {0xA0, hicb},
    2: {locb, 0x9F},
    3: {0x90, hicb},
    4: {locb, 0x8F},
}

RuneCountInString

计算字符串中的rune数量,原理:首先取出字符串的码值,然后判断是不是个小于128的,如果是小于则直接continue.rune个数++.
如果是个十六进制f1.的则是无效字符,直接continue.rune个数++,也就是说一个无效的字符也当成一个字长为1的rune.如果字符的码值在first列表中的值和7按位的结果为其字长,比如上面示例中的。其字长为三位,第一位的值为233.二进制形式为11101001;与7按位与后的值为0.从acceptRanges中取出的结果为{locb, hicb}。也就是标识ox800xbf之间的值。而结果n也就是直接size+3跳过3个字节后,rune个数++。其他函数的处理流程差不多,不再过多叙述。

// RuneCountInString is like RuneCount but its input is a string.
func RuneCountInString(s string) (n int) {
    ns := len(s)
    fmt.Println(ns)
    for i := 0; i < ns; n++ {
        c := s[i]
        if c < RuneSelf {
            // ASCII fast path
            i++
            continue
        }
        fmt.Println("c=", c)
        x := first[c]
        fmt.Println("x=", x)
        if x == xx {
            i++ // invalid.
            continue
        }
        size := int(x & 7)
        fmt.Println("size=", size)
        if i+size > ns {
            i++ // Short or invalid.
            continue
        }
        accept := acceptRanges[x>>4]
        fmt.Println("accept: ", accept)
        if c := s[i+1]; c < accept.lo || accept.hi < c {
            size = 1
        } else if size == 2 {
        } else if c := s[i+2]; c < locb || hicb < c {
            size = 1
        } else if size == 3 {
        } else if c := s[i+3]; c < locb || hicb < c {
            size = 1
        }
        i += size
    }
    return n
}

示例:

package main

import (
    "fmt"
    "unicode/utf8"
)

func main(){
    str := "Hello, 钢铁侠"
    fmt.Println(utf8.RuneCountInString(str)) // 10
}

ValidString

ValidString返回值表明参数字符串是否是一个合法的可utf8编码的字符串。

// ValidString reports whether s consists entirely of valid UTF-8-encoded runes.
func ValidString(s string) bool {
    n := len(s)
    for i := 0; i < n; {
        si := s[i]
        if si < RuneSelf {
            i++
            continue
        }
        x := first[si]
        if x == xx {
            return false // Illegal starter byte.
        }
        size := int(x & 7)
        if i+size > n {
            return false // Short or invalid.
        }
        accept := acceptRanges[x>>4]
        if c := s[i+1]; c < accept.lo || accept.hi < c {
            return false
        } else if size == 2 {
        } else if c := s[i+2]; c < locb || hicb < c {
            return false
        } else if size == 3 {
        } else if c := s[i+3]; c < locb || hicb < c {
            return false
        }
        i += size
    }
    return true
}

RuneCount

RuneCount返回参数中包含的rune数量,第一个例子中将utf8.RuneCountInString,改成该方法调用,返回的结果相同。错误的和短的被当成一个长一字节的rune.单个字符H就表示一个长度为1字节的rune.

// RuneCount returns the number of runes in p. Erroneous and short
// encodings are treated as single runes of width 1 byte.
func RuneCount(p []byte) int {
    np := len(p)
    var n int
    for i := 0; i < np; {
        n++
        c := p[i]
        if c < RuneSelf {
            // ASCII fast path
            i++
            continue
        }
        x := first[c]
        if x == xx {
            i++ // invalid.
            continue
        }
        size := int(x & 7)
        if i+size > np {
            i++ // Short or invalid.
            continue
        }
        accept := acceptRanges[x>>4]
        if c := p[i+1]; c < accept.lo || accept.hi < c {
            size = 1
        } else if size == 2 {
        } else if c := p[i+2]; c < locb || hicb < c {
            size = 1
        } else if size == 3 {
        } else if c := p[i+3]; c < locb || hicb < c {
            size = 1
        }
        i += size
    }
    return n
}

FullRune

该函数标识参数是否以一个可编码的rune开头,上面的例子中,因为字符串是以一个ascii码值在0-127内的字符开头,所以在执行
first[p[0]]时,取到的是p[0]是72,在first列表中,127之前的值都相同都为0xF0,十进制标识为240,与7按位与后值为0,所以,直接返回true.

// FullRune reports whether the bytes in p begin with a full UTF-8 encoding of a rune.
// An invalid encoding is considered a full Rune since it will convert as a width-1 error rune.
func FullRune(p []byte) bool {
    n := len(p)
    if n == 0 {
        return false
    }
    x := first[p[0]]
    if n >= int(x&7) {
        return true // ASCII, invalid or valid.
    }
    // Must be short or invalid.
    accept := acceptRanges[x>>4]
    if n > 1 && (p[1] < accept.lo || accept.hi < p[1]) {
        return true
    } else if n > 2 && (p[2] < locb || hicb < p[2]) {
        return true
    }
    return false
}

FullRuneInString

和FullRune类似,只是参数为字符串形式

// FullRuneInString is like FullRune but its input is a string.
func FullRuneInString(s string) bool {
    n := len(s)
    if n == 0 {
        return false
    }
    x := first[s[0]]
    if n >= int(x&7) {
        fmt.Println("--------")
        return true // ASCII, invalid, or valid.
    }
    // Must be short or invalid.
    accept := acceptRanges[x>>4]
    if n > 1 && (s[1] < accept.lo || accept.hi < s[1]) {
        fmt.Println("xxxxxx")
        return true
    } else if n > 2 && (s[2] < locb || hicb < s[2]) {
        fmt.Println("eeeee")
        return true
    }
    return false
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,560评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,104评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,297评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,869评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,275评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,563评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,833评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,543评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,245评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,512评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,011评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,359评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,006评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,062评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,825评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,590评论 2 273
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,501评论 2 268