11// NOTE: The following code was generated by "library/core/src/unicode/printable.py",
22// do not edit directly!
33
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 ( ) ;
617 let mut lower_start = 0 ;
718 for & ( upper, lower_count) in singletons_upper {
819 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 {
1226 return false ;
1327 }
1428 }
@@ -23,9 +37,14 @@ fn check(x: u16, singletons_upper: &[(u8, u8)], singletons_lower: &[u8], normal:
2337 let mut current = true ;
2438 while let Some ( v) = normal. next ( ) {
2539 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] ) )
2746 } else {
28- v as i32
47+ i32:: from ( v )
2948 } ;
3049 x -= len;
3150 if x < 0 {
@@ -43,8 +62,38 @@ pub(crate) fn is_printable(x: char) -> bool {
4362 match x {
4463 ..32 => false , // ASCII fast path
4564 ..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+ }
4897 0x2a6e0 ..0x2a700 => false ,
4998 0x2b73a ..0x2b740 => false ,
5099 0x2b81e ..0x2b820 => false ,
0 commit comments