authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-10-07 11:43:23-06:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-10-07 11:43:23-06:00
loge9bca9de3cbe6dbf166b931c06af6134476c9f50
tree605415f8ae61f6d2b2244ce846715884000aba86
parent49e68bdcf34d9c9a61c0dc8e55f487d51b6b8188
signaturelock-open Commit is signed but in an unrecognized format.

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


1 files changed, 3 insertions(+), 0 deletions(-)

lib/std/fmt.zig+3
...@@ -1184,10 +1184,13 @@ fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, opti...@@ -1184,10 +1184,13 @@ fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, opti
1184pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args):0]u8 {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;1185 comptime var buf: [count(fmt, args):0]u8 = undefined;
1186 _ = bufPrint(&buf, fmt, args) catch unreachable;1186 _ = bufPrint(&buf, fmt, args) catch unreachable;
1187 buf[buf.len] = 0;
1187 return &buf;1188 return &buf;
1188}1189}
11891190
1190test "comptimePrint" {1191test "comptimePrint" {
1192 @setEvalBranchQuota(2000);
1193 std.testing.expectEqual(*const [3:0]u8, @TypeOf(comptime comptimePrint("{}", .{100})));
1191 std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));1194 std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));
1192}1195}
11931196