I am using the code below as a WebAssembly container. I want to use as a “captalizing” machine.
I am unsure of my code so I am here to learn.
I call the function allocated_memory_ptr
twice, once for the WebAssembly user to get a pointer to write the input string to, and once to send to the WebAssembly user the output pointer that contains the result.
I was concerned about memory allocation but I realise that the WebAssembly caller should manage it, not Zig since it’s only using a view, the slice, nothing else.
Thks.
export fn allocated_memory_ptr(len: usize) ?[*]u8 {
return if (allocator.alloc(u8, len)) |slice|
slice.ptr
else |_|
null;
}
export fn to_uppercase(ptr: [*]const u8, input_len: usize, output_ptr: [*]u8) void {
// Create a slice from the input pointer and length
const input = ptr[0..input_len];
// Convert to uppercase directly in memory
for (input, 0..) |char, i| {
output_ptr[i] = std.ascii.toUpper(char);
}
}