Skip to content

std/encoding/binary

import "github.com/eugenioenko/goalchemy/std/encoding/binary"

Package binary translates between unsigned integers and byte sequences, and encodes varints.

const (
MaxVarintLen16 = 3
MaxVarintLen32 = 5
MaxVarintLen64 = 10
)

Maximum encoded lengths of varints.

var BigEndian bigEndian

BigEndian is the big-endian ByteOrder and AppendByteOrder.

var LittleEndian littleEndian

LittleEndian is the little-endian ByteOrder and AppendByteOrder.

func AppendUvarint(buf []byte, x uint64) []byte

AppendUvarint appends the varint encoding of x to buf.

func AppendVarint(buf []byte, x int64) []byte

AppendVarint appends the zig-zag varint encoding of x to buf.

func PutUvarint(buf []byte, x uint64) int

PutUvarint encodes x into buf, which must be large enough, and returns the number of bytes written.

func PutVarint(buf []byte, x int64) int

PutVarint encodes x with zig-zag varint encoding into buf.

func Uvarint(buf []byte) (uint64, int)

Uvarint decodes a varint from buf and returns the value and the number of bytes read. n == 0 means buf is too short; n < 0 means the value overflows 64 bits and -n bytes were read.

func Varint(buf []byte) (int64, int)

Varint decodes a zig-zag varint from buf like Uvarint.

type AppendByteOrder interface {
AppendUint16([]byte, uint16) []byte
AppendUint32([]byte, uint32) []byte
AppendUint64([]byte, uint64) []byte
String() string
}

AppendByteOrder appends unsigned integers in a byte order.

type ByteOrder interface {
Uint16([]byte) uint16
Uint32([]byte) uint32
Uint64([]byte) uint64
PutUint16([]byte, uint16)
PutUint32([]byte, uint32)
PutUint64([]byte, uint64)
String() string
}

ByteOrder reads and writes unsigned integers in a byte order.