-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbits.go
177 lines (154 loc) · 3.93 KB
/
bits.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package iabtcf
import (
"fmt"
"time"
)
// //////////////////////////////////////////////////
// bits
// Bits represents a bitset with some helpers to read int, bool, string and time fields
//
// Bits are stored in a byte slice
// First byte will store the first 8 bits, second byte the next 8 bits, and so on
//
// note: the last byte may contain less than 8 bits. Those bits are left aligned.
type Bits []byte
// HasBit checks if the bit number is set
//
// note: number is not the index and it starts at 1.
func (b Bits) HasBit(number int) bool {
return b.ReadBoolField(number - 1)
}
// Length returns the number of bits in the bitset
func (b Bits) Length() int {
return len(b) * nbBitInByte
}
const (
nbBitInByte = 8
lastBitIndex = nbBitInByte - 1
)
var (
bitMasks = [nbBitInByte]byte{
1 << 7,
1 << 6,
1 << 5,
1 << 4,
1 << 3,
1 << 2,
1 << 1,
1,
}
)
// ReadInt64Field reads an int64 field of nbBits bits starting at offset
//
// note: if offset is negative, the result will be zero
// note: if offset + nbBits is out of bound, the result will be the same if we were adding trailing zeros
// example: 00101 > read with offset 2 and nbBits 5 > equivalent to reading 10100 = 20
func (b Bits) ReadInt64Field(offset, nbBits int) int64 {
if offset < 0 {
return 0
}
var result int64
byteIndex := offset / nbBitInByte
if byteIndex >= len(b) {
return result
}
bitIndex := offset % nbBitInByte
for i := 0; i < nbBits; i++ {
mask := bitMasks[bitIndex]
if b[byteIndex]&mask == mask {
result |= 1 << (nbBits - 1 - i)
}
if bitIndex == lastBitIndex {
byteIndex++
if byteIndex >= len(b) {
return result
}
bitIndex = 0
} else {
bitIndex++
}
}
return result
}
// ReadIntField reads an int field of nbBits bits starting at offset
func (b *Bits) ReadIntField(offset, nbBits int) int {
return int(b.ReadInt64Field(offset, nbBits))
}
const (
timeNbBits = 36
)
// ReadTimeField reads a time field of 36 bits starting at offset
func (b *Bits) ReadTimeField(offset int) time.Time {
ds := b.ReadInt64Field(offset, timeNbBits)
return time.Unix(ds/dsPerSec, (ds%dsPerSec)*nsPerDs).UTC()
}
const (
characterNbBits = 6
)
// ReadStringField reads a string field of nbBits bits starting at offset
//
// note: each character is represented by 6 bits, so the number of bits must be a multiple of 6
// note: the characters are represented by the uppercase alphabet starting from 'A'
func (b *Bits) ReadStringField(offset, nbBits int) string {
length := nbBits / characterNbBits
var buf = make([]byte, 0, length)
nextOffset := offset
for i := 0; i < length; i++ {
value := b.ReadInt64Field(nextOffset, characterNbBits)
buf = append(buf, byte(value)+'A')
nextOffset += characterNbBits
}
return string(buf)
}
const (
boolNbBits = 1
)
// ReadBoolField reads a bool field of 1 bit starting at offset
func (b *Bits) ReadBoolField(offset int) bool {
return b.ReadInt64Field(offset, boolNbBits) == 1
}
// ToBitString returns the bitset as a string of bits ( human readable 0s and 1s )
func (bits Bits) ToBitString() string {
if bits == nil {
return ""
}
result := ""
for i, b := range bits {
if i != 0 {
result += " "
}
result += fmt.Sprintf("%08b", b)
}
return result
}
// //////////////////////////////////////////////////
// bit string helper
// BitStringToBits converts a bit string to a Bits struct
func BitStringToBits(value string) Bits {
return Bits(BitStringToBytes(value))
}
// BitStringToBytes converts a bit string to a byte slice
func BitStringToBytes(value string) []byte {
bytes := make([]byte, 0, len(value)/nbBitInByte)
position := lastBitIndex
var lastByte byte
for i := 0; i < len(value); i++ {
if value[i] == ' ' {
continue
}
if value[i] == '1' {
lastByte |= 1 << position
}
if position == 0 {
position = lastBitIndex
bytes = append(bytes, lastByte)
lastByte = 0
} else {
position--
}
}
if position != nbBitInByte-1 {
bytes = append(bytes, lastByte)
}
return bytes
}