-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
type_uint8.go
120 lines (103 loc) · 1.86 KB
/
type_uint8.go
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package parco
import "math"
func CompileUInt8(u8 uint8, box []byte) (err error) {
box[0] = u8
return
}
func ParseUInt8(data []byte) (uint8, error) {
if len(data) < 1 {
return 0, NewErrUnSufficientBytesError(1, 0)
}
return data[0], nil
}
func CompileUInt8Header(i int, box []byte) (err error) {
if i > 255 || i < 0 {
err = ErrOverflow
return
}
box[0] = byte(i)
return
}
func ParseUInt8Header(data []byte) (int, error) {
if len(data) < 1 {
return 0, NewErrUnSufficientBytesError(1, 0)
}
return int(data[0]), nil
}
func UInt8() Type[uint8] {
return NewFixedType[uint8](
1,
ParseUInt8,
CompileUInt8,
)
}
func Byte() Type[byte] {
return UInt8()
}
func UInt8Header() Type[int] {
return NewFixedType[int](
1,
ParseUInt8Header,
CompileUInt8Header,
)
}
func CompileInt8(i8 int8, box []byte) (err error) {
return CompileUInt8(uint8(i8), box)
}
func ParseInt8(data []byte) (int8, error) {
if len(data) < 1 {
return 0, NewErrUnSufficientBytesError(1, 0)
}
if data[0] > math.MaxInt8 {
return 0, ErrOverflow
}
return int8(data[0]), nil
}
func CompileInt8Header(i int, box []byte) (err error) {
if i > math.MaxInt8 || i < math.MinInt8 {
err = ErrOverflow
return
}
box[0] = byte(i)
return
}
func ParseInt8Header(box []byte) (int, error) {
x, err := ParseInt8(box)
if err != nil {
return 0, err
}
return int(x), nil
}
func Int8() Type[int8] {
return NewFixedType[int8](
1,
ParseInt8,
CompileInt8,
)
}
func Int8Header() Type[int] {
return NewFixedType[int](
1,
ParseInt8Header,
CompileInt8Header,
)
}
func Bool() Type[bool] {
return NewFixedType[bool](
1,
func(data []byte) (bool, error) {
n, err := ParseUInt8(data)
if err != nil {
return false, err
}
return n == 1, err
},
func(value bool, box []byte) (err error) {
var n uint8
if value {
n = 1
}
return CompileUInt8(n, box)
},
)
}