bitarray

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 22, 2021 License: MIT Imports: 5 Imported by: 0

README

BitArray

This package provides a bit array structure with sub-byte methods like packing and shifting for arrays of bits of arbitrary length.

This is not useful for set operations. It facilitates encoding data that is not necessarily aligned to 8 bits. It was originally designed to cater to the packed encoding required for the ISO/IEC 20248 digital signature format.

Usage

import (
	"fmt"

	"src.userspace.com.au/bitarray"
)

func main() {
	ba := &bitarray.BitArray{}

	// Add single bits, never truncated
	ba.AddBit(1)
	ba.AddBit(0)
	ba.AddBit(1)
	fmt.Println(ba.Len()) // => 3
	fmt.Printf("%08b\n", ba.Bytes()) // => [10100000]

	// Add with truncated leading padding
	ba.Add(5)
	fmt.Printf("%08b\n", ba.Bytes()) // => [10110100]

	// Add with fixed length
	ba.AddN(5, 10)
	fmt.Printf("%08b\n", ba.Bytes()) // => [10110100 00000101]

	r := bitarray.NewReader(ba)
	var out uint
	r.ReadBits(&out, 16)
	fmt.Printf("%08b\n", out) // => 1011010000000101

	ba.ShiftL(2)
	fmt.Println(ba.String()) // => [11010000 000101--]

	ba2, _ := ba.Slice(9, 6)
	fmt.Println(ba2.String()) // => [000101--]
}

Documentation

Index

Constants

View Source
const (
	// SeekStart seeks relative to the origin of the file
	SeekStart = 0
	// SeekCurrent seeks relative to the current offset
	SeekCurrent = 1
	// SeekEnd seeks relative to the end
	SeekEnd = 2
)

Variables

View Source
var (
	// EOF is returned when no more input is available.
	EOF = errors.New("EOF") // revive:disable:error-naming
	// ErrInvalidWhence is return for invalid seeking.
	ErrInvalidWhence = errors.New("invalid whence")
	// ErrInvalidOffset is returned for invalid seek offsets.
	ErrInvalidOffset = errors.New("invalid offset")
)

Functions

This section is empty.

Types

type BitArray

type BitArray struct {
	// contains filtered or unexported fields
}

BitArray holds an array of bits.

func New added in v0.7.0

func New(opts ...Option) *BitArray

New creates a BitArray.

func NewFromBytes

func NewFromBytes(b []byte, count int64) *BitArray

NewFromBytes creates a BitArray from the given []byte and count bits. Count is from position 0 of b.

func Pack

func Pack(fields ...interface{}) (*BitArray, error)

Pack stuff together into a BitArray.

func (*BitArray) Add

func (ba *BitArray) Add(u uint) int

Add an uint to the array with leading zeros removed, returns the number of bits added.

func (*BitArray) AddBit

func (ba *BitArray) AddBit(u uint) int

AddBit adds a single bit to the array.

func (*BitArray) AddN

func (ba *BitArray) AddN(u uint, width int) int

AddN adds a uint with a fixed width of n, left padded to width with zeros, returns the number of bits added.

func (*BitArray) Append

func (ba *BitArray) Append(others ...BitArray)

Append packs another BitArray on the end.

func (BitArray) Bytes

func (ba BitArray) Bytes() []byte

Bytes returns the BitArray as bytes.

func (BitArray) Len

func (ba BitArray) Len() int64

Len returns the BitArray length.

func (*BitArray) Pack

func (ba *BitArray) Pack(fields ...interface{}) error

Pack stuff together into existing array.

func (*BitArray) Pad

func (ba *BitArray) Pad(n uint) int

Pad array with n zeros.

func (*BitArray) ReadUint

func (ba *BitArray) ReadUint(start, length int64) (uint, error)

ReadUint reads a uint from the BitArray.

func (*BitArray) Set

func (ba *BitArray) Set(n int64)

Set a single bit to 1 at position n.

func (*BitArray) ShiftL

func (ba *BitArray) ShiftL(n int64)

ShiftL returns a new BitArray with all bits to the left n times.

func (*BitArray) Slice

func (ba *BitArray) Slice(startBit, length int64) (*BitArray, error)

Slice reads a range from the BitArray.

func (BitArray) String

func (ba BitArray) String() string

Bytes returns the BitArray as bytes. The remaining bits of the last byte are set to zero.

func (BitArray) Test

func (ba BitArray) Test(i int64) bool

Test returns true if bit at (zero based) offset i is 1, false otherwise.

func (*BitArray) Unset

func (ba *BitArray) Unset(n int64)

Unset a single bit to 0 at position n.

type Option added in v0.7.0

type Option option

func SetBytes added in v0.7.0

func SetBytes(b []byte) Option

SetBytes configures the BitArray with initial data.

func SetSize added in v0.7.0

func SetSize(n int64) Option

SetSize configures the BitArray with an initial size.

type Reader

type Reader struct {
	// contains filtered or unexported fields
}

A Reader implements the BitReader interface.

func NewReader

func NewReader(ba *BitArray) *Reader

NewReader creates a new Reader.

func (*Reader) Pos

func (r *Reader) Pos() int64

Pos returns the current position of the reader.

func (*Reader) ReadBit

func (r *Reader) ReadBit() bool

ReadBit advances one bit and returns the result of a boolean AND test on it.

func (*Reader) ReadBits

func (r *Reader) ReadBits(out *uint, n int) error

ReadBits reads n bits from the BitArray into out.

func (*Reader) Seek

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek sets the internal pointer to position n. It returns the resulting offset.

Jump to

Keyboard shortcuts

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