mirror of
https://github.com/ziglang/zig.git
synced 2026-01-21 23:05:24 +00:00
Support comptime floats in std.fmt
This commit is contained in:
parent
0fd77c2de3
commit
0ecdbdb3cb
@ -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();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user