bytes

package
v0.0.0-...-782cc26 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2018 License: MIT Imports: 5 Imported by: 1

Documentation

Overview

Alternative to the Go standard lib's bytes package that avoids the GC problems.

Index

Constants

This section is empty.

Variables

View Source
var ErrTooLarge = errors.New("bytes.Buffer: too large")

ErrTooLarge is passed to panic if memory cannot be allocated to store data in a buffer.

Functions

This section is empty.

Types

type Buffer

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

func (*Buffer) Bytes

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

Returns a byte slice that directly accesses the buffer's data. WARNING! Appending anything to the buffer invalidates any slices obtained via this function. They may end up pointing at invalid memory locations.

NOTE: The return value is always a valid slice, even if the buffer is empty. The function never returns nil.

func (*Buffer) Capacity

func (b *Buffer) Capacity() int

Returns the total buffer space (used + unused).

func (*Buffer) Contains

func (b *Buffer) Contains(s string) bool

Return true if the buffer contains the string s. Returns true if s == "".

func (*Buffer) Grow

func (b *Buffer) Grow(n int) int

Grow() grows the buffer to guarantee space for n more bytes. It returns Len(). If the buffer can't grow it will panic with ErrTooLarge. Grow() does not change Len(), only Capacity().

func (*Buffer) Len

func (b *Buffer) Len() int

Returns the number of meaningful bytes in the buffer (as opposed to Capacity()).

func (*Buffer) Pointer

func (b *Buffer) Pointer() unsafe.Pointer

Returns nil if no space is currently allocated for the Buffer, otherwise a raw memory pointer to the buffer space.

func (*Buffer) Read

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

Read reads the next len(p) bytes from the buffer or until the buffer is drained. The return value n is the number of bytes read. If the buffer has no data to return, err is io.EOF (unless len(p) is zero); otherwise it is nil. Appending more data after an EOF was returned by Read() will clear the EOF condition and further calls to Read() will return the newly appended data.

func (*Buffer) ReadFrom

func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)

Reads data from r until EOF and appends it to the buffer, growing the buffer as needed. The return value n is the number of bytes read. Any error except io.EOF encountered during the read is also returned. If the buffer becomes too large, ReadFrom will panic with ErrTooLarge.

func (*Buffer) Reset

func (b *Buffer) Reset()

Frees the memory held by the Buffer. The Buffer remains valid and is ready to take new data.

func (*Buffer) Seek

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

Sets the pointer for the next Read() operation. If whence == 0, offset is the absolute offset from the buffer start. If whence == 1, offset is added to the current position. If whence == 2, offset is added to "1 past the last byte", i.e. offset==-1 with whence==2 positions the read pointer to return the last byte in the buffer on the next Read().

If the resulting read pointer would point before the 1st byte in the buffer, it is placed at absolute offset 0. If it would point after the last byte, it is placed at "1 past the last byte".

The return value is the new absolute offset of the read pointer counted from the beginning of the buffer. An error is never returned.

func (*Buffer) Split

func (b *Buffer) Split(sep string) []string

Split slices the buffer into all substrings separated by sep and returns a slice of the substrings between those separators. The buffer itself is unchanged and the strings are copies (of course). If sep is empty, this function panics.

func (*Buffer) String

func (b *Buffer) String() string

Returns a copy of the Buffer contents as string.

func (*Buffer) Trim

func (b *Buffer) Trim(start, end int)

Replaces the contents of the buffer b with b.Bytes()[start:end]. Unlike a sublicing operation this function permits start and end to be out of bounds. If start >= end, the buffer will be Reset().

NOTE: The operation does NOT free any memory. The buffer's Capacity() will remain unchanged.

func (*Buffer) TrimSpace

func (b *Buffer) TrimSpace()

removes all characters <= ' ' from both ends of the buffer.

func (*Buffer) Write

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

Appends the bytes from p to the buffer. Always returns len(p), nil. If out of memory occurs trying to grow the buffer, the function will panic with ErrTooLarge.

func (*Buffer) Write0

func (b *Buffer) Write0(n int)

Appends n 0-bytes to the buffer.

func (*Buffer) WriteByte

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

WriteByte appends the byte c to the buffer. The returned error is always nil, but is included to match bufio.Writer's WriteByte. If the buffer becomes too large, WriteByte will panic with ErrTooLarge.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (n int, err error)

Appends the bytes from s to the buffer. Always returns len(s), nil. If out of memory occurs trying to grow the buffer, the function will panic with ErrTooLarge.

Jump to

Keyboard shortcuts

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