go - 检查是否分配了 Unicode 代码点

标签 go unicode codepoint

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(软连字符)未分配,这是错误的。

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

最佳答案

我认为您可以使用 unicode.Is 验证代码点已分配unicode.Categories (尽管效率不高),即

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

工作示例位于 The Go Playground .

关于go - 检查是否分配了 Unicode 代码点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49763103/

相关文章:

pointers - 为什么在本 golang 教程中将指针用于结构实例?

go - 将 `errors.Is` 和 `errors.As` 应用于自定义结构错误

c# - 在 C# 中使用反斜杠符号转义 unicode 字符串

c++ - 跨平台 C++ : Use the native string encoding or standardise across platforms?

unicode - Unicode 是否有定义的最大代码点数?

go - 如何使用godoc正确生成文档?

go - 是否有任何系统调用可以在 golang 中捕获 ctrl+v 或 shift+insert?

python - 在 Python 中从带有重音符号的字符串中删除所有非字母字符

perl - 如何将 Unicode 代码点 (\uXXXX) 转换为 Perl 中的字符?