// Borrow UTF-8 bytes as a Ziran string at native foreign-function boundaries. // The caller must keep the byte storage alive while the string is in use. TextDescriptor :: struct { data: *u8 length: usize } ByteDescriptor :: struct { data: *u8 length: s64 } #program_export TextFromBytes :: (buffer: []u8) -> string { return TextView(buffer) } // Borrow native storage at a foreign-function boundary. The caller must keep // it allocated and writable throughout the slice's use. #program_export BytesFromPointer :: (data: *u8, count: u64) -> []u8 { result: []u8 if data == null || count > cast(u64)9223372036854775807 { return result } descriptor: *ByteDescriptor = cast(*ByteDescriptor)(*result) descriptor.data = data descriptor.length = cast(s64)count return result } // The returned string borrows the C buffer and retains its lifetime. #program_export TextFromCString :: (value: *u8) -> string { if value == null { return "" } length: u64 = 0 while value[length] != cast(u8)0 { if length >= cast(u64)9223372036854775807 { return "" } length += 1 } result: string = "" descriptor: *TextDescriptor = cast(*TextDescriptor)(*result) descriptor.data = value descriptor.length = cast(usize)length return result }