-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbit_writer.go
More file actions
66 lines (59 loc) · 1.54 KB
/
bit_writer.go
File metadata and controls
66 lines (59 loc) · 1.54 KB
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
package gzip
// BitWriter is a struct for writing bits to a byte slice.
//
// (8i+j)-th bit is stored in i-th byte at j-th bit.
// Specifically, the least significant bit is stored in the first bit of the byte.
// It can be retrieved with (slice[i] >> j) & 1.
type BitWriter struct {
// The byte slice to write to.
bytes []byte
// The current byte being written to.
currentByte byte
// The number of bits written to the current byte.
numBitsWritten int
}
// Bits appends the given bits represented as a string.
func (b *BitWriter) Bits(s string) {
for _, c := range s {
b.Bit(c == '1')
}
}
// Bit appends the given bit.
func (b *BitWriter) Bit(bit bool) {
if bit {
b.currentByte |= 1 << b.numBitsWritten
}
b.numBitsWritten++
if b.numBitsWritten == 8 {
b.bytes = append(b.bytes, b.currentByte)
b.currentByte = 0
b.numBitsWritten = 0
}
}
// Bytes appends the given bytes.
func (b *BitWriter) Bytes(data []byte) {
for _, c := range data {
for i := 0; i < 8; i++ {
b.Bit((c>>i)&1 == 1)
}
}
}
// Int appends the given integer as a length-bit bit string.
func (b *BitWriter) Int(i uint64, length int) {
for j := 0; j < length; j++ {
b.Bit((i>>j)&1 == 1)
}
}
// SkipToByteBoundary skips to the next byte boundary, filling the current byte with zeros.
func (b *BitWriter) SkipToByteBoundary() {
if b.numBitsWritten > 0 {
b.bytes = append(b.bytes, b.currentByte)
b.currentByte = 0
b.numBitsWritten = 0
}
}
// Emit emits the bit sequence as a byte slice.
func (b *BitWriter) Emit() []byte {
b.SkipToByteBoundary()
return b.bytes
}