为什么golang中的符文是int32的别名,而不是uint32? [英] Why is rune in golang an alias for int32 and not uint32?

查看:1178
本文介绍了为什么golang中的符文是int32的别名,而不是uint32?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Go中的 rune 类型是 defined int32 的别名,并且在所有方面都相当于 int32

The type rune in Go is defined as "an alias for int32 and is equivalent to int32 in all ways. It is used, by convention, to distinguish character values from integer values."

如果意图是使用这种类型来表示字符值,那么为什么Go语言的作者不使用 uint32 而不是 int32 ?他们如何期望一个符文价值在负面的情况下在一个程序中被处理?另一个类似的类型 byte uint8 的别名(而不是 int8

If the intention is to use this type to represent character values, why did the authors of Go language not use uint32 instead of int32? How do they expect a rune value to be handled in a program when it is negative? The other similar type byte is an alias for uint8 (and not int8), which seems reasonable.

推荐答案

提到:

"Golang, Go : what is rune by the way?" mentioned:


最近的Unicode 6.3 ,定义了超过110,000个符号。这需要每个代码点至少有21位表示,所以符文就像int32一样,并且有很多位。

With the recent Unicode 6.3, there are over 110,000 symbols defined. This requires at least 21-bit representation of each code point, so a rune is like int32 and has plenty of bits.

但是关于溢出或负值问题,请注意一些unicode函数的实现,如 unicode.IsGraphic 包括:

But regarding the overflow or negative value issues, note that the implementation of some of the unicode functions like unicode.IsGraphic do include:


我们将转换为 uint32 以避免额外的负面测试

We convert to uint32 to avoid the extra test for negative

代码:

Code:

const MaxLatin1 = '\u00FF' // maximum Latin-1 value.

// IsGraphic reports whether the rune is defined as a Graphic by Unicode.
// Such characters include letters, marks, numbers, punctuation, symbols, and
// spaces, from categories L, M, N, P, S, Zs.
func IsGraphic(r rune) bool {
    // We convert to uint32 to avoid the extra test for negative,
    // and in the index we convert to uint8 to avoid the range check.
    if uint32(r) <= MaxLatin1 {
        return properties[uint8(r)]&pg != 0
    }
    return In(r, GraphicRanges...)
}

这可能是因为符文应该是 constant (如符文类型解释,其中符文可以位于 int32 uint32 甚至 float32 或...:它的常量值授权它存储在任何数字类型)。

That maybe because a rune is supposed to be constant (as mentioned in "Go rune type explanation", where a rune could be in an int32 or uint32 or even float32 or ...: its constant value authorizes it to be stored in any of those numeric types).

这篇关于为什么golang中的符文是int32的别名,而不是uint32?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆