Documentation
¶
Index ¶
- Variables
- type BitReader
- func (r *BitReader[T]) AnyData() any
- func (r *BitReader[T]) Bits() int
- func (r *BitReader[T]) Data() []T
- func (r *BitReader[T]) Pos() int
- func (r *BitReader[T]) Read16R(bits, n int) (b uint16)
- func (r *BitReader[T]) Read32R(bits, n int) (b uint32)
- func (r *BitReader[T]) Read64R(bits, n int) (b uint64)
- func (r *BitReader[T]) Read8R(bits, n int) uint8
- func (r *BitReader[T]) ReadBit() (bool, error)
- func (r *BitReader[T]) ReadBitAt(pos int) (bool, error)
- func (r *BitReader[T]) Seek(pos int) error
- func (r *BitReader[T]) SetBits(bits int)
- type BitWriter
- func (w *BitWriter[T]) AnyData() any
- func (w *BitWriter[T]) Bits() int
- func (w *BitWriter[T]) Data() []T
- func (w *BitWriter[T]) Pos() int
- func (w *BitWriter[T]) Seek(pos int) error
- func (w *BitWriter[T]) Write16(leftPadd, bits int, data uint16)
- func (w *BitWriter[T]) Write32(leftPadd, bits int, data uint32)
- func (w *BitWriter[T]) Write64(leftPadd, bits int, data uint64)
- func (w *BitWriter[T]) Write8(leftPadd, bits int, data uint8)
- func (w *BitWriter[T]) WriteBit(bit bool) error
- func (w *BitWriter[T]) WriteBitAt(pos int, bit bool) error
- func (w *BitWriter[T]) WriteBool(data bool)
- type Unsigned
Constants ¶
This section is empty.
Variables ¶
var ( // ErrNegativePosition is returned when a negative position is provided to Seek or WriteBitAt/ReadBitAt. ErrNegativePosition = errors.New("bitstream: negative position") )
Functions ¶
This section is empty.
Types ¶
type BitReader ¶
type BitReader[T Unsigned] struct { // contains filtered or unexported fields }
BitReader provides bit-level reading operations on integer slice data. It treats the data as a continuous bit stream, allowing precise bit extraction.
BitReader is not safe for concurrent use. If multiple goroutines access the same BitReader, external synchronization is required. For concurrent reading scenarios, create separate BitReader instances for each goroutine.
func NewBitReader ¶
NewBitReader creates a new BitReader for manipulating bits from integer slice data. leftPadd specifies how many upper bits in each element should be treated as padding. rightPadd specifies how many lower bits in each element should be treated as padding. For example, if each element has 2 bits of padding at the top and 1 bit at the bottom, set leftPadd=2 and rightPadd=1. The reader will only access bits from position leftPadd to (element size - rightPadd).
Panics if leftPadd + rightPadd >= element bit size, as this would leave no valid bits to read.
func (*BitReader[T]) AnyData ¶ added in v0.2.0
AnyData returns the source data as an any type. This is useful when the exact type of the underlying data slice is not known at compile time. Use Bits() to get the total number of valid bits.
func (*BitReader[T]) Bits ¶ added in v0.1.1
Bits returns the total number of valid bits in the BitReader.
func (*BitReader[T]) Data ¶ added in v0.2.0
func (r *BitReader[T]) Data() []T
Data returns the source data slice. Use Bits() to get the total number of valid bits.
func (*BitReader[T]) Read16R ¶ added in v0.2.0
Read16R reads a specified number of bits from the n-th position in the data. bits specifies how many bits to read (up to 16 bits). n specifies which block to read (0-indexed). Returns the bits as a uint16 value, right-aligned (LSB-aligned).
Panics if bits > 16, as uint16 can only hold 16 bits.
func (*BitReader[T]) Read32R ¶ added in v0.2.0
Read32R reads a specified number of bits from the n-th position in the data. bits specifies how many bits to read (up to 32 bits). n specifies which block to read (0-indexed). Returns the bits as a uint16 value, right-aligned (LSB-aligned).
Panics if bits > 32, as uint16 can only hold 32 bits.
func (*BitReader[T]) Read64R ¶ added in v0.2.0
Read64R reads a specified number of bits from the n-th position in the data. bits specifies how many bits to read (up to 64 bits). n specifies which block to read (0-indexed). Returns the bits as a uint16 value, right-aligned (LSB-aligned).
Panics if bits > 64, as uint16 can only hold 64 bits.
func (*BitReader[T]) Read8R ¶ added in v0.2.0
Read8R reads a specified number of bits from the n-th position in the data. bits specifies how many bits to read (up to 8 bits). n specifies which block to read (0-indexed). Returns the bits as a uint16 value, right-aligned (LSB-aligned).
Panics if bits > 8, as uint16 can only hold 8 bits.
func (*BitReader[T]) ReadBit ¶ added in v0.2.0
ReadBit reads one bit at the current position and advances the cursor. Returns false and io.EOF if the position is beyond the valid bits.
func (*BitReader[T]) ReadBitAt ¶ added in v0.2.0
ReadBitAt reads one bit at the specified position without moving the cursor. Returns false and io.EOF if the position is beyond the valid bits. Returns false and ErrNegativePosition for negative positions.
func (*BitReader[T]) Seek ¶ added in v0.2.0
Seek sets the read position (cursor). Allows seeking to any non-negative position, including beyond the valid bits. Subsequent reads will return io.EOF if the position is at or beyond the valid bits. Returns ErrNegativePosition for negative positions.
func (*BitReader[T]) SetBits ¶
SetBits sets the total number of valid bits in the BitReader. if bits exceeds the maximum possible bits based on the data length and padding, it will be capped to that maximum. Data beyond the specified bits position will be treated as zero, regardless of the actual padding configuration. This is useful for limiting the readable range within the data.
type BitWriter ¶
type BitWriter[T Unsigned] struct { // contains filtered or unexported fields }
BitWriter provides bit-level writing operations to integer slice data. It treats the destination as a continuous bit stream, allowing precise bit insertion.
BitWriter is safe for concurrent use. All methods are protected by an internal mutex, allowing multiple goroutines to safely write to the same BitWriter instance.
func NewBitWriter ¶
NewBitWriter creates a new BitWriter for writing bits to integer slice data. leftPadd specifies how many upper bits in each element should be treated as padding. rightPadd specifies how many lower bits in each element should be treated as padding. The writer will only write bits to the valid range between paddings.
Panics if leftPadd + rightPadd >= element bit size, as this would leave no valid bits to write.
func (*BitWriter[T]) AnyData ¶ added in v0.1.2
AnyData returns the accumulated data as an any type. This is useful when the exact type of the underlying data slice is not known at compile time. Use Bits() to get the total number of valid bits written.
func (*BitWriter[T]) Bits ¶ added in v0.2.0
Bits returns the total number of valid bits in the BitWriter.
func (*BitWriter[T]) Data ¶
func (w *BitWriter[T]) Data() []T
Data returns the accumulated data slice. Use Bits() to get the total number of valid bits written.
func (*BitWriter[T]) Seek ¶ added in v0.2.0
Seek sets the write position (cursor). Allows seeking to any non-negative position, including beyond current data. Returns ErrNegativePosition for negative positions.
func (*BitWriter[T]) Write16 ¶ added in v0.2.0
Write16 writes the specified bits from a uint16 value to the stream. leftPadd specifies how many upper bits to skip in the source data. bits specifies how many bits to write after skipping leftPadd bits.
Panics if leftPadd + bits > 16.
func (*BitWriter[T]) Write32 ¶ added in v0.2.0
Write32 writes the specified bits from a uint32 value to the stream. leftPadd specifies how many upper bits to skip in the source data. bits specifies how many bits to write after skipping leftPadd bits.
Panics if leftPadd + bits > 32.
func (*BitWriter[T]) Write64 ¶ added in v0.2.0
Write64 writes the specified bits from a uint64 value to the stream. leftPadd specifies how many upper bits to skip in the source data. bits specifies how many bits to write after skipping leftPadd bits.
Panics if leftPadd + bits > 64.
func (*BitWriter[T]) Write8 ¶ added in v0.2.0
Write8 writes the specified bits from a uint8 value to the stream. leftPadd specifies how many upper bits to skip in the source data. bits specifies how many bits to write after skipping leftPadd bits.
Panics if leftPadd + bits > 8.
func (*BitWriter[T]) WriteBit ¶ added in v0.2.0
WriteBit writes one bit at the current position and advances the cursor. Automatically extends the data slice if writing beyond current length.
func (*BitWriter[T]) WriteBitAt ¶ added in v0.2.0
WriteBitAt writes one bit at the specified position without moving the cursor. Automatically extends the data slice if writing beyond current length. Returns ErrNegativePosition for negative positions.