1
1
// NOTE: The following code was generated by "library/core/src/unicode/printable.py",
2
2
// do not edit directly!
3
3
4
- fn check ( x : u16 , singletons_upper : & [ ( u8 , u8 ) ] , singletons_lower : & [ u8 ] , normal : & [ u8 ] ) -> bool {
5
- let x_upper = ( x >> 8 ) as u8 ;
4
+ /// # Safety
5
+ ///
6
+ /// - The sum of all lengths (i.e. the second field of each pair) in `singletons_upper` must be
7
+ /// equal to the length of `singletons_lower`.
8
+ /// - `normal` must be encoded such that lengths greater than `0x7f` consist of two bytes in big
9
+ /// endian, with the highest bit set and the length contained in the remaining 15 bits.
10
+ unsafe fn check (
11
+ x : u16 ,
12
+ singletons_upper : & [ ( u8 , u8 ) ] ,
13
+ singletons_lower : & [ u8 ] ,
14
+ normal : & [ u8 ] ,
15
+ ) -> bool {
16
+ let [ x_upper, x_lower] = x. to_be_bytes ( ) ;
6
17
let mut lower_start = 0 ;
7
18
for & ( upper, lower_count) in singletons_upper {
8
19
let lower_end = lower_start + lower_count as usize ;
9
- if x_upper == upper {
10
- for & lower in & singletons_lower[ lower_start..lower_end] {
11
- if lower == x as u8 {
20
+ if upper == x_upper {
21
+ // SAFETY: The caller ensures that the sum of all lengths in `singletons_upper`
22
+ // is equal to the length of `singletons_lower`, so `lower_end` is guaranteed to be
23
+ // less than `singletons_lower.len()`.
24
+ for & lower in unsafe { singletons_lower. get_unchecked ( lower_start..lower_end) } {
25
+ if lower == x_lower {
12
26
return false ;
13
27
}
14
28
}
@@ -23,9 +37,14 @@ fn check(x: u16, singletons_upper: &[(u8, u8)], singletons_lower: &[u8], normal:
23
37
let mut current = true ;
24
38
while let Some ( v) = normal. next ( ) {
25
39
let len = if v & 0x80 != 0 {
26
- ( ( v & 0x7f ) as i32 ) << 8 | normal. next ( ) . unwrap ( ) as i32
40
+ let upper = v & 0x7f ;
41
+ // SAFETY: The encoding of `normal` is guaranteed by the caller such that
42
+ // if the length is greater than 0x7f, it consists of two bytes, so there
43
+ // must be a next byte.
44
+ let lower = unsafe { normal. next ( ) . unwrap_unchecked ( ) } ;
45
+ i32:: from ( u16:: from_be_bytes ( [ upper, lower] ) )
27
46
} else {
28
- v as i32
47
+ i32:: from ( v )
29
48
} ;
30
49
x -= len;
31
50
if x < 0 {
@@ -43,8 +62,38 @@ pub(crate) fn is_printable(x: char) -> bool {
43
62
match x {
44
63
..32 => false , // ASCII fast path
45
64
..127 => true , // ASCII fast path
46
- ..0x10000 => check ( lower, SINGLETONS0_UPPER , SINGLETONS0_LOWER , NORMAL0 ) ,
47
- ..0x20000 => check ( lower, SINGLETONS1_UPPER , SINGLETONS1_LOWER , NORMAL1 ) ,
65
+ ..0x10000 => {
66
+ const {
67
+ let mut lower_count_total = 0 ;
68
+ let mut i = 0 ;
69
+ while i < SINGLETONS0_UPPER . len ( ) {
70
+ lower_count_total += SINGLETONS0_UPPER [ i] . 1 as usize ;
71
+ i += 1 ;
72
+ }
73
+ assert ! ( lower_count_total == SINGLETONS0_LOWER . len( ) ) ;
74
+ }
75
+ // SAFETY: We just asserted that the sum of all lengths in `SINGLETONS0_UPPER` is equal
76
+ // to the length of `SINGLETONS0_LOWER`, and `NORMAL0` is encoded such that lengths
77
+ // greater than `0x7f` consist of two bytes in big endian, with the highest bit set and
78
+ // the length contained in the remaining 15 bits.
79
+ unsafe { check ( lower, SINGLETONS0_UPPER , SINGLETONS0_LOWER , NORMAL0 ) }
80
+ }
81
+ ..0x20000 => {
82
+ const {
83
+ let mut lower_count_total = 0 ;
84
+ let mut i = 0 ;
85
+ while i < SINGLETONS1_UPPER . len ( ) {
86
+ lower_count_total += SINGLETONS1_UPPER [ i] . 1 as usize ;
87
+ i += 1 ;
88
+ }
89
+ assert ! ( lower_count_total == SINGLETONS1_LOWER . len( ) ) ;
90
+ }
91
+ // SAFETY: We just asserted that the sum of all lengths in `SINGLETONS1_UPPER` is equal
92
+ // to the length of `SINGLETONS1_LOWER`, and `NORMAL1` is encoded such that lengths
93
+ // greater than `0x7f` consist of two bytes in big endian, with the highest bit set and
94
+ // the length contained in the remaining 15 bits.
95
+ unsafe { check ( lower, SINGLETONS1_UPPER , SINGLETONS1_LOWER , NORMAL1 ) }
96
+ }
48
97
0x2a6e0 ..0x2a700 => false ,
49
98
0x2b73a ..0x2b740 => false ,
50
99
0x2b81e ..0x2b820 => false ,
0 commit comments