bytes #
bytes
bytes provides manipulation helpers for raw byte arrays ([]u8). All searches, offsets, separators, and cutsets operate on bytes. NUL bytes and invalid UTF-8 are preserved. Functions that return arrays make independent copies of their results.
to_lower and to_upper convert ASCII letters only. trim_space removes the ASCII space, tab, newline, carriage return, form feed, and vertical tab bytes. These functions do not perform Unicode case conversion or Unicode whitespace trimming.
import bytes
assert bytes.contains([u8(1), 2, 3], [u8(2), 3])
assert bytes.join([[u8(1)], [u8(2)]], [u8(0)]) == [u8(1), 0, 2]
assert bytes.split_n([u8(1), 2, 3], []u8{}, 2) == [[u8(1)], [u8(2), 3]]
assert bytes.replace([u8(1), 2], []u8{}, [u8(0)], -1) == [u8(0), 1, 0, 2, 0]
Empty separators and counts
count(s, [])counts every byte boundary, including both ends:s.len + 1.replace(s, [], replacement, n)inserts at those same boundaries. A negativenreplaces every match, zero replaces none, and a positivenlimits the replacements.split(s, [])returns individual bytes. Splitting empty input with an empty separator returns no parts. With a nonempty separator, it returns one empty part.split_nreturns no parts forn == 0and all parts forn < 0. For positiven, it returns at mostnparts and keeps the unsplit remainder in the last part.repeatreturns an empty array for a nonpositive count or empty input.
Empty-separator behavior is byte-oriented even for UTF-8 text. For example, the two bytes encoding é count as two bytes, not one character.
count panics if its result exceeds the maximum int. join, repeat, and replace panic if their result length exceeds that limit, before allocating the result.
fn clone #
fn clone(b []u8) []u8
clone returns a copy of b.
fn compare #
fn compare(a []u8, b []u8) int
compare returns -1 if a < b, 0 if equal, 1 if a > b (lexicographic).
fn contains #
fn contains(b []u8, subslice []u8) bool
contains reports whether subslice is within b.
fn count #
fn count(s []u8, sep []u8) int
count counts non-overlapping instances of sep in s. An empty sep matches at every byte boundary, including both ends. It panics if the count does not fit in an int.
fn equal #
fn equal(a []u8, b []u8) bool
equal reports whether a and b are the same length and contain the same bytes.
fn has_prefix #
fn has_prefix(s []u8, prefix []u8) bool
has_prefix reports whether s begins with prefix.
fn has_suffix #
fn has_suffix(s []u8, suffix []u8) bool
has_suffix reports whether s ends with suffix.
fn index #
fn index(s []u8, sep []u8) int
index returns the index of the first occurrence of sep in s, or -1.
fn index_byte #
fn index_byte(s []u8, c u8) int
index_byte returns the index of the first occurrence of c in s, or -1.
fn join #
fn join(elems [][]u8, sep []u8) []u8
join concatenates elems with sep between them into a new array. It panics if the result length does not fit in an int.
fn repeat #
fn repeat(b []u8, count int) []u8
repeat returns a new array with b repeated count times. A nonpositive count returns an empty array. It panics if the result length does not fit in an int.
fn replace #
fn replace(s []u8, old []u8, new []u8, n int) []u8
replace returns a copy of s with the first n (or all if n < 0) non-overlapping instances of old replaced by new. An empty old matches at every byte boundary, including both ends. It panics if the result length does not fit in an int.
fn split #
fn split(s []u8, sep []u8) [][]u8
split returns copies of the parts of s separated by sep. An empty sep splits into individual bytes; an empty s then yields no parts.
fn split_n #
fn split_n(s []u8, sep []u8, n int) [][]u8
split_n returns at most n copied parts, with the unsplit remainder in the last part. A zero n returns no parts; a negative n returns all parts. An empty sep splits into individual bytes; an empty s then yields no parts.
fn to_lower #
fn to_lower(s []u8) []u8
to_lower returns a copy with ASCII letters lowercased.
fn to_upper #
fn to_upper(s []u8) []u8
to_upper returns a copy with ASCII letters uppercased.
fn trim #
fn trim(s []u8, cutset []u8) []u8
trim removes leading/trailing bytes that are in cutset.
fn trim_prefix #
fn trim_prefix(s []u8, prefix []u8) []u8
trim_prefix removes prefix from s if present.
fn trim_space #
fn trim_space(s []u8) []u8
trim_space trims leading/trailing ASCII whitespace.
fn trim_suffix #
fn trim_suffix(s []u8, suffix []u8) []u8
trim_suffix removes suffix from s if present.