fastbytes

package module
v2.3.0 Latest Latest
Warning

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

Go to latest
Published: Jan 28, 2023 License: MIT Imports: 4 Imported by: 1

README

Test Go Report CardGo Reference

FastBytes

FastBytes is a go package for translating between slices with fixed-size integers/floats and byte slices.

Supported Types

Floats and all signed/unsigned integers except uint and int can be translated by this package. uint and int are not supported since their size is platform dependent.

Usage

GoDoc

Usage of assembly code

This package uses assembly for copying data on certain platforms. To disable the usage of assembly set the purego build tag when building.

Usage of unsafe.Pointer

This package uses the unsafe package to covert between slice/array types and to extract pointers from interface{} values. To disable the usage of unsafe set the no_unsafe build tag when building. Note that this also disables the usage of assembly.

Documentation

Index

Constants

View Source
const (
	// ErrUnsupported the given type is not supported.
	// All signed and unsigned integers except uint and int, and floats are supported
	// uint and int are unsupported since their size is platform dependent.
	ErrUnsupported = errors.Const("fastbytes: unsupported target/source type")
	// ErrUnaddressable the given [reflect.Value] cannot be addressed
	ErrUnaddressable = errors.Const("fastbytes: un-addressable value")
	// ErrOffset returned by [Endianess.FromValueOffset] and [Endianess.ToValueOffset] if the given offsets are not valid.
	ErrOffset = errors.Const("fastbytes: invalid offsets given")
)
View Source
const (
	// ErrUnadressable typo
	//
	// Deprecated: Use [ErrUnaddressable] instead.
	ErrUnadressable = ErrUnaddressable
)

Variables

View Source
var (
	// BigEndian copies bytes to and from big endian byte slices
	BigEndian = Endianess{/* contains filtered or unexported fields */}
	// LittleEndian copies bytes to and from little endian byte slices
	LittleEndian = Endianess{/* contains filtered or unexported fields */}
)

Functions

This section is empty.

Types

type ByteOrder

type ByteOrder interface {
	// FromI8 converts and copies bytes from `src` into `dst`.
	// The number of bytes copied is min(len(src), len(dst))
	FromI8(src []int8, dst []byte) (n int)
	// FromI16 converts and copies []int16 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*2, len(dst))
	FromI16(src []int16, dst []byte) (n int)
	// FromU16 converts and copies []uint16 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*2, len(dst))
	FromU16(src []uint16, dst []byte) (n int)
	// FromI32 converts and copies []int32 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*4, len(dst))
	FromI32(src []int32, dst []byte) (n int)
	// FromU32 converts and copies []uint32 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*4, len(dst))
	FromU32(src []uint32, dst []byte) (n int)
	// FromF32 converts and copies []float32 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*4, len(dst))
	FromF32(src []float32, dst []byte) (n int)
	// FromI64 converts and copies []int64 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*8, len(dst))
	FromI64(src []int64, dst []byte) (n int)
	// FromU64 converts and copies []int64 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*8, len(dst))
	FromU64(src []uint64, dst []byte) (n int)
	// FromF64 converts and copies []float64 from `src` into `dst`.
	// The number of bytes copied is min(len(src)*8, len(dst))
	FromF64(src []float64, dst []byte) (n int)
	// ToI8 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst))
	ToI8(src []byte, dst []int8) (n int)
	// ToI16 converts and copies bytes form `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*2)
	ToI16(src []byte, dst []int16) (n int)
	// ToU16 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*2)
	ToU16(src []byte, dst []uint16) (n int)
	// ToI32 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*4)
	ToI32(src []byte, dst []int32) (n int)
	// ToU32 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*4)
	ToU32(src []byte, dst []uint32) (n int)
	// ToF32 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*4)
	ToF32(src []byte, dst []float32) (n int)
	// ToI64 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*8)
	ToI64(src []byte, dst []int64) (n int)
	// ToU64 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*8)
	ToU64(src []byte, dst []uint64) (n int)
	// ToU64 converts and copies bytes from `src` into `dst`
	// The number of bytes copied is min(len(src), len(dst)*8)
	ToF64(src []byte, dst []float64) (n int)

	// To copies bytes from `s` into the given slice.
	// The given interface must be a type  that can be safely written to.
	// The number of bytes copied is min(len(src), len(dst)* element size of dst)
	To(src []byte, dst interface{}) (n int, err error)
	// From copies bytes from the given interface.
	// The provided interface must be a type that can be safely copied.
	// The number of bytes copied is min(len(src)* element size of dst, len(dst))
	From(src interface{}, dst []byte) (n int, err error)
	// ToValue copies bytes from `src` into the given value
	// The given interface must be a type that can be safely written to.
	// The number of bytes copied is min(len(src), len(dst)* element size of dst)
	ToValue(src []byte, dst reflect.Value) (n int, err error)
	// FromValue copies bytes from the given value.
	// The provided value must be a type that can be safely converted to bytes.
	// The number of bytes copied is min(len(src)* element size of dst, len(dst))
	FromValue(src reflect.Value, dst []byte) (n int, err error)
	// FromValueOffset copies bytes from the given value.
	// This is the equivalent of calling FromValue(src.Slice(start, end), dst).
	// This avoids the allocation in [reflect.Value.Slice].
	// The provided value must be a type that can be safely converted to bytes.
	// The number of bytes copied is min(len(src)* element size of dst, len(dst))
	FromValueOffset(src reflect.Value, dst []byte, start, end int) (int, error)
	// ToValueOffset copies bytes from `src` into the given value
	// This is the equivalent of calling ToValue(src, dst.Slice(start, end)).
	// This avoids the allocation in [reflect.Value.Slice].
	// The given interface must be a type that can be safely written to.
	// The number of bytes copied is min(len(src), len(dst)* element size of dst)
	ToValueOffset(src []byte, dst reflect.Value, start, end int) (int, error)
	// contains filtered or unexported methods
}

ByteOrder a byteorder.

It is recommended to use Endianess instead.

type Endianess added in v2.2.0

type Endianess struct {
	// contains filtered or unexported fields
}

Endianess provides methods to read and write integer/float slices.

func (*Endianess) From added in v2.2.0

func (b *Endianess) From(src interface{}, dst []byte) (int, error)

From copies bytes from the given interface. The provided interface must be a type that can be safely copied. The number of bytes copied is min(len(src)* element size of dst, len(dst))

func (*Endianess) FromF32 added in v2.2.0

func (b *Endianess) FromF32(src []float32, dst []byte) (n int)

FromF32 converts and copies []float32 from `src` into `dst`. The number of bytes copied is min(len(src)*4, len(dst))

func (*Endianess) FromF64 added in v2.2.0

func (b *Endianess) FromF64(src []float64, dst []byte) (n int)

FromF64 converts and copies []float64 from `src` into `dst`. The number of bytes copied is min(len(src)*8, len(dst))

func (*Endianess) FromI8 added in v2.2.0

func (b *Endianess) FromI8(src []int8, dst []byte) (n int)

FromI8 converts and copies bytes from `src` into `dst`. The number of bytes copied is min(len(src), len(dst))

func (*Endianess) FromI16 added in v2.2.0

func (b *Endianess) FromI16(src []int16, dst []byte) (n int)

FromI16 converts and copies []int16 from `src` into `dst`. The number of bytes copied is min(len(src)*2, len(dst))

func (*Endianess) FromI32 added in v2.2.0

func (b *Endianess) FromI32(src []int32, dst []byte) (n int)

FromI32 converts and copies []int32 from `src` into `dst`. The number of bytes copied is min(len(src)*4, len(dst))

func (*Endianess) FromI64 added in v2.2.0

func (b *Endianess) FromI64(src []int64, dst []byte) (n int)

FromI64 converts and copies []int64 from `src` into `dst`. The number of bytes copied is min(len(src)*8, len(dst))

func (*Endianess) FromU16 added in v2.2.0

func (b *Endianess) FromU16(src []uint16, dst []byte) (n int)

FromU16 converts and copies []uint16 from `src` into `dst`. The number of bytes copied is min(len(src)*2, len(dst))

func (*Endianess) FromU32 added in v2.2.0

func (b *Endianess) FromU32(src []uint32, dst []byte) (n int)

FromU32 converts and copies []uint32 from `src` into `dst`. The number of bytes copied is min(len(src)*4, len(dst))

func (*Endianess) FromU64 added in v2.2.0

func (b *Endianess) FromU64(src []uint64, dst []byte) (n int)

FromU64 converts and copies []int64 from `src` into `dst`. The number of bytes copied is min(len(src)*8, len(dst))

func (*Endianess) FromValue added in v2.2.0

func (b *Endianess) FromValue(src reflect.Value, dst []byte) (int, error)

FromValue copies bytes from the given value. The provided value must be a type that can be safely converted to bytes. The number of bytes copied is min(len(src)* element size of dst, len(dst))

func (*Endianess) FromValueOffset added in v2.3.0

func (b *Endianess) FromValueOffset(src reflect.Value, dst []byte, start, end int) (int, error)

FromValueOffset copies bytes from the given value. This is the equivalent of calling FromValue(src.Slice(start, end), dst). This avoids the allocation in reflect.Value.Slice. The provided value must be a type that can be safely converted to bytes. The number of bytes copied is min(len(src)* element size of dst, len(dst))

func (*Endianess) IsBigEndian added in v2.2.0

func (b *Endianess) IsBigEndian() bool

IsBigEndian returns if the endianess of this struct is big endian. If this returns false, the endianess is little endian. This returns the endianess this struct read/writes not the system endianess.

func (*Endianess) To added in v2.2.0

func (b *Endianess) To(src []byte, dst interface{}) (int, error)

To copies bytes from `s` into the given slice. The given interface must be a type that can be safely written to. The number of bytes copied is min(len(src), len(dst)* element size of dst)

func (*Endianess) ToF32 added in v2.2.0

func (b *Endianess) ToF32(src []byte, dst []float32) (n int)

ToF32 converts and copies bytes from `src` into `dst` The number of bytes copied is min(len(src), len(dst)*4)

func (*Endianess) ToF64 added in v2.2.0

func (b *Endianess) ToF64(src []byte, dst []float64) (n int)

ToF64 converts and copies bytes from `src` into `dst` The number of bytes copied is min(len(src), len(dst)*8)

func (*Endianess) ToI8 added in v2.2.0

func (b *Endianess) ToI8(src []byte, dst []int8) (n int)

ToI8 converts and copies bytes from `src` into `dst` The number of bytes copied is min(len(src), len(dst))

func (*Endianess) ToI16 added in v2.2.0

func (b *Endianess) ToI16(src []byte, dst []int16) (n int)

ToI16 converts and copies bytes form `src` into `dst` The number of bytes copied is min(len(src), len(dst)*2)

func (*Endianess) ToI32 added in v2.2.0

func (b *Endianess) ToI32(src []byte, dst []int32) (n int)

ToI32 converts and copies bytes from `src` into `dst` The number of bytes copied is min(len(src), len(dst)*4)

func (*Endianess) ToI64 added in v2.2.0

func (b *Endianess) ToI64(src []byte, dst []int64) (n int)

ToI64 converts and copies bytes from `src` into `dst` The number of bytes copied is min(len(src), len(dst)*8)

func (*Endianess) ToU16 added in v2.2.0

func (b *Endianess) ToU16(src []byte, dst []uint16) (n int)

ToU16 converts and copies bytes form `src` into `dst` The number of bytes copied is min(len(src), len(dst)*2)

func (*Endianess) ToU32 added in v2.2.0

func (b *Endianess) ToU32(src []byte, dst []uint32) (n int)

ToU32 converts and copies bytes from `src` into `dst` The number of bytes copied is min(len(src), len(dst)*4)

func (*Endianess) ToU64 added in v2.2.0

func (b *Endianess) ToU64(src []byte, dst []uint64) (n int)

ToU64 converts and copies bytes from `src` into `dst` The number of bytes copied is min(len(src), len(dst)*8)

func (*Endianess) ToValue added in v2.2.0

func (b *Endianess) ToValue(src []byte, dst reflect.Value) (int, error)

ToValue copies bytes from `src` into the given value The given interface must be a type that can be safely written to. The number of bytes copied is min(len(src), len(dst)* element size of dst)

func (*Endianess) ToValueOffset added in v2.3.0

func (b *Endianess) ToValueOffset(src []byte, dst reflect.Value, start, end int) (int, error)

ToValueOffset copies bytes from `src` into the given value This is the equivalent of calling ToValue(src, dst.Slice(start, end)). This avoids the allocation in reflect.Value.Slice. The given interface must be a type that can be safely written to. The number of bytes copied is min(len(src), len(dst)* element size of dst)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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