bytearray.go
bytearray.go - Overview
This file defines the ByteArray
type and related functions for manipulating byte arrays, including creating, converting, and performing bitwise operations.
Detailed Documentation
ByteArray
type ByteArray struct {
data []byte
Length int64
}
- Purpose: Represents a byte array with its data and length.
- Fields:
data
: The underlying byte slice.Length
: The length of the byte array.
NewByteArray
func NewByteArray(size int) *ByteArray
- Purpose: Initializes a new
ByteArray
with the given size. - Parameters:
size
(int): The size of the byte array to create.
- Returns: A pointer to the newly created
ByteArray
.
NewByteArrayFromObj
func NewByteArrayFromObj(obj *object.Obj) (*ByteArray, error)
- Purpose: Creates a
ByteArray
from anobject.Obj
. - Parameters:
obj
(*object.Obj): The object to convert.
- Returns: A pointer to the created
ByteArray
and an error, if any.
getValueAsByteSlice
func getValueAsByteSlice(obj *object.Obj) ([]byte, error)
- Purpose: Converts an
object.Obj
's value to a byte slice. - Parameters:
obj
(*object.Obj): The object to convert.
- Returns: A byte slice and an error, if any.
getStringValueAsByteSlice
func getStringValueAsByteSlice(obj *object.Obj) ([]byte, error)
- Purpose: Converts a string
object.Obj
's value to a byte slice. - Parameters:
obj
(*object.Obj): The object to convert.
- Returns: A byte slice and an error, if any.
getByteArrayValueAsByteSlice
func getByteArrayValueAsByteSlice(obj *object.Obj) ([]byte, error)
- Purpose: Extracts the byte array value from an
object.Obj
of typeByteArray
. - Parameters:
obj
(*object.Obj): The object containing the byte array.
- Returns: The byte slice and an error, if any.
ByteSliceToObj
func ByteSliceToObj(store *dstore.Store, oldObj *object.Obj, b []byte, objType object.ObjectType) (*object.Obj, error)
- Purpose: Converts a byte slice to an
object.Obj
of the specified type. - Parameters:
store
(*dstore.Store): The data store.oldObj
(*object.Obj): The old object.b
([]byte): The byte slice to convert.objType
(object.ObjectType): The desired object type.
- Returns: A pointer to the new
object.Obj
and an error, if any.
ByteSliceToIntObj
func ByteSliceToIntObj(store *dstore.Store, oldObj *object.Obj, b []byte) (*object.Obj, error)
- Purpose: Converts a byte slice to an
object.Obj
with an integer value. - Parameters:
store
(*dstore.Store): The data store.oldObj
(*object.Obj): The old object.b
([]byte): The byte slice to convert.
- Returns: A pointer to the new
object.Obj
and an error, if any.
ByteSliceToStringObj
func ByteSliceToStringObj(store *dstore.Store, oldObj *object.Obj, b []byte) (*object.Obj, error)
- Purpose: Converts a byte slice to an
object.Obj
with a string value. - Parameters:
store
(*dstore.Store): The data store.oldObj
(*object.Obj): The old object.b
([]byte): The byte slice to convert.
- Returns: A pointer to the new
object.Obj
and an error, if any.
ByteSliceToByteArrayObj
func ByteSliceToByteArrayObj(store *dstore.Store, oldObj *object.Obj, b []byte) (*object.Obj, error)
- Purpose: Converts a byte slice to an
object.Obj
with aByteArray
value. - Parameters:
store
(*dstore.Store): The data store.oldObj
(*object.Obj): The old object.b
([]byte): The byte slice to convert.
- Returns: A pointer to the new
object.Obj
and an error, if any.
SetBit
func (b *ByteArray) SetBit(pos int, value bool)
- Purpose: Sets the bit at the given position to the specified value.
- Parameters:
pos
(int): The bit position to set.value
(bool): The value to set (true for 1, false for 0).
- Returns: None.
GetBit
func (b *ByteArray) GetBit(pos int) bool
- Purpose: Gets the bit at the given position.
- Parameters:
pos
(int): The bit position to get.
- Returns:
true
if the bit is 1,false
if it is 0.
BitCount
func (b *ByteArray) BitCount() int
- Purpose: Counts the number of bits set to 1 in the byte array.
- Parameters: None.
- Returns: The number of set bits.
IncreaseSize
func (b *ByteArray) IncreaseSize(increaseSizeTo int) *ByteArray
- Purpose: Increases the size of the
ByteArray
to the specified size. - Parameters:
increaseSizeTo
(int): The new size of the byte array.
- Returns: A pointer to the resized
ByteArray
.
ResizeIfNecessary
func (b *ByteArray) ResizeIfNecessary() *ByteArray
- Purpose: Resizes the
ByteArray
by removing trailing zero bytes. - Parameters: None.
- Returns: A pointer to the resized
ByteArray
.
DeepCopy
func (b *ByteArray) DeepCopy() *ByteArray
- Purpose: Creates a deep copy of the
ByteArray
. - Parameters: None.
- Returns: A pointer to the new
ByteArray
copy.
getBits
func (b *ByteArray) getBits(offset, width int, signed bool) int64
- Purpose: Extracts a sequence of bits from the
ByteArray
as an integer. - Parameters:
offset
(int): The starting position of the bit sequence.width
(int): The number of bits to extract.signed
(bool): Whether the resulting integer should be treated as signed.
- Returns: The extracted integer value.
setBits
func (b *ByteArray) setBits(offset, width int, value int64)
- Purpose: Sets a sequence of bits in the
ByteArray
to the bits of an integer value. - Parameters:
offset
(int): The starting position of the bit sequence.width
(int): The number of bits to set.value
(int64): The integer value whose bits will be set.
- Returns: None.
incrByBits
func (b *ByteArray) incrByBits(offset, width int, increment int64, overflow string, signed bool) (int64, error)
- Purpose: Increments a bitfield within the
ByteArray
by a given value, handling overflow according to the specified policy. - Parameters:
offset
(int): The starting bit position of the field.width
(int): The width of the bitfield.increment
(int64): The amount to increment by.overflow
(string): The overflow handling policy ("WRAP", "SAT", "FAIL").signed
(bool): Whether the bitfield is signed.
- Returns: The new value of the bitfield and an error, if any.
popcount
func popcount(x byte) byte
- Purpose: Counts the number of set bits (bits with a value of 1) in a byte.
- Parameters:
x
(byte): The byte to count the set bits in.
- Returns: The number of set bits in the byte.
reverseByte
func reverseByte(b byte) byte
- Purpose: Reverses the order of bits in a single byte.
- Parameters:
b
(byte): The byte to reverse.
- Returns: The reversed byte.
Code Examples
Creating a ByteArray and setting a bit
byteArray := NewByteArray(1) // Creates a byte array of size 1
byteArray.SetBit(0, true) // Sets the first bit to 1