Documentation ¶
Overview ¶
Package rye is a helper for writing serializing and deserializing logic.
There are two related compact formats. One for Uint64 and one for []byte. The []byte format is PrefixSlice. If the slice is one byte and that byte is less than 129, it is written directly. So all one byte values upto 128 are encoded in a single byte. The byte 129 is reseverved for a nil slice. For any slice up to 121 bytes, the length is encoded in a single slice as len+129. For values longer than 121 bytes, a meta length is encoded as metalength+250. Then the data length is encoded with that many bytes little-endian.
Compact Uint64s are encoded similarly. Any value up to and including 128 is encoded as a single byte. Values above 128 have their length encoded as 121+len and then the value is encoded little-endian. It would be slightly more efficient to have length encoding start at 247, allowing for more single byte values. But this strategy was chosen for consistency. Converting a Uint64 to a little-endian byte slice and encoding that will produce the same output as encoding the Uint64 directly.
For signed ints, simply casting them will not work well. Twos compliment means that for any negative value the most significant bit will be 1 and so all 8 bytes would always be written. Instead, the absolute value is bit shifted left one and the sign is placed in the least significant bit.
Index ¶
- func Inverse(bs []byte)
- func Reverse(b []byte)
- type Bits
- func (b *Bits) Copy() *Bits
- func (b *Bits) Read() byte
- func (b *Bits) ReadSubBits(bitLn byte) *Bits
- func (b *Bits) ReadUint(n byte) uint64
- func (b *Bits) Reset() *Bits
- func (b *Bits) ShallowCopy() *Bits
- func (b *Bits) Write(bit byte) *Bits
- func (b *Bits) WriteBits(from *Bits) *Bits
- func (b *Bits) WriteSubBits(sub *Bits, bitLn byte)
- func (b *Bits) WriteUint(u uint64, n byte)
- type D
- func (D) Float64(b []byte) float64
- func (d D) Float64Ordered(b []byte) float64
- func (d D) Int16(b []byte) int16
- func (d D) Int16Ordered(b []byte) int16
- func (d D) Int32(b []byte) int32
- func (d D) Int32Ordered(b []byte) int32
- func (d D) Int64(b []byte) int64
- func (d D) Int64Ordered(b []byte) int64
- func (D) Uint16(b []byte) uint16
- func (D) Uint32(b []byte) uint32
- func (D) Uint64(b []byte) uint64
- type Deserializer
- func (d *Deserializer) Byte() byte
- func (d *Deserializer) Done() bool
- func (d *Deserializer) Float32() float32
- func (d *Deserializer) Float64() float64
- func (d *Deserializer) Int16() int16
- func (d *Deserializer) Int32() int32
- func (d *Deserializer) Int64() int64
- func (d *Deserializer) Int8() int8
- func (d *Deserializer) Slice(ln int) []byte
- func (d *Deserializer) String(ln int) string
- func (d *Deserializer) Sub(ln int) *Deserializer
- func (d *Deserializer) Uint(size byte) uint64
- func (d *Deserializer) Uint16() uint16
- func (d *Deserializer) Uint32() uint32
- func (d *Deserializer) Uint64() uint64
- func (d *Deserializer) Uint8() uint8
- type S
- func (s S) Any(i any, buf []byte) []byte
- func (s S) Float64(b []byte, f float64)
- func (s S) Float64Ordered(b []byte, f float64)
- func (s S) Int16(b []byte, x int16)
- func (s S) Int16Ordered(b []byte, x int16)
- func (s S) Int32(b []byte, x int32)
- func (s S) Int32Ordered(b []byte, x int32)
- func (s S) Int64(b []byte, x int64)
- func (s S) Int64Ordered(b []byte, x int64)
- func (S) Uint16(b []byte, x uint16)
- func (S) Uint32(b []byte, x uint32)
- func (S) Uint64(b []byte, x uint64)
- type Serializer
- func (s *Serializer) Byte(b byte)
- func (s *Serializer) CheckFree(ln int)
- func (s *Serializer) Float32(f float32)
- func (s *Serializer) Float64(f float64)
- func (s *Serializer) Int16(x int16)
- func (s *Serializer) Int32(x int32)
- func (s *Serializer) Int64(x int64)
- func (s *Serializer) Int8(x int8)
- func (s *Serializer) Make() *Serializer
- func (s *Serializer) Slice(data []byte)
- func (s *Serializer) String(data string)
- func (s *Serializer) Sub(ln int) *Serializer
- func (s *Serializer) Uint(size byte, value uint64) byte
- func (s *Serializer) Uint16(x uint16)
- func (s *Serializer) Uint32(x uint32)
- func (s *Serializer) Uint64(x uint64)
- func (s *Serializer) Uint8(x uint8)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Bits ¶
Bits supports reading and writing individual bits.
func (*Bits) ReadSubBits ¶
ReadSubBits reads a *Bits using bitLn to decode the length of the Bits.
func (*Bits) ShallowCopy ¶
ShallowCopy shares the underlying Data, but not the Ln or Idx values.
func (*Bits) WriteBits ¶
WriteBits takes all the bits in "from" starting at from.Idx to from.Ln and writes them to b. The value of from.Idx is reset to 0 after this.
func (*Bits) WriteSubBits ¶
WriteSubBits writes contents of sub with the length encoded with bitLn bits. This can be used to combine multiple *Bits all encoded with a common bitLn.
type D ¶
type D byte
var Deserialize D
func (D) Float64Ordered ¶
func (D) Int16Ordered ¶
func (D) Int32Ordered ¶
func (D) Int64Ordered ¶
type Deserializer ¶
Deserializer provides a helper for deserializing binary data
func NewDeserializer ¶
func NewDeserializer(data []byte) *Deserializer
NewDeserializer returns a Deserializer prepared to deserialize the provided data
func (*Deserializer) Byte ¶
func (d *Deserializer) Byte() byte
Byte returns one byte from the Deserializer and increases the index.
func (*Deserializer) Done ¶
func (d *Deserializer) Done() bool
Done checks if the Index it at or past the end of the Data.
func (*Deserializer) Slice ¶
func (d *Deserializer) Slice(ln int) []byte
Slice of a known length is returned.
func (*Deserializer) Sub ¶
func (d *Deserializer) Sub(ln int) *Deserializer
Sub returns a Sub-Deserializer of a given length. The index of the parent is placed at the end of the data allocated to the Sub-Deserializer.
func (*Deserializer) Uint ¶
func (d *Deserializer) Uint(size byte) uint64
Uint of a known number of bytes is returned.
func (*Deserializer) Uint16 ¶
func (d *Deserializer) Uint16() uint16
Uint16 returns a uint16 from the Deserializer and increases the index.
func (*Deserializer) Uint32 ¶
func (d *Deserializer) Uint32() uint32
Uint32 returns a uint32 from the Deserializer and increases the index.
func (*Deserializer) Uint64 ¶
func (d *Deserializer) Uint64() uint64
Uint64 returns a uint64 from the Deserializer and increases the index.
func (*Deserializer) Uint8 ¶
func (d *Deserializer) Uint8() uint8
Uint8 returns a uint8 from the Deserializer and increases the index.
type S ¶
type S byte
TODO: why isn't this struct{}
var Serialize S
func (S) Float64Ordered ¶
func (S) Int16Ordered ¶
func (S) Int32Ordered ¶
func (S) Int64Ordered ¶
type Serializer ¶
Serializer is used to Serialize into the Data field.
func (*Serializer) Byte ¶
func (s *Serializer) Byte(b byte)
Byte writes a byte to the Serializer and increases the index.
func (*Serializer) CheckFree ¶
func (s *Serializer) CheckFree(ln int)
CheckFree guarentees that there is at least ln free bytes after index, appending more bytes if necessary.
func (*Serializer) Float32 ¶
func (s *Serializer) Float32(f float32)
Float32 writes a float32 to the Serializer and increases the index.
func (*Serializer) Float64 ¶
func (s *Serializer) Float64(f float64)
Float64 writes a float64 to the Serializer and increases the index.
func (*Serializer) Int16 ¶
func (s *Serializer) Int16(x int16)
Int16 writes a int16 to the Serializer and increases the index.
func (*Serializer) Int32 ¶
func (s *Serializer) Int32(x int32)
Int32 writes a int32 to the Serializer and increases the index.
func (*Serializer) Int64 ¶
func (s *Serializer) Int64(x int64)
Int64 writes a int64 to the Serializer and increases the index.
func (*Serializer) Int8 ¶
func (s *Serializer) Int8(x int8)
Int8 writes a int8 to the Serializer and increases the index.
func (*Serializer) Make ¶
func (s *Serializer) Make() *Serializer
Make will set Data to the length of Size. If Data is already populated, it will be appeded to.
func (*Serializer) Slice ¶
func (s *Serializer) Slice(data []byte)
Slice writes a byte slice to the Serializer and increases the index.
func (*Serializer) String ¶
func (s *Serializer) String(data string)
String writes a string to the Serializer and increases the index.
func (*Serializer) Sub ¶
func (s *Serializer) Sub(ln int) *Serializer
Sub serializer of a specific length.
func (*Serializer) Uint ¶
func (s *Serializer) Uint(size byte, value uint64) byte
Uint serializes the value little-endian. If size is >0, that number of bytes will be written. If size==0, it will write the value with fewest bytes, omitting leading zeros. The returned byte indicates the number of bytes written. Size should be between 0 and 8, but it is not checked. This does not increase the Idx.
func (*Serializer) Uint16 ¶
func (s *Serializer) Uint16(x uint16)
Uint16 writes a uint16 to the Serializer and increases the index.
func (*Serializer) Uint32 ¶
func (s *Serializer) Uint32(x uint32)
Uint32 writes a uint32 to the Serializer and increases the index.
func (*Serializer) Uint64 ¶
func (s *Serializer) Uint64(x uint64)
Uint64 writes a uint64 to the Serializer and increases the index.
func (*Serializer) Uint8 ¶
func (s *Serializer) Uint8(x uint8)
Uint8 writes a uint8 to the Serializer and increases the index.