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.
Index
fn Equal(a: []byte, b: []byte): bool
fn Compare(a: []byte, b: []byte): int
fn Count(s: []byte, sep: []byte): int
fn Contains(b: []byte, subslice: []byte): bool
fn ContainsAny(b: []byte, chars: str): bool
fn ContainsRune(b: []byte, r: rune): bool
fn ContainsFunc(b: []byte, f: fn(rune): bool): bool
fn IndexByte(b: []byte, c: byte): int
fn LastIndex(s: []byte, sep: []byte): int
fn LastIndexByte(s: []byte, c: byte): int
fn IndexRune(s: []byte, r: rune): int
fn IndexAny(s: []byte, chars: str): int
fn LastIndexAny(s: []byte, chars: str): int
fn SplitN(mut s: []byte, sep: []byte, n: int): [][]byte
fn SplitAfterN(mut s: []byte, sep: []byte, n: int): [][]byte
fn Split(mut s: []byte, sep: []byte): [][]byte
fn SplitAfter(mut s: []byte, sep: []byte): [][]byte
fn Join(s: [][]byte, sep: []byte): []byte
fn HasPrefix(s: []byte, prefix: []byte): bool
fn HasSuffix(s: []byte, suffix: []byte): bool
fn Map(mapping: fn(rune): rune, s: []byte): []byte
fn Repeat(b: []byte, count: int): []byte
fn IndexFunc(s: []byte, f: fn(rune): bool): int
fn LastIndexFunc(s: []byte, f: fn(rune): bool): int
fn TrimLeftFunc(mut s: []byte, f: fn(rune): bool): []byte
fn TrimRight(mut s: []byte, cutset: str): []byte
fn TrimRightFunc(mut s: []byte, f: fn(rune): bool): []byte
fn TrimFunc(mut s: []byte, f: fn(rune): bool): []byte
fn TrimPrefix(mut s: []byte, prefix: []byte): []byte
fn TrimSuffix(mut s: []byte, suffix: []byte): []byte
fn Trim(mut s: []byte, cutset: str): []byte
fn TrimLeft(mut s: []byte, cutset: str): []byte
fn TrimSpace(mut s: []byte): []byte
fn Runes(s: []byte): []rune
fn Replace(s: []byte, old: []byte, new: []byte, mut n: int): []byte
fn ReplaceAll(s: []byte, old: []byte, new: []byte): []byte
fn EqualFold(s: []byte, t: []byte): bool
fn Index(s: []byte, sep: []byte): int
fn Cut(mut s: []byte, sep: []byte): (before: []byte, after: []byte, found: bool)
fn Clone(b: []byte): []byte
fn CutPrefix(mut s: []byte, prefix: []byte): (after: []byte, found: bool)
fn CutSuffix(mut s: []byte, suffix: []byte): (before: []byte, found: bool)
fn ToUpper(s: []byte): []byte
fn ToLower(s: []byte): []byte
Equal
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.
Compare
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.
Count
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.
Contains
fn Contains(b: []byte, subslice: []byte): bool
Reports whether subslice is within b.
ContainsAny
fn ContainsAny(b: []byte, chars: str): bool
Reports whether any of the UTF-8-encoded code points in chars are within b.
ContainsRune
fn ContainsRune(b: []byte, r: rune): bool
Reports whether the rune is contained in the UTF-8-encoded byte slice b.
ContainsFunc
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).
IndexByte
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.
LastIndex
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.
LastIndexByte
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.
IndexRune
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.
IndexAny
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.
LastIndexAny
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.
SplitN
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].
SplitAfterN
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.
Split
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].
SplitAfter
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.
Join
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.
HasPrefix
fn HasPrefix(s: []byte, prefix: []byte): bool
Reports whether the byte slice s begins with prefix.
HasSuffix
fn HasSuffix(s: []byte, suffix: []byte): bool
Reports whether the byte slice s ends with suffix.
Map
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.
Repeat
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.
IndexFunc
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.
LastIndexFunc
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.
TrimLeftFunc
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).
TrimRight
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.
TrimRightFunc
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).
TrimFunc
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).
TrimPrefix
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.
TrimSuffix
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.
Trim
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.
TrimLeft
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.
TrimSpace
fn TrimSpace(mut s: []byte): []byte
Returns a subslice of s by slicing off all leading and trailing white space, as defined by Unicode.
Runes
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.
Replace
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.
ReplaceAll
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.
EqualFold
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.
Index
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.
Cut
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.
Clone
fn Clone(b: []byte): []byte
Returns a copy of b[:len(b)]. The result may have additional unused capacity. Clone(nil) returns nil.
CutPrefix
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.
CutSuffix
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.
ToUpper
fn ToUpper(s: []byte): []byte
Returns a copy of the byte slice s with all Unicode letters mapped to their upper case.
ToLower
fn ToLower(s: []byte): []byte
Returns a copy of the byte slice s with all Unicode letters mapped to their lower case.