| 1 |
package proto |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"math" |
| 6 |
"unsafe" |
| 7 |
) |
| 8 |
|
| 9 |
var int32Codec = codec{ |
| 10 |
wire: varint, |
| 11 |
size: sizeOfInt32, |
| 12 |
encode: encodeInt32, |
| 13 |
decode: decodeInt32, |
| 14 |
} |
| 15 |
|
| 16 |
func sizeOfInt32(p unsafe.Pointer, flags flags) int { |
| 17 |
if p != nil { |
| 18 |
if v := *(*int32)(p); v != 0 || flags.has(wantzero) { |
| 19 |
return sizeOfVarint(flags.uint64(int64(v))) |
| 20 |
} |
| 21 |
} |
| 22 |
return 0 |
| 23 |
} |
| 24 |
|
| 25 |
func encodeInt32(b []byte, p unsafe.Pointer, flags flags) (int, error) { |
| 26 |
if p != nil { |
| 27 |
if v := *(*int32)(p); v != 0 || flags.has(wantzero) { |
| 28 |
return encodeVarint(b, flags.uint64(int64(v))) |
| 29 |
} |
| 30 |
} |
| 31 |
return 0, nil |
| 32 |
} |
| 33 |
|
| 34 |
func decodeInt32(b []byte, p unsafe.Pointer, flags flags) (int, error) { |
| 35 |
u, n, err := decodeVarint(b) |
| 36 |
v := flags.int64(u) |
| 37 |
if v < math.MinInt32 || v > math.MaxInt32 { |
| 38 |
return n, fmt.Errorf("integer overflow decoding %v into int32", v) |
| 39 |
} |
| 40 |
*(*int32)(p) = int32(v) |
| 41 |
return n, err |
| 42 |
} |