buf

package
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2025 License: MIT Imports: 2 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buf

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

Buf is a dynamically-sized byte buffer, wrapping a []byte. It manages its own position, logical length, and capacity growth.

func NewBuf

func NewBuf(initialCapacity int) *Buf

NewBuf creates a new Buf with an initial capacity.

func (*Buf) Bytes

func (b *Buf) Bytes() []byte

Bytes returns a slice of all logical bytes currently in the buffer. The returned slice is valid until the next write operation that might cause reallocation.

func (*Buf) Cap

func (b *Buf) Cap() int

Cap returns the current capacity of the buffer's underlying storage.

func (*Buf) Get

func (b *Buf) Get(position int64) (byte, error)

Get returns the byte at the given absolute position in the buffer, without advancing the current position. Position is relative to the start of the buffer's logical content.

func (*Buf) IncrementPosition

func (b *Buf) IncrementPosition(numBytes int64)

IncrementPosition increments the current position by numBytes.

func (*Buf) Len

func (b *Buf) Len() int

Len returns the current logical length of the buffer (number of bytes written).

func (*Buf) Peek

func (b *Buf) Peek(n int) ([]byte, error)

Peek returns the next n bytes from the current position without advancing the reader. The returned slice shares the underlying array of the buffer. If n is larger than the available bytes, Peek returns all available bytes.

func (*Buf) Position

func (b *Buf) Position() int64

Position returns the current position in the buffer.

func (*Buf) Read

func (b *Buf) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes into p from the current position. It returns the number of bytes read and any error encountered. EOF is returned when no more bytes are available from the current position within the logical length.

func (*Buf) ReadByte

func (b *Buf) ReadByte() (byte, error)

ReadByte reads and returns the next byte from the current position. If no byte is available, it returns error io.EOF.

func (*Buf) Reset

func (b *Buf) Reset()

Reset resets the buffer to be empty, but it retains the underlying storage for use by future writes by slicing data to zero length. Reset also resets the position and logical length to 0.

func (*Buf) SetPosition

func (b *Buf) SetPosition(position int64)

SetPosition sets the current position in the buffer. No bounds checking is performed on the position value itself, but subsequent operations (Read/Write) will handle boundaries.

func (*Buf) Write

func (b *Buf) Write(p []byte) (n int, err error)

Write writes len(p) bytes from p to the buffer at the current position. It returns the number of bytes written and any error encountered. If the write extends beyond the current logical length, the buffer's logical length is updated. If the write starts beyond the current logical length, the gap is filled with zeros.

func (*Buf) WriteBuf

func (b *Buf) WriteBuf(bb *Buf) (n int, err error)

WriteBuf writes the content of bb into b at the current position. It updates b's position and length accordingly. Returns the number of bytes written from bb and any error encountered.

func (*Buf) WriteByte

func (b *Buf) WriteByte(c byte) error

WriteByte writes a single byte to the buffer at the current position.

type BufPosition

type BufPosition interface {
	Position() int64
	SetPosition(position int64)
	IncrementPosition(numBytes int64)
}

BufPosition defines an interface for types that track a position.

type BufReadonly

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

BufReadonly is a read-only wrapper around a byte slice. It's analogous to TypeScript's BufReadonly class.

func NewBufReadonly

func NewBufReadonly(data []byte) *BufReadonly

NewBufReadonly creates a new BufReadonly wrapping the given byte slice. The provided slice is used directly and should not be modified externally if read-only behavior is to be guaranteed.

func (*BufReadonly) Bytes

func (br *BufReadonly) Bytes() []byte

Bytes returns all bytes in the buffer, regardless of position.

func (*BufReadonly) Get

func (br *BufReadonly) Get(position int64) (byte, error)

Get returns the byte at the given absolute position in the buffer, without advancing the current position.

func (*BufReadonly) IncrementPosition

func (br *BufReadonly) IncrementPosition(numBytes int64)

IncrementPosition increments the current position by numBytes.

func (*BufReadonly) Len

func (br *BufReadonly) Len() int

Len returns the total length of the underlying byte slice.

func (*BufReadonly) Peek

func (br *BufReadonly) Peek(n int) ([]byte, error)

Peek returns the next n bytes without advancing the reader. If n is negative, an error is returned. If n is 0, Peek returns an empty slice and no error. If the buffer's read position is out of bounds (negative or >= len(br.bytes)), it returns (nil, io.EOF). If n is larger than the available bytes from br.pos, Peek returns all available bytes and no error.

func (*BufReadonly) Position

func (br *BufReadonly) Position() int64

Position returns the current position in the buffer.

func (*BufReadonly) Read

func (br *BufReadonly) Read(p []byte) (n int, err error)

Read reads up to len(p) bytes into p. It returns the number of bytes read (0 <= n <= len(p)) and any error encountered. If the buffer's read position is negative, it returns (0, io.EOF).

func (*BufReadonly) ReadByte

func (br *BufReadonly) ReadByte() (byte, error)

ReadByte reads and returns the next byte from the buffer. If no byte is available, it returns error io.EOF. If the buffer's read position is negative, it returns (0, io.EOF).

func (*BufReadonly) SetPosition

func (br *BufReadonly) SetPosition(position int64)

SetPosition sets the current position in the buffer.

type Read

type Read interface {
	BufPosition
	io.Reader
	io.ByteReader
	Get(position int64) (byte, error)
	Bytes() []byte
	Len() int
	Peek(n int) ([]byte, error)
}

Read defines an interface for readable buffers.

type Write

type Write interface {
	BufPosition
	io.Writer
	io.ByteWriter
	Cap() int
}

Write defines an interface for writable buffers.

Jump to

Keyboard shortcuts

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