mirror of
https://github.com/rust-lang/rust.git
synced 2025-02-19 18:34:08 +00:00
Auto merge of #52744 - RalfJung:align_offset, r=Kimundi
make memrchr use align_offset I hope I did not screw that up... Cc @oli-obk who authored the original https://github.com/rust-lang/rust/pull/44537 Fixes #50567 (thanks @bjorn3)
This commit is contained in:
commit
d754582005
@ -102,16 +102,13 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
|
||||
let ptr = text.as_ptr();
|
||||
let usize_bytes = mem::size_of::<usize>();
|
||||
|
||||
// search to an aligned boundary
|
||||
let end_align = (ptr as usize + len) & (usize_bytes - 1);
|
||||
let mut offset;
|
||||
if end_align > 0 {
|
||||
offset = if end_align >= len { 0 } else { len - end_align };
|
||||
if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
|
||||
return Some(offset + index);
|
||||
}
|
||||
} else {
|
||||
offset = len;
|
||||
let mut offset = {
|
||||
// We call this just to obtain the length of the suffix
|
||||
let (_, _, suffix) = unsafe { text.align_to::<usize>() };
|
||||
len - suffix.len()
|
||||
};
|
||||
if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
|
||||
return Some(offset + index);
|
||||
}
|
||||
|
||||
// search the body of the text
|
||||
|
Loading…
Reference in New Issue
Block a user