英文:
Get unicode category from rune
问题
我正在寻找一种在Go语言中从rune
获取Unicode类别(RangeTable
)的方法。例如,字符a
映射到Ll
类别。unicode
包指定了所有的类别(http://golang.org/pkg/unicode/#pkg-variables),但我没有找到从给定的rune
查找类别的方法。我需要手动使用适当的偏移量构建RangeTable
吗?
英文:
I'm looking for a way to get the unicode category (RangeTable
) from a rune
in Go. For example, the character a
maps to the Ll
category. The unicode
package specifies all of the categories (http://golang.org/pkg/unicode/#pkg-variables), but I don't see any way to lookup the category from a given rune
. Do I need to manually construct the RangeTable
from the rune
using the appropriate offsets?
答案1
得分: 8
“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
}
英文:
The docs for the "unicode" package does not have a method that returns ranges for the rune but it is not very tricky to build one:
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
}
答案2
得分: 0
这是基于接受的答案的另一种版本,它返回给定符文的Unicode类别:
// UnicodeCategory 返回给定符文的Unicode字符类别。
func UnicodeCategory(r rune) string {
for name, table := range unicode.Categories {
if len(name) == 2 && unicode.Is(table, r) {
return name
}
}
return "Cn"
}
英文:
Here is an alternative version based on the accepted answer, that returns the Unicode Category:
// 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"
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论