From 236af776fd21c76d7ca272f65355767413821011 Mon Sep 17 00:00:00 2001 From: Tadeo Kondrak Date: Fri, 25 Sep 2020 10:51:57 -0600 Subject: [PATCH] std.fmt: add comptimePrint --- lib/std/fmt.zig | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 56a1aba217..0bc5093fb9 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -1181,6 +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; + _ = bufPrint(&buf, fmt, args) catch |err| @compileError(err); + return &buf; +} + +test "comptimePrint" { + std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100})); +} + test "parse u64 digit too big" { _ = parseUnsigned(u64, "123a", 10) catch |err| { if (err == error.InvalidCharacter) return;