-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
format: do not force user to provide an alignment field when it's not necessary #19049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
1fee440 to
837e7b8
Compare
e8e480e to
39f5dc5
Compare
b321b41 to
379b4d6
Compare
4842adb to
f6a251f
Compare
6caaba7 to
d21a252
Compare
d21a252 to
94dd8ce
Compare
e07ad0d to
de1784b
Compare
e64a232 to
96c24c6
Compare
96c24c6 to
348a90c
Compare
On one hand this pull-request introduce a light and surgical fix for a very common case in a non-breaking way: get If I understand correctly, #19488 suggests using python format which would supersede this PR as then This PR has been up for review for a few months now, it's ripe for being merged in because it's a non-breaking change implementing a step towards either of these two PR (which will take much longer to get ready). With this PR, users could start using zero padding (e.g. |
348a90c to
b5ef309
Compare
Head branch was pushed to by a user without write access
091c200 to
9cd776a
Compare
9cd776a to
e82f7d3
Compare
When no alignment is specified, the character that should be used is the
fill character that is otherwise provided, not space.
This is closer to the default that C programmers (and other languages)
use: "04x" fills with zeroes (in zig as of today x:04 fills with spaces)
Test:
const std = @import("std");
const expectFmt = std.testing.expectFmt;
test "fmt.defaultchar.no-alignment" {
// as of today the following test passes:
try expectFmt("0x00ff", "0x{x:0>4}", .{255});
// as of today the following test fails (returns "0x ff" instead)
try expectFmt("0x00ff", "0x{x:04}", .{255});
}
|
Not sure why github has closed the PR, I've reopened it. Github also had automatically requested a review because I've pushed to my branch changes that are in master, and github thought I modified the files ¯_(ツ)_/¯ In any case, I've started from a clean So the pipeline should now pass, the review is not necessary because only |
|
@Vexu would you mind triggering the pipeline for the latest? It should be passing now. Thanks. |
… necessary (ziglang#19049) * format: fix default character when no alignment When no alignment is specified, the character that should be used is the fill character that is otherwise provided, not space. This is closer to the default that C programmers (and other languages) use: "04x" fills with zeroes (in zig as of today x:04 fills with spaces) Test: const std = @import("std"); const expectFmt = std.testing.expectFmt; test "fmt.defaultchar.no-alignment" { // as of today the following test passes: try expectFmt("0x00ff", "0x{x:0>4}", .{255}); // as of today the following test fails (returns "0x ff" instead) try expectFmt("0x00ff", "0x{x:04}", .{255}); } * non breaking improvement of string formatting * improved comment * simplify the code a little * small improvement around how characters identified as valid are consumed
Hi guys,
In this changelist I'm suggesting to improve upon the existing Format Options parsing to make it a little more flexible, closer actually to the defaults that C programmers are used to and that most languages implement. To me, it follows the following principles from the Zen:
Padding with
0is very common for hexadecimal output: in many languages04xleft-pads with zeroes, but in Zig{x:04}left-pads with blanks. However for most fill characters like zero, adding an alignment specifier is actually not necessary (it's only necessary for1-9and.)The new behaviour is non-breaking (e.g current unit tests pass) and feels more natural and closer to how other languages handle zero-padding in their format options:
I've also added new unit tests for the new cases.