Skip to content
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

printf: pad unsigned numbers properly #5955

Merged
merged 3 commits into from
Feb 10, 2024

Conversation

tertsdiepraam
Copy link
Member

@tertsdiepraam tertsdiepraam commented Feb 7, 2024

See #5794 (comment)

printf 'before %#15.6o after' 42

Expected (and this PR):

before          000052 after

Current main branch:

before          052    after

Note: this PR has expanded in scope a bit. It turned out that the padding logic was faulty for unsigned numbers, so I fixed that, which fixes the problem with 0 as well.

@tertsdiepraam tertsdiepraam force-pushed the printf-pad-octal-prefix branch 2 times, most recently from 4a48d27 to 8bb81d9 Compare February 7, 2024 11:51
@cakebaker
Copy link
Contributor

While playing around I noticed two additional cases where GNU printf pads with zeros whereas uutils printf doesn't:

$ /usr/bin/printf ">%#15.6o<\n" "0"
>         000000<
$ cargo run printf ">%#15.6o<\n" "0"
>         0     <

$ /usr/bin/printf ">%#15.6o<\n" "08"
>/usr/bin/printf: ‘08’: value not completely converted
         000000<
$ cargo run printf ">%#15.6o<\n" "08"
printf: '08': value not completely converted
>         0     <

@tertsdiepraam
Copy link
Member Author

tertsdiepraam commented Feb 7, 2024

I think that's just one case right? The second case just parses the zero and then exits. The difference in spacing comes from how the error message is printed. Good catch, though. I'll try to get that edge case in.

@tertsdiepraam tertsdiepraam force-pushed the printf-pad-octal-prefix branch from 8bb81d9 to 8904e50 Compare February 7, 2024 14:40
@tertsdiepraam
Copy link
Member Author

Ah hold on actually. 0 values for other formats get padded too.

@tertsdiepraam tertsdiepraam changed the title printf: pad octal numbers with zeros on the left printf: pad unsigned numbers properly Feb 7, 2024
@tertsdiepraam tertsdiepraam force-pushed the printf-pad-octal-prefix branch from a13e4a8 to 8e7ee3f Compare February 7, 2024 15:17
@cakebaker cakebaker force-pushed the printf-pad-octal-prefix branch from dd6aedf to 337bfee Compare February 9, 2024 12:28
@tertsdiepraam tertsdiepraam merged commit da139c9 into uutils:main Feb 10, 2024
57 of 62 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants