Documentation ¶
Overview ¶
Package fpack provides a functional approach to encoding and decoding byte sequences.
Example ¶
// encode buf, ref, err := Encode(Global(), func(enc *Encoder) error { enc.Uint8(42) enc.FixString("Hello World!", 2) return nil }) if err != nil { panic(err) } // ensure release defer ref.Release() // decode var num uint8 var str string err = Decode(buf, func(dec *Decoder) error { num = dec.Uint8() str = dec.FixString(2, false) return nil }) if err != nil { panic(err) } // print fmt.Println(len(buf)) fmt.Println(num) fmt.Println(str)
Output: 15 42 Hello World!
Index ¶
- Variables
- func Decode(bytes []byte, fn func(dec *Decoder) error) error
- func EncodeInto(buf []byte, fn func(enc *Encoder) error) (int, error)
- func Measure(fn func(enc *Encoder) error) (int, error)
- func MustDecode(bytes []byte, fn func(dec *Decoder)) bool
- func MustEncodeInto(buf []byte, fn func(enc *Encoder)) (int, bool)
- func MustMeasure(fn func(enc *Encoder)) int
- type Arena
- type Decoder
- func (d *Decoder) Bool() bool
- func (d *Decoder) Bytes(length int, clone bool) []byte
- func (d *Decoder) DelBytes(delim []byte, clone bool) []byte
- func (d *Decoder) DelString(delim string, clone bool) string
- func (d *Decoder) Error() error
- func (d *Decoder) FixBytes(lenSize int, clone bool) []byte
- func (d *Decoder) FixString(lenSize int, clone bool) string
- func (d *Decoder) Float32() float32
- func (d *Decoder) Float64() float64
- func (d *Decoder) Int(size int) int64
- func (d *Decoder) Int16() int16
- func (d *Decoder) Int32() int32
- func (d *Decoder) Int64() int64
- func (d *Decoder) Int8() int8
- func (d *Decoder) Length() int
- func (d *Decoder) Remaining() bool
- func (d *Decoder) Reset(buf []byte)
- func (d *Decoder) Skip(num int)
- func (d *Decoder) String(length int, clone bool) string
- func (d *Decoder) Tail(clone bool) []byte
- func (d *Decoder) Uint(size int) uint64
- func (d *Decoder) Uint16() uint16
- func (d *Decoder) Uint32() uint32
- func (d *Decoder) Uint64() uint64
- func (d *Decoder) Uint8() uint8
- func (d *Decoder) UseLittleEndian()
- func (d *Decoder) VarBytes(clone bool) []byte
- func (d *Decoder) VarInt() int64
- func (d *Decoder) VarString(clone bool) string
- func (d *Decoder) VarUint() uint64
- type Encoder
- func (e *Encoder) Bool(yes bool)
- func (e *Encoder) Bytes(buf []byte)
- func (e *Encoder) Counting() bool
- func (e *Encoder) DelBytes(buf, delim []byte)
- func (e *Encoder) DelString(str, delim string)
- func (e *Encoder) FixBytes(buf []byte, lenSize int)
- func (e *Encoder) FixString(str string, lenSize int)
- func (e *Encoder) Float32(num float32)
- func (e *Encoder) Float64(num float64)
- func (e *Encoder) Int(n int64, size int)
- func (e *Encoder) Int16(num int16)
- func (e *Encoder) Int32(num int32)
- func (e *Encoder) Int64(num int64)
- func (e *Encoder) Int8(num int8)
- func (e *Encoder) Length() int
- func (e *Encoder) Reset(buf []byte)
- func (e *Encoder) Skip(num int)
- func (e *Encoder) String(str string)
- func (e *Encoder) Tail(buf []byte)
- func (e *Encoder) Uint(num uint64, size int)
- func (e *Encoder) Uint16(num uint16)
- func (e *Encoder) Uint32(num uint32)
- func (e *Encoder) Uint64(num uint64)
- func (e *Encoder) Uint8(num uint8)
- func (e *Encoder) UseLittleEndian()
- func (e *Encoder) VarBytes(buf []byte)
- func (e *Encoder) VarInt(num int64)
- func (e *Encoder) VarString(str string)
- func (e *Encoder) VarUint(num uint64)
- type Pool
- type Ref
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrBufferTooShort = errors.New("buffer too short")
ErrBufferTooShort is returned if the provided buffer is too short.
var ErrRemainingBytes = errors.New("remaining bytes")
ErrRemainingBytes is returned if the provided buffer is not fully consumed.
Functions ¶
func Decode ¶
Decode will decode data using the provided decoding function. The function is run once to decode the data. It will return ErrBufferTooShort if the buffer was not long enough to read all data, ErrRemainingBytes if the provided buffers has not been full consumed or any error returned by the callback.
func EncodeInto ¶
EncodeInto will encode data into the specified byte slice using the provided encoding function. The function is run once to assess the length of the buffer and once to encode the data. Any error returned by the callback is returned immediately. If the provided buffer is too small ErrBufferTooShort is returned.
func Measure ¶
Measure will measure the required byte slice to run the provided encoding function. Any error returned by the callback is returned immediately.
func MustDecode ¶
MustDecode wraps Decode but omits error propagation. It will return false if the buffer was not long enough to read all data or the buffer has not been fully consumed.
func MustEncodeInto ¶
MustEncodeInto wraps EncodeInto but omits the error propagation. It will return false if the buffer was not long enough to write all data.
func MustMeasure ¶
MustMeasure wraps Measure but omits the error propagation.
Types ¶
type Arena ¶
Arena is a basic arena allocator that allocates fixed size buffers to provide memory for many small buffers. The memory must be returned all at once when no longer needed.
type Decoder ¶
type Decoder struct {
// contains filtered or unexported fields
}
Decoder manages data decoding.
func (*Decoder) Bytes ¶
Bytes reads a raw byte slice. If the byte slice is not cloned it may change if the source byte slice changes.
func (*Decoder) DelBytes ¶
DelBytes reads a suffix delimited byte slice. If the byte slice is not cloned it may change if the source byte slice changes.
func (*Decoder) DelString ¶
DelString reads a suffix delimited string. If the string is not cloned it may change if the source byte slice changes.
func (*Decoder) FixBytes ¶
FixBytes reads a fixed length prefixed byte slice. If the byte slice is not cloned it may change if the source byte slice changes.
func (*Decoder) FixString ¶
FixString reads a fixed length prefixed string. If the string is not cloned it may change if the source byte slice changes.
func (*Decoder) String ¶
String reads a raw string. If the string is not cloned it may change if the source byte slice changes.
func (*Decoder) Tail ¶
Tail reads a tail byte slice. If the byte slice is not cloned it may change if the source byte slice changes.
func (*Decoder) UseLittleEndian ¶
func (d *Decoder) UseLittleEndian()
UseLittleEndian will set the used binary byte order to little endian.
func (*Decoder) VarBytes ¶
VarBytes reads a variable length prefixed byte slice. If the byte slice is not cloned it may change if the source byte slice changes.
type Encoder ¶
type Encoder struct {
// contains filtered or unexported fields
}
Encoder manages data encoding.
func (*Encoder) UseLittleEndian ¶
func (e *Encoder) UseLittleEndian()
UseLittleEndian will set the used binary byte order to little endian.
type Pool ¶
type Pool struct {
// contains filtered or unexported fields
}
Pool is dynamic slice length pool.
func (*Pool) Borrow ¶
Borrow will return a slice that has the specified length. If the requested length is too small or too big, a slice will be allocated. To recycle the slice, it must be released by calling Release() on the returned Ref value. Always release any returned value, even if the slice grows, it is possible to at least return the originally requested slice. If zero is true, the returned slice will be zeroed (but not the full underlying buffer).
Note: For values up to 8 bytes (64 bits) the internal Go arena allocator is used by calling make(). From benchmarks this seems to be faster than calling the pool to borrow and return a value.
type Ref ¶
type Ref struct {
// contains filtered or unexported fields
}
Ref is a reference to a borrowed slice. A zero reference represents a no-op reference.
func Encode ¶
Encode will encode data using the provided encoding function. The function is run once to assess the length of the buffer and once to encode the data. Any error returned by the callback is returned immediately.
func MustEncode ¶
MustEncode wraps Encode but omits the error propagation.