Files
ziran/std/vec.zi
T
waozi 92d81d2661 Keep Text call results where an owner takes them, as strings elsewhere
A Text call result stays owned in a declared, assigned or returned Text,
a Text parameter, element or field, and a discarded statement, which
frees it. Elsewhere the checker rewrites it into vec.TextDetach(result),
a string that keeps its bytes as BuilderFinish strings do, so callers
written for string results keep working. BuilderFinish in an owning
position becomes vec.TextFromBuilder. Dotted module_name.Procedure names
resolve, as types already did, through modules reached only by other
imports, in the checker and in generated C.
2026-11-06 12:51:58 -03:00

48 lines
2.1 KiB
Ziran

// VecResizeBounded(values, count:s64, maximum_capacity:s64) -> bool has
// VecResize's initialized-length semantics and caps the backing element
// capacity. Negative sizes, count above the ceiling or existing capacity
// above the ceiling fail unchanged; shrinking never silently frees capacity.
// Bounds logical backing capacity, not allocator/runtime overhead or RSS.
// VecReserve(values, capacity: s64) -> bool reserves at least that total
// capacity without changing count. VecResize(values, count: s64) -> bool
// adjusts initialized length, giving new elements their language defaults
// and dropping removed owned elements. Both retain capacity on shrink/no-op,
// reject negative/overflow sizes, and leave storage unchanged on failure.
// Pass mutable storage, not a pointer; live borrowed views forbid mutation.
// Owned growable storage. Operations on Vec values are compiler primitives;
// the data pointer is private to those operations.
Vec :: struct($T: Type) {
data: *T;
count: s64;
capacity: s64;
}
// 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.
Text :: struct {
bytes: Vec(u8);
}
// Copy borrowed text into an owned Text; empty when it cannot be allocated.
TextCopy :: (value: string) -> Text {
text: Text
if !BuilderAppend(text.bytes, value) { return .{} }
return text
}
// Take a byte builder's bytes as an owned Text without copying them.
// BuilderFinish(builder) does the same where a Text is expected.
TextFromBuilder :: (bytes: Vec(u8)) -> Text {
return .{bytes = bytes}
}
// 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.
TextDetach :: (text: Text) -> string {
return BuilderFinish(text.bytes)
}