numfmt: fix zero-padding placement for negative numbers#11694
Merged
cakebaker merged 2 commits intouutils:mainfrom Apr 7, 2026
Merged
numfmt: fix zero-padding placement for negative numbers#11694cakebaker merged 2 commits intouutils:mainfrom
cakebaker merged 2 commits intouutils:mainfrom
Conversation
When using zero-padded format (e.g. %018.2f), the sign character is now placed before the padding zeros, matching C printf behavior. Fixes uutils#11664
|
GNU testsuite comparison: |
cakebaker
reviewed
Apr 7, 2026
Comment on lines
+584
to
+590
| let zero_padded = if let Some(unsigned) = number_with_suffix.strip_prefix('-') { | ||
| format!("-{}", pad_string(unsigned, p as usize - 1, '0', true)) | ||
| } else if let Some(unsigned) = number_with_suffix.strip_prefix('+') { | ||
| format!("+{}", pad_string(unsigned, p as usize - 1, '0', true)) | ||
| } else { | ||
| pad_string(&number_with_suffix, p as usize, '0', true) | ||
| }; |
Contributor
There was a problem hiding this comment.
While this works fine, you can simplify it by passing both prefixes (['-', '+']) to strip_prefix.
Contributor
Author
There was a problem hiding this comment.
Good catch, updated to use a combined strip_prefix(['-', '+']). Also removed the unnecessary borrow per clippy.
cakebaker
approved these changes
Apr 7, 2026
|
GNU testsuite comparison: |
Contributor
|
Thanks for your PR! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #11664
When using
--format '%018.2f'with negative numbers, zero-padding was placed before the sign:The fix strips the sign character before zero-padding, pads the unsigned portion (with width reduced by 1), then prepends the sign back. Handles both
-and+.Enabled the existing ignored test for this issue.