mirror of
https://github.com/wader/fq.git
synced 2024-11-23 09:56:07 +03:00
06245d1295
Rename buffer to binary. Still some work left what to call buffer/binary in decode code. Document decode value and binary type Fix proper unit padding for tobytes and add still undocumenated extra padding argument. Add some additional binary tests
59 lines
998 B
Go
59 lines
998 B
Go
package bitioextra
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/wader/fq/pkg/bitio"
|
|
)
|
|
|
|
type ZeroReadAtSeeker struct {
|
|
pos int64
|
|
nBits int64
|
|
}
|
|
|
|
func NewZeroAtSeeker(nBits int64) *ZeroReadAtSeeker {
|
|
return &ZeroReadAtSeeker{nBits: nBits}
|
|
}
|
|
|
|
func (z *ZeroReadAtSeeker) SeekBits(bitOffset int64, whence int) (int64, error) {
|
|
p := z.pos
|
|
switch whence {
|
|
case io.SeekStart:
|
|
p = bitOffset
|
|
case io.SeekCurrent:
|
|
p += bitOffset
|
|
case io.SeekEnd:
|
|
p = z.nBits + bitOffset
|
|
default:
|
|
panic("unknown whence")
|
|
}
|
|
|
|
if p < 0 || p > z.nBits {
|
|
return z.pos, bitio.ErrOffset
|
|
}
|
|
z.pos = p
|
|
|
|
return p, nil
|
|
}
|
|
|
|
func (z *ZeroReadAtSeeker) ReadBitsAt(p []byte, nBits int64, bitOff int64) (n int64, err error) {
|
|
if bitOff < 0 || bitOff > z.nBits {
|
|
return 0, bitio.ErrOffset
|
|
}
|
|
if bitOff == z.nBits {
|
|
return 0, io.EOF
|
|
}
|
|
|
|
lBits := z.nBits - bitOff
|
|
rBits := nBits
|
|
if rBits > lBits {
|
|
rBits = lBits
|
|
}
|
|
rBytes := bitio.BitsByteCount(rBits)
|
|
for i := int64(0); i < rBytes; i++ {
|
|
p[i] = 0
|
|
}
|
|
|
|
return rBits, nil
|
|
}
|