diff options
| author | data-man <datamanrb@gmail.com> | 2020-05-25 11:05:08 +0500 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2020-05-25 12:48:59 -0400 |
| commit | 0ecdbdb3cb3d9558b5d2dbd928ead124d98c74ca (patch) | |
| tree | 103b859d0c732c79bb8d675cc37bbe4a93d86825 /lib/std | |
| parent | 0fd77c2de3b0355a6e66bf7919cd25612c73654f (diff) | |
| download | zig-0ecdbdb3cb3d9558b5d2dbd928ead124d98c74ca.tar.gz zig-0ecdbdb3cb3d9558b5d2dbd928ead124d98c74ca.zip | |
Support comptime floats in std.fmt
Diffstat (limited to 'lib/std')
| -rw-r--r-- | lib/std/fmt.zig | 16 |
1 files 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(); |
