bytebuf

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2024 License: MIT Imports: 5 Imported by: 2

README

Byte buffers

Collection of various byte buffer implementations.

Chain buffer

A wrapper around []byte slice that supports chain write methods. Example of usage:

import "github.com/koykov/bytebuf"

var (
	buf bytebuf.Chain
	msg protobuf.ExampleMessage
)
b := buf.Write([]byte("foo")).
	WriteString("bar").
	WriteByte('@').
	WriteInt(-123).
	WriteUint(123).
	WriteFloat(3.1415).
	WriteBool(true).
	WriteX(1.23456).
	Bytes()
println(string(b)) // foobar@-1231233.1415true1.23456

The same operations may be proceeded using append()/AppendInt()/... functions around []byte slice, but Chain provides handy API for that.

Accumulative buffer

A wrapper around Chain buffer with "staked" features. The main idea is to accumulate (bufferize) various data and provide handy access to chunks of buffered data. Example of usage:

import "github.com/koykov/bytebuf"

var (
    buf bytebuf.Chain
    msg protobuf.ExampleMessage
)
chunk0 := buf.WriteMarshallerTo(&msg).Bytes()
chunk1 := buf.StakeOut().Write([]byte("foo")).
    WriteString("bar").
    WriteByte('@').
    WriteInt(-123).
    WriteUint(123).
    WriteFloat(3.1415).
    WriteBool(true).
    WriteX(1.23456).
    StakedBytes()
println(string(chunk0)) // h�����,����?�ihttps
println(string(chunk1)) // foobar@-1231233.1415true1.23456

Thus, one buffer may be used to bufferize multiple data and hence reduce pointers in application.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AccumulativeToBytes added in v1.0.5

func AccumulativeToBytes(dst []byte, val any) ([]byte, error)

AccumulativeToBytes registers x2bytes conversion function accepts Accumulative.

func ChainToBytes added in v1.0.5

func ChainToBytes(dst []byte, val any) ([]byte, error)

ChainToBytes registers x2bytes conversion function accepts Chain.

Types

type AccBufWriter added in v1.0.4

type AccBufWriter = AccumulativeWriter

AccBufWriter is an alias of AccumulativeWriter type. DEPRECATED: use AccumulativeWriter instead. Kept for backward compatibility.

type Accumulative added in v1.0.5

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

Accumulative is a wrapper around Chain that allows to accumulate buffer data and use only necessary part.

See StakeOut and Staked* methods.

func (*Accumulative) Bytes added in v1.0.5

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

Bytes returns contents of the buffer.

func (*Accumulative) BytesCopy added in v1.0.5

func (b *Accumulative) BytesCopy() []byte

BytesCopy returns copy of buffer contents.

func (*Accumulative) Cap added in v1.0.5

func (b *Accumulative) Cap() int

func (*Accumulative) Error added in v1.0.5

func (b *Accumulative) Error() error

Get last error caught in Write* methods.

func (*Accumulative) Grow added in v1.0.5

func (b *Accumulative) Grow(newLen int) *Accumulative

Grow increases length of the buffer.

func (*Accumulative) GrowDelta added in v1.0.5

func (b *Accumulative) GrowDelta(delta int) *Accumulative

GrowDelta increases length of the buffer to actual length + delta.

See Grow().

func (*Accumulative) Len added in v1.0.5

func (b *Accumulative) Len() int

func (*Accumulative) RangeBytes added in v1.0.5

func (b *Accumulative) RangeBytes(off, len int) []byte

RangeBytes returns buffer bytes from offset off with length len.

func (*Accumulative) RangeBytesCopy added in v1.0.5

func (b *Accumulative) RangeBytesCopy(off, len int) []byte

RangeBytesCopy copies result of RangeBytes().

func (*Accumulative) RangeString added in v1.0.5

func (b *Accumulative) RangeString(off, len int) string

RangeString returns buffer bytes as string from offset off with length len.

func (*Accumulative) RangeStringCopy added in v1.0.5

func (b *Accumulative) RangeStringCopy(off, len int) string

RangeStringCopy copies result of RangeString().

func (*Accumulative) Replace added in v1.0.5

func (b *Accumulative) Replace(old, new []byte, n int) *Accumulative

Replace replaces old bytes to new in buffer.

func (*Accumulative) ReplaceAll added in v1.0.5

func (b *Accumulative) ReplaceAll(old, new []byte) *Accumulative

ReplaceAll replace all occurrences of old bytes to new in buffer.

func (*Accumulative) ReplaceStr added in v1.0.5

func (b *Accumulative) ReplaceStr(old, new string, n int) *Accumulative

ReplaceStr replace old to new substrings in buffer. DEPRECATED: use ReplaceString() instead.

func (*Accumulative) ReplaceStrAll added in v1.0.5

func (b *Accumulative) ReplaceStrAll(old, new string) *Accumulative

ReplaceStrAll replaces all occurrences of old substrings to new in buffer. DEPRECATED: use ReplaceStringAll() instead.

func (*Accumulative) ReplaceString added in v1.0.5

func (b *Accumulative) ReplaceString(old, new string, n int) *Accumulative

ReplaceString replace old to new substrings in buffer.

func (*Accumulative) ReplaceStringAll added in v1.0.5

func (b *Accumulative) ReplaceStringAll(old, new string) *Accumulative

ReplaceStringAll replaces all occurrences of old substrings to new in buffer.

func (*Accumulative) Reset added in v1.0.5

func (b *Accumulative) Reset() *Accumulative

func (*Accumulative) StakeOut added in v1.0.5

func (b *Accumulative) StakeOut() *Accumulative

StakeOut saves current offset for further use.

func (*Accumulative) StakedBytes added in v1.0.5

func (b *Accumulative) StakedBytes() []byte

StakedBytes returns accumulated bytes from staked offset.

func (*Accumulative) StakedBytesCopy added in v1.0.5

func (b *Accumulative) StakedBytesCopy() []byte

StakedBytesCopy returns copy of accumulated bytes since staked offset.

func (*Accumulative) StakedLen added in v1.0.5

func (b *Accumulative) StakedLen() int

StakedLen returns length of accumulated bytes since staked offset.

func (*Accumulative) StakedOffset added in v1.0.5

func (b *Accumulative) StakedOffset() int

StakedOffset returns staked offset.

func (*Accumulative) StakedString added in v1.0.5

func (b *Accumulative) StakedString() string

StakedString returns accumulated bytes as string.

func (*Accumulative) StakedStringCopy added in v1.0.5

func (b *Accumulative) StakedStringCopy() string

StakedStringCopy returns copy of accumulated bytes as string.

func (*Accumulative) String added in v1.0.5

func (b *Accumulative) String() string

Get contents of the buffer as string.

func (*Accumulative) StringCopy added in v1.0.5

func (b *Accumulative) StringCopy() string

StringCopy returns copy of the buffer contents as string.

func (*Accumulative) ToWriter added in v1.0.5

func (b *Accumulative) ToWriter() *AccumulativeWriter

ToWriter wraps buffer with class implementing IO interfaces.

func (*Accumulative) Write added in v1.0.5

func (b *Accumulative) Write(p []byte) *Accumulative

Write bytes to the buffer.

func (*Accumulative) WriteApplyFn added in v1.0.5

func (b *Accumulative) WriteApplyFn(p []byte, fn func(dst, p []byte) []byte) *Accumulative

WriteApplyFn applies fn to p and write result to the buffer.

func (*Accumulative) WriteApplyFnIf added in v1.0.6

func (b *Accumulative) WriteApplyFnIf(cond bool, p []byte, fn func(dst, p []byte) []byte) *Accumulative

func (*Accumulative) WriteApplyFnStr added in v1.0.5

func (b *Accumulative) WriteApplyFnStr(s string, fn func(dst, p []byte) []byte) *Accumulative

WriteApplyFnStr applies fn to s and write result to the buffer. DEPRECATED: use WriteApplyFnString() instead.

func (*Accumulative) WriteApplyFnString added in v1.0.5

func (b *Accumulative) WriteApplyFnString(s string, fn func(dst, p []byte) []byte) *Accumulative

WriteApplyFnString applies fn to s and write result to the buffer.

func (*Accumulative) WriteApplyFnStringIf added in v1.0.6

func (b *Accumulative) WriteApplyFnStringIf(cond bool, s string, fn func(dst, p []byte) []byte) *Accumulative

func (*Accumulative) WriteBool added in v1.0.5

func (b *Accumulative) WriteBool(v bool) *Accumulative

WriteBool writes boolean value to the buffer.

func (*Accumulative) WriteBoolIf added in v1.0.6

func (b *Accumulative) WriteBoolIf(cond bool, v bool) *Accumulative

func (*Accumulative) WriteByte added in v1.0.5

func (b *Accumulative) WriteByte(p byte) *Accumulative

WriteByte writes single byte.

func (*Accumulative) WriteByteIf added in v1.0.6

func (b *Accumulative) WriteByteIf(cond bool, p byte) *Accumulative

func (*Accumulative) WriteFloat added in v1.0.5

func (b *Accumulative) WriteFloat(f float64) *Accumulative

WriteFloat writes float value to the buffer.

func (*Accumulative) WriteFloatIf added in v1.0.6

func (b *Accumulative) WriteFloatIf(cond bool, f float64) *Accumulative

func (*Accumulative) WriteIf added in v1.0.6

func (b *Accumulative) WriteIf(cond bool, p []byte) *Accumulative

func (*Accumulative) WriteInt added in v1.0.5

func (b *Accumulative) WriteInt(i int64) *Accumulative

WriteInt writes integer value to the buffer.

func (*Accumulative) WriteIntIf added in v1.0.6

func (b *Accumulative) WriteIntIf(cond bool, i int64) *Accumulative

func (*Accumulative) WriteMarshallerTo added in v1.0.5

func (b *Accumulative) WriteMarshallerTo(m MarshallerTo) *Accumulative

WriteMarshallerTo marshal data of struct implemented MarshallerTo interface and write it to the buffer.

func (*Accumulative) WriteMarshallerToIf added in v1.0.6

func (b *Accumulative) WriteMarshallerToIf(cond bool, m MarshallerTo) *Accumulative

func (*Accumulative) WriteStr added in v1.0.5

func (b *Accumulative) WriteStr(s string) *Accumulative

WriteStr writes string to the buffer. DEPRECATED: use WriteString() instead.

func (*Accumulative) WriteString added in v1.0.5

func (b *Accumulative) WriteString(s string) *Accumulative

WriteString writes string to the buffer.

func (*Accumulative) WriteStringIf added in v1.0.6

func (b *Accumulative) WriteStringIf(cond bool, s string) *Accumulative

func (*Accumulative) WriteUint added in v1.0.5

func (b *Accumulative) WriteUint(u uint64) *Accumulative

WriteUint writes unsigned integer value to the buffer.

func (*Accumulative) WriteUintIf added in v1.0.6

func (b *Accumulative) WriteUintIf(cond bool, u uint64) *Accumulative

func (*Accumulative) WriteX added in v1.0.5

func (b *Accumulative) WriteX(x any) *Accumulative

WriteX write v with arbitrary type to the buffer.

func (*Accumulative) WriteXIf added in v1.0.6

func (b *Accumulative) WriteXIf(cond bool, x any) *Accumulative

type AccumulativeBuf

type AccumulativeBuf = Accumulative

AccumulativeBuf is an alias of Accumulative type. DEPRECATED: use Accumulative instead. Kept for backward compatibility.

type AccumulativeWriter added in v1.0.5

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

AccumulativeWriter is a wrapper around Accumulative that implements IO writers interfaces.

func (AccumulativeWriter) Bytes added in v1.0.6

func (b AccumulativeWriter) Bytes() []byte

func (AccumulativeWriter) Cap added in v1.0.6

func (b AccumulativeWriter) Cap() int

func (AccumulativeWriter) Len added in v1.0.6

func (b AccumulativeWriter) Len() int

func (AccumulativeWriter) Reset added in v1.0.6

func (b AccumulativeWriter) Reset()

func (AccumulativeWriter) String added in v1.0.6

func (b AccumulativeWriter) String() string

func (AccumulativeWriter) Write added in v1.0.5

func (b AccumulativeWriter) Write(p []byte) (int, error)

func (AccumulativeWriter) WriteApplyFn added in v1.0.6

func (b AccumulativeWriter) WriteApplyFn(p []byte, fn func(dst, p []byte) []byte) (int, error)

func (AccumulativeWriter) WriteApplyFnString added in v1.0.6

func (b AccumulativeWriter) WriteApplyFnString(s string, fn func(dst, p []byte) []byte) (int, error)

func (AccumulativeWriter) WriteBool added in v1.0.6

func (b AccumulativeWriter) WriteBool(b_ bool) (int, error)

func (AccumulativeWriter) WriteByte added in v1.0.5

func (b AccumulativeWriter) WriteByte(p byte) error

func (AccumulativeWriter) WriteFloat added in v1.0.6

func (b AccumulativeWriter) WriteFloat(f float64) (int, error)

func (AccumulativeWriter) WriteInt added in v1.0.6

func (b AccumulativeWriter) WriteInt(i int64) (int, error)

func (AccumulativeWriter) WriteMarshallerTo added in v1.0.6

func (b AccumulativeWriter) WriteMarshallerTo(m MarshallerTo) (int, error)

func (AccumulativeWriter) WriteString added in v1.0.5

func (b AccumulativeWriter) WriteString(s string) (int, error)

func (AccumulativeWriter) WriteUint added in v1.0.6

func (b AccumulativeWriter) WriteUint(u uint64) (int, error)

func (AccumulativeWriter) WriteX added in v1.0.6

func (b AccumulativeWriter) WriteX(x any) (int, error)

type Chain added in v1.0.5

type Chain []byte

Chain is a primitive byte buffer with chain call support.

func (*Chain) Bytes added in v1.0.5

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

Bytes returns contents of the buffer.

func (*Chain) BytesCopy added in v1.0.5

func (b *Chain) BytesCopy() []byte

BytesCopy returns copy of the buffer.

func (*Chain) Cap added in v1.0.5

func (b *Chain) Cap() int

func (*Chain) Grow added in v1.0.5

func (b *Chain) Grow(newLen int) *Chain

Grow increases length of the buffer.

func (*Chain) GrowDelta added in v1.0.5

func (b *Chain) GrowDelta(delta int) *Chain

GrowDelta increases length of the buffer to actual length + delta.

See Grow().

func (*Chain) Len added in v1.0.5

func (b *Chain) Len() int

func (*Chain) Replace added in v1.0.5

func (b *Chain) Replace(old, new []byte, n int) *Chain

Replace replaces old bytes to new in buffer.

func (*Chain) ReplaceAll added in v1.0.5

func (b *Chain) ReplaceAll(old, new []byte) *Chain

ReplaceAll replace all occurrences of old bytes to new in buffer.

func (*Chain) ReplaceStr added in v1.0.5

func (b *Chain) ReplaceStr(old, new string, n int) *Chain

ReplaceStr replace old to new substrings in buffer. DEPRECATED: use ReplaceString() instead.

func (*Chain) ReplaceStrAll added in v1.0.5

func (b *Chain) ReplaceStrAll(old, new string) *Chain

ReplaceStrAll replaces all occurrences of old substrings to new in buffer. DEPRECATED: use ReplaceStringAll() instead.

func (*Chain) ReplaceString added in v1.0.5

func (b *Chain) ReplaceString(old, new string, n int) *Chain

ReplaceString replace old to new substrings in buffer.

func (*Chain) ReplaceStringAll added in v1.0.5

func (b *Chain) ReplaceStringAll(old, new string) *Chain

ReplaceStringAll replaces all occurrences of old substrings to new in buffer.

func (*Chain) Reset added in v1.0.5

func (b *Chain) Reset() *Chain

func (*Chain) String added in v1.0.5

func (b *Chain) String() string

Get contents of the buffer as string.

func (*Chain) StringCopy added in v1.0.5

func (b *Chain) StringCopy() string

StringCopy returns copy of the buffer contents as string.

func (*Chain) Write added in v1.0.5

func (b *Chain) Write(p []byte) *Chain

Write bytes to the buffer.

func (*Chain) WriteApplyFn added in v1.0.5

func (b *Chain) WriteApplyFn(p []byte, fn func(dst, p []byte) []byte) *Chain

WriteApplyFn applies fn to p and write result to the buffer.

func (*Chain) WriteApplyFnIf added in v1.0.6

func (b *Chain) WriteApplyFnIf(cond bool, p []byte, fn func(dst, p []byte) []byte) *Chain

func (*Chain) WriteApplyFnStr added in v1.0.5

func (b *Chain) WriteApplyFnStr(s string, fn func(dst, p []byte) []byte) *Chain

WriteApplyFnStr applies fn to s and write result to the buffer. DEPRECATED: use WriteApplyFnString() instead.

func (*Chain) WriteApplyFnString added in v1.0.5

func (b *Chain) WriteApplyFnString(s string, fn func(dst, p []byte) []byte) *Chain

WriteApplyFnString applies fn to s and write result to the buffer.

func (*Chain) WriteApplyFnStringIf added in v1.0.6

func (b *Chain) WriteApplyFnStringIf(cond bool, s string, fn func(dst, p []byte) []byte) *Chain

func (*Chain) WriteBool added in v1.0.5

func (b *Chain) WriteBool(v bool) *Chain

WriteBool writes boolean value to the buffer.

func (*Chain) WriteBoolIf added in v1.0.6

func (b *Chain) WriteBoolIf(cond bool, v bool) *Chain

func (*Chain) WriteByte added in v1.0.5

func (b *Chain) WriteByte(p byte) *Chain

WriteByte writes single byte.

func (*Chain) WriteByteIf added in v1.0.6

func (b *Chain) WriteByteIf(cond bool, p byte) *Chain

func (*Chain) WriteFloat added in v1.0.5

func (b *Chain) WriteFloat(f float64) *Chain

WriteFloat writes float value to the buffer.

func (*Chain) WriteFloatIf added in v1.0.6

func (b *Chain) WriteFloatIf(cond bool, f float64) *Chain

func (*Chain) WriteIf added in v1.0.6

func (b *Chain) WriteIf(cond bool, p []byte) *Chain

func (*Chain) WriteInt added in v1.0.5

func (b *Chain) WriteInt(i int64) *Chain

WriteInt writes integer value to the buffer.

func (*Chain) WriteIntIf added in v1.0.6

func (b *Chain) WriteIntIf(cond bool, i int64) *Chain

func (*Chain) WriteMarshallerTo added in v1.0.5

func (b *Chain) WriteMarshallerTo(m MarshallerTo) *Chain

WriteMarshallerTo marshal data of struct implemented MarshallerTo interface and write it to the buffer.

func (*Chain) WriteMarshallerToIf added in v1.0.6

func (b *Chain) WriteMarshallerToIf(cond bool, m MarshallerTo) *Chain

func (*Chain) WriteStr added in v1.0.5

func (b *Chain) WriteStr(s string) *Chain

WriteStr writes string to the buffer. DEPRECATED: use WriteString() instead.

func (*Chain) WriteString added in v1.0.5

func (b *Chain) WriteString(s string) *Chain

WriteString writes string to the buffer.

func (*Chain) WriteStringIf added in v1.0.6

func (b *Chain) WriteStringIf(cond bool, s string) *Chain

func (*Chain) WriteUint added in v1.0.5

func (b *Chain) WriteUint(u uint64) *Chain

WriteUint writes unsigned integer value to the buffer.

func (*Chain) WriteUintIf added in v1.0.6

func (b *Chain) WriteUintIf(cond bool, u uint64) *Chain

func (*Chain) WriteX added in v1.0.5

func (b *Chain) WriteX(x any) *Chain

WriteX write x with arbitrary type to the buffer.

func (*Chain) WriteXIf added in v1.0.6

func (b *Chain) WriteXIf(cond bool, x any) *Chain

type ChainBuf

type ChainBuf = Chain

ChainBuf is an alias of Chain type. DEPRECATED: use Chain instead. Kept for backward compatibility.

type MarshallerTo

type MarshallerTo interface {
	Size() int
	MarshalTo([]byte) (int, error)
}

MarshallerTo interface to write struct like Protobuf.

Jump to

Keyboard shortcuts

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