Skip to content

Commit

Permalink
add printf to handle strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dnjulek committed Apr 25, 2024
1 parent 3f6e7f6 commit 91c4f52
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/vshelper.zig
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,10 @@ pub fn mapGetN(comptime T: type, in: ?*const vs.Map, key: [*]const u8, index: c_

return if (err == .Success) val else null;
}

/// Format the string with a null-terminator to work properly with C API, you need to free the buf manually.
pub fn printf(allocator: std.mem.Allocator, buf: *?[]u8, comptime fmt: []const u8, args: anytype) []const u8 {
const err_msg = "Out of memory occurred while writing string.";
buf.* = allocator.alloc(u8, std.fmt.count(fmt, args) + 1) catch null; // +1 for "\x00" in bufPrintZ
return if (buf.*) |b| std.fmt.bufPrintZ(b, fmt, args) catch err_msg else err_msg;
}

0 comments on commit 91c4f52

Please sign in to comment.