File: int32.go

package info (click to toggle)
golang-github-segmentio-encoding 0.5.3-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 14,472 kB
  • sloc: makefile: 286
file content (42 lines) | stat: -rw-r--r-- 856 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package proto

import (
	"fmt"
	"math"
	"unsafe"
)

var int32Codec = codec{
	wire:   varint,
	size:   sizeOfInt32,
	encode: encodeInt32,
	decode: decodeInt32,
}

func sizeOfInt32(p unsafe.Pointer, flags flags) int {
	if p != nil {
		if v := *(*int32)(p); v != 0 || flags.has(wantzero) {
			return sizeOfVarint(flags.uint64(int64(v)))
		}
	}
	return 0
}

func encodeInt32(b []byte, p unsafe.Pointer, flags flags) (int, error) {
	if p != nil {
		if v := *(*int32)(p); v != 0 || flags.has(wantzero) {
			return encodeVarint(b, flags.uint64(int64(v)))
		}
	}
	return 0, nil
}

func decodeInt32(b []byte, p unsafe.Pointer, flags flags) (int, error) {
	u, n, err := decodeVarint(b)
	v := flags.int64(u)
	if v < math.MinInt32 || v > math.MaxInt32 {
		return n, fmt.Errorf("integer overflow decoding %v into int32", v)
	}
	*(*int32)(p) = int32(v)
	return n, err
}