-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
printf: trim leading whitespace when parsing numeric values #7512
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
|
Sorry, my PR text was unclear. This PR does change
Yup! Good suggestion.
👍 awesome. The current patch ends up with the same output, but there is still the question of should |
Oh! I misunderstood yes ,-) Do you know what code is rejecting non-whitespace leading characters in I feel I'd rather have the logic in the common
Great ,-) |
Highly surprising, but Did some digging and ran some examples with GNU coreutils:
I've learned something new 😆 I did not expect this to work. Escape sequences are another story:
So I'll update the tests with some additions that cover more real whitespace types and add some tests to |
GNU testsuite comparison:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh nice find!
For some reason I thought the expansion of special characters like \t
happened on the shell side ,-) Now I know ,-)
// Initial minus sign | ||
let (negative, unsigned) = if let Some(input) = input.strip_prefix('-') { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not a big deal, but the lack of symmetry is not so nice, maybe.
I wonder if you want to do this intead:
let (negative, unsigned) = if let Some(trimmed_input) = trimmed_input.strip_prefix('-') {
(true, trimmed_input)
} else {
(false, trimmed_input)
}
Or just shadow input above:
let input = input.trim_ascii_start();
new_ucmd!() | ||
.args(&["%f", "\u{2029}0.1"]) | ||
.fails() | ||
.stderr_contains("expected a numeric value"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coming from that interesting discovery, do you want to test that printf doesn't actually expand \t
macro?
i.e. .args(&["%f", "\\t0.1"])
fails. You'll need to add a comment to explain too, I'm sure others will get confused about this one too.
GNU testsuite comparison:
|
Don't forget to run Otherwise, LGTM! (turns out I'll need this one to pass |
Trim leading whitespace from numeric input to printf.
GNU testsuite comparison:
|
Trim leading whitespace from numeric input to printf.
This is what seq does here. Note that the rust docs for
trim_start
state the following:So technically the following assert would pass
It looks like this does not currently impact
printf
orseq
, but is probably something to be aware of.Fixes: #7505