]> git.lizzy.rs Git - rust.git/blobdiff - src/libstd/sys_common/memchr.rs
Add `<*const T>::align_offset` and use it in `memchr`
[rust.git] / src / libstd / sys_common / memchr.rs
index 3824a5fb5284c85c430f148704c8a5516ddb44f1..50f998eb4867dc087476835f2feef4bd25f6056e 100644 (file)
@@ -65,15 +65,12 @@ pub fn memchr(x: u8, text: &[u8]) -> Option<usize> {
         let usize_bytes = mem::size_of::<usize>();
 
         // search up to an aligned boundary
-        let align = (ptr as usize) & (usize_bytes- 1);
-        let mut offset;
-        if align > 0 {
-            offset = cmp::min(usize_bytes - align, len);
+        let mut offset = ptr.align_offset(usize_bytes);
+        if offset > 0 {
+            offset = cmp::min(offset, len);
             if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
                 return Some(index);
             }
-        } else {
-            offset = 0;
         }
 
         // search the body of the text