about summary refs log tree commit diff
path: root/src/libcore/slice
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2018-07-28 16:44:21 +0000
committerbors <bors@rust-lang.org>2018-07-28 16:44:21 +0000
commitd75458200516f06455d175adc001fd993d674050 (patch)
treea8ff0a6b30fe1900c4b647fe76d2c8639f4961d3 /src/libcore/slice
parent26e73dabeb7a15e0e38feb2cadca3c1f740a61d2 (diff)
parent3bc59b5205410b229eb66236b4aafbb90487fa34 (diff)
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)
Diffstat (limited to 'src/libcore/slice')
-rw-r--r--src/libcore/slice/memchr.rs17
1 files changed, 7 insertions, 10 deletions
diff --git a/src/libcore/slice/memchr.rs b/src/libcore/slice/memchr.rs
index 7b62e7b0620..72e7b57a6cb 100644
--- a/src/libcore/slice/memchr.rs
+++ b/src/libcore/slice/memchr.rs
@@ -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