bitio

package
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 3, 2025 License: MIT Imports: 1 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrShort = errors.New("deckcodec/bitio: unexpected EOF")

ErrShort is returned when there are not enough bytes left in the source to satisfy a read.

Functions

This section is empty.

Types

type Reader

type Reader struct {
	Src []byte // Source byte slice to read from.
	// contains filtered or unexported fields
}

Reader reads bits from a byte slice, accumulating bits in 'acc'. 'cur' tracks the current position in the source byte slice. 'nbits' is the number of bits currently in the accumulator.

func NewReader

func NewReader(src []byte, validBits int) Reader

NewReader constructs a Reader with a known number of valid bits. If validBits < 0, all bits in src are considered valid (len(src)*8).

func (*Reader) ReadBits

func (r *Reader) ReadBits(width int) (uint32, error)

ReadBits reads 'width' bits from the source and returns them as a uint32. If there are not enough bits in the accumulator, it loads more bytes from Src. Returns ErrShort if the source runs out of bytes before enough bits are available.

type Writer

type Writer struct {
	Buf []byte // Output buffer where bytes are written as they are completed.
	// contains filtered or unexported fields
}

Writer is a bit-level writer that allows writing arbitrary numbers of bits into a byte buffer. The bits are accumulated in 'acc' until at least 8 bits are available, at which point a byte is flushed to 'Buf'.

func (*Writer) Finish

func (w *Writer) Finish() []byte

Finish flushes any remaining bits in the accumulator to the buffer as a final byte. If there are leftover bits (less than 8), they are written as the lowest bits of the last byte. After flushing, the accumulator and bit count are reset to zero.

func (*Writer) WriteBits

func (w *Writer) WriteBits(v uint32, width int)

WriteBits writes the lowest 'width' bits of 'v' into the buffer. Bits are accumulated in 'acc' until at least 8 bits are available, at which point a byte is flushed to the buffer.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL