Documentation ¶
Overview ¶
Package gopack provides utilities for performing bit packing.
Index ¶
- func Pack(b []byte, strct interface{})
- func PackSigned(target uint64, val int64, lsb, width uint8) uint64
- func PackUnsigned(target, val uint64, lsb, width uint8) uint64
- func PackedSizeof(strct interface{}) int
- func Unpack(b []byte, strct interface{})
- func UnpackSigned(target uint64, lsb, width uint8) int64
- func UnpackUnsigned(target uint64, lsb, width uint8) uint64
- type Error
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Pack ¶
func Pack(b []byte, strct interface{})
Pack the fields of strct into b. Fields must be of an int or bool type, or must be of a struct type whose fields are properly typed (structs may be nested arbitrarily deep). If strct is not a struct or a pointer to a struct, or if any of the fields are not of an allowed type, Pack will panic.
Fields may optionally be tagged with a size in bits using the key "gopack".
type unixMode struct { User, Group, Other uint8 `gopack:"3"` }
Fields without a tag are taken to be their native sizes (for example, 8 bits for uint8, 16 for int16, etc). If a field is tagged with an impossible size (less than 1, or larger than the native size of the field), or if the tag value cannot be parsed as an integer, Pack will panic. If a field holds a value which cannot be packed in the specified number of bits, Pack will panic (for example, if unixMode.User from the above example were set to 8, which cannot be stored in 3 bits).
bool-typed fields always take up 1 bit, and any field tags are ignored.
If there are bits in the last used byte of b which are beyond the end of the packed data (for example, the last four bits of the second byte when packing 12 bits), those bits will be zeroed. If b is not sufficiently long to hold all of the bits of strct, Pack will panic.
Fields which are not exported are ignored, and take up no space in the packing.
// Only requires 2 bytes to store type person struct { name string Age, Height uint8 }
func PackSigned ¶
Pack val into bits [lsb, lsb + width) in target, returning the new value of target. If val doesn't fit in width bits, the behavior of PackSigned is undefined.
func PackUnsigned ¶
Pack val into bits [lsb, lsb + width) in target, returning the new value of target. If val doesn't fit in width bits, the behavior of PackUnsigned is undefined.
func PackedSizeof ¶
func PackedSizeof(strct interface{}) int
PackedSizeof returns the number of bytes needed to pack the given value.
func Unpack ¶
func Unpack(b []byte, strct interface{})
Unpack the data in b into the fields of strct. strct must be either a struct or a pointer to a struct, or else Unpack will panic. However, if strct is not a pointer, all values extracted from b will be discarded since strct is passed by value.
All of the restrictions on the type of strct documented for Pack apply to Unpack.
If b is not sufficiently long to hold all of the bits of strct, Unpack will panic.
func UnpackSigned ¶
Unpack the value stored in [lsb, lsb + width) in target.
func UnpackUnsigned ¶
Unpack the value stored in [lsb, lsb + width) in target.