authorgravatar for datamanrb@gmail.comdata-man <datamanrb@gmail.com> 2020-05-25 11:05:08+05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-25 12:48:59-04:00
log0ecdbdb3cb3d9558b5d2dbd928ead124d98c74ca
tree103b859d0c732c79bb8d675cc37bbe4a93d86825
parent0fd77c2de3b0355a6e66bf7919cd25612c73654f

Support comptime floats in std.fmt


1 files changed, 14 insertions(+), 2 deletions(-)

lib/std/fmt.zig+14-2
...@@ -330,7 +330,7 @@ pub fn formatType(...@@ -330,7 +330,7 @@ pub fn formatType(
330 }330 }
331331
332 switch (@typeInfo(T)) {332 switch (@typeInfo(T)) {
333 .ComptimeInt, .Int, .Float => {333 .ComptimeInt, .Int, .ComptimeFloat, .Float => {
334 return formatValue(value, fmt, options, out_stream);334 return formatValue(value, fmt, options, out_stream);
335 },335 },
336 .Void => {336 .Void => {
...@@ -493,7 +493,7 @@ fn formatValue(...@@ -493,7 +493,7 @@ fn formatValue(
493493
494 const T = @TypeOf(value);494 const T = @TypeOf(value);
495 switch (@typeInfo(T)) {495 switch (@typeInfo(T)) {
496 .Float => return formatFloatValue(value, fmt, options, out_stream),496 .Float, .ComptimeFloat => return formatFloatValue(value, fmt, options, out_stream),
497 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream),497 .Int, .ComptimeInt => return formatIntValue(value, fmt, options, out_stream),
498 .Bool => return formatBuf(if (value) "true" else "false", options, out_stream),498 .Bool => return formatBuf(if (value) "true" else "false", options, out_stream),
499 else => comptime unreachable,499 else => comptime unreachable,
...@@ -1594,6 +1594,18 @@ test "formatIntValue with comptime_int" {...@@ -1594,6 +1594,18 @@ test "formatIntValue with comptime_int" {
1594 std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789"));1594 std.testing.expect(mem.eql(u8, fbs.getWritten(), "123456789123456789"));
1595}1595}
15961596
1597test "formatFloatValue with comptime_float" {
1598 const value: comptime_float = 1.0;
1599
1600 var buf: [20]u8 = undefined;
1601 var fbs = std.io.fixedBufferStream(&buf);
1602 try formatFloatValue(value, "", FormatOptions{}, fbs.outStream());
1603 std.testing.expect(mem.eql(u8, fbs.getWritten(), "1.0e+00"));
1604
1605 try testFmt("1.0e+00", "{}", .{value});
1606 try testFmt("1.0e+00", "{}", .{1.0});
1607}
1608
1597test "formatType max_depth" {1609test "formatType max_depth" {
1598 const Vec2 = struct {1610 const Vec2 = struct {
1599 const SelfType = @This();1611 const SelfType = @This();