Skip to content

std/bytes

import "github.com/eugenioenko/goalchemy/std/bytes"

Package bytes manipulates byte slices. Functions that return parts of their input return subslices that share its storage, as in Go.

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

ErrTooLarge is returned when a Buffer cannot grow.

func Clone(b []byte) []byte

Clone returns a copy of b, or nil if b is nil.

func Compare(a, b []byte) int

Compare returns 0 if a == b, -1 if a < b, and +1 if a > b.

func Contains(b, sub []byte) bool

Contains reports whether sub is within b.

func ContainsRune(b []byte, r rune) bool

ContainsRune reports whether r is within b.

func Count(s, sep []byte) int

Count counts the non-overlapping instances of sep in s.

func Equal(a, b []byte) bool

Equal reports whether a and b have the same bytes; nil equals empty.

func EqualFold(s, t []byte) bool

EqualFold reports whether s and t are equal under simple Unicode case folding.

func Fields(s []byte) [][]byte

Fields splits s around runs of white space into subslices of s.

func HasPrefix(s, prefix []byte) bool

HasPrefix reports whether s begins with prefix.

func HasSuffix(s, suffix []byte) bool

HasSuffix reports whether s ends with suffix.

func Index(s, sep []byte) int

Index returns the index of the first instance of sep in s, or -1.

func IndexByte(b []byte, c byte) int

IndexByte returns the index of the first instance of c in b, or -1.

func IndexRune(s []byte, r rune) int

IndexRune returns the index of the first instance of r in s, or -1.

func Join(s [][]byte, sep []byte) []byte

Join concatenates s with sep between elements into a new slice.

func LastIndex(s, sep []byte) int

LastIndex returns the index of the last instance of sep in s, or -1.

func Repeat(b []byte, count int) []byte

Repeat returns count copies of b. It panics if count is negative.

func Split(s, sep []byte) [][]byte

Split slices s into all subslices separated by sep.

func SplitN(s, sep []byte, n int) [][]byte

SplitN slices s into at most n subslices separated by sep; n < 0 means all.

func ToLower(s []byte) []byte

ToLower returns a copy of s with every rune mapped to lower case.

func ToUpper(s []byte) []byte

ToUpper returns a copy of s with every rune mapped to upper case.

func TrimPrefix(s, prefix []byte) []byte

TrimPrefix returns s without prefix if present.

func TrimSpace(s []byte) []byte

TrimSpace returns the subslice of s without leading and trailing white space.

func TrimSuffix(s, suffix []byte) []byte

TrimSuffix returns s without suffix if present.

type Buffer struct {
}

Buffer is a growable byte buffer. The zero value is an empty buffer.

func NewBuffer(buf []byte) *Buffer

NewBuffer returns a Buffer whose initial contents are buf.

func NewBufferString(s string) *Buffer

NewBufferString returns a Buffer whose initial contents are s.

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

Bytes returns the unread portion of the buffer, sharing its storage.

func (b *Buffer) Grow(n int)

Grow ensures space for another n bytes. It panics if n is negative.

func (b *Buffer) Len() int

Len returns the number of unread bytes.

func (b *Buffer) Next(n int) []byte

Next returns the next n unread bytes, or all of them if fewer remain, and advances past them.

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

Read reads the next len(p) bytes from the buffer, or until it is drained. It returns io.EOF when the buffer has no data and p is not empty.

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

ReadByte returns the next byte, or io.EOF when the buffer is empty.

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

ReadFrom appends data from r until io.EOF and returns the number of bytes read. Errors other than io.EOF are returned.

func (b *Buffer) Reset()

Reset empties the buffer.

func (b *Buffer) String() string

String returns the unread portion of the buffer as a string.

func (b *Buffer) Truncate(n int)

Truncate keeps the first n unread bytes. It panics if n is out of range.

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

Write appends p and always returns len(p), nil.

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

WriteByte appends c and always returns nil.

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

WriteRune appends the UTF-8 encoding of r and returns its length.

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

WriteString appends s and always returns len(s), nil.

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

WriteTo writes the unread data to w until the buffer is drained or an error occurs.

type Reader struct {
}

Reader implements io.Reader, io.ReaderAt, io.Seeker, io.WriterTo, io.ByteScanner and io.RuneScanner over a byte slice. It does not modify the slice. The zero value reads nothing.

func NewReader(b []byte) *Reader

NewReader returns a Reader reading from b.

func (r *Reader) Len() int

Len returns the number of unread bytes.

func (r *Reader) Read(b []byte) (n int, err error)

Read implements io.Reader.

func (r *Reader) ReadAt(b []byte, off int64) (n int, err error)

ReadAt implements io.ReaderAt. It does not change the read position.

func (r *Reader) ReadByte() (byte, error)

ReadByte implements io.ByteReader.

func (r *Reader) ReadRune() (ch rune, size int, err error)

ReadRune implements io.RuneReader.

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

Reset resets the Reader to read from b.

func (r *Reader) Seek(offset int64, whence int) (int64, error)

Seek implements io.Seeker. Seeking past the end is allowed.

func (r *Reader) Size() int64

Size returns the original length of the underlying slice.

func (r *Reader) UnreadByte() error

UnreadByte steps back one byte.

func (r *Reader) UnreadRune() error

UnreadRune steps back over the rune returned by the previous ReadRune.

func (r *Reader) WriteTo(w io.Writer) (n int64, err error)

WriteTo implements io.WriterTo, writing the unread data to w.