From b259696cfba3c78b0641d30f159250465aa75af8 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 15 Oct 2020 12:19:50 +0200 Subject: [PATCH 1/2] std/fmt: rename allocPrint0() to allocPrintZ() This is consistent with other standard library functions working with null terminated pointers/slices. --- lib/std/fmt.zig | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 92e5f9c392..2a5f17c159 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1151,7 +1151,10 @@ pub fn allocPrint(allocator: *mem.Allocator, comptime fmt: []const u8, args: any }; } -pub fn allocPrint0(allocator: *mem.Allocator, comptime fmt: []const u8, args: anytype) AllocPrintError![:0]u8 { +/// Deprecated, use allocPrintZ +pub const allocPrint0 = allocPrintZ; + +pub fn allocPrintZ(allocator: *mem.Allocator, comptime fmt: []const u8, args: anytype) AllocPrintError![:0]u8 { const result = try allocPrint(allocator, fmt ++ "\x00", args); return result[0 .. result.len - 1 :0]; } From d52035f4012f4f3225e2dcf9374b03ccd6600071 Mon Sep 17 00:00:00 2001 From: Isaac Freund Date: Thu, 15 Oct 2020 12:11:03 +0200 Subject: [PATCH 2/2] std/fmt: add bufPrintZ() --- lib/std/fmt.zig | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 2a5f17c159..a3a97020bf 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1131,6 +1131,11 @@ pub fn bufPrint(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintErro return fbs.getWritten(); } +pub fn bufPrintZ(buf: []u8, comptime fmt: []const u8, args: anytype) BufPrintError![:0]u8 { + const result = try bufPrint(buf, fmt ++ "\x00", args); + return result[0 .. result.len - 1 :0]; +} + // Count the characters needed for format. Useful for preallocating memory pub fn count(comptime fmt: []const u8, args: anytype) u64 { var counting_writer = std.io.countingWriter(std.io.null_writer);