Skip to content

Commit

Permalink
Rollup merge of rust-lang#52744 - RalfJung:align_offset, r=Kimundi
Browse files Browse the repository at this point in the history
make memrchr use align_offset

I hope I did not screw that up...

Cc @oli-obk who authored the original rust-lang#44537
  • Loading branch information
Mark-Simulacrum authored Jul 27, 2018
2 parents a35af88 + 3bc59b5 commit 0805a6b
Showing 1 changed file with 7 additions and 10 deletions.
17 changes: 7 additions & 10 deletions src/libcore/slice/memchr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0805a6b

Please sign in to comment.