std / bytes

bytes

import "std:bytes"

Provides byte slice manipulation functions. Functions are organized by allocation behavior: zero-alloc functions return views into input slices, Buffer writes for explicit memory ownership, Split/Fields return list[[]byte] where caller frees array. Mirrors the strings package API.

View source on Codeberg →

Types

#
type Buffer list[byte]

Buffer is a type alias for list[byte] used for building byte sequences. The caller owns the buffer and must free it with BufFree when no longer needed.

Comparison

#
func Equal(a, b []byte) bool

Reports whether a and b are the same length and contain the same bytes.

#
func Compare(a, b []byte) int

Returns an integer comparing two byte slices lexicographically. Returns 0 if a == b, -1 if a < b, and +1 if a > b.

#
func Contains(b, subslice []byte) bool

Reports whether subslice is within b.

#
func ContainsByte(b []byte, c byte) bool

Reports whether the byte c is within b.

#
func HasPrefix(b, prefix []byte) bool

Reports whether the byte slice b begins with prefix.

#
func HasSuffix(b, suffix []byte) bool

Reports whether the byte slice b ends with suffix.

#
func Index(b, sep []byte) int

Returns the index of the first occurrence of sep in b, or -1 if sep is not present.

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

Returns the index of the first occurrence of byte c in b, or -1 if c is not present.

#
func LastIndex(b, sep []byte) int

Returns the index of the last occurrence of sep in b, or -1 if sep is not present.

#
func LastIndexByte(b []byte, c byte) int

Returns the index of the last occurrence of byte c in b, or -1 if c is not present.

#
func Count(b, sep []byte) int

Counts the number of non-overlapping occurrences of sep in b.

Trimming

#
func TrimSpace(b []byte) []byte

Returns a subslice of b with all leading and trailing whitespace removed. Zero allocation; returns a view into the original slice.

#
func TrimPrefix(b, prefix []byte) []byte

Returns b without the provided leading prefix. If b does not start with prefix, b is returned unchanged.

#
func TrimSuffix(b, suffix []byte) []byte

Returns b without the provided trailing suffix. If b does not end with suffix, b is returned unchanged.

Cutting

#
func Cut(b, sep []byte) ([]byte, []byte, bool)

Slices b around the first occurrence of sep, returning the parts before and after sep. The found result reports whether sep appears in b. If sep does not appear, Cut returns b, nil, false.

#
func CutPrefix(b, prefix []byte) ([]byte, bool)

Returns b without the provided leading prefix and reports whether it was found. If b does not start with prefix, returns b, false.

#
func CutSuffix(b, suffix []byte) ([]byte, bool)

Returns b without the provided trailing suffix and reports whether it was found. If b does not end with suffix, returns b, false.

Splitting

#
func Split(b []byte, sep []byte) list[[]byte]

Slices b into all subslices separated by sep and returns a dynamic array of the subslices. The caller is responsible for freeing the returned array.

#
func SplitN(b []byte, sep []byte, n int) list[[]byte]

Slices b into at most n subslices separated by sep and returns a dynamic array. The caller is responsible for freeing the returned array.

#
func Fields(b []byte) list[[]byte]

Splits the byte slice b around each run of consecutive whitespace characters and returns a dynamic array of subslices. The caller is responsible for freeing the returned array.

Buffer

#
func NewBuffer() Buffer

Creates a new empty Buffer with the default initial capacity.

#
func NewBufferSize(capacity int) Buffer

Creates a new empty Buffer with the specified initial capacity.

#
func WriteBuf(b Buffer, data []byte) int

Appends the contents of data to the buffer. Returns the number of bytes written.

#
func WriteByteBuf(b Buffer, c byte)

Appends a single byte to the buffer.

#
func WriteStringBuf(b Buffer, s string) int

Appends the contents of the string s to the buffer. Returns the number of bytes written.

#
func BufBytes(b Buffer) []byte

Returns the contents of the buffer as a byte slice. The slice is valid only until the next buffer modification.

#
func BufLen(b Buffer) int

Returns the number of bytes currently stored in the buffer.

#
func BufCap(b Buffer) int

Returns the capacity of the buffer's underlying storage.

#
func BufReset(b Buffer)

Resets the buffer to be empty, but retains the underlying storage for future writes.

#
func BufFree(b Buffer)

Frees the buffer's underlying dynamic array. The buffer must not be used after calling BufFree.