buffer

package module
v3.3.0 Latest Latest
Warning

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

Go to latest
Published: Jul 12, 2021 License: BSD-2-Clause Imports: 8 Imported by: 1

README

buffer

Go Latest Release Go Reference License Go Report Card

This package implements high-efficiency byte buffer types.

Documentation

Overview

Package buffer provides high-performance byte buffer types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer implements a byte buffer. The Buffer has space for 2**N bytes for user-specified N.

func New

func New(numBits uint) *Buffer

New is a convenience function that allocates a new Buffer and calls Init on it.

func (Buffer) Bytes

func (buffer Buffer) Bytes() []byte

Bytes allocates and returns a copy of the Buffer's contents.

func (Buffer) BytesView

func (buffer Buffer) BytesView() []byte

BytesView returns a slice into the Buffer's contents.

func (*Buffer) Clear

func (buffer *Buffer) Clear()

Clear erases the contents of the Buffer.

func (*Buffer) CommitBulkRead

func (buffer *Buffer) CommitBulkRead(length uint)

CommitBulkRead completes the bulk read begun by the previous call to PrepareBulkRead. The argument must be between 0 and the length of the slice returned by PrepareBulkRead.

func (*Buffer) CommitBulkWrite

func (buffer *Buffer) CommitBulkWrite(length uint)

CommitBulkWrite completes the bulk write begun by the previous call to PrepareBulkWrite. The argument must be between 0 and the length of the slice returned by PrepareBulkWrite.

func (Buffer) DebugString

func (buffer Buffer) DebugString() string

DebugString returns a detailed dump of the Buffer's internal state.

func (Buffer) GoString

func (buffer Buffer) GoString() string

GoString returns a brief dump of the Buffer's internal state.

func (*Buffer) Init

func (buffer *Buffer) Init(numBits uint)

Init initializes the Buffer. The Buffer will hold a maximum of 2**N bits, where N is the argument provided. The argument must be a number between 0 and 31 inclusive.

func (Buffer) IsEmpty

func (buffer Buffer) IsEmpty() bool

IsEmpty returns true iff the Buffer contains no bytes.

func (Buffer) IsFull

func (buffer Buffer) IsFull() bool

IsFull returns true iff the Buffer contains the maximum number of bytes.

func (Buffer) Len

func (buffer Buffer) Len() uint

Len returns the number of bytes currently in the Buffer.

func (Buffer) NumBits

func (buffer Buffer) NumBits() uint

NumBits returns the number of bits used to initialize this Buffer.

func (*Buffer) PrepareBulkRead

func (buffer *Buffer) PrepareBulkRead(length uint) []byte

PrepareBulkRead obtains a slice from which the caller can read bytes. The bytes do not leave the buffer's contents until CommitBulkRead is called. If CommitBulkRead is not subsequently called, the read acts as a "peek" operation.

The returned slice may contain fewer bytes than requested; it will return a zero-length slice iff the buffer is empty. The caller must check its length before using it. A short but non-empty return slice does *not* indicate an empty buffer.

The returned slice is only valid until the next call to any mutating method on this Buffer; mutating methods are those which take a pointer receiver.

func (*Buffer) PrepareBulkWrite

func (buffer *Buffer) PrepareBulkWrite(length uint) []byte

PrepareBulkWrite obtains a slice into which the caller can write bytes. The bytes do not become a part of the buffer's contents until CommitBulkWrite is called. If CommitBulkWrite is not subsequently called, the write is considered abandoned.

The returned slice may contain fewer bytes than requested; it will return a nil slice iff the buffer is full. The caller must check the slice's length before using it. A short but non-empty return slice does *not* indicate a full buffer.

The returned slice is only valid until the next call to any mutating method on this Buffer; mutating methods are those which take a pointer receiver.

func (*Buffer) Read

func (buffer *Buffer) Read(data []byte) (int, error)

Read reads a slice of bytes from the Buffer. If the buffer is empty, ErrEmpty is returned.

func (*Buffer) ReadByte

func (buffer *Buffer) ReadByte() (byte, error)

ReadByte reads a single byte from the Buffer. If the buffer is empty, ErrEmpty is returned.

func (*Buffer) ReadFrom

func (buffer *Buffer) ReadFrom(r io.Reader) (int64, error)

ReadFrom attempts to fill this Buffer by reading from the provided Reader. May return any error returned by the Reader, including io.EOF. If a nil error is returned, then the buffer is now full.

func (Buffer) Size

func (buffer Buffer) Size() uint

Size returns the maximum byte capacity of the Buffer.

func (Buffer) String

func (buffer Buffer) String() string

String returns the contents of the Buffer as a string.

func (*Buffer) Swap added in v3.3.0

func (buffer *Buffer) Swap(other *Buffer)

Swap exchanges this Buffer's contents with another.

func (*Buffer) Write

func (buffer *Buffer) Write(data []byte) (int, error)

Write writes a slice of bytes to the Buffer. If the Buffer is full, as many bytes as possible are written to the Buffer and ErrFull is returned.

func (*Buffer) WriteByte

func (buffer *Buffer) WriteByte(ch byte) error

WriteByte writes a single byte to the Buffer. If the Buffer is full, ErrFull is returned.

func (*Buffer) WriteTo

func (buffer *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo attempts to drain this Buffer by writing to the provided Writer. May return any error returned by the Writer. If a nil error is returned, then the Buffer is now empty.

type Error

type Error byte

Error is the type for the error constants returned by this package.

const (
	// ErrEmpty is returned when reading from an empty Buffer.
	ErrEmpty Error = iota

	// ErrFull is returned when writing to a full Buffer.
	ErrFull

	// ErrBadDistance is returned when Window.LookupByte is called with a
	// distance that isn't contained within the Window.
	ErrBadDistance
)

func (Error) Error

func (err Error) Error() string

Error returns the error message for this error.

func (Error) GoString

func (err Error) GoString() string

GoString returns the name of the Go constant.

type LZ77

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

LZ77 implements a combination Window/Buffer that uses the Window to remember bytes that were recently removed from the Buffer, and that hashes all data that enters the Window so that LZ77-style prefix matching can be made efficient.

func NewLZ77

func NewLZ77(o LZ77Options) *LZ77

NewLZ77 is a convenience function that allocates a LZ77 and calls Init on it.

func (*LZ77) Advance

func (lz77 *LZ77) Advance() (buf []byte, matchDistance uint, matchLength uint, matchFound bool)

Advance moves a slice of bytes from the LZ77's Buffer to its Window. The nature of the slice depends on the LZ77's prefix match settings, the contents of the LZ77's Window, and the contents of the LZ77's Buffer.

func (LZ77) BufferBytes added in v3.1.0

func (lz77 LZ77) BufferBytes() []byte

BufferBytes allocates and returns a copy of the Hybrid's Buffer's contents.

func (LZ77) BufferBytesView added in v3.1.0

func (lz77 LZ77) BufferBytesView() []byte

BufferBytesView returns a slice into the Hybrid's Buffer's contents.

func (LZ77) BufferNumBits

func (lz77 LZ77) BufferNumBits() uint

BufferNumBits returns the size of the buffer in bits.

func (LZ77) BufferSize

func (lz77 LZ77) BufferSize() uint

BufferSize returns the size of the buffer, in bytes.

func (*LZ77) Clear

func (lz77 *LZ77) Clear()

Clear clears all data, emptying both the buffer and the sliding window.

func (*LZ77) CommitBulkRead

func (lz77 *LZ77) CommitBulkRead(length uint)

CommitBulkRead completes the bulk read begun by the previous call to PrepareBulkRead. The argument must be between 0 and the length of the slice returned by PrepareBulkRead.

func (*LZ77) CommitBulkWrite

func (lz77 *LZ77) CommitBulkWrite(length uint)

CommitBulkWrite completes the bulk write begun by the previous call to PrepareBulkWrite. The argument must be between 0 and the length of the slice returned by PrepareBulkWrite.

func (LZ77) DebugString

func (lz77 LZ77) DebugString() string

DebugString returns a detailed dump of the LZ77's internal state.

func (LZ77) GoString

func (lz77 LZ77) GoString() string

GoString returns a brief dump of the LZ77's internal state.

func (LZ77) HashNumBits

func (lz77 LZ77) HashNumBits() uint

HashNumBits returns the size of the hash function output in bits.

func (*LZ77) Init

func (lz77 *LZ77) Init(o LZ77Options)

Init initializes a LZ77.

func (LZ77) IsEmpty

func (lz77 LZ77) IsEmpty() bool

IsEmpty returns true iff the buffer is empty.

func (LZ77) IsFull

func (lz77 LZ77) IsFull() bool

IsFull returns true iff the buffer is full.

func (LZ77) IsWindowEmpty added in v3.1.0

func (lz77 LZ77) IsWindowEmpty() bool

IsWindowEmpty returns true iff the Window is empty.

func (LZ77) IsWindowFull added in v3.1.0

func (lz77 LZ77) IsWindowFull() bool

IsWindowFull returns true iff the Window is full.

func (LZ77) Len

func (lz77 LZ77) Len() uint

Len returns the number of bytes currently in the LZ77's Buffer.

func (LZ77) Options

func (lz77 LZ77) Options() LZ77Options

Options returns a LZ77Options struct which can be used to construct a new LZ77 with the same settings.

func (*LZ77) PrepareBulkRead

func (lz77 *LZ77) PrepareBulkRead(length uint) []byte

PrepareBulkRead obtains a slice from which the caller can read bytes. See Buffer.PrepareBulkRead for more details.

func (*LZ77) PrepareBulkWrite

func (lz77 *LZ77) PrepareBulkWrite(length uint) []byte

PrepareBulkWrite obtains a slice into which the caller can write bytes. See Buffer.PrepareBulkWrite for more details.

func (*LZ77) Read

func (lz77 *LZ77) Read(data []byte) (int, error)

Read reads a slice of bytes from the LZ77's Buffer. If the buffer is empty, ErrEmpty is returned.

func (*LZ77) ReadByte

func (lz77 *LZ77) ReadByte() (byte, error)

ReadByte reads a single byte, or returns ErrEmpty if the buffer is empty.

func (*LZ77) SetWindow

func (lz77 *LZ77) SetWindow(data []byte)

SetWindow replaces the sliding window with the given data.

func (LZ77) String

func (lz77 LZ77) String() string

String returns the contents of the LZ77's Buffer as a string.

func (LZ77) WindowBytes added in v3.1.0

func (lz77 LZ77) WindowBytes() []byte

WindowBytes allocates and returns a copy of the Hybrid's Window's contents.

func (LZ77) WindowBytesView added in v3.1.0

func (lz77 LZ77) WindowBytesView() []byte

WindowBytesView returns a slice into the Hybrid's Window's contents.

func (*LZ77) WindowClear

func (lz77 *LZ77) WindowClear()

WindowClear clears the sliding window.

func (LZ77) WindowLen added in v3.1.0

func (lz77 LZ77) WindowLen() uint

WindowLen returns the number of bytes currently in the LZ77's Window.

func (LZ77) WindowNumBits

func (lz77 LZ77) WindowNumBits() uint

WindowNumBits returns the size of the sliding window in bits.

func (LZ77) WindowSize

func (lz77 LZ77) WindowSize() uint

WindowSize returns the size of the sliding window, in bytes.

func (*LZ77) Write

func (lz77 *LZ77) Write(data []byte) (int, error)

Write writes a slice of bytes to the LZ77's Buffer.

func (*LZ77) WriteByte

func (lz77 *LZ77) WriteByte(ch byte) error

WriteByte writes a single byte to the LZ77's Buffer.

type LZ77Options

type LZ77Options struct {
	BufferNumBits       uint
	WindowNumBits       uint
	HashNumBits         uint
	MinMatchLength      uint
	MaxMatchLength      uint
	MaxMatchDistance    uint
	HasMinMatchLength   bool
	HasMaxMatchLength   bool
	HasMaxMatchDistance bool
}

LZ77Options holds options for initializing an instance of LZ77.

func (LZ77Options) Equal

func (opts LZ77Options) Equal(other LZ77Options) bool

Equal returns true iff the given LZ77Options is semantically equal to this one.

type Window

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

Window implements a sliding window. The Window has space for 2**N bytes for user-specified N.

func NewWindow

func NewWindow(numBits uint) *Window

NewWindow is a convenience function that allocates a Window and calls Init on it.

func (Window) Bytes

func (window Window) Bytes() []byte

Bytes allocates and returns a copy of the Window's contents.

func (Window) BytesView

func (window Window) BytesView() []byte

BytesView returns a slice into the Window's contents.

The returned slice is only valid until the next call to any mutating method on this Window; mutating methods are those which take a pointer receiver.

func (*Window) Clear

func (window *Window) Clear()

Clear erases the contents of the Window.

func (*Window) CommitBulkWrite

func (window *Window) CommitBulkWrite(length uint)

CommitBulkWrite completes the bulk write begun by the previous call to PrepareBulkWrite. The argument must be between 0 and the length of the slice returned by PrepareBulkWrite.

func (Window) DebugString

func (window Window) DebugString() string

DebugString returns a detailed dump of the Window's internal state.

func (Window) GoString

func (window Window) GoString() string

GoString returns a brief dump of the Window's internal state.

func (Window) Hash

func (window Window) Hash(hashes ...hash.Hash)

Hash non-destructively writes the contents of the Window into the provided Hash object(s).

func (Window) Hash32

func (window Window) Hash32(fn func() hash.Hash32) uint32

Hash32 is a convenience method that constructs a Hash32, calls Window.Hash with it, and calls Sum32 on it.

func (*Window) Init

func (window *Window) Init(numBits uint)

Init initializes the Window. The Window will hold a maximum of 2**N bits, where N is the argument provided. The argument must be a number between 0 and 31 inclusive.

func (Window) IsZero

func (window Window) IsZero() bool

IsZero returns true iff the Window contains only 0 bytes.

func (Window) LookupByte

func (window Window) LookupByte(distance uint) (byte, error)

LookupByte returns a byte which was written previously. The argument is the offset into the window, with 1 representing the most recently written byte and Window.Size() representing the oldest byte still within the Window.

func (Window) LookupSlice added in v3.2.0

func (window Window) LookupSlice(distance uint, length uint) ([]byte, error)

LookupSlice returns a slice which was written previously. The distance argument measures the offset into the Window, with 1 representing the most recently written byte and Window.Size() representing the oldest byte still within the Window. The length argument is the maximum length of the slice to be returned; it may be shorter if it would otherwise extend past the most recently written byte.

func (Window) NumBits

func (window Window) NumBits() uint

NumBits returns the number of bits used to initialize this Window.

func (*Window) PrepareBulkWrite

func (window *Window) PrepareBulkWrite(length uint) []byte

PrepareBulkWrite obtains a slice into which the caller can write bytes. The bytes do not become a part of the Window's contents until CommitBulkWrite is called. If CommitBulkWrite is not subsequently called, the write is considered abandoned.

The returned slice may contain fewer bytes than requested, if the provided length is greater than the size of the Window. The caller must check the slice's length before using it.

The returned slice is only valid until the next call to any mutating method on this Window; mutating methods are those which take a pointer receiver.

func (Window) Size

func (window Window) Size() uint

Size returns the maximum byte capacity of the Window.

func (Window) String

func (window Window) String() string

String returns the contents of the Window as a string.

func (*Window) Write

func (window *Window) Write(data []byte) (int, error)

Write writes a slice of bytes to the Window. The oldest len(data) bytes in the Window are dropped to make room. If len(data) exceeds Window.Size(), then only the last Window.Size() bytes of the slice will be recorded.

func (*Window) WriteByte

func (window *Window) WriteByte(ch byte) error

WriteByte writes a single byte to the Window. The oldest byte in the Window is dropped to make room.

Jump to

Keyboard shortcuts

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