5
5
use core:: { cmp, usize} ;
6
6
7
7
const USIZE_BYTES : usize = core:: mem:: size_of :: < usize > ( ) ;
8
+ const ALIGN_MASK : usize = core:: mem:: align_of :: < usize > ( ) - 1 ;
8
9
9
10
// The number of bytes to loop at in one iteration of memchr/memrchr.
10
11
const LOOP_SIZE : usize = 2 * USIZE_BYTES ;
@@ -22,7 +23,6 @@ pub fn inv_memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
22
23
let vn1 = repeat_byte ( n1) ;
23
24
let confirm = |byte| byte != n1;
24
25
let loop_size = cmp:: min ( LOOP_SIZE , haystack. len ( ) ) ;
25
- let align = USIZE_BYTES - 1 ;
26
26
let start_ptr = haystack. as_ptr ( ) ;
27
27
28
28
unsafe {
@@ -38,7 +38,7 @@ pub fn inv_memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
38
38
return forward_search ( start_ptr, end_ptr, ptr, confirm) ;
39
39
}
40
40
41
- ptr = ptr. add ( USIZE_BYTES - ( start_ptr as usize & align ) ) ;
41
+ ptr = ptr. add ( USIZE_BYTES - ( start_ptr as usize & ALIGN_MASK ) ) ;
42
42
debug_assert ! ( ptr > start_ptr) ;
43
43
debug_assert ! ( end_ptr. sub( USIZE_BYTES ) >= start_ptr) ;
44
44
while loop_size == LOOP_SIZE && ptr <= end_ptr. sub ( loop_size) {
@@ -62,7 +62,6 @@ pub fn inv_memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
62
62
let vn1 = repeat_byte ( n1) ;
63
63
let confirm = |byte| byte != n1;
64
64
let loop_size = cmp:: min ( LOOP_SIZE , haystack. len ( ) ) ;
65
- let align = USIZE_BYTES - 1 ;
66
65
let start_ptr = haystack. as_ptr ( ) ;
67
66
68
67
unsafe {
@@ -78,7 +77,7 @@ pub fn inv_memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
78
77
return reverse_search ( start_ptr, end_ptr, ptr, confirm) ;
79
78
}
80
79
81
- ptr = ptr. sub ( end_ptr as usize & align ) ;
80
+ ptr = ptr. sub ( end_ptr as usize & ALIGN_MASK ) ;
82
81
debug_assert ! ( start_ptr <= ptr && ptr <= end_ptr) ;
83
82
while loop_size == LOOP_SIZE && ptr >= start_ptr. add ( loop_size) {
84
83
debug_assert_eq ! ( 0 , ( ptr as usize ) % USIZE_BYTES ) ;
0 commit comments