Why is rune in golang an alias for int32 and not uint32?

前端 未结 5 2090
我寻月下人不归
我寻月下人不归 2021-01-31 15:10

The type rune in Go is defined as

an alias for int32 and is equivalent to int32 in all ways. It is used, by conv

5条回答
  •  后悔当初
    2021-01-31 15:36

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

    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.

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

    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...)
    }
    

    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).

提交回复
热议问题