Search
Compares a borrowed prefix using ordinal UTF-8 bytes.
Module std.text
import std.text;Public declaration contracts and authored examples. A page can document several related symbols. Implementation bodies are excluded.
fn
pub const fn StartsWith(Text: str, Prefix: str) -> boolCompares a borrowed prefix using ordinal UTF-8 bytes. Text: Valid UTF-8 view borrowed only for this call. Prefix: Valid UTF-8 prefix; an empty prefix always matches. Return: True when Text begins with every byte of Prefix, without allocation.
Example
module example.borrowed_text_search;
from std.text import StartsWith, EndsWith, Find;
fn Main() -> i32 {
let Text: str = "λ/path/λ";
if !StartsWith(Text, "λ/") || !EndsWith(Text, "/λ") {
return 1;
}
switch Find(Text, "path") {
case .Some { Value = Offset } => {
if Offset != 3_usize { return 2; }
}
case .None => return 3;
}
if Find(Text, "missing") is .Some { Value = _ } { return 4; }
switch Find(Text, "") {
case .Some { Value = Offset } => {
if Offset != 0_usize { return 5; }
}
case .None => return 6;
}
if !StartsWith("", "") || !EndsWith(Text, "") { return 7; }
return 0;
}fn
pub const fn EndsWith(Text: str, Suffix: str) -> boolCompares a borrowed suffix using ordinal UTF-8 bytes. Text: Valid UTF-8 view borrowed only for this call. Suffix: Valid UTF-8 suffix; an empty suffix always matches. Return: True when Text ends with every byte of Suffix, without allocation.
fn
pub const fn Find(Text: str, Pattern: str) -> usize?Finds the first exact UTF-8 pattern and returns its borrowed-view byte offset. Text: Valid UTF-8 view borrowed only for this call. Pattern: Valid UTF-8 pattern; empty matches at offset zero. Return: First matching byte offset, or none when no match exists. @details No allocation, retained borrow or locale transformation is introduced. Worst-case work is O(Text.ByteLength * Pattern.ByteLength).