byteops

package
v1.37.15 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2026 License: BSD-3-Clause Imports: 5 Imported by: 6

Documentation

Overview

Package byteops provides helper functions to (un-) marshal objects from or into a buffer

Index

Constants

View Source
const (
	Uint64Len = 8
	Uint32Len = 4
	Uint16Len = 2
	Uint8Len  = 1
)

Variables

View Source
var ErrBufferOverrun = errors.New("read exceeds buffer")

ErrBufferOverrun signals that a length or offset decoded out of a buffer runs past that buffer's end.

The unchecked readers above slice as Buffer[Position : Position+n], which Go bounds against capacity rather than length. A buffer here is routinely a subslice of a larger allocation — an mmapped segment, a pooled read buffer — so an overrun that stays within capacity does not panic: it yields whatever bytes follow the value. Any decoder whose lengths come from the data it is decoding must use the checked readers.

Functions

func CopyBytesToSlice added in v1.37.0

func CopyBytesToSlice[T float32 | uint64](dst []T, src []byte)

CopyBytesToSlice bulk-copies src bytes into dst typed slice using a single memmove. This is safe on little-endian architectures where the in-memory representation of float32/uint64 matches the little-endian byte layout used on disk.

func CopySliceToBytes added in v1.37.0

func CopySliceToBytes[T float32 | uint64](dst []byte, src []T)

CopySliceToBytes bulk-copies a typed slice into dst bytes using a single memmove. This is safe on little-endian architectures where the in-memory representation of float32/uint64 matches the little-endian byte layout used on disk.

func Fp32SliceFromBytes added in v1.29.0

func Fp32SliceFromBytes(vector []byte) []float32

func Fp32SliceOfSlicesFromBytes added in v1.29.0

func Fp32SliceOfSlicesFromBytes(bytes []byte) ([][]float32, error)

Fp32SliceOfSlicesToBytes converts a slice of slices of float64 to a byte slice

Within the byte slice, it determines the dimensions of the inner slices using the first two bytes inferred as uint16 type The rest of the bytes are the float32 values.

If the byte slice is empty, it returns an empty slice of slices. If the dimension is found to be 0 then an error is returned as this is invalid.

func Fp32SliceOfSlicesToBytes added in v1.29.0

func Fp32SliceOfSlicesToBytes(slices [][]float32) []byte

Fp32SliceOfSlicesToBytes converts a slice of slices of float64 to a byte slice

Within the byte slice, it encodes the first two bytes as the number of dimensions with uint16 type The rest of the bytes are the float32 values.

If the outer slice is empty, it returns an empty byte slice. If the first slice is empty, it returns an empty byte slice.

func Fp32SliceToBytes added in v1.29.0

func Fp32SliceToBytes(slice []float32) []byte

func Fp64SliceFromBytes added in v1.29.0

func Fp64SliceFromBytes(vector []byte) []float64

func Fp64SliceToBytes added in v1.29.0

func Fp64SliceToBytes(floats []float64) []byte

func IntsFromByteVector added in v1.25.0

func IntsFromByteVector(vector []byte) []int64

func IntsToByteVector added in v1.25.0

func IntsToByteVector(ints []float64) []byte

func WithPosition

func WithPosition(pos uint64) func(*ReadWriter)

Types

type ReadWriter

type ReadWriter struct {
	Position uint64
	Buffer   []byte
}

func NewReadWriter

func NewReadWriter(buf []byte) ReadWriter

func NewReadWriterWithOps added in v1.27.18

func NewReadWriterWithOps(buf []byte, opts ...func(writer *ReadWriter)) ReadWriter

NewReadWriterWithOps escapes to heap even if no ops are given

func (*ReadWriter) CopyBytesFromBuffer

func (bo *ReadWriter) CopyBytesFromBuffer(length uint64, out []byte) ([]byte, error)

func (*ReadWriter) CopyBytesFromBufferChecked added in v1.37.15

func (bo *ReadWriter) CopyBytesFromBufferChecked(length uint64, out []byte) ([]byte, error)

func (*ReadWriter) CopyBytesToBuffer

func (bo *ReadWriter) CopyBytesToBuffer(copyBytes []byte) error

func (*ReadWriter) CopyBytesToBufferWithUint32LengthIndicator

func (bo *ReadWriter) CopyBytesToBufferWithUint32LengthIndicator(copyBytes []byte) error

Writes a uint32 length indicator about the buffer that's about to follow, then writes the buffer itself

func (*ReadWriter) CopyBytesToBufferWithUint64LengthIndicator

func (bo *ReadWriter) CopyBytesToBufferWithUint64LengthIndicator(copyBytes []byte) error

Writes a uint64 length indicator about the buffer that's about to follow, then writes the buffer itself

func (*ReadWriter) DiscardBytesFromBufferWithUint32LengthIndicator

func (bo *ReadWriter) DiscardBytesFromBufferWithUint32LengthIndicator() uint32

func (*ReadWriter) DiscardBytesFromBufferWithUint64LengthIndicator

func (bo *ReadWriter) DiscardBytesFromBufferWithUint64LengthIndicator() uint64

func (*ReadWriter) MoveBufferPositionForward

func (bo *ReadWriter) MoveBufferPositionForward(length uint64)

func (*ReadWriter) MoveBufferToAbsolutePosition

func (bo *ReadWriter) MoveBufferToAbsolutePosition(pos uint64)

func (*ReadWriter) ReadBytesFromBuffer

func (bo *ReadWriter) ReadBytesFromBuffer(length uint64) []byte

func (*ReadWriter) ReadBytesFromBufferChecked added in v1.37.15

func (bo *ReadWriter) ReadBytesFromBufferChecked(length uint64) ([]byte, error)

func (*ReadWriter) ReadBytesFromBufferWithUint32LengthIndicator

func (bo *ReadWriter) ReadBytesFromBufferWithUint32LengthIndicator() []byte

func (*ReadWriter) ReadBytesFromBufferWithUint32LengthIndicatorChecked added in v1.37.15

func (bo *ReadWriter) ReadBytesFromBufferWithUint32LengthIndicatorChecked() ([]byte, error)

func (*ReadWriter) ReadBytesFromBufferWithUint64LengthIndicator

func (bo *ReadWriter) ReadBytesFromBufferWithUint64LengthIndicator() []byte

func (*ReadWriter) ReadUint8

func (bo *ReadWriter) ReadUint8() uint8

func (*ReadWriter) ReadUint16

func (bo *ReadWriter) ReadUint16() uint16

func (*ReadWriter) ReadUint16Checked added in v1.37.15

func (bo *ReadWriter) ReadUint16Checked() (uint16, error)

func (*ReadWriter) ReadUint32

func (bo *ReadWriter) ReadUint32() uint32

func (*ReadWriter) ReadUint32Checked added in v1.37.15

func (bo *ReadWriter) ReadUint32Checked() (uint32, error)

func (*ReadWriter) ReadUint64

func (bo *ReadWriter) ReadUint64() uint64

func (*ReadWriter) Remaining added in v1.37.15

func (bo *ReadWriter) Remaining() uint64

Remaining saturates at 0 once Position has run past the end of the buffer.

func (*ReadWriter) ResetBuffer added in v1.23.0

func (bo *ReadWriter) ResetBuffer(buf []byte)

func (*ReadWriter) SkipChecked added in v1.37.15

func (bo *ReadWriter) SkipChecked(length uint64) error

SkipChecked bounds MoveBufferPositionForward, so a length read out of the data cannot park the cursor past the buffer.

func (*ReadWriter) Write added in v1.27.25

func (bo *ReadWriter) Write(p []byte) (int, error)

for io.Writer interface

func (*ReadWriter) WriteByte

func (bo *ReadWriter) WriteByte(b byte)

func (*ReadWriter) WriteUint16

func (bo *ReadWriter) WriteUint16(value uint16)

func (*ReadWriter) WriteUint32

func (bo *ReadWriter) WriteUint32(value uint32)

func (*ReadWriter) WriteUint64

func (bo *ReadWriter) WriteUint64(value uint64)

Jump to

Keyboard shortcuts

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