std/cstr
A safe, non-owning view of a NUL-terminated C string (*u8). CStr.wrap answers None for a null pointer, byte is bounds-checked, and the view never frees the memory: the bytes belong to whoever produced the pointer.
milo
from "std/cstr" import { CStr }Example
milo
let s = "hello"
// CStr.wrap is @unsafe: the pointer must be NUL-terminated and outlive the CStr.
unsafe {
match CStr.wrap(s.cstr()) {
Some(c) => print(c.len()), // 5
None => print("null"),
}
}API reference
CStr
milo
pub struct CStr@copy: a non-owning view. The bytes belong to whoever produced the pointer (C, or a Milo string's buffer), so two CStr values over them are two readers, not two owners.
CStr.byte
milo
fn CStr.byte(self: &CStr, i: i64): u8Bounds-checked byte access. Panics if i >= len.
CStr.eq
milo
fn CStr.eq(self: &CStr, other: CStr): boolCStr.len
milo
fn CStr.len(self: &CStr): i64CStr.ptr
milo
fn CStr.ptr(self: &CStr): *u8CStr.toString
milo
fn CStr.toString(self: &CStr): stringCStr.wrap
milo
fn CStr.wrap(ptr: *u8): Option<CStr>