IsDigit和IsNumber在Go语言中的Unicode中的区别

huangapple go评论79阅读模式
英文:

Differences between IsDigit and IsNumber in unicode in Go

问题

似乎在unicode包中,IsDigit和IsNumber函数的行为没有区别,至少在我下面的测试代码中是这样的:

package main

import "fmt"
import "unicode"

func main() {
    r := rune('1')
    fmt.Println(unicode.IsDigit(r))
    fmt.Println(unicode.IsNumber(r))
    //true
    //true
}

它们都打印出了true

我试图从它们的源代码中理解它们的区别。然而,即使从源代码中我仍然不明白它们之间的区别。

// IsNumber报告给定的符文是否为数字(类别N)。
func IsNumber(r rune) bool {
    if uint32(r) <= MaxLatin1 {
        return properties[uint8(r)]&pN != 0
    }
    return isExcludingLatin(Number, r)
}


// IsDigit报告给定的符文是否为十进制数字。
func IsDigit(r rune) bool {
    if r <= MaxLatin1 {
        return '0' <= r && r <= '9'
    }
    return isExcludingLatin(Digit, r)
}

以上是它们的源代码。

英文:

It seems IsDigit and IsNumber in the unicode package don't behave differently, at least in my following test code:

package main

import &quot;fmt&quot;
import &quot;unicode&quot;

func main() {
	r := rune(&#39;1&#39;)
	fmt.Println(unicode.IsDigit(r))
	fmt.Println(unicode.IsNumber(r))
    //true
    //true
}

They both print true.

I tried to understand from their source code. However, I still don't understand what the differences are between them, even from their source code.

// IsNumber reports whether the rune is a number (category N).
func IsNumber(r rune) bool {
	if uint32(r) &lt;= MaxLatin1 {
		return properties[uint8(r)]&amp;pN != 0
	}
	return isExcludingLatin(Number, r)
}


// IsDigit reports whether the rune is a decimal digit.
func IsDigit(r rune) bool {
	if r &lt;= MaxLatin1 {
		return &#39;0&#39; &lt;= r &amp;&amp; r &lt;= &#39;9&#39;
	}
	return isExcludingLatin(Digit, r)
}

答案1

得分: 27

一般类别是数字,子类别是十进制数字。

Unicode标准中的相关信息如下:

Unicode标准

4. 字符属性

4.5 一般类别

Nd = 数字,十进制数字
Nl = 数字,字母
No = 数字,其他

4.6 数值

数值和数值类型是表示数字的字符的规范属性。

十进制数字。

十进制数字通常是用于构成十进制数的数字。

例如,

在“数字,十进制数字”类别中的Unicode字符(Nd)

在“数字,字母”类别中的Unicode字符(Nl)

在“数字,其他”类别中的Unicode字符(No)

package main

import (
	"fmt"
	"unicode"
)

func main() {
	digit := rune('1')
	fmt.Println(unicode.IsDigit(digit))
	fmt.Println(unicode.IsNumber(digit))
	letter := rune('Ⅷ')
	fmt.Println(unicode.IsDigit(letter))
	fmt.Println(unicode.IsNumber(letter))
	other := rune('½')
	fmt.Println(unicode.IsDigit(other))
	fmt.Println(unicode.IsNumber(other))
}

输出:

true
true
false
true
false
true
英文:

The general category is number and the sub category is decimal digit.

> Unicode Standard
>
> 4. Character Properties
>
>
> 4.5 General Category
>
> Nd = Number, decimal digit
> Nl = Number, letter
> No = Number, other
>
> 4.6 Numeric Value
>
> Numeric_Value and Numeric_Type are normative properties of characters
> that represent numbers.
>
> Decimal Digits.
>
> Decimal digits, as commonly understood, are digits used to form
> decimal-radix numbers.

For example,

Unicode Characters in the 'Number, Decimal Digit' Category (Nd)

Unicode Characters in the 'Number, Letter' Category (Nl)

Unicode Characters in the 'Number, Other' Category (No)

package main

import (
	&quot;fmt&quot;
	&quot;unicode&quot;
)

func main() {
	digit := rune(&#39;1&#39;)
	fmt.Println(unicode.IsDigit(digit))
	fmt.Println(unicode.IsNumber(digit))
	letter := rune(&#39;Ⅷ&#39;)
	fmt.Println(unicode.IsDigit(letter))
	fmt.Println(unicode.IsNumber(letter))
	other := rune(&#39;&#189;&#39;)
	fmt.Println(unicode.IsDigit(other))
	fmt.Println(unicode.IsNumber(other))
}

Output:

true
true
false
true
false
true

答案2

得分: 5

据我所知,IsDigit()IsNumber()的一个子集,所以你得到的结果是正确的,因为两者都应该评估为trueIsNumber用于确定字符是否属于任何数字类别,而IsDigit()则检查字符是否为十进制数字。

英文:

As far as I know IsDigit() is a subset of IsNumber() so the result which you are getting is fine since both should evaluate to true. The IsNumber is use to determine if it is in any numeric Unicode category and IsDigit() checks if it is a radix-10 digit..

huangapple
  • 本文由 发表于 2014年8月28日 12:52:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/25540951.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定