Documentation
¶
Overview ¶
The package provides typical bitmap storage and manipulation functions. BitSet is a data structure that holds a set of bits, where each bit is represented by a single bit in a byte slice. The first bit is stored in the most significant bit of the first byte, and the last bit is stored in the least significant bit of the last byte. Examples:
1000 0010: Bits 0 and 6 are set. The hexadecimal representation is "82". 1000 0011: Bits 0, 6, and 7 are set. The hexadecimal representation is "83". 1100 0001 1100 0011: Bits 0, 1, 7, 8, 9, 14, and 15 are set. The hexadecimal representation is "c1c3".
Index ¶
- Variables
- func AreSet(hexStr []byte, rule CompareRule, bits ...uint) (bool, error)
- func AreSetHexStr(hexStr string, rule CompareRule, bits ...uint) (bool, error)
- func Validate(buf []byte) error
- type BitSet
- type ByteBitSet
- func (bbs ByteBitSet) AreSet(rule CompareRule, bits ...uint) bool
- func (bbs ByteBitSet) BinaryString() string
- func (bbs ByteBitSet) Bytes() []byte
- func (bbs ByteBitSet) IsSet(bit uint) bool
- func (bbs ByteBitSet) Len() uint
- func (bbs *ByteBitSet) Set(val bool, bits ...uint)
- func (bbs ByteBitSet) String() string
- type CompareRule
Constants ¶
This section is empty.
Variables ¶
var ( ErrParseFailed = errors.New("invalid character found") ErrInvalidSourceString = errors.New("invalid source string") )
ErrParseFailed is returned by the Parse function when an invalid character is found in the input string. Valid characters are 0-9, a-f, A-F.
Functions ¶
func AreSet ¶
func AreSet(hexStr []byte, rule CompareRule, bits ...uint) (bool, error)
AreSet evaluates whether all or any specified bits are set, based on the rule, using a hexadecimal string representation of the bitset as input.
func AreSetHexStr ¶ added in v1.0.4
func AreSetHexStr(hexStr string, rule CompareRule, bits ...uint) (bool, error)
AreSetHexStr evaluates whether all or any specified bits are set, based on the rule, using a hexadecimal string representation of the bitset as input.
Types ¶
type BitSet ¶
type BitSet interface {
IsSet(pos uint) bool
AreSet(rule CompareRule, bitpos ...uint) bool
Set(val bool, bitpos ...uint)
Len() uint
String() string
BinaryString() string
Bytes() []byte
}
BitSet defines an interface for manipulating a set of bits. It provides methods to check, set, retrieve, and convert the bit set.
type ByteBitSet ¶ added in v1.0.0
type ByteBitSet struct {
// contains filtered or unexported fields
}
ByteBitSet is a BitSet implementation that stores bits in a byte slice. Each bit is represented by a single bit in the byte array, starting from the most significant bit of the first byte.
func Clone ¶ added in v0.0.2
func Clone(src ByteBitSet) ByteBitSet
Clone returns a deep copy of the provided ByteBitSet.
func New ¶
func New(size int) ByteBitSet
New returns a new ByteBitSet with enough space to store the specified number of bits.
func ParseBinaryString ¶ added in v1.0.2
func ParseBinaryString(src string) (ByteBitSet, error)
ParseBinaryString creates a BitSet from a binary string of '0' and '1' characters. Returns an error if any characters other than '0' or '1' are found.
func ParseHexBytes ¶ added in v1.0.2
func ParseHexBytes(hexStr []byte) (ByteBitSet, error)
ParseHexBytes is a sugar function that creates a ByteBitSet from a byte slice.
func ParseHexString ¶ added in v1.0.2
func ParseHexString(hexStr string) (ByteBitSet, error)
ParseHexString creates a ByteBitSet from a hexadecimal string representation. Returns an error if the input string is invalid.
func (ByteBitSet) AreSet ¶ added in v1.0.0
func (bbs ByteBitSet) AreSet(rule CompareRule, bits ...uint) bool
AreSet checks whether all or any of the specified bits are set, depending on the rule provided. Use IsSet to check a single bit.
func (ByteBitSet) BinaryString ¶ added in v1.0.0
func (bbs ByteBitSet) BinaryString() string
BinaryString returns the binary string representation of the bitset. The leftmost bit corresponds to the lowest index.
func (ByteBitSet) Bytes ¶ added in v1.0.0
func (bbs ByteBitSet) Bytes() []byte
Bytes returns the underlying byte slice representing the bitset.
func (ByteBitSet) IsSet ¶ added in v1.0.0
func (bbs ByteBitSet) IsSet(bit uint) bool
IsSet returns true if the bit at the specified position is set to 1 or false if it is 0.
If the position is out of bounds, it returns false. Use Len() to check the size of the BitSet.
func (ByteBitSet) Len ¶ added in v1.0.0
func (bbs ByteBitSet) Len() uint
Len returns the total number of bits currently allocated in the bit set.
func (*ByteBitSet) Set ¶ added in v1.0.0
func (bbs *ByteBitSet) Set(val bool, bits ...uint)
Set updates the bits at the specified positions to the given value (true to set, false to clear). Automatically expands the internal byte slice if necessary.
func (ByteBitSet) String ¶ added in v1.0.0
func (bbs ByteBitSet) String() string
String returns the hexadecimal string representation of the bitset.
type CompareRule ¶ added in v1.0.0
type CompareRule int8
CompareRule defines the rule for comparing bits in a BitSet. It is used to determine whether all or any of the specified bits are set.
The following constants are available:
- All: All specified bits must be set.
- Any: At least one of the specified bits must be set.
const ( All CompareRule = iota Any )