Source File
utf8.go
Belonging Package
runtime
// Copyright 2016 The Go Authors. All rights reserved.// Use of this source code is governed by a BSD-style// license that can be found in the LICENSE file.package runtime// 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.)// Code points in the surrogate range are not valid for UTF-8.const (surrogateMin = 0xD800surrogateMax = 0xDFFF)const (t1 = 0x00 // 0000 0000tx = 0x80 // 1000 0000t2 = 0xC0 // 1100 0000t3 = 0xE0 // 1110 0000t4 = 0xF0 // 1111 0000t5 = 0xF8 // 1111 1000maskx = 0x3F // 0011 1111mask2 = 0x1F // 0001 1111mask3 = 0x0F // 0000 1111mask4 = 0x07 // 0000 0111rune1Max = 1<<7 - 1rune2Max = 1<<11 - 1rune3Max = 1<<16 - 1// The default lowest and highest continuation byte.locb = 0x80 // 1000 0000hicb = 0xBF // 1011 1111)// countrunes returns the number of runes in s.func ( string) int {:= 0for range {++}return}// decoderune returns the non-ASCII rune at the start of// s[k:] and the index after the rune in s.//// decoderune assumes that caller has checked that// the to be decoded rune is a non-ASCII rune.//// If the string appears to be incomplete or decoding problems// are encountered (runeerror, k + 1) is returned to ensure// progress when decoderune is used to iterate over a string.func ( string, int) ( rune, int) {=if >= len() {return runeError, + 1}= [:]switch {case t2 <= [0] && [0] < t3:// 0080-07FF two byte sequenceif len() > 1 && (locb <= [1] && [1] <= hicb) {= rune([0]&mask2)<<6 | rune([1]&maskx)+= 2if rune1Max < {return}}case t3 <= [0] && [0] < t4:// 0800-FFFF three byte sequenceif len() > 2 && (locb <= [1] && [1] <= hicb) && (locb <= [2] && [2] <= hicb) {= rune([0]&mask3)<<12 | rune([1]&maskx)<<6 | rune([2]&maskx)+= 3if rune2Max < && !(surrogateMin <= && <= surrogateMax) {return}}case t4 <= [0] && [0] < t5:// 10000-1FFFFF four byte sequenceif len() > 3 && (locb <= [1] && [1] <= hicb) && (locb <= [2] && [2] <= hicb) && (locb <= [3] && [3] <= hicb) {= rune([0]&mask4)<<18 | rune([1]&maskx)<<12 | rune([2]&maskx)<<6 | rune([3]&maskx)+= 4if rune3Max < && <= maxRune {return}}}return runeError, + 1}// encoderune writes into p (which must be large enough) the UTF-8 encoding of the rune.// It returns the number of bytes written.func ( []byte, rune) int {// Negative values are erroneous. Making it unsigned addresses the problem.switch := uint32(); {case <= rune1Max:[0] = byte()return 1case <= rune2Max:_ = [1] // eliminate bounds checks[0] = t2 | byte(>>6)[1] = tx | byte()&maskxreturn 2case > maxRune, surrogateMin <= && <= surrogateMax:= runeErrorfallthroughcase <= rune3Max:_ = [2] // eliminate bounds checks[0] = t3 | byte(>>12)[1] = tx | byte(>>6)&maskx[2] = tx | byte()&maskxreturn 3default:_ = [3] // eliminate bounds checks[0] = t4 | byte(>>18)[1] = tx | byte(>>12)&maskx[2] = tx | byte(>>6)&maskx[3] = tx | byte()&maskxreturn 4}}