File tree 6 files changed +29
-13
lines changed
6 files changed +29
-13
lines changed Original file line number Diff line number Diff line change @@ -77,6 +77,6 @@ fn is_ascii_align_to_unrolled(bytes: &[u8]) -> bool {
77
77
78
78
#[ inline]
79
79
fn contains_nonascii ( v : usize ) -> bool {
80
- const NONASCII_MASK : usize = 0x80808080_80808080u64 as usize ;
80
+ const NONASCII_MASK : usize = usize :: from_ne_bytes ( [ 0x80 ; core :: mem :: size_of :: < usize > ( ) ] ) ;
81
81
( NONASCII_MASK & v) != 0
82
82
}
Original file line number Diff line number Diff line change @@ -890,6 +890,27 @@ impl usize {
890
890
widening_impl ! { usize , u128 , 64 , unsigned }
891
891
}
892
892
893
+ impl usize {
894
+ /// Returns an `usize` where every byte is equal to `x`.
895
+ #[ inline]
896
+ pub ( crate ) const fn repeat_u8 ( x : u8 ) -> usize {
897
+ usize:: from_ne_bytes ( [ x; mem:: size_of :: < usize > ( ) ] )
898
+ }
899
+
900
+ /// Returns an `usize` where every byte pair is equal to `x`.
901
+ #[ inline]
902
+ pub ( crate ) const fn repeat_u16 ( x : u16 ) -> usize {
903
+ let mut r = 0usize ;
904
+ let mut i = 0 ;
905
+ while i < mem:: size_of :: < usize > ( ) {
906
+ // Use `wrapping_shl` to make it work on targets with 16-bit `usize`
907
+ r = r. wrapping_shl ( 16 ) | ( x as usize ) ;
908
+ i += 2 ;
909
+ }
910
+ r
911
+ }
912
+ }
913
+
893
914
/// A classification of floating point numbers.
894
915
///
895
916
/// This `enum` is used as the return type for [`f32::classify`] and [`f64::classify`]. See
Original file line number Diff line number Diff line change @@ -235,7 +235,7 @@ impl<'a> fmt::Debug for EscapeAscii<'a> {
235
235
/// from `../str/mod.rs`, which does something similar for utf8 validation.
236
236
#[ inline]
237
237
fn contains_nonascii ( v : usize ) -> bool {
238
- const NONASCII_MASK : usize = 0x80808080_80808080u64 as usize ;
238
+ const NONASCII_MASK : usize = usize:: repeat_u8 ( 0x80 ) ;
239
239
( NONASCII_MASK & v) != 0
240
240
}
241
241
Original file line number Diff line number Diff line change 4
4
use crate :: cmp;
5
5
use crate :: mem;
6
6
7
- const LO_U64 : u64 = 0x0101010101010101 ;
8
- const HI_U64 : u64 = 0x8080808080808080 ;
9
-
10
- // Use truncation.
11
- const LO_USIZE : usize = LO_U64 as usize ;
12
- const HI_USIZE : usize = HI_U64 as usize ;
7
+ const LO_USIZE : usize = usize:: repeat_u8 ( 0x01 ) ;
8
+ const HI_USIZE : usize = usize:: repeat_u8 ( 0x80 ) ;
13
9
const USIZE_BYTES : usize = mem:: size_of :: < usize > ( ) ;
14
10
15
11
/// Returns `true` if `x` contains any zero byte.
Original file line number Diff line number Diff line change @@ -112,16 +112,16 @@ fn do_count_chars(s: &str) -> usize {
112
112
// true)
113
113
#[ inline]
114
114
fn contains_non_continuation_byte ( w : usize ) -> usize {
115
- const LSB : usize = 0x0101_0101_0101_0101u64 as usize ;
115
+ const LSB : usize = usize:: repeat_u8 ( 0x01 ) ;
116
116
( ( !w >> 7 ) | ( w >> 6 ) ) & LSB
117
117
}
118
118
119
119
// Morally equivalent to `values.to_ne_bytes().into_iter().sum::<usize>()`, but
120
120
// more efficient.
121
121
#[ inline]
122
122
fn sum_bytes_in_usize ( values : usize ) -> usize {
123
- const LSB_SHORTS : usize = 0x0001_0001_0001_0001_u64 as usize ;
124
- const SKIP_BYTES : usize = 0x00ff_00ff_00ff_00ff_u64 as usize ;
123
+ const LSB_SHORTS : usize = usize:: repeat_u16 ( 0x0001 ) ;
124
+ const SKIP_BYTES : usize = usize:: repeat_u16 ( 0x00ff ) ;
125
125
126
126
let pair_sum: usize = ( values & SKIP_BYTES ) + ( ( values >> 8 ) & SKIP_BYTES ) ;
127
127
pair_sum. wrapping_mul ( LSB_SHORTS ) >> ( ( USIZE_SIZE - 2 ) * 8 )
Original file line number Diff line number Diff line change @@ -112,8 +112,7 @@ where
112
112
Some ( ch)
113
113
}
114
114
115
- // use truncation to fit u64 into usize
116
- const NONASCII_MASK : usize = 0x80808080_80808080u64 as usize ;
115
+ const NONASCII_MASK : usize = usize:: repeat_u8 ( 0x80 ) ;
117
116
118
117
/// Returns `true` if any byte in the word `x` is nonascii (>= 128).
119
118
#[ inline]
You can’t perform that action at this time.
0 commit comments