golang.org/x/text@v0.14.0/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 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 https://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(entries), func(j int) bool { 20 return entries[j].startRune() > r 21 }) 22 if i == 0 { 23 return "" 24 } 25 e := entries[i-1] 26 27 offset := int(r - e.startRune()) 28 if offset >= e.numRunes() { 29 return "" 30 } 31 32 if e.direct() { 33 o := e.index() 34 n := e.len() 35 return directData[o : o+n] 36 } 37 38 start := int(index[e.index()+offset]) 39 end := int(index[e.index()+offset+1]) 40 base1 := e.base() << 16 41 base2 := base1 42 if start > end { 43 base2 += 1 << 16 44 } 45 return singleData[start+base1 : end+base2] 46 } 47 48 func (e entry) len() int { return e.base() }