Repository navigation
Releases: yyyoichi/bitstream-go
Releases · yyyoichi/bitstream-go
Release list
v0.2.1
Bug fix
reader := NewBitReader([]uint8{255}, 0, 0)
reader.SetBits(8 + 1)
got := reader.Read8R(8, 1) // ->> panic!!func (r *BitReader[T]) SetBits(bits int) {
+ if max := len(r.data) * r.s; bits > max {
+ bits = max
+ return
+ }
r.bits = bits
}v0.2.0
🚨 Breaking Changes
Method Naming Changes
The following methods have been renamed for improved clarity and consistency:
BitReader:
UXXR(bits, n int) uintXX→ReadXXR(bits, n int) uintXX
BitWriter:
UXX(leftPadd, bits int, data uintXX)→WriteXX(leftPadd, bits int, data uintXX)Bool(data bool)→WriteBool(data bool)
Migration Guide:
// Before
reader.U8R(8, 0)
reader.U16R(12, 0)
writer.Bool(true)
// After
reader.Read8R(8, 0)
reader.Read16R(12, 0)
writer.WriteBool(true)The R suffix indicates "Right-aligned" block-based reading, distinguishing these methods from the new cursor-based operations.
✨ New Features
Cursor-based Bit Manipulation
Added flexible cursor-based reading and writing operations, enabling sequential and random access bit manipulation patterns.
BitReader - New Methods:
ReadBit() (bool, error)- Sequential reading with automatic cursor advancementReadBitAt(pos int) (bool, error)- Random access reading without cursor movementPos() int- Get current read positionSeek(pos int) error- Set read positionData() []T- Access source data sliceAnyData() any- Access source data asanytype
BitWriter - New Methods:
WriteBit(bit bool) error- Sequential writing with automatic cursor advancementWriteBitAt(pos int, bit bool) error- Random access writing with overwrite supportPos() int- Get current write positionSeek(pos int) error- Set write position
Use Cases:
- Bit shuffling and permutation operations
- Selective bit modification without data recreation
- Stream-based bit processing
- Pattern matching and bit scanning
Example:
// Sequential reading
reader := bitstream.NewBitReader(data, 0, 0)
for {
bit, err := reader.ReadBit()
if err == io.EOF {
break
}
// Process bit...
}
// Random access writing
writer := bitstream.NewBitWriter[uint8](0, 0)
writer.WriteBitAt(5, true) // Write to specific position
writer.WriteBitAt(10, false) // Overwrite capabilityFor detailed examples and documentation, see PR #1.
🐛 Bug Fixes & Improvements
Error Handling
- Introduced
ErrNegativePositionfor semantically correct error handling io.EOFnow strictly indicates end-of-data condition- All position-accepting methods validate negative inputs
Input Validation
- Added negative position validation to prevent panics
- Comprehensive bounds checking for all cursor operations
Thread-Safety
BitWriter.Bits()now protected by mutex for thread-safe access- Clearly documented thread-safety guarantees:
- BitReader: Not thread-safe (use separate instances for concurrent access)
- BitWriter: Thread-safe (all operations protected by mutex)
📚 Documentation
- Comprehensive README updates with cursor-based operation examples
- Detailed API reference with categorized methods
- Thread-safety model clearly documented
- Error handling patterns and best practices added
🧪 Testing
- Comprehensive test coverage for all new methods
- Edge case testing including padding scenarios
- Negative position validation tests
- Thread-safety and consistency tests
For complete details on all changes, see Pull Request #1.
v0.1.2
v0.1.1
v0.1.0
A Go library for efficient bit-level reading and writing operations on integer slices.
type BitReader[T IntegerType] struct {}
func NewBitReader[T IntegerType](data []T, leftPadd int, rightPadd int) *BitReader[T]
func (r *BitReader[T]) SetBits(bits int)
func (r *BitReader[T]) U16R(bits int, n int) (b uint16)
func (r *BitReader[T]) U32R(bits int, n int) (b uint32)
func (r *BitReader[T]) U64R(bits int, n int) (b uint64)
func (r *BitReader[T]) U8R(bits int, n int) uint8type BitWriter[T IntegerType] struct {}
func NewBitWriter[T IntegerType](leftPadd int, rightPadd int) *BitWriter[T]
func (w *BitWriter[T]) Bool(data bool)
func (w *BitWriter[T]) Data() ([]T, int)
func (w *BitWriter[T]) U16(leftPadd int, bits int, data uint16)
func (w *BitWriter[T]) U32(leftPadd int, bits int, data uint32)
func (w *BitWriter[T]) U64(leftPadd int, bits int, data uint64)
func (w *BitWriter[T]) U8(leftPadd int, bits int, data uint8)Full Changelog: https://github.com/yyyoichi/bitstream-go/commits/v0.1.0