authorgravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-09-25 10:51:57-06:00
committergravatar for me@tadeo.caTadeo Kondrak <me@tadeo.ca> 2020-09-25 12:53:35-06:00
log236af776fd21c76d7ca272f65355767413821011
tree22e3625b34729013ede81ead051f86adbf205806
parentf8b3543cabc28df15e85085a72adb474b345cf93
signature Commit is signed but in an unrecognized format.

std.fmt: add comptimePrint


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

lib/std/fmt.zig+10
...@@ -1181,6 +1181,16 @@ fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, opti...@@ -1181,6 +1181,16 @@ fn bufPrintIntToSlice(buf: []u8, value: anytype, base: u8, uppercase: bool, opti
1181 return buf[0..formatIntBuf(buf, value, base, uppercase, options)];1181 return buf[0..formatIntBuf(buf, value, base, uppercase, options)];
1182}1182}
11831183
1184pub fn comptimePrint(comptime fmt: []const u8, args: anytype) *const [count(fmt, args)]u8 {
1185 comptime var buf: [count(fmt, args)]u8 = undefined;
1186 _ = bufPrint(&buf, fmt, args) catch |err| @compileError(err);
1187 return &buf;
1188}
1189
1190test "comptimePrint" {
1191 std.testing.expectEqualSlices(u8, "100", comptime comptimePrint("{}", .{100}));
1192}
1193
1184test "parse u64 digit too big" {1194test "parse u64 digit too big" {
1185 _ = parseUnsigned(u64, "123a", 10) catch |err| {1195 _ = parseUnsigned(u64, "123a", 10) catch |err| {
1186 if (err == error.InvalidCharacter) return;1196 if (err == error.InvalidCharacter) return;