Import with #import "std/vec".

Vec :: struct($T: Type) { data: *T; count: s64; capacity: s64; }
Text :: struct { bytes: Vec(u8); }

Owned UTF-8 text. It moves and drops like the Vec it holds, so its bytes are freed when its owner goes away. TextView(text) borrows it as a string while the Text stays in place, and a Text passed where a string is expected lends the same view. The view cannot outlive the Text, and the Text cannot move or change while a view is live.

TextCopy :: (value: string) -> Text

Copy borrowed text into an owned Text; empty when it cannot be allocated.

TextFromBuilder :: (bytes: Vec(u8)) -> Text

Take a byte builder's bytes as an owned Text without copying them. BuilderFinish(builder) does the same where a Text is expected.

TextDetach :: (text: Text) -> string

Give up ownership: the returned string keeps the bytes for the rest of the program. A Text call result used where it is not kept as a Text, such as `name := JsonWrite(value)`, detaches this way.

Source ↗ · All modules