-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
simplify eat_digits #81427
simplify eat_digits #81427
Conversation
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @m-ou-se (or someone else) soon. If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes. Please see the contribution instructions for more information. |
I'd prefer to fix the LLVM optimizing issue to using unsafe code for this. |
Also works that: #![feature(core_intrinsics)]
use std::intrinsics::assume;
pub fn eat_digits(s: &[u8]) -> (&[u8], &[u8]) {
let sep = s.iter().take_while(|x| (b'0'..=b'9').contains(x)).count();
unsafe{ assume(sep <= s.len()); };
(&s[..sep], &s[sep..])
} |
Or that without bound check, but looks ugly: pub fn eat_digits(s: &[u8]) -> (&[u8], &[u8]) {
for i in 0..s.len(){
if !s[i].is_ascii_digit(){
return s.split_at(i);
}
}
s.split_at(s.len())
} |
How about pub fn eat_digits(s: &[u8]) -> (&[u8], &[u8]) {
let pos = s.iter().position(|c| !c.is_ascii_digit()).unwrap_or(s.len());
s.split_at(pos)
} |
@camsteffen's version looks nice. It compiles to the exact same assembly as the versions with |
@camsteffen Yes, this looks nice, can i PR this version? |
Yes you can use it 🙂 |
This removes bound checks, and as this function get inlined, few more. @rustbot label -S-waiting-on-author +S-waiting-on-review |
@bors r+ |
📌 Commit ec09d7f has been approved by |
☀️ Test successful - checks-actions |
A job failed! Check out the build log: (web) (plain) Click to see the possible cause of the failure (guessed by this bot)
|
Simplify eat_digits by checking values in iterator, plus decrease function size, by returning unchecked slices.
https://godbolt.org/z/cxjav4