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 Track(fn func([]byte))
- type Arena
- type Buffer
- func (b *Buffer) Length() int
- func (b *Buffer) Range(offset, length int, fn func(offset int, data []byte))
- func (b *Buffer) Read(buf []byte) (int, error)
- func (b *Buffer) ReadAt(buf []byte, off int64) (int, error)
- func (b *Buffer) Release()
- func (b *Buffer) Seek(offset int64, whence int) (int64, error)
- func (b *Buffer) Write(buf []byte) (int, error)
- func (b *Buffer) WriteAt(buf []byte, off int64) (int, error)
- 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) TimeUnix() time.Time
- 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) UseArena(arena *Arena)
- 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) Error() error
- 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) TimeUnix(ts time.Time)
- 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 ErrEmptyDelimiter = errors.New("empty delimiter")
ErrEmptyDelimiter is returned if a provided delimiter is empty.
var ErrInvalidOffset = errors.New("invalid offset")
ErrInvalidOffset is return for offsets that under or overflow the buffer.
var ErrInvalidSize = errors.New("invalid size")
ErrInvalidSize is returned if a provided number size is invalid.
var ErrNumberOverflow = errors.New("number overflow")
ErrNumberOverflow is returned if a provided number overflows its size.
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.
Types ¶
type Arena ¶
type Arena struct {
// contains filtered or unexported fields
}
Arena is a basic arena allocator that allocates fixed size buffers to provide memory for many small buffers.
func NewArena ¶ added in v0.6.0
NewArena creates and returns a new arena using the specified pool and buffer size. The arena is obtained from a global pool and recycled upon release.
type Buffer ¶ added in v0.6.1
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is basic buffer that dynamically allocates needed chunks.
func NewBuffer ¶ added in v0.6.1
NewBuffer will return a new buffer that uses the provided pool and allocation size to dynamically allocate chunks as needed to hold the data.
func (*Buffer) Range ¶ added in v0.6.2
Range will iterate over the buffer in the given range and call the provided function with the offset and data for each chunk.
func (*Buffer) Release ¶ added in v0.6.1
func (b *Buffer) Release()
Release will release the buffer and all memory.
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) UseArena ¶ added in v0.6.0
UseArena will use the specified arena for string and bytes cloning.
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. Also values above 32 MiB are allocated using the Go allocator to ensure not used memory is available to be freed immediately if not used anymore.
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.