aboutsummaryrefslogtreecommitdiff
path: root/lib/std
diff options
context:
space:
mode:
authorTadeo Kondrak <me@tadeo.ca>2020-10-07 11:43:23 -0600
committerTadeo Kondrak <me@tadeo.ca>2020-10-07 11:43:23 -0600
commite9bca9de3cbe6dbf166b931c06af6134476c9f50 (patch)
tree605415f8ae61f6d2b2244ce846715884000aba86 /lib/std
parent49e68bdcf34d9c9a61c0dc8e55f487d51b6b8188 (diff)
downloadzig-e9bca9de3cbe6dbf166b931c06af6134476c9f50.tar.gz
zig-e9bca9de3cbe6dbf166b931c06af6134476c9f50.zip
std.fmt.comptimePrint: Properly null-terminate result and add test
Diffstat (limited to 'lib/std')
-rw-r--r--lib/std/fmt.zig3
1 files changed, 3 insertions, 0 deletions
diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig
index c8aed33585..ce90f67585 100644
--- a/lib/std/fmt.zig
+++ b/lib/std/fmt.zig
@@ -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}));
}