From 06a1184c92dd51630c542df6f34b09ec4dad341b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=28xq=29=20Quei=C3=9Fner?= Date: Sun, 9 Aug 2020 12:48:26 +0200 Subject: [PATCH 1/3] Fixes double alignment --- lib/std/fmt.zig | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index c9ba3b3470..4e35e23d4b 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -560,13 +560,19 @@ fn formatFloatValue( options: FormatOptions, writer: anytype, ) !void { + // this buffer should be enough to display all decimal places of a decimal f64 number. + var buf: [512]u8 = undefined; + var buf_stream = std.io.fixedBufferStream(&buf); + if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { - return formatFloatScientific(value, options, writer); + try formatFloatScientific(value, options, buf_stream.writer()); } else if (comptime std.mem.eql(u8, fmt, "d")) { - return formatFloatDecimal(value, options, writer); + try formatFloatDecimal(value, options, buf_stream.writer()); } else { @compileError("Unknown format string: '" ++ fmt ++ "'"); } + + return formatBuf(buf[0..buf_stream.pos], options, writer); } pub fn formatText( @@ -1791,3 +1797,17 @@ test "padding" { try testFmt("==================Filled", "{:=>24}", .{"Filled"}); try testFmt(" Centered ", "{:^24}", .{"Centered"}); } + +test "decimal float padding" { + var number: f32 = 3.1415; + try testFmt("left-pad: **3.141\n", "left-pad: {d:*>7.3}\n", .{number}); + try testFmt("center-pad: *3.141*\n", "center-pad: {d:*^7.3}\n", .{number}); + try testFmt("right-pad: 3.141**\n", "right-pad: {d:*<7.3}\n", .{number}); +} + +test "sci float padding" { + var number: f32 = 3.1415; + try testFmt("left-pad: **3.141e+00\n", "left-pad: {e:*>11.3}\n", .{number}); + try testFmt("center-pad: *3.141e+00*\n", "center-pad: {e:*^11.3}\n", .{number}); + try testFmt("right-pad: 3.141e+00**\n", "right-pad: {e:*<11.3}\n", .{number}); +} From ada06e2996c70f5d25c60f06d2d171e105a020ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20=28xq=29=20Quei=C3=9Fner?= Date: Sun, 9 Aug 2020 14:09:02 +0200 Subject: [PATCH 2/3] Makes formatFloatValue not return error.NoSpaceLeft anymore. --- lib/std/fmt.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 4e35e23d4b..6141b18b5b 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -565,9 +565,15 @@ fn formatFloatValue( var buf_stream = std.io.fixedBufferStream(&buf); if (fmt.len == 0 or comptime std.mem.eql(u8, fmt, "e")) { - try formatFloatScientific(value, options, buf_stream.writer()); + formatFloatScientific(value, options, buf_stream.writer()) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + else => |e| return e, + }; } else if (comptime std.mem.eql(u8, fmt, "d")) { - try formatFloatDecimal(value, options, buf_stream.writer()); + formatFloatDecimal(value, options, buf_stream.writer()) catch |err| switch (err) { + error.NoSpaceLeft => unreachable, + else => |e| return e, + }; } else { @compileError("Unknown format string: '" ++ fmt ++ "'"); } From 6701046cdd85d7a702be38a593e8385a5dfb4562 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20Quei=C3=9Fner?= Date: Sun, 9 Aug 2020 17:40:58 +0200 Subject: [PATCH 3/3] Uses getWritten instead of .pos + slicing Co-authored-by: Joachim Schmidt --- lib/std/fmt.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/std/fmt.zig b/lib/std/fmt.zig index 6141b18b5b..6dbef5db67 100644 --- a/lib/std/fmt.zig +++ b/lib/std/fmt.zig @@ -578,7 +578,7 @@ fn formatFloatValue( @compileError("Unknown format string: '" ++ fmt ++ "'"); } - return formatBuf(buf[0..buf_stream.pos], options, writer); + return formatBuf(buf_stream.getWritten(), options, writer); } pub fn formatText(