std/bytes
Package std/bytes
implements functions for the manipulation of byte slices. It is analogous to the facilities of the std/strings
package. But optimized for byte slices, may provide more efficient functions. If you have byte slice form of strings, this package is the best option for most cases.
Functions
fn Equal(a: []byte, b: []byte): bool
Reports whether a and b are the same length and contain the same bytes. A nil argument is equivalent to an empty slice.
fn Compare(a: []byte, b: []byte): int
Returns an integer comparing two byte slices lexicographically. The result will be 0 if a == b, -1 if a < b, and +1 if a > b. A nil argument is equivalent to an empty slice.
fn Count(s: []byte, sep: []byte): int
Counts the number of non-overlapping instances of sep in s. If sep is an empty slice, returns 1 + the number of UTF-8-encoded code points in s.
fn Contains(b: []byte, subslice: []byte): bool
Reports whether subslice is within b.
fn ContainsAny(b: []byte, chars: str): bool
Reports whether any of the UTF-8-encoded code points in chars are within b.
fn ContainsRune(b: []byte, r: rune): bool
Reports whether the rune is contained in the UTF-8-encoded byte slice b.
fn ContainsFunc(b: []byte, f: fn(rune): bool): bool
Reports whether any of the UTF-8-encoded code points r within b satisfy f(r).
fn IndexByte(b: []byte, c: byte): int
Returns the index of the first instance of c in b, or -1 if c is not present in b.
fn LastIndex(s: []byte, sep: []byte): int
Returns the index of the last instance of sep in s, or -1 if sep is not present in s.
fn LastIndexByte(s: []byte, c: byte): int
Returns the index of the last instance of c in s, or -1 if c is not present in s.
fn IndexRune(s: []byte, r: rune): int
Interprets s as a sequence of UTF-8-encoded code points. It returns the byte index of the first occurrence in s of the given rune. It returns -1 if rune is not present in s. If r is [utf8::RuneError], it returns the first instance of any invalid UTF-8 byte sequence.
fn IndexAny(s: []byte, chars: str): int
Interprets s as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the first occurrence in s of any of the Unicode code points in chars. It returns -1 if chars is empty or if there is no code point in common.
fn LastIndexAny(s: []byte, chars: str): int
Interprets s as a sequence of UTF-8-encoded Unicode code points. It returns the byte index of the last occurrence in s of any of the Unicode code points in chars. It returns -1 if chars is empty or if there is no code point in common.
fn SplitN(mut s: []byte, sep: []byte, n: int): [][]byte
Slices s into subslices separated by sep and returns a slice of the subslices between those separators. If sep is empty, SplitN splits after each UTF-8 sequence. The count determines the number of subslices to return:
- n > 0: at most n subslices; the last subslice will be the unsplit remainder;
- n == 0: the result is nil (zero subslices);
- n < 0: all subslices.
To split around the first instance of a separator, see [Cut].
fn SplitAfterN(mut s: []byte, sep: []byte, n: int): [][]byte
Slices s into subslices after each instance of sep and returns a slice of those subslices. If sep is empty, SplitAfterN splits after each UTF-8 sequence. The count determines the number of subslices to return:
- n > 0: at most n subslices; the last subslice will be the unsplit remainder;
- n == 0: the result is nil (zero subslices);
- n < 0: all subslices.
fn Split(mut s: []byte, sep: []byte): [][]byte
Slices s into all subslices separated by sep and returns a slice of the subslices between those separators. If sep is empty, Split splits after each UTF-8 sequence. It is equivalent to SplitN with a count of -1.
To split around the first instance of a separator, see [Cut].
fn SplitAfter(mut s: []byte, sep: []byte): [][]byte
Slices s into all subslices after each instance of sep and returns a slice of those subslices. If sep is empty, SplitAfter splits after each UTF-8 sequence. It is equivalent to SplitAfterN with a count of -1.
fn Join(s: [][]byte, sep: []byte): []byte
Concatenates the elements of s to create a new byte slice. The separator sep is placed between elements in the resulting slice.
fn HasPrefix(s: []byte, prefix: []byte): bool
Reports whether the byte slice s begins with prefix.
fn HasSuffix(s: []byte, suffix: []byte): bool
Reports whether the byte slice s ends with suffix.
fn Map(mapping: fn(rune): rune, s: []byte): []byte
Returns a copy of the byte slice s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the byte slice with no replacement. The characters in s and the output are interpreted as UTF-8-encoded code points.
fn Repeat(b: []byte, count: int): []byte
Returns a new byte slice consisting of count copies of b.
It panics if count is negative or if the result of (len(b) * count) overflows.
fn IndexFunc(s: []byte, f: fn(rune): bool): int
Interprets s as a sequence of UTF-8-encoded code points. It returns the byte index in s of the first Unicode code point satisfying f(c), or -1 if none do.
fn LastIndexFunc(s: []byte, f: fn(rune): bool): int
Interprets s as a sequence of UTF-8-encoded code points. It returns the byte index in s of the last Unicode code point satisfying f(c), or -1 if none do.
fn TrimLeftFunc(mut s: []byte, f: fn(rune): bool): []byte
Treats s as UTF-8-encoded bytes and returns a subslice of s by slicing off all leading UTF-8-encoded code points c that satisfy f(c).
fn TrimRight(mut s: []byte, cutset: str): []byte
Returns a subslice of s by slicing off all trailing UTF-8-encoded code points that are contained in cutset.
fn TrimRightFunc(mut s: []byte, f: fn(rune): bool): []byte
Returns a subslice of s by slicing off all trailing UTF-8-encoded code points c that satisfy f(c).
fn TrimFunc(mut s: []byte, f: fn(rune): bool): []byte
Returns a subslice of s by slicing off all leading and trailing UTF-8-encoded code points c that satisfy f(c).
fn TrimPrefix(mut s: []byte, prefix: []byte): []byte
Returns s without the provided leading prefix string. If s doesn't start with prefix, s is returned unchanged.
fn TrimSuffix(mut s: []byte, suffix: []byte): []byte
Returns s without the provided trailing suffix string. If s doesn't end with suffix, s is returned unchanged.
fn Trim(mut s: []byte, cutset: str): []byte
Returns a subslice of s by slicing off all leading and trailing UTF-8-encoded code points contained in cutset.
fn TrimLeft(mut s: []byte, cutset: str): []byte
Returns a subslice of s by slicing off all leading UTF-8-encoded code points contained in cutset.
fn TrimSpace(mut s: []byte): []byte
Returns a subslice of s by slicing off all leading and trailing white space, as defined by Unicode.
fn Runes(s: []byte): []rune
Interprets s as a sequence of UTF-8-encoded code points. It returns a slice of runes (Unicode code points) equivalent to s.
fn Replace(s: []byte, old: []byte, new: []byte, mut n: int): []byte
Returns a copy of the slice s with the first n non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the slice and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune slice. If n < 0, there is no limit on the number of replacements.
fn ReplaceAll(s: []byte, old: []byte, new: []byte): []byte
Returns a copy of the slice s with all non-overlapping instances of old replaced by new. If old is empty, it matches at the beginning of the slice and after each UTF-8 sequence, yielding up to k+1 replacements for a k-rune slice.
fn EqualFold(s: []byte, t: []byte): bool
Reports whether s and t, interpreted as UTF-8 strings, are equal under simple Unicode case-folding, which is a more general form of case-insensitivity.
fn Index(s: []byte, sep: []byte): int
Returns the index of the first instance of sep in s, or -1 if sep is not present in s.
fn Cut(mut s: []byte, sep: []byte): (before: []byte, after: []byte, found: bool)
Slices s around the first instance of sep, returning the text before and after sep. The found result reports whether sep appears in s. If sep does not appear in s, returns s, nil, false.
Returns slices of the original slice s, not copies.
fn Clone(b: []byte): []byte
Returns a copy of b[:len(b)]. The result may have additional unused capacity. Clone(nil) returns nil.
fn CutPrefix(mut s: []byte, prefix: []byte): (after: []byte, found: bool)
Returns s without the provided leading prefix byte slice and reports whether it found the prefix. If s doesn't start with prefix, returns s, false. If prefix is the empty byte slice, returns s, true.
Returns slices of the original slice s, not copies.
fn CutSuffix(mut s: []byte, suffix: []byte): (before: []byte, found: bool)
Returns s without the provided ending suffix byte slice and reports whether it found the suffix. If s doesn't end with suffix, returns s, false. If suffix is the empty byte slice, returns s, true.
Returns slices of the original slice s, not copies.