-
Notifications
You must be signed in to change notification settings - Fork 1
/
header.go
35 lines (30 loc) · 920 Bytes
/
header.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
package datarun
// Header describes the allocation of bytes in a mapping pair.
type Header byte
// LengthBytes returns the number of length bytes in a mapping pair.
func (h Header) LengthBytes() int {
return int(h & 0x0f) // Lower nibble
}
// OffsetBytes returns the number of offset bytes in a mapping pair.
func (h Header) OffsetBytes() int {
return int((h & 0xf0) >> 4) // Upper nibble
}
// DataBytes returns the number of data bytes in a mapping pair,
// excluding the header itself.
func (h Header) DataBytes() int {
return h.LengthBytes() + h.OffsetBytes()
}
// Valid returns false if the header contains invalid data.
func (h Header) Valid() bool {
length := h.LengthBytes()
offset := h.OffsetBytes()
if length > 8 || offset > 8 {
// We've exceeded a 64-bit integer
return false
}
if offset > 0 && length == 0 {
// Length must be present when Offset is present
return false
}
return true
}