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

std.fmt.comptimePrint: Return null terminated string
This commit is contained in:
Andrew Kelley 2020-10-07 16:55:03 -04:00 committed by GitHub
commit 3c43eeceab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1181,13 +1181,16 @@ fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, opti
return buf[0..formatIntBuf(buf, value, base, uppercase, options)];
}
pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args)]u8 {
comptime var buf: [count(fmt, args)]u8 = undefined;
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}));
}