Skip to content

Commit 18ab95f

Browse files
committed
Also use in memchr
1 parent db65375 commit 18ab95f

File tree

2 files changed

+6
-8
lines changed

2 files changed

+6
-8
lines changed

src/libcore/ptr.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -646,7 +646,7 @@ impl<T: ?Sized> *const T {
646646
/// Accessing adjacent `u8` as `u16`
647647
///
648648
/// ```
649-
/// # #![feature(core_intrinsics)]
649+
/// # #![feature(align_offset)]
650650
/// # fn foo(n: usize) {
651651
/// # use std::mem::align_of;
652652
/// # unsafe {
@@ -875,7 +875,7 @@ impl<T: ?Sized> *mut T {
875875
/// Accessing adjacent `u8` as `u16`
876876
///
877877
/// ```
878-
/// # #![feature(core_intrinsics)]
878+
/// # #![feature(align_offset)]
879879
/// # fn foo(n: usize) {
880880
/// # use std::mem::align_of;
881881
/// # unsafe {

src/libstd/sys_common/memchr.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
pub mod fallback {
1616
use cmp;
1717
use mem;
18+
use intrinsics::align_offset;
1819

1920
const LO_U64: u64 = 0x0101010101010101;
2021
const HI_U64: u64 = 0x8080808080808080;
@@ -65,15 +66,12 @@ pub mod fallback {
6566
let usize_bytes = mem::size_of::<usize>();
6667

6768
// search up to an aligned boundary
68-
let align = (ptr as usize) & (usize_bytes- 1);
69-
let mut offset;
70-
if align > 0 {
71-
offset = cmp::min(usize_bytes - align, len);
69+
let mut offset = unsafe { align_offset(ptr as *const _, usize_bytes) };
70+
if offset > 0 {
71+
offset = cmp::min(offset, len);
7272
if let Some(index) = text[..offset].iter().position(|elt| *elt == x) {
7373
return Some(index);
7474
}
75-
} else {
76-
offset = 0;
7775
}
7876

7977
// search the body of the text

0 commit comments

Comments
 (0)