Documentation
¶
Overview ¶
Alternative to the Go standard lib's bytes package that avoids the GC problems.
Index ¶
- Variables
- type Buffer
- func (b *Buffer) Bytes() []byte
- func (b *Buffer) Capacity() int
- func (b *Buffer) Contains(s string) bool
- func (b *Buffer) Grow(n int) int
- func (b *Buffer) Len() int
- func (b *Buffer) Pointer() unsafe.Pointer
- func (b *Buffer) Read(p []byte) (n int, err error)
- func (b *Buffer) ReadFrom(r io.Reader) (n int64, err error)
- func (b *Buffer) Reset()
- func (b *Buffer) Seek(offset int64, whence int) (int64, error)
- func (b *Buffer) Split(sep string) []string
- func (b *Buffer) String() string
- func (b *Buffer) Trim(start, end int)
- func (b *Buffer) TrimSpace()
- func (b *Buffer) Write(p []byte) (n int, err error)
- func (b *Buffer) Write0(n int)
- func (b *Buffer) WriteByte(c byte) error
- func (b *Buffer) WriteString(s string) (n int, err error)
Constants ¶
This section is empty.
Variables ¶
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 ¶
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) Grow ¶
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 ¶
Returns the number of meaningful bytes in the buffer (as opposed to Capacity()).
func (*Buffer) Pointer ¶
Returns nil if no space is currently allocated for the Buffer, otherwise a raw memory pointer to the buffer space.
func (*Buffer) Read ¶
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 ¶
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 ¶
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 ¶
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) Trim ¶
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 ¶
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.