【问题标题】:Check whether a Unicode code point is assigned检查是否分配了 Unicode 代码点
【发布时间】:2018-04-10 21:27:45
【问题描述】:

Go 有 unicode 包,其中包含有用的函数,例如 IsGraphicIsPrint。但缺少的一项功能是IsAssigned。当然,我可以使用其他函数编写自己的函数。但我宁愿期望标准库提供这个功能。在 Java 中,编写这个函数很容易:

boolean isAssigned(int codePoint) {
    return Character.getType(codePoint) != Character.UNASSIGNED;
}

在 Go 中没有函数 unicode.Type(rune)unicode.IsAssigned(rune)。我能找到的最接近的是:

func IsAssigned(r rune) bool {
    return unicode.IsControl(r) ||
            unicode.IsGraphic(r) ||
            unicode.IsSymbol(r)
}

但该代码认为 U+00AD(软连字符)未分配,这是错误的。

如何获得有关未分配代码点的正确信息?

【问题讨论】:

    标签: go unicode codepoint


    【解决方案1】:

    我认为您可以使用unicode.Isunicode.Categories(虽然效率不高)验证代码点是已分配还是,即

    func IsAssigned(r rune) bool {
        for _, v := range unicode.Categories {
            if unicode.Is(v, r) {
                return true
            }
        }
        return false
    }
    

    工作示例位于The Go Playground

    【讨论】:

    • 您确定所有分配的字符都属于某个类别吗? (这很明显,但也许你有一个快速的证据)
    • 实际上下面的代码显示 260+K 符文属于类别,而仅分配了 ~130K 代码点。我做错了吗? play.golang.org/p/gIcNVHa5PG6
    猜你喜欢
    • 2021-12-15
    • 1970-01-01
    • 1970-01-01
    • 2012-08-23
    • 2016-12-08
    • 1970-01-01
    • 2015-12-30
    • 1970-01-01
    • 2012-07-29
    相关资源
    最近更新 更多