encode/integer.go   B
last analyzed

Size/Duplication

Total Lines 196
Duplicated Lines 0 %

Test Coverage

Coverage 65%

Importance

Changes 0
Metric Value
cc 50
eloc 130
dl 0
loc 196
c 0
b 0
f 0
ccs 52
cts 80
cp 0.65
crap 157.1875
rs 8.4

14 Methods

Rating   Name   Duplication   Size   Complexity  
A encode.Uint64 0 5 1
A encode.Uint32 0 8 1
A encode.Uint16 0 8 1
A encode.Int64 0 5 1
A encode.Int16 0 8 1
A encode.Uint 0 10 5
A encode.detectUintType 0 10 5
F encode.detectIntType 0 43 18
A encode.Uint8 0 2 1
C encode.Int 0 25 9
A encode.Int8 0 2 1
A encode.encodeSize32 0 7 1
A encode.Int32 0 8 1
A encode.Size 0 16 4
1
package encode
2
3
import (
4
	"encoding/binary"
5
	"math"
6
7
	"github.com/et-nik/binngo/binn"
8
)
9
10
type intType uint8
11
12
func Uint(v uint) []byte {
13
	switch detectUintType(v) {
14
	case binn.Uint8Type:
15
		return Uint8(uint8(v))
16
	case binn.Uint16Type:
17
		return Uint16(uint16(v))
18
	case binn.Uint32Type:
19
		return Uint32(uint32(v))
20
	default:
21
		return Uint64(uint64(v))
22
	}
23
}
24
25
func Int(v int) []byte {
26 1
	t := detectIntType(v)
27
28 1
	result := []byte{}
29
30 1
	switch t {
31
	case binn.Int8Type:
32
		result = Int8(int8(v))
33
	case binn.Uint8Type:
34 1
		result = Uint8(uint8(v))
35
	case binn.Int16Type:
36 1
		result = Int16(int16(v))
37
	case binn.Uint16Type:
38 1
		result = Uint16(uint16(v))
39
	case binn.Int32Type:
40 1
		result = Int32(int32(v))
41
	case binn.Uint32Type:
42
		result = Uint32(uint32(v))
43
	case binn.Int64Type:
44
		result = Int64(int64(v))
45
	case binn.Uint64Type:
46 1
		result = Uint64(uint64(v))
47
	}
48
49 1
	return result
50
}
51
52
func detectUintType(v uint) intType {
53
	switch {
54
	case v <= math.MaxUint8:
55
		return binn.Uint8Type
56
	case v <= math.MaxUint16:
57
		return binn.Uint16Type
58
	case v <= math.MaxUint32:
59
		return binn.Uint32Type
60
	default:
61
		return binn.Uint64Type
62
	}
63
}
64
65
func detectIntType(v int) intType {
66 1
	t := binn.Int64Type
67
68 1
	if v > 0 {
69 1
		switch t {
70
		case binn.Int64Type:
71 1
			t = binn.Uint64Type
72
		case binn.Int32Type:
73
			t = binn.Uint32Type
74
		case binn.Int16Type:
75
			t = binn.Uint16Type
76
		case binn.Int8Type:
77
			t = binn.Uint8Type
78
		}
79
	}
80
81 1
	if t == binn.Int64Type ||
82
		t == binn.Int32Type ||
83
		t == binn.Int16Type {
84 1
		switch {
85
		case v >= math.MinInt8:
86
			t = binn.Int8Type
87
		case v >= math.MinInt16:
88 1
			t = binn.Int16Type
89
		case v >= math.MinInt32:
90 1
			t = binn.Int32Type
91
		}
92
	}
93
94 1
	if t == binn.Uint64Type ||
95
		t == binn.Uint32Type ||
96
		t == binn.Uint16Type {
97 1
		switch {
98
		case v <= math.MaxUint8:
99 1
			t = binn.Uint8Type
100
		case v <= math.MaxUint16:
101 1
			t = binn.Uint16Type
102
		case v <= math.MaxInt32:
103
			t = binn.Uint32Type
104
		}
105
	}
106
107 1
	return intType(t)
108
}
109
110
func Int8(v int8) []byte {
111
	return []byte{uint8(v)}
112
}
113
114
func Uint8(v uint8) []byte {
115 1
	return []byte{v}
116
}
117
118
func Uint16(v uint16) []byte {
119 1
	t := make([]byte, 2)
120 1
	binary.BigEndian.PutUint16(t, v)
121
122 1
	var r []byte
123 1
	r = append(r, t...)
124
125 1
	return r
126
}
127
128
func Int16(v int16) []byte {
129 1
	t := make([]byte, 2)
130 1
	binary.BigEndian.PutUint16(t, uint16(v))
131
132 1
	var r []byte
133 1
	r = append(r, t...)
134
135 1
	return r
136
}
137
138
func Uint32(v uint32) []byte {
139
	t := make([]byte, 4)
140
	binary.BigEndian.PutUint32(t, v)
141
142
	var r []byte
143
	r = append(r, t...)
144
145
	return r
146
}
147
148
func Int32(v int32) []byte {
149 1
	t := make([]byte, 4)
150 1
	binary.BigEndian.PutUint32(t, uint32(v))
151
152 1
	var r []byte
153 1
	r = append(r, t...)
154
155 1
	return r
156
}
157
158
func Uint64(v uint64) []byte {
159 1
	t := make([]byte, 8)
160 1
	binary.BigEndian.PutUint64(t, v)
161
162 1
	return t
163
}
164
165
func Int64(v int64) []byte {
166
	t := make([]byte, 8)
167
	binary.BigEndian.PutUint64(t, uint64(v))
168
169
	return t
170
}
171
172
func Size(size int, totalSize bool) []byte {
173 1
	sz := size
174
175 1
	if totalSize {
176 1
		sz++
177
	}
178
179 1
	if sz <= math.MaxInt8 {
180 1
		return []byte{byte(sz)}
181
	}
182
183 1
	if totalSize {
184
		sz += 3
185
	}
186
187 1
	return encodeSize32(sz)
188
}
189
190
func encodeSize32(s int) []byte {
191 1
	i := s | (-1 << 31)
192
193 1
	b := make([]byte, 4)
194 1
	binary.BigEndian.PutUint32(b, uint32(i))
195
196 1
	return b
197
}
198