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(s1: []byte, s2: []byte): bool
Reports whether two byte slices are the same length and contains same bytes. The nil slice considered as zero-length empty slice.
fn Repeat(s: []byte, mut n: int): []byte
Returns bytes that equals to concatenation of n-count s. Returns nil slice is n <= 0.
fn HasPrefix(s: []byte, sub: []byte): bool
Reports slice has prefix as specified sub-slice or not.
fn HasSuffix(s: []byte, sub: []byte): bool
Reports slice has suffix as specified sub-slice or not.
fn FindAt(s: []byte, sub: []byte, mut i: int): int
Returns index of first matched item with specified sub-slice, returns -1 if not exist any match. Starts searching at left of slice to right. Starts searching s at given index. Returns -1, if i < 0 || i >= len(s).
fn Find(s: []byte, sub: []byte): int
Returns index of first matched item with specified sub-slice, returns -1 if not exist any match. Starts searching at left of slice to right.
fn FindLastAt(s: []byte, sub: []byte, i: int): int
Returns index of first matched item with specified sub-slice, returns -1 if not exist any match. Starts searching at right of slice to left. Starts searching s at given index. Returns -1, if i < 0 || i >= len(s).
fn FindLast(s: []byte, sub: []byte): int
Returns index of first matched item with specified sub-slice, returns -1 if not exist any match. Starts searching at right of slice to left.
fn FindByte(s: []byte, b: byte): int
Returns index of first matched item with specified byte, returns -1 if not exist any match. Starts searching at left of slice to right.
fn FindLastByte(s: []byte, b: byte): int
Returns index of first matched item with specified byte, returns -1 if not exist any match. Starts searching at right of slice to left.
fn FindRune(s: []byte, r: rune): int
Returns index of first matched item with specified rune, returns -1 if not exist any match. Starts searching at left of slice to right.
fn FindLastRune(s: []byte, r: rune): int
Returns index of first matched item with specified rune, returns -1 if not exist any match. Starts searching at right of slice to left.
fn FindFnAt(s: []byte, mut i: int, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at left of slice to right. Starts searching s at given index. Returns -1, if i < 0.
fn FindFn(s: []byte, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at left of slice to right.
fn FindFnLastAt(s: []byte, mut i: int, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at right of slice to left. Starts searching s at given index. Returns -1, if i < 0 || i >= len(s).
fn FindFnLast(s: []byte, f: fn(mut rune): bool): int
Returns index of first matched item with finder function, returns -1 if not exist any match. Starts searching at right of slice to left.
fn FindAny(s: []byte, runes: []byte): int
Returns index of first matched item with any of runes, returns -1 if not exist any match. Starts searching at left of slice to right.
fn FindLastAny(s: []byte, runes: []byte): int
Returns index of first matched item with any of runes, returns -1 if not exist any match. Starts searching at right of slice to left.
fn Split(mut s: []byte, sub: []byte, mut n: int): [][]byte
Splits the slice into the specified number of parts to the specified sub-slice. Appends parts as immutable slice. Returns empty slice if n is equals to zero. Returns all parts if n less than zero.
fn SplitAll(mut s: []byte, sub: []byte): [][]byte
Same as the Split function. But splits all parts. Basically equals to Split(s, sub, -1)
call.
fn Contains(s: []byte, sub: []byte): bool
Reports whether slice includes sub-slice.
fn ContainsByte(s: []byte, b: byte): bool
Reports whether slice includes byte.
fn ContainsRune(b: []byte, r: rune): bool
Reports whether slice includes rune.
fn ContainsAny(s: []byte, runes: []byte): bool
Reports whether slice includes any of runes.
fn Count(s: []byte, sub: []byte): int
Counts the number of non-overlapping instances of sub-slice in s. Returns zero if sub-slice is empty.
fn Replace(mut s: []byte, sub: []byte, new: []byte, mut n: int): []byte
Replaces all sub-slices matching sub in the slice with new. Returns same slice if n is equals to zero. Replaces all matches if n less than zero. This function may return mutable copy of s, of new slice allocation.
fn ReplaceAll(mut s: []byte, sub: []byte, new: []byte): []byte
Same as the Replace function. But replaces all matched subs. Basically equals to Replace(s, sub, new, -1)
call.
fn Map(s: []byte, mapping: fn(mut rune): rune): []byte
Returns a immutable copy of the slice s with all its characters modified according to the mapping function. If mapping returns a negative value, the character is dropped from the slice with no replacement.
fn ToLower(s: []byte): []byte
Returns s with all Unicode letters mapped to their lower case. Returns immutable new slice allocation.
fn ToUpper(s: []byte): []byte
Returns s with all Unicode letters mapped to their upper case. Returns immutable new slice allocation.
fn TrimLeft(mut s: []byte, cutset: []byte): []byte
Trims slice by specified runes at left. Cutset should include runes to trim. Returns mutable copy of s.
fn TrimRight(mut s: []byte, cutset: []byte): []byte
Trims slice by specified runes at right. Cutset should include runes to trim. Returns mutable copy of s.
fn Trim(mut s: []byte, cutset: []byte): []byte
Trims slice by specified runes at left and right. Cutset should include runes to trim. Returns mutable copy of s.
fn Join(parts: [][]byte, sep: []byte): []byte
Concatenates the parts of its first argument to create a single slice. The separator sep is placed between parts in the resulting slice.
fn Runes(s: []byte): []rune
Returns runes from UTF-8 encoded bytes.
fn Cut(mut s: []byte, sep: []byte): (before: []byte, after: []byte, found: bool)
Cut 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, cut returns s, nil, false.
Cut returns slices of the original slice s, not copies.