github.com/go-xe2/third@v1.0.3/golang.org/x/text/unicode/runenames/runenames.go (about) 1 // Copyright 2016 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 //go:generate go run gen.go gen_bits.go 6 7 // Package runenames provides rune names from the Unicode Character Database. 8 // For example, the name for '\u0100' is "LATIN CAPITAL LETTER A WITH MACRON". 9 // 10 // See http://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt 11 package runenames 12 13 import ( 14 "sort" 15 ) 16 17 // Name returns the name for r. 18 func Name(r rune) string { 19 i := sort.Search(len(table0), func(j int) bool { 20 e := table0[j] 21 rOffset := rune(e >> shiftRuneOffset) 22 return r < rOffset 23 }) 24 if i == 0 { 25 return "" 26 } 27 28 e := table0[i-1] 29 rOffset := rune(e >> shiftRuneOffset) 30 rLength := rune(e>>shiftRuneLength) & maskRuneLength 31 if r >= rOffset+rLength { 32 return "" 33 } 34 35 if (e>>shiftDirect)&maskDirect != 0 { 36 o := int(e>>shiftDataOffset) & maskDataOffset 37 n := int(e>>shiftDataLength) & maskDataLength 38 return data[o : o+n] 39 } 40 41 base := uint32(e>>shiftDataBase) & maskDataBase 42 base <<= dataBaseUnit 43 j := rune(e>>shiftTable1Offset) & maskTable1Offset 44 j += r - rOffset 45 d0 := base + uint32(table1[j-1]) // dataOffset 46 d1 := base + uint32(table1[j-0]) // dataOffset + dataLength 47 return data[d0:d1] 48 }