authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-10-07 16:55:03-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-10-07 16:55:03-04:00
log3c43eeceab70f78939401b68811f152a7f29b191
treee9f4c984cfb62ef4a7dfee96677ca562cdb612af
parent07c33dfc951af5e7bf1c29c94142d49e3acf08b0
parente9bca9de3cbe6dbf166b931c06af6134476c9f50
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6595 from tadeokondrak/comptime-print-0

std.fmt.comptimePrint: Return null terminated string

1 files changed, 5 insertions(+), 2 deletions(-)

lib/std/fmt.zig+5-2
......@@ -1181,13 +1181,16 @@ fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, opti
11811181 return buf[0..formatIntBuf(buf, value, base, uppercase, options)];
11821182}
11831183
1184pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args)]u8 {
1185 comptime var buf: [count(fmt, args)]u8 = undefined;
1184pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
1185 comptime var buf: [count(fmt, args):0]u8 = undefined;
11861186 _ = bufPrint(&buf, fmt, args) catch unreachable;
1187 buf[buf.len] = 0;
11871188 return &buf;
11881189}
11891190
11901191test "comptimePrint" {
1192 @setEvalBranchQuota(2000);
1193 std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptime comptimePrint("{}", .{100})));
11911194 std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));
11921195}
11931196