-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathucd.go
54 lines (39 loc) · 847 Bytes
/
ucd.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package ucd
import (
"fmt"
"strings"
"unicode/utf8"
unicodedata "github.com/aaronland/go-ucd/v15/unicodedata"
unihan "github.com/aaronland/go-ucd/v15/unihan"
)
type UCDName struct {
Char string
Hex string
Name string
}
func (u UCDName) String() string {
return u.Name
}
func Name(char string) (f UCDName) {
rune, _ := utf8.DecodeRuneInString(char)
hex := fmt.Sprintf("%04X", rune)
name, ok := unicodedata.UCD[hex]
if ok == false {
name, ok = unihan.UCDHan[hex]
}
if ok == false {
hex = fmt.Sprintf("%05X", rune)
name, ok = unihan.UCDHan[hex]
}
return UCDName{char, hex, name}
}
func NamesForString(s string) (n []UCDName) {
chars := strings.Split(s, "")
count := len(chars)
results := make([]UCDName, count)
for idx, char := range chars {
name := Name(char)
results[idx] = name
}
return results
}