Skip to content

printf: pad unsigned numbers properly#5955

Merged
tertsdiepraam merged 3 commits intouutils:mainfrom
tertsdiepraam:printf-pad-octal-prefix
Feb 10, 2024
Merged

printf: pad unsigned numbers properly#5955
tertsdiepraam merged 3 commits intouutils:mainfrom
tertsdiepraam:printf-pad-octal-prefix

Conversation

@tertsdiepraam
Copy link
Copy Markdown
Collaborator

@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
Copy Markdown
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
Copy Markdown
Collaborator 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
Copy Markdown
Collaborator 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
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