byte_utils

package module
v1.0.0 Latest Latest
Warning

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

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

README

Byte Utilities for Go

This package defines some basic utilities for manipulating byte slices. It includes a buffer that supports Read, Write, and Seek operations, as well as a "fast" Xor operation.

Documentation on pkg.go.dev.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FastXor

func FastXor(dst, a, b []byte)

Used to xor byte slices a and b, writing the result into dst. Based on github.com/golang/go/issues/31586#issuecomment-487436401. Panics b and dst aren't at least as long as a. It's valid for dst to be the same as either a or b. Benchmarks on my machine (tm) indicate that this is over 3x faster than SimpleXor alone for 1 MB slices, though this may change depending on architecture.

func SimpleXor

func SimpleXor(dst, a, b []byte)

Sets dst to the xor of bytes in slices a and b, one byte at a time. Panics if b and dst aren't at least as long as a. It is valid for dst to be the same as either a or b.

Types

type SeekableBuffer

type SeekableBuffer struct {
	// This will grow as needed, based on either the farthest write or seek
	// offset. Writing past the end of this will increase its size. Seeking
	// past the end will add zeros to the necessary size.
	Data []byte
	// The current read or write offset in the file. It's an error to read past
	// the end of the data, but writing pas
	Offset int64
}

This type implements an in-memory io.Reader, io.Writer, and io.Seeker.

func NewSeekableBuffer

func NewSeekableBuffer() *SeekableBuffer

func (*SeekableBuffer) Read

func (b *SeekableBuffer) Read(dst []byte) (int, error)

Provides the normal io.Reader interface.

func (*SeekableBuffer) Seek

func (b *SeekableBuffer) Seek(offset int64, whence int) (int64, error)

Sets the next read or write offset to the specified offset, returning the new offset. Expands the underlying buffer if the new offset is greater than its current size. Returns an error without changing the current offset if an error occurs.

func (*SeekableBuffer) Write

func (b *SeekableBuffer) Write(data []byte) (int, error)

Provides the normal io.Writer interface.

Jump to

Keyboard shortcuts

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