Skip to content

Commit 3bc59b5

Browse files
committed
use slice::align_to
1 parent 70cb75c commit 3bc59b5

File tree

1 file changed

+7
-26
lines changed

1 file changed

+7
-26
lines changed

src/libcore/slice/memchr.rs

+7-26
Original file line numberDiff line numberDiff line change
@@ -102,32 +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-
// a version of align_offset that says how much must be *subtracted*
106-
// from a pointer to be aligned.
107-
#[inline(always)]
108-
fn align_offset_down(ptr: *const u8, align: usize) -> usize {
109-
let align_offset = ptr.align_offset(align);
110-
if align_offset > align {
111-
// Not possible to align
112-
usize::max_value()
113-
} else if align_offset == 0 {
114-
0
115-
} else {
116-
// E.g. if align=8 and we have to add 1, then we can also subtract 7.
117-
align - align_offset
118-
}
119-
}
120-
121-
// search to an aligned boundary
122-
let end_align = align_offset_down(unsafe { ptr.offset(len as isize) }, usize_bytes);
123-
let mut offset;
124-
if end_align > 0 {
125-
offset = if end_align >= len { 0 } else { len - end_align };
126-
if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) {
127-
return Some(offset + index);
128-
}
129-
} else {
130-
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);
131112
}
132113

133114
// search the body of the text

0 commit comments

Comments
 (0)