Skip to content

Commit 0805a6b

Browse files
Rollup merge of rust-lang#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 rust-lang#44537
2 parents a35af88 + 3bc59b5 commit 0805a6b

File tree

1 file changed

+7
-10
lines changed

1 file changed

+7
-10
lines changed

src/libcore/slice/memchr.rs

+7-10
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,13 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option<usize> {
102102
let ptr = text.as_ptr();
103103
let usize_bytes = mem::size_of::<usize>();
104104

105-
// search to an aligned boundary
106-
let end_align = (ptr as usize + len) & (usize_bytes - 1);
107-
let mut offset;
108-
if end_align > 0 {
109-
offset = if end_align >= len { 0 } else { len - end_align };
110-
if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
111-
return Some(offset + index);
112-
}
113-
} else {
114-
offset = len;
105+
let mut offset = {
106+
// We call this just to obtain the length of the suffix
107+
let (_, _, suffix) = unsafe { text.align_to::<usize>() };
108+
len - suffix.len()
109+
};
110+
if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
111+
return Some(offset + index);
115112
}
116113

117114
// search the body of the text

0 commit comments

Comments
 (0)