Skip to content

Commit 12bf4ec

Browse files
committed
add helpful comments so i dont have to re-interpret my old code
1 parent edcaa8f commit 12bf4ec

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

aces.go

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,9 @@ func (bs *bitStreamer) next() (byte, error) {
157157

158158
var result byte
159159
if bitNum+bs.chunkLen > 8 { // want to slice past current byte
160-
currByte := bs.buf[byteNum]
161-
firstByte := sliceByteLen(currByte, bitNum, 8-bitNum)
162-
var nextByte byte
163-
nextByte = bs.buf[byteNum+1]
164-
secondByteLen := bs.chunkLen + bitNum - 8
165-
result = (firstByte << byte(secondByteLen)) + sliceByteLen(nextByte, 0, secondByteLen)
160+
firstByte := sliceByteLen(bs.buf[byteNum], bitNum, 8-bitNum)
161+
secondPartLen := bs.chunkLen + bitNum - 8
162+
result = (firstByte << secondPartLen) + sliceByteLen(bs.buf[byteNum+1], 0, secondPartLen)
166163
bs.bitIdx += bs.chunkLen
167164
return result, nil
168165
}
@@ -171,10 +168,6 @@ func (bs *bitStreamer) next() (byte, error) {
171168
return result, nil
172169
}
173170

174-
func errPrint(a ...interface{}) {
175-
fmt.Fprintln(os.Stderr, a...)
176-
}
177-
178171
type bitWriter struct {
179172
chunkLen int
180173
out io.Writer
@@ -198,13 +191,19 @@ func (bw *bitWriter) write(b byte) error {
198191
}
199192
bw.init()
200193
bw.bitIdx = 0
201-
bitNum = bw.bitIdx % 8
202-
byteNum = bw.bitIdx / 8
194+
bitNum = 0
195+
byteNum = 0
203196
}
204197

205-
if bitNum+bw.chunkLen > 8 {
206-
bw.buf[byteNum] = bw.buf[byteNum] + sliceByteLen(b, 8-bw.chunkLen, 8-bitNum)
207-
bw.buf[byteNum+1] = sliceByteLen(b, 8-bw.chunkLen+8-bitNum, bw.chunkLen+bitNum-8) << byte(8-bw.chunkLen+8-bitNum)
198+
if bitNum+bw.chunkLen > 8 { // write across byte boundary?
199+
// 8-bw.chunkLen is where b's actual data starts from.
200+
bStart := 8 - bw.chunkLen
201+
// space left in current byte
202+
left := 8 - bitNum
203+
204+
bw.buf[byteNum] = bw.buf[byteNum] + sliceByteLen(b, bStart, left)
205+
// bStart + left is up to where b has been read from. (bw.chunkLen+bitNum) - 8 is how many bits go to the next byte.
206+
bw.buf[byteNum+1] = sliceByteLen(b, bStart+left, bw.chunkLen-left) << (bStart - left) // simplified 8 - (bw.chunkLen + bitNum - 8)
208207
} else {
209208
bw.buf[byteNum] = bw.buf[byteNum] + (b << (8 - (bitNum + bw.chunkLen)))
210209
}

0 commit comments

Comments
 (0)