-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
type_uint16.go
139 lines (116 loc) · 2.91 KB
/
type_uint16.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package parco
import (
"encoding/binary"
"math"
)
func ParseUInt16(data []byte, order binary.ByteOrder) (uint16, error) {
if len(data) < 2 {
return 0, NewErrUnSufficientBytesError(2, 0)
}
return order.Uint16(data), nil
}
func ParseUInt16Factory(order binary.ByteOrder) ParserFunc[uint16] {
return func(data []byte) (uint16, error) {
return ParseUInt16(data, order)
}
}
func ParseUInt16HeaderFactory(order binary.ByteOrder) ParserFunc[int] {
return func(raw []byte) (int, error) {
data, err := ParseUInt16(raw, order)
if err != nil {
return 0, err
}
return int(data), nil
}
}
func CompileUInt16Factory(order binary.ByteOrder) CompilerFunc[uint16] {
return func(u16 uint16, box []byte) (err error) {
return CompileUInt16(u16, box, order)
}
}
func CompileUInt16HeaderFactory(order binary.ByteOrder) CompilerFunc[int] {
return func(u16 int, box []byte) (err error) {
return CompileUInt16(uint16(u16), box, order)
}
}
func CompileUInt16(u16 uint16, box []byte, order binary.ByteOrder) (err error) {
order.PutUint16(box, u16)
// TODO: widen Writer interface
//b.WriteByte(byte(u16))
//b.WriteByte(byte(u16 >> 8))
return
}
func UInt16(order binary.ByteOrder) Type[uint16] {
return NewFixedType[uint16](
2,
ParseUInt16Factory(order),
CompileUInt16Factory(order),
)
}
func UInt16LE() Type[uint16] {
return UInt16(binary.LittleEndian)
}
func UInt16BE() Type[uint16] {
return UInt16(binary.BigEndian)
}
func UInt16Header(order binary.ByteOrder) Type[int] {
return NewFixedType[int](
2,
ParseUInt16HeaderFactory(order),
CompileUInt16HeaderFactory(order),
)
}
func UInt16HeaderLE() Type[int] {
return UInt16Header(binary.LittleEndian)
}
func UInt16HeaderBE() Type[int] {
return UInt16Header(binary.BigEndian)
}
// Int16
func ParseInt16(data []byte, order binary.ByteOrder) (int16, error) {
if len(data) < 2 {
return 0, NewErrUnSufficientBytesError(2, 0)
}
return int16(order.Uint16(data)), nil
}
func CompileInt16(i16 int16, box []byte, order binary.ByteOrder) (err error) {
return CompileUInt16(uint16(i16), box, order)
}
func Int16(order binary.ByteOrder) Type[int16] {
return NewFixedType[int16](
2,
func(data []byte) (int16, error) {
return ParseInt16(data, order)
},
func(i16 int16, box []byte) (err error) {
return CompileInt16(i16, box, order)
},
)
}
func Int16LE() Type[int16] {
return Int16(binary.LittleEndian)
}
func Int16BE() Type[int16] {
return Int16(binary.BigEndian)
}
func Int16Header(order binary.ByteOrder) Type[int] {
return NewFixedType[int](
2,
func(data []byte) (int, error) {
n, err := ParseInt16(data, order)
return int(n), err
},
func(i int, box []byte) (err error) {
if i > math.MaxInt16 || i < math.MinInt16 {
return ErrOverflow
}
return CompileInt16(int16(i), box, order)
},
)
}
func Int16LEHeader() Type[int] {
return Int16Header(binary.LittleEndian)
}
func Int16BEHeader() Type[int] {
return Int16Header(binary.BigEndian)
}