From 0ecdbdb3cb3d9558b5d2dbd928ead124d98c74ca Mon Sep 17 00:00:00 2001 From: data-man Date: Mon, 25 May 2020 11:05:08 +0500 Subject: [PATCH] Support comptime floats in std.fmt --- lib/std/fmt.zig | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index f609ca69ac..db4232ec0e 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -330,7 +330,7 @@ pub fn formatType( } switch (@typeInfo(T)) { - .ComptimeInt, .Int, .Float => { + .ComptimeInt, .Int, .ComptimeFloat, .Float => { return formatValue(value, fmt, options, out_stream); }, .Void => { @@ -493,7 +493,7 @@ fn formatValue( const T = @TypeOf(value); switch (@typeInfo(T)) { - .Float => return formatFloatValue(value, fmt, options, out_stream), + .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, out_stream), .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream), .Bool => return formatBuf(if (value) "true" else "false", options, out_stream), else => comptime unreachable, @@ -1594,6 +1594,18 @@ test "formatIntValue with comptime_int" { std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789")); } +test "formatFloatValue with comptime_float" { + const value: comptime_float = 1.0; + + var buf: [20]u8 = undefined; + var fbs = std.io.fixedBufferStream(&buf); + try formatFloatValue(value, "", FormatOptions{}, fbs.outStream()); + std.testing.expect(mem.eql(u8, fbs.getWritten(), "1.0e+00")); + + try testFmt("1.0e+00", "{}", .{value}); + try testFmt("1.0e+00", "{}", .{1.0}); +} + test "formatType max_depth" { const Vec2 = struct { const SelfType = @This();