Text buffers
Build a growable string
Text:new returns an owned wrapper with data, length and capacity. length counts bytes before the terminating zero. append, append_byte and reserve return 1 on success, 0 on failure. Check appends when incomplete output would be harmful.
import language-kit
let join_label = fn (name:u8*) -> u8* {
let text = LanguageKit:Text:new()
if !text { return cast(u8*, 0) }
defer text.destroy()
if !text.append("package: ") || !text.append(name) { return cast(u8*, 0) }
return text.take()
}
let check_label = fn () -> i64 {
let result = join_label("example")
if !result { return 1 }
defer free(result)
return strcmp(result, "package: example") != 0
}
assert check_label() == 0Borrow or transfer
text.datais borrowed. Mutation or destruction can invalidate it.text.take()transfers the allocated byte string and empties the wrapper.- Destroy the emptied wrapper separately; release the transferred string with
free. LanguageKit:copy_textcopies null-terminated text;copy_bytescopies a bounded range and adds a terminator.
Do not free data and then destroy its still-owning wrapper. Do not use a borrowed pointer across an append that may reallocate. Byte offsets are not Unicode character offsets; the text helper preserves bytes and does not interpret grapheme clusters.