从符文中获取unicode类别
我正在寻找一种从Go中的从符文中获取unicode类别,unicode,go,rune,Unicode,Go,Rune,我正在寻找一种从Go中的rune获取unicode类别(RangeTable)的方法。例如,字符a映射到Ll类别。unicode的unicode包指定了所有类别(),但我看不到从给定的rune中查找类别的任何方法。我是否需要使用适当的偏移量从符文手动构建范围表?unicode”包的文档没有返回符文范围的方法,但构建一个范围并不难: func cat(r rune) (names []string) { names = make([]string, 0) for name, tab
rune
获取unicode类别(RangeTable
)的方法。例如,字符a
映射到Ll
类别。unicode的unicode
包指定了所有类别(),但我看不到从给定的rune
中查找类别的任何方法。我是否需要使用适当的偏移量从符文
手动构建范围表
?unicode”包的文档没有返回符文范围的方法,但构建一个范围并不难:
func cat(r rune) (names []string) {
names = make([]string, 0)
for name, table := range unicode.Categories {
if unicode.Is(table, r) {
names = append(names, name)
}
}
return
}
以下是基于公认答案的替代版本,返回Unicode类别:
// UnicodeCategory returns the Unicode Character Category of the given rune.
func UnicodeCategory(r rune) string {
for name, table := range unicode.Categories {
if len(name) == 2 && unicode.Is(table, r) {
return name
}
}
return "Cn"
}