binary

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2019 License: MIT Imports: 4 Imported by: 2

Documentation

Overview

Package binary implements utility functions to encode and decode values to and from a byte slice.

Index

Constants

This section is empty.

Variables

View Source
var BigEndian = binary.BigEndian
View Source
var LittleEndian = binary.LittleEndian
View Source
var TooShort = errors.New("byte slice is too short")

TooShort should be returned by Unmarshalers when a Scan function returns a nil byte slice.

Functions

func AppendBool

func AppendBool(b []byte, on bool) []byte

AppendBool encodes bool on to buffer b as a single byte where 0 is false and 1 is true.

func AppendBytes

func AppendBytes(b, bytes []byte) []byte

AppendBytes encodes the bytes slice to buffer b by first writing the length of the bytes slice followed by the byte slice iteself.

func AppendDuration

func AppendDuration(b []byte, d time.Duration) []byte

AppendDuration encodes time.Duration d by casting it to an int64 and calling AppendInt64.

func AppendFloat32

func AppendFloat32(b []byte, f float32) []byte

AppendFloat32 encodes the float f to buffer b.

func AppendFloat64

func AppendFloat64(b []byte, f float64) []byte

AppendFloat64 encodes the float f to buffer b.

func AppendInt

func AppendInt(b []byte, i int) []byte

AppendInt encodes int i by casting it to an int64 and calling AppendInt64.

func AppendInt16

func AppendInt16(b []byte, i int16) []byte

AppendInt16 encodes int16 i to buffer b.

func AppendInt32

func AppendInt32(b []byte, i int32) []byte

AppendInt32 encodes int32 i to buffer b.

func AppendInt64

func AppendInt64(b []byte, i int64) []byte

AppendInt64 encodes int64 i to buffer b.

func AppendPtr

func AppendPtr(b []byte, o Marshaler) []byte

AppendPtr appends a boolean nil indicator followed by Marshaler o.

func AppendString

func AppendString(b []byte, str string) []byte

AppendString encodes string str to buffer b by writing the length of str as an int64 followed by the string's bytes.

func AppendTime

func AppendTime(b []byte, t time.Time) []byte

AppendTime encodes time.Time t to buffer b by calling t's MarshalBinary method and appending the resulting bytes.

func AppendUint16

func AppendUint16(b []byte, i uint16) []byte

AppendUint16 encodes uint16 i to buffer b.

func AppendUint32

func AppendUint32(b []byte, i uint32) []byte

AppendUint32 encodes uint32 i to buffer b.

func AppendUint64

func AppendUint64(b []byte, i uint64) []byte

AppendUint64 encodes uint64 i to buffer b.

func Bits

func Bits(bits ...bool) int64

Bits returns an int64 whose bits are set to 1 when the corresponding bits parameter is true. E.g. Bite(true, true, false, true) results in b1011. Bits will panic when more than 63 bool values are povided as parameters.

func GetBytes

func GetBytes(b []byte) (data, remaining []byte)

GetBytes decodes a byte slice from buffer b and returns the slice followed by the remaining bytes. The encoded value is assumed to be a length followed by that many bytes. If there is insufficient data, both return values will be nil.

func GetNBytes

func GetNBytes(b []byte, n int) (data, remaining []byte)

GetNBytes decodes n bytes from buffer b and returns the slice followed by the remaining bytes. If there is insufficient data, both return values will be nil.

func ScanBool

func ScanBool(b []byte) ([]byte, bool)

ScanBool decodes a bool encoded using AppendBool.

func ScanBytes

func ScanBytes(b []byte) ([]byte, []byte)

ScanBytes decodes a byte slice encoded using AppendBytes.

func ScanDuration

func ScanDuration(b []byte) ([]byte, time.Duration)

ScanDuration decodes a time.Duration encoded using AppendDuration.

func ScanFloat32

func ScanFloat32(b []byte) ([]byte, float32)

ScanFloat32 decodes a float from buffer b encoded using AppendFloat32.

func ScanFloat64

func ScanFloat64(b []byte) ([]byte, float64)

ScanFloat32 decodes a float from buffer b encoded using AppendFloat64.

func ScanInt

func ScanInt(b []byte) ([]byte, int)

SpanInt decodes an int encoded using AppendInt.

func ScanInt16

func ScanInt16(b []byte) ([]byte, int16)

ScanInt16 decodes an int16 encoded using AppendInt16.

func ScanInt32

func ScanInt32(b []byte) ([]byte, int32)

ScanInt32 decodes an int32 encoded using AppendInt32.

func ScanInt64

func ScanInt64(b []byte) ([]byte, int64)

ScanInt64 decodes an int64 encoded using AppendInt64.

func ScanPtr

func ScanPtr(b []byte, ctor func() Unmarshaler) []byte

ScanPtr decodes an object encoded using AppendPtr. It first reads a bool indicating whether the encoded value is nil. When the value is not nil, it calls ctor method to receive an Unmarshaler. Then is calls Unmarshal to decode the object.

func ScanString

func ScanString(b []byte) ([]byte, string)

ScanString decodes a string encoded using AppendString.

func ScanTime

func ScanTime(b []byte) ([]byte, time.Time)

ScanTime decodes a time.Time encoded using AppendTime.

func ScanUint16

func ScanUint16(b []byte) ([]byte, uint16)

ScanUint16 decodes a uint16 encoded using AppendUint16.

func ScanUint32

func ScanUint32(b []byte) ([]byte, uint32)

ScanUint32 decodes a uint32 encoded using AppendUint32.

func ScanUint64

func ScanUint64(b []byte) ([]byte, uint64)

ScanUint64 decodes a uint32 encoded using AppendUint64.

Types

type Marshaler

type Marshaler interface {
	// MarshalAppend encodes itself and append the resulting bytes to
	// the provided byte slice and return the new byte slice.
	MarshalAppend([]byte) []byte
}

Marshaler defines an interface with a MarshalAppend method.

type Unmarshaler

type Unmarshaler interface {
	// Unmarshal decodes itself from the provided byte slice returning any
	// bytes remaining. TooShort is returned when the provided slice is
	// too short. Other errors are returned when decoding fails.
	Unmarshal([]byte) ([]byte, error)
}

Unmarshaler defines an interface with an Unmarshal method. TooShort should be returned when a Scan function returns a nil byte slice.

Jump to

Keyboard shortcuts

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