Bytes Methods
Bytes values in Risor have built-in methods for searching, comparing, and
transforming byte sequences. All bytes methods are non-mutating. They return
new bytes values rather than modifying the original. Bytes can be created using
the bytes() built-in function, and many methods accept both bytes and string
arguments interchangeably.
Method Reference
| Method | Description |
|---|---|
clone() | Create a copy of the bytes |
contains(b) | True if the bytes contains subsequence b |
contains_any(chars) | True if the bytes contains any character in chars |
contains_rune(r) | True if the bytes contains rune r |
count(b) | Count non-overlapping occurrences of b |
equals(other) | True if equal to other |
has_prefix(prefix) | True if the bytes starts with prefix |
has_suffix(suffix) | True if the bytes ends with suffix |
index(b) | Index of first occurrence of b, or -1 |
index_any(chars) | Index of first occurrence of any character in chars, or -1 |
index_byte(b) | Index of first occurrence of byte b, or -1 |
index_rune(r) | Index of first occurrence of rune r, or -1 |
repeat(count) | Repeat the bytes count times |
replace(old, new, n) | Replace first n occurrences of old with new |
replace_all(old, new) | Replace all occurrences of old with new |
Searching
contains
Check whether bytes contains a subsequence.
let b =bytes("hello world")
b.contains("world") // true
b.contains("xyz") // falsecontains_any
Check whether bytes contains any of the given characters.
let b =bytes("hello")
b.contains_any("aeiou") // true
b.contains_any("xyz") // falsecontains_rune
Check whether bytes contains a specific rune (single character).
let b =bytes("hello")
b.contains_rune("e") // true
b.contains_rune("z") // falsehas_prefix
Check whether bytes starts with a given prefix.
let b =bytes("hello")
b.has_prefix("he") // true
b.has_prefix("lo") // falsehas_suffix
Check whether bytes ends with a given suffix.
let b =bytes("hello")
b.has_suffix("lo") // true
b.has_suffix("he") // falseindex
Return the index of the first occurrence of a subsequence, or -1 if not found.
let b =bytes("hello")
b.index("ll") // 2
b.index("xyz") // -1index_any
Return the index of the first occurrence of any character from the given set, or -1 if none are found.
let b =bytes("hello")
b.index_any("aeiou") // 1
b.index_any("xyz") // -1index_byte
Return the index of the first occurrence of a specific byte, or -1 if not found. The argument must be a single byte.
let b =bytes("hello")
b.index_byte("l") // 2
b.index_byte("z") // -1index_rune
Return the index of the first occurrence of a specific rune (single character), or -1 if not found.
let b =bytes("hello")
b.index_rune("e") // 1
b.index_rune("z") // -1count
Count the number of non-overlapping occurrences of a subsequence.
let b =bytes("hello hello")
b.count("hello") // 2
b.count("ll") // 2equals
Check equality with another bytes or string value.
let b =bytes("hello")
b.equals(bytes("hello")) // true
b.equals(bytes("world")) // falseTransforming
clone
Create an independent copy of the bytes.
let b =bytes("hello")
let c = b.clone()
c.equals(b) // truerepeat
Repeat bytes a given number of times.
let b =bytes("ab")
b.repeat(3) // bytes("ababab")
b.repeat(0) // bytes("")replace
Replace the first n occurrences of a subsequence. Use -1 to replace all.
let b =bytes("aaa")
b.replace("a", "b", 2) // bytes("bba")
b.replace("a", "b", -1) // bytes("bbb")replace_all
Replace all occurrences of a subsequence with another value.
let b =bytes("hello world")
b.replace_all("o", "0") // bytes("hell0 w0rld")
b.replace_all("l", "r") // bytes("herro worrd")