Skip to content

Commit 1fee440

Browse files
allow any character, not only zero
Any digit except those used by width (1-9 and dot) can be used for padding without an explicit alignment specifier.
1 parent add5d7c commit 1fee440

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

lib/std/fmt.zig

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,15 @@ pub const Placeholder = struct {
271271
// When none of the fill character and the alignment specifier have
272272
// been provided, check whether the width starts with a zero.
273273
if (fill == null and alignment == null) {
274-
fill = comptime if (parser.peek(0) == '0') '0' else null;
274+
fill = comptime if (parser.peek(0)) |ch| init: {
275+
switch (ch) {
276+
'.', '1', '2', '3', '4', '5', '6', '7', '8', '9' => break :init null,
277+
else => {
278+
_ = parser.char(); // consume the character
279+
break :init ch;
280+
},
281+
}
282+
} else null;
275283
}
276284

277285
// Parse the width parameter
@@ -2865,15 +2873,30 @@ test "sci float padding" {
28652873
try expectFmt("right-pad: 3.141e+00**\n", "right-pad: {e:*<11.3}\n", .{number});
28662874
}
28672875

2868-
test "padding.zero" {
2876+
test "zero padding" {
28692877
try expectFmt("zero-pad: '0042'", "zero-pad: '{:04}'", .{42});
2870-
try expectFmt("std-pad: ' 42'", "std-pad: '{:10}'", .{42});
28712878
try expectFmt("std-pad-1: '001'", "std-pad-1: '{:0>3}'", .{1});
28722879
try expectFmt("std-pad-2: '911'", "std-pad-2: '{:1<03}'", .{9});
28732880
try expectFmt("std-pad-3: ' 1'", "std-pad-3: '{:>03}'", .{1});
28742881
try expectFmt("center-pad: '515'", "center-pad: '{:5^03}'", .{1});
28752882
}
28762883

2884+
test "padding width, no alignment" {
2885+
try expectFmt("std-pad: ' 42'", "std-pad: '{:10}'", .{42});
2886+
}
2887+
2888+
test "padding with misc fill chars" {
2889+
try expectFmt("space-pad: ' 1'", "space-pad: '{: 3}'", .{1});
2890+
try expectFmt("score-pad: '__42'", "score-pad: '{:_04}'", .{42});
2891+
try expectFmt("lt-pad-1: '42<<'", "lt-pad-1: '{:<<04}'", .{42});
2892+
try expectFmt("lt-pad-2: '<<42'", "lt-pad-2: '{:<>04}'", .{42});
2893+
try expectFmt("gt-pad: '>42>'", "gt-pad: '{:>^04}'", .{42});
2894+
try expectFmt("float-pad-1: '42.123'", "float-pad-1: '{d:.3}'", .{42.1234});
2895+
try expectFmt("float-pad-2: '42.123'", "float-pad-2: '{d:.>.3}'", .{42.1234});
2896+
try expectFmt("float-pad-3: '42.123'", "float-pad-3: '{d:.>0.3}'", .{42.1234});
2897+
try expectFmt("float-pad-4: '...42.1'", "float-pad-4: '{d:.>7.1}'", .{42.1234});
2898+
}
2899+
28772900
test "null" {
28782901
const inst = null;
28792902
try expectFmt("null", "{}", .{inst});

0 commit comments

Comments
 (0)