From 2787a285bd211ecbf75fd95d990226242005d848 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Wed, 13 Sep 2017 12:20:39 +0200 Subject: Add `<*const T>::align_offset` and use it in `memchr` --- src/libstd/sys_common/memchr.rs | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'src/libstd/sys_common') diff --git a/src/libstd/sys_common/memchr.rs b/src/libstd/sys_common/memchr.rs index 3824a5fb528..50f998eb486 100644 --- a/src/libstd/sys_common/memchr.rs +++ b/src/libstd/sys_common/memchr.rs @@ -65,15 +65,12 @@ pub mod fallback { let usize_bytes = mem::size_of::(); // 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 -- cgit 1.4.1-3-g733a5