From 70cb75c31e5d084da3a06457484096330ca72a61 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Thu, 26 Jul 2018 18:29:38 +0200 Subject: [PATCH 1/2] make memrchr use align_offset --- src/libcore/slice/memchr.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/libcore/slice/memchr.rs b/src/libcore/slice/memchr.rs index 7b62e7b0620fd..dbbe1c07c2338 100644 --- a/src/libcore/slice/memchr.rs +++ b/src/libcore/slice/memchr.rs @@ -102,8 +102,24 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option { let ptr = text.as_ptr(); let usize_bytes = mem::size_of::(); + // a version of align_offset that says how much must be *subtracted* + // from a pointer to be aligned. + #[inline(always)] + fn align_offset_down(ptr: *const u8, align: usize) -> usize { + let align_offset = ptr.align_offset(align); + if align_offset > align { + // Not possible to align + usize::max_value() + } else if align_offset == 0 { + 0 + } else { + // E.g. if align=8 and we have to add 1, then we can also subtract 7. + align - align_offset + } + } + // search to an aligned boundary - let end_align = (ptr as usize + len) & (usize_bytes - 1); + let end_align = align_offset_down(unsafe { ptr.offset(len as isize) }, usize_bytes); let mut offset; if end_align > 0 { offset = if end_align >= len { 0 } else { len - end_align }; From 3bc59b5205410b229eb66236b4aafbb90487fa34 Mon Sep 17 00:00:00 2001 From: Ralf Jung Date: Fri, 27 Jul 2018 11:54:38 +0200 Subject: [PATCH 2/2] use slice::align_to --- src/libcore/slice/memchr.rs | 33 +++++++-------------------------- 1 file changed, 7 insertions(+), 26 deletions(-) diff --git a/src/libcore/slice/memchr.rs b/src/libcore/slice/memchr.rs index dbbe1c07c2338..72e7b57a6cb3c 100644 --- a/src/libcore/slice/memchr.rs +++ b/src/libcore/slice/memchr.rs @@ -102,32 +102,13 @@ pub fn memrchr(x: u8, text: &[u8]) -> Option { let ptr = text.as_ptr(); let usize_bytes = mem::size_of::(); - // a version of align_offset that says how much must be *subtracted* - // from a pointer to be aligned. - #[inline(always)] - fn align_offset_down(ptr: *const u8, align: usize) -> usize { - let align_offset = ptr.align_offset(align); - if align_offset > align { - // Not possible to align - usize::max_value() - } else if align_offset == 0 { - 0 - } else { - // E.g. if align=8 and we have to add 1, then we can also subtract 7. - align - align_offset - } - } - - // search to an aligned boundary - let end_align = align_offset_down(unsafe { ptr.offset(len as isize) }, usize_bytes); - 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::() }; + len - suffix.len() + }; + if let Some(index) = text[offset..].iter().rposition(|elt| *elt == x) { + return Some(offset + index); } // search the body of the text