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#

silk
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#

silk
pub struct StaticText

The 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#

silk
impl StaticText

Associated function byteLength#

silk
pub static fn byteLength(value: string) -> usize

Returns the number of UTF-8 bytes in value. This is not the number of characters.

Associated function byteAt#

silk
pub static fn byteAt(value: string, index: usize) -> u8

Returns 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#

silk
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#

silk
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.

Associated function errorAt#

silk
pub static fn errorAt(text: string, start: usize, end: usize, message: string) -> never

Stops static evaluation with the compile error message, reported at bytes start to end of text, with end not included.

Details#

When text is a string literal of the calling package, the error points inside the literal, at the characters those bytes are written as, escapes included. When text is the label Reflect.fieldLabel gives for a struct field, the error points at that part of the field's name where the struct declares it. Otherwise, as for a text that StaticText.concat built, the error points at the call, like compileError(message). Either way the error also names the call in the evaluated module that reached it.

Gotchas#

start <= end <= byteLength(text) must hold. Otherwise, static evaluation stops with an out-of-range error instead.

A static fn whose body has a static for evaluates the statements before the loop apart from its caller, where a text parameter no longer knows its literal, so the error points at the call. Report from a function without such a loop, and move the loop into a helper.

Examples#

Point at the bad character of a template#

silk
import silk.core.static_text { StaticText }

static fn checked(template: string<'static>) -> string<'static> {
  let length = StaticText.byteLength(template)
  let mut at: usize = 0
  while at < length {
    // 37 is the byte of `%`.
    if StaticText.byteAt(template, at) == 37 {
      StaticText.errorAt(template, at, at + 1, "use {} instead of %")
    }
    at = at + 1
  }
  return template
}

pub fn main() -> i32 {
  let text = checked("{} items")
  return 0
}
silk
pub static fn link<Owner>(text: string, start: usize, end: usize, field: string) -> ()

Tells the editor that bytes start to end of text, with end not included, name the field of Owner labeled field.

Details#

When text is a string literal of the calling package, those characters of the literal act as a written use of the field: hovering them shows the field, go to definition opens it, find references on the field lists them, and an inlay hint after them shows the field's type. A text with no literal behind it, such as one StaticText.concat built, links nothing. The link changes nothing in the build.

Gotchas#

start <= end <= byteLength(text) must hold and Owner must have a field labeled field. Otherwise, static evaluation stops with an error at those bytes instead.

Renaming the field does not edit the text. Pair the link with a check that reports a name the type lacks, so the build names each place that must follow.

As for StaticText.errorAt, a static fn whose body has a static for evaluates the statements before the loop apart from its caller, where a text parameter no longer knows its literal, so a link made there links nothing.

The editor shows links of a constant's initializer.

Examples#

silk
import silk.core.static_text { StaticText }

struct Greeting {
  pub name: string<'static>,
}

static fn linked(template: string<'static>) -> string<'static> {
  // `{name}` starts at byte 7 of "Hello, {name}"; the name is bytes 8 to 12.
  StaticText.link<Greeting>(template, 8, 12, "name")
  return template
}

const TEMPLATE: string<'static> = linked("Hello, {name}")

pub fn main() -> i32 {
  if StaticText.byteLength(TEMPLATE) != 13 {
    return 1
  }
  return 0
}

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, x86_64-pc-windows-msvc, wasm64-unknown-unknown.

View the source of this page