gopack

package module
v0.0.0-...-2e9b5e5 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2016 License: BSD-3-Clause Imports: 6 Imported by: 0

README

gopack Build Status

Bitpacking for Go.

##Usage

// Define arbitrary structs for packing.
type color struct {
  R, G, B uint8
}

red := color{255, 0, 0}
b := make([]byte, 3)
gopack.Pack(b, red)

// Use field tags to specify custom bit widths.
type unixMode struct {
  User, Group, Other uint8 `gopack:"3"`
}

// Embed structs nested arbitrarily deep.
type file struct {
  Size uint32
  Mode unixMode
}

// Unexported fields are ignored.
type person struct {
  name string
  Age uint8
}

// Use int types and bool types.
type fileHandle struct {
  File file
  Open bool
  Seek uint32
}

##Documentation

See the documentation.

Documentation

Overview

Package gopack provides utilities for performing bit packing.

Index

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

func PackSigned(target uint64, val int64, lsb, width uint8) uint64

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

func PackUnsigned(target, val uint64, lsb, width uint8) uint64

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

func UnpackSigned(target uint64, lsb, width uint8) int64

Unpack the value stored in [lsb, lsb + width) in target.

func UnpackUnsigned

func UnpackUnsigned(target uint64, lsb, width uint8) uint64

Unpack the value stored in [lsb, lsb + width) in target.

Types

type Error

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

Any panic originating from this package will be of type Error.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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