解析GO中的Unicode Digits

发布于 2025-02-02 15:57:05 字数 849 浏览 2 评论 0 原文

使用 unicode.isdigit() 给定符文是一个数字,但是我如何找出那是哪个数字?

atoi和parseint来自 strconv 不会解析它。

isDigit在所有这些codepoints 中但是我从中弄清楚了任何东西。许多数字范围以其0数字在0结束的代码点开始,但并非全部,所以我不能只是 char& 0xf

我唯一的想法是,是否有一种方法可以访问符文的Unicode名称,或者您是否可以访问属性。每个数字Unicode字符(甚至分数)似乎都具有与之相关的平原ASCII编号作为属性,但我似乎找不到一种访问该信息或名称的方法(例如,所有Unicode数字都以“ Digit Zero”结尾为“ Digit Zero”)。我在标准库外面寻找/建筑物吗?

Other answers mention using unicode.IsDigit() to check if a given rune is a digit or not, but how do I figure out which digit it is then?

Atoi and ParseInt from strconv won't parse it.

IsDigit checks a table with all of these codepoints in it, but I can't figure out anything from that. Many of the number ranges start with their 0 digit at a codepoint ending in 0, but not all of them so I can't just char & 0xF.

My only other thoughts is whether there's a way to either access the unicode name of a rune, or whether you can access properties. Every numeric unicode character (even fractions) seems to have a plain ASCII number associated with it behind the scenes as a property, but I can't seem to find a way to access either that information or the name (all unicode digits have names ending in "DIGIT ZERO" for example) anywhere. Am I looking/building outside of the standard library on this one?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

十级心震 2025-02-09 15:57:05

使用 runeNames package 软件包以根据名称识别数字。

这不是一个星德库软件包,但它是 golang.org/x/

这些软件包是GO项目的一部分,但在主GO树之外。它们是根据宽松的兼容性要求开发的,而不是GO核心。用“ Go Get”安装它们。

import (
    "golang.org/x/text/unicode/runenames"

    "fmt"
    "strings"
)

func whatDigit(digit rune) int {
    var name = runenames.Name(digit)
    switch {
    case strings.Contains(name, "DIGIT ZERO"):
        return 0
    case strings.Contains(name, "DIGIT ONE"):
        return 1
    case strings.Contains(name, "DIGIT TWO"):
        return 2
    case strings.Contains(name, "DIGIT THREE"):
        return 3
    case strings.Contains(name, "DIGIT FOUR"):
        return 4
    case strings.Contains(name, "DIGIT FIVE"):
        return 5
    case strings.Contains(name, "DIGIT SIX"):
        return 6
    case strings.Contains(name, "DIGIT SEVEN"):
        return 7
    case strings.Contains(name, "DIGIT EIGHT"):
        return 8
    case strings.Contains(name, "DIGIT NINE"):
        return 9
    default:
        return -1
    }

    return 0
}

该软件包的确提到了一个文档似乎为每个字符提供了更多信息,包括指定字符在Pline Ascii中的数字,但是,此软件包仅提供名称。只需查看文档,名称似乎遵循 WhatDigit 函数中所示的模式。

Using the runenames package to identify a digit based on the name.

This isn't a stardard library package, but it is part of golang.org/x/

These packages are part of the Go Project but outside the main Go tree. They are developed under looser compatibility requirements than the Go core. Install them with "go get".

import (
    "golang.org/x/text/unicode/runenames"

    "fmt"
    "strings"
)

func whatDigit(digit rune) int {
    var name = runenames.Name(digit)
    switch {
    case strings.Contains(name, "DIGIT ZERO"):
        return 0
    case strings.Contains(name, "DIGIT ONE"):
        return 1
    case strings.Contains(name, "DIGIT TWO"):
        return 2
    case strings.Contains(name, "DIGIT THREE"):
        return 3
    case strings.Contains(name, "DIGIT FOUR"):
        return 4
    case strings.Contains(name, "DIGIT FIVE"):
        return 5
    case strings.Contains(name, "DIGIT SIX"):
        return 6
    case strings.Contains(name, "DIGIT SEVEN"):
        return 7
    case strings.Contains(name, "DIGIT EIGHT"):
        return 8
    case strings.Contains(name, "DIGIT NINE"):
        return 9
    default:
        return -1
    }

    return 0
}

The package does mention a document https://www.unicode.org/Public/UCD/latest/ucd/UnicodeData.txt which seems to have further information for each character, including specifying which digit the character is in plain ASCII, however, this package only provides the name. Just looking through the document, the names seem to follow the pattern as shown in the whatDigit function.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文