std.fmt.comptimePrint: Properly null-terminate result and add test

This commit is contained in:
Tadeo Kondrak 2020-10-07 11:43:23 -06:00
parent 49e68bdcf3
commit e9bca9de3c
No known key found for this signature in database
GPG Key ID: D41E092CA43F1D8B

View File

@ -1184,10 +1184,13 @@ fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, opti
pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {
comptime var buf: [count(fmt, args):0]u8 = undefined;
_ = bufPrint(&buf, fmt, args) catch unreachable;
buf[buf.len] = 0;
return &buf;
}
test "comptimePrint" {
@setEvalBranchQuota(2000);
std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptime comptimePrint("{}", .{100})));
std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));
}