silk.core.static_text.
Operations on string literals during static evaluation: length, byte access, concatenation, and slices.
When to use#
Use StaticText to check or build text at compile time, such as to parse a format template
and report an error in the compiler. Use silk.text.string.String for text at runtime.
Details#
The operations work on UTF-8 bytes. They run only during static evaluation. The results are static values, so they add no runtime cost.
Examples#
Measure a literal at compile time#
import silk.core.static_text { StaticText }
static fn greeting() -> string<'static> {
return StaticText.concat("hello, ", "world")
}
static fn greetingLength() -> usize {
return StaticText.byteLength(greeting())
}
pub fn main() -> i32 {
let length = greetingLength()
if length != 12 {
return 1
}
return 0
}Import as StaticText with import silk.core.static_text { StaticText }.
Public declarations: 1.
StaticText#
pub struct StaticTextThe owner type of the static text operations. It has no fields and is never constructed.
Details#
Each operation is static. It runs only during static evaluation, such as in a static fn, a
static for, or a const initializer.
Implementation StaticText#
impl StaticTextAssociated function byteLength#
pub static fn byteLength(value: string) -> usizeReturns the number of UTF-8 bytes in value. This is not the number of characters.
Associated function byteAt#
pub static fn byteAt(value: string, index: usize) -> u8Returns the UTF-8 byte at the zero-based byte position index of value.
Gotchas#
If index is not less than StaticText.byteLength, static evaluation stops and the
compiler reports an error.
Associated function concat#
pub static fn concat(left: string, right: string) -> string<'static>Returns left followed by right.
Details#
A compiler error about the result points to the source location of left.
Associated function slice#
pub static fn slice(value: string, start: usize, end: usize) -> string<'static>Returns the bytes of value from byte position start to byte position end, with end not
included.
Gotchas#
start and end must be at character boundaries, with start <= end <= byteLength(value).
Otherwise, static evaluation stops and the compiler reports an error.
Category: Core.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.