byteutil

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2023 License: MIT Imports: 6 Imported by: 24

README

go-byteutil

A useful package of byte utilities.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrBeyondBufferLen is returned if .WriteAt() is attempted beyond buffer length.
	ErrBeyondBufferLen = errors.New("start beyond buffer length")
)

Functions

func B2S

func B2S(b []byte) string

B2S returns a string representation of []byte without allocation.

According to the Go spec strings are immutable and byte slices are not. The way this gets implemented is strings under the hood are:

type StringHeader struct {
	Data uintptr
	Len  int
}

while slices are:

type SliceHeader struct {
	Data uintptr
	Len  int
	Cap  int
}

because being mutable, you can change the data, length etc, but the string has to promise to be read-only to all who get copies of it.

So in practice when you do a conversion of `string(byteSlice)` it actually performs an allocation because it has to copy the contents of the byte slice into a safe read-only state.

Being that the shared fields are in the same struct indices (no different offsets), means that if you have a byte slice you can "forcibly" cast it to a string. Which in a lot of situations can be risky, because then it means you have a string that is NOT immutable, as if someone changes the data in the originating byte slice then the string will reflect that change! Now while this does seem hacky, and it _kind_ of is, it is something that you see performed in the standard library. If you look at the definition for `strings.Builder{}.String()` you'll see this :)

func Copy

func Copy(b []byte) []byte

Copy returns a copy of []byte.

func S2B

func S2B(s string) []byte

S2B returns a []byte representation of string without allocation (minus slice header). See B2S() code comment, and this function's implementation for a better understanding.

Types

type Buffer

type Buffer struct{ B []byte }

Buffer is a simple wrapper around a byte slice.

func (*Buffer) Cap

func (buf *Buffer) Cap() int

Cap returns the capacity of the buffer's underlying byte slice.

func (*Buffer) Full added in v1.0.2

func (buf *Buffer) Full() []byte

Full returns the full capacity byteslice allocated for this buffer.

func (*Buffer) Grow

func (buf *Buffer) Grow(sz int)

Grow will increase the buffers length by 'sz', and the capacity by at least this.

func (*Buffer) Guarantee

func (buf *Buffer) Guarantee(sz int)

Guarantee will guarantee buffer containers at least 'sz' remaining capacity.

func (*Buffer) Len

func (buf *Buffer) Len() int

Len returns the length of the buffer's underlying byte slice.

func (*Buffer) ReadFrom added in v1.2.0

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

ReadFrom will read bytes from reader into buffer, fulfilling io.ReaderFrom.

func (*Buffer) Reset

func (buf *Buffer) Reset()

Reset will reset the buffer length to 0 (retains capacity).

func (*Buffer) String

func (buf *Buffer) String() string

String returns the underlying byte slice as a string. Please note this value is tied directly to the underlying byte slice, if you write to the buffer then returned string values will also change.

To get an immutable string from buffered data, use string(buf.B).

func (*Buffer) Truncate

func (buf *Buffer) Truncate(n int)

Truncate will reduce the length of the buffer by 'n'.

func (*Buffer) Write

func (buf *Buffer) Write(b []byte) (int, error)

Write will append given byte slice to buffer, fulfilling io.Writer.

func (*Buffer) WriteAt

func (buf *Buffer) WriteAt(b []byte, start int64) (int, error)

WriteAt will append given byte slice to buffer at index 'start', fulfilling io.WriterAt.

func (*Buffer) WriteByte

func (buf *Buffer) WriteByte(c byte) error

WriteByte will append given byte to buffer, fulfilling io.ByteWriter.

func (*Buffer) WriteRune

func (buf *Buffer) WriteRune(r rune) (int, error)

WriteRune will append given rune to buffer.

func (*Buffer) WriteString

func (buf *Buffer) WriteString(s string) (int, error)

WriteString will append given string to buffer, fulfilling io.StringWriter.

func (*Buffer) WriteStringAt

func (buf *Buffer) WriteStringAt(s string, start int64) (int, error)

WriteStringAt will append given string to buffer at index 'start'.

func (*Buffer) WriteTo added in v1.2.0

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

WriteTo will write bytes from buffer into writer, fulfilling io.WriterTo.

type ReadNopCloser added in v1.1.2

type ReadNopCloser struct {
	Reader
}

ReadNopCloser wraps a Reader{} to provide nop close method.

func (*ReadNopCloser) Close added in v1.1.2

func (*ReadNopCloser) Close() error

type Reader added in v1.1.0

type Reader struct {
	B []byte
	bytes.Reader
}

Reader wraps a bytes.Reader{} to provide Rewind() capabilities.

func NewReader added in v1.1.1

func NewReader(b []byte) *Reader

NewReader returns a new Reader{} instance reset to b.

func (*Reader) Reset added in v1.1.0

func (r *Reader) Reset(b []byte)

Reset resets the Reader{} to be reading from b and sets Reader{}.B.

func (*Reader) Rewind added in v1.1.0

func (r *Reader) Rewind()

Rewind resets the Reader{} to be reading from the start of Reader{}.B.

Jump to

Keyboard shortcuts

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