vle

package module
v0.0.0-...-3c8dcaa Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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.

Types

type BufioReader

type BufioReader interface {
	Discard(n int) (discarded int, err error)
	Peek(n int) ([]byte, error)
}

BufioReader is an interface used to read in a buffered manner. In practice, it should often be a bufio.Reader.

Jump to

Keyboard shortcuts

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