1
1
mirror of https://github.com/wader/fq.git synced 2024-11-23 00:57:15 +03:00
fq/internal/bitiox/bitiox.go
Mattias Wadman bf7fa07c41 fq: Use go 1.20 and cleanup
Also rename *ex packages to *x
2024-04-01 19:14:10 +02:00

49 lines
1.0 KiB
Go

package bitiox
// bitio helpers that im not sure belong in bitio
import (
"errors"
"io"
"github.com/wader/fq/pkg/bitio"
)
func CopyBitsBuffer(dst io.Writer, src bitio.Reader, buf []byte) (int64, error) {
return io.CopyBuffer(dst, bitio.NewIOReader(src), buf)
}
func CopyBits(dst io.Writer, src bitio.Reader) (int64, error) {
return CopyBitsBuffer(dst, src, nil)
}
func Len(br bitio.ReadAtSeeker) (int64, error) {
bPos, err := br.SeekBits(0, io.SeekCurrent)
if err != nil {
return 0, err
}
bEnd, err := br.SeekBits(0, io.SeekEnd)
if err != nil {
return 0, err
}
if _, err := br.SeekBits(bPos, io.SeekStart); err != nil {
return 0, err
}
return bEnd, nil
}
func Range(br bitio.ReadAtSeeker, firstBitOffset int64, nBits int64) (bitio.ReaderAtSeeker, error) {
l, err := Len(br)
if err != nil {
return nil, err
}
// TODO: move error check?
if nBits < 0 {
return nil, errors.New("negative nBits")
}
if firstBitOffset+nBits > l {
return nil, errors.New("outside buffer")
}
return bitio.NewSectionReader(br, firstBitOffset, nBits), nil
}