DecodeInt32 decodes a varint-encoded 32-bit integer from the stream and returns the value. io.ErrUnexpectedEOF is returned if the operation would read past the end of the data.
()
| 253 | // |
| 254 | // io.ErrUnexpectedEOF is returned if the operation would read past the end of the data. |
| 255 | func (d *Decoder) DecodeInt32() (int32, error) { |
| 256 | if d.offset >= len(d.p) { |
| 257 | return 0, io.ErrUnexpectedEOF |
| 258 | } |
| 259 | v, n, err := DecodeVarint(d.p[d.offset:]) |
| 260 | if err != nil { |
| 261 | return 0, fmt.Errorf("invalid data at byte %d: %w", d.offset, err) |
| 262 | } |
| 263 | if n == 0 { |
| 264 | return 0, fmt.Errorf("invalid data at byte %d: %w", d.offset, ErrInvalidVarintData) |
| 265 | } |
| 266 | // ensure the result is within [-math.MaxInt32, math.MaxInt32] when converted to a signed value |
| 267 | if i64 := int64(v); i64 > math.MaxInt32 || i64 < math.MinInt32 { |
| 268 | return 0, ErrValueOverflow |
| 269 | } |
| 270 | d.offset += n |
| 271 | return int32(v), nil |
| 272 | } |
| 273 | |
| 274 | // DecodeInt64 decodes a varint-encoded 64-bit integer from the stream and returns the value. |
| 275 | // |