为什么符文符号在golang是一个别名为int32而不是uint32?
Go中的类型符号是
defined作为“int32的别名,并且在所有方面相当于int32,它被按照惯例用于区分字符值和整数值.
如果使用这种类型来表示字符值,为什么Go语言的作者不使用uint32而不是int32?当他们是否定的时候,他们如何期待在程序中处理符文值?其他类似的字节是uint(而不是int)的别名,这是合理的.
“
Golang,Go : what is rune by the way?”提到:
但是关于溢出或负值问题,请注意,执行某些unicode函数(如unicode.IsGraphic)包括:
码: 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(如“Go rune type explanation”所述),其中符文可以在一个int32或uint32甚至是float32或…中:它的常数值授权它存储在任何numeric types中) . (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |