Documentation
¶
Overview ¶
Package vle provides variable length encoding (un-)marshaling. Format:
- most significant bit of each byte is 0 if it's the last byte
- for signed numbers:
- the second most significant bit of the first byte is 1 if it's negative
- the rest is the int itself if it's >=0 or abs(the int) - 1 otherwise
- the 7 least significant bits of the last byte are the 7 least significant bits of the encoded value
- then walking backwards, the 7 least significant bits of each byte are the next 7 bits of the encoded value
Index ¶
- Variables
- func DummySigned[N constraints.Signed]() []byte
- func DummyUnsigned() []byte
- func EncodeSigned[N constraints.Signed](n N) []byte
- func EncodeUnsigned[N constraints.Unsigned](n N) []byte
- func ReadSigned[N constraints.Signed](r BufioReader) (N, int, error)
- func ReadUnsigned[N constraints.Unsigned](r BufioReader) (N, int, error)
- type BufioReader
Constants ¶
This section is empty.
Variables ¶
var Dummy = errors.New("vle parse error: dummy value found")
Functions ¶
func DummySigned ¶
func DummySigned[N constraints.Signed]() []byte
DummySigned returns a sequence of bytes that will be recognized by ReadSigned[THE SAME TYPE] as a signed dummy value. It's one byte longer than the maximum possible len of the EncodeSigned result for the same type.
func DummyUnsigned ¶
func DummyUnsigned() []byte
DummyUnsigned returns a sequence of bytes that will be recognized by ReadUnsigned as an unsigned dummy value.
func EncodeSigned ¶
func EncodeSigned[N constraints.Signed](n N) []byte
EncodeSigned marshals a signed integer.
func EncodeUnsigned ¶
func EncodeUnsigned[N constraints.Unsigned](n N) []byte
EncodeUnsigned marshals an unsigned integer.
func ReadSigned ¶
func ReadSigned[N constraints.Signed](r BufioReader) (N, int, error)
ReadSigned reads and parses a signed integer. It returns the integer, the number of bytes Discard()ed from the reader, and an error. Note the error can be non-nil even if an integer was successfully read and parsed. The real test to know if an integer was parsed is to check that the number of bytes discarded (second returned item) is >0. If the sequence of bytes starts with whatever is returned by DummySigned[N](), the returned N is 0 and the error is vle.Dummy.
func ReadUnsigned ¶
func ReadUnsigned[N constraints.Unsigned](r BufioReader) (N, int, error)
ReadUnsigned reads and parses an unsigned integer. It returns the integer, the number of bytes Discard()ed from the reader, and an error. Note the error can be non-nil even if an integer was successfully read and parsed. The real test to know if an integer was parsed is to check that the number of bytes discarded (second returned item) is >0. If the sequence of bytes starts with whatever is returned by DummyUnsigned(), the returned N is 0 and the error is vle.Dummy.