@@ -8,25 +8,25 @@ use super::Utf8Error;
8
8
/// The first byte is special, only want bottom 5 bits for width 2, 4 bits
9
9
/// for width 3, and 3 bits for width 4.
10
10
#[ inline]
11
- fn utf8_first_byte ( byte : u8 , width : u32 ) -> u32 {
11
+ const fn utf8_first_byte ( byte : u8 , width : u32 ) -> u32 {
12
12
( byte & ( 0x7F >> width) ) as u32
13
13
}
14
14
15
15
/// Returns the value of `ch` updated with continuation byte `byte`.
16
16
#[ inline]
17
- fn utf8_acc_cont_byte ( ch : u32 , byte : u8 ) -> u32 {
17
+ const fn utf8_acc_cont_byte ( ch : u32 , byte : u8 ) -> u32 {
18
18
( ch << 6 ) | ( byte & CONT_MASK ) as u32
19
19
}
20
20
21
21
/// Checks whether the byte is a UTF-8 continuation byte (i.e., starts with the
22
22
/// bits `10`).
23
23
#[ inline]
24
- pub ( super ) fn utf8_is_cont_byte ( byte : u8 ) -> bool {
24
+ pub ( super ) const fn utf8_is_cont_byte ( byte : u8 ) -> bool {
25
25
( byte as i8 ) < -64
26
26
}
27
27
28
28
#[ inline]
29
- fn unwrap_or_0 ( opt : Option < & u8 > ) -> u8 {
29
+ const fn unwrap_or_0 ( opt : Option < & u8 > ) -> u8 {
30
30
match opt {
31
31
Some ( & byte) => byte,
32
32
None => 0 ,
@@ -105,14 +105,15 @@ const NONASCII_MASK: usize = 0x80808080_80808080u64 as usize;
105
105
106
106
/// Returns `true` if any byte in the word `x` is nonascii (>= 128).
107
107
#[ inline]
108
- fn contains_nonascii ( x : usize ) -> bool {
108
+ const fn contains_nonascii ( x : usize ) -> bool {
109
109
( x & NONASCII_MASK ) != 0
110
110
}
111
111
112
112
/// Walks through `v` checking that it's a valid UTF-8 sequence,
113
113
/// returning `Ok(())` in that case, or, if it is invalid, `Err(err)`.
114
114
#[ inline( always) ]
115
- pub ( super ) fn run_utf8_validation ( v : & [ u8 ] ) -> Result < ( ) , Utf8Error > {
115
+ #[ rustc_const_unstable( feature = "str_internals" , issue = "none" ) ]
116
+ pub ( super ) const fn run_utf8_validation ( v : & [ u8 ] ) -> Result < ( ) , Utf8Error > {
116
117
let mut index = 0 ;
117
118
let len = v. len ( ) ;
118
119
@@ -142,7 +143,7 @@ pub(super) fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
142
143
143
144
let first = v[ index] ;
144
145
if first >= 128 {
145
- let w = UTF8_CHAR_WIDTH [ first as usize ] ;
146
+ let w = utf8_char_width ( first) ;
146
147
// 2-byte encoding is for codepoints \u{0080} to \u{07ff}
147
148
// first C2 80 last DF BF
148
149
// 3-byte encoding is for codepoints \u{0800} to \u{ffff}
@@ -230,7 +231,7 @@ pub(super) fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
230
231
}
231
232
232
233
// https://tools.ietf.org/html/rfc3629
233
- static UTF8_CHAR_WIDTH : [ u8 ; 256 ] = [
234
+ const UTF8_CHAR_WIDTH : & [ u8 ; 256 ] = & [
234
235
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
235
236
1 , // 0x1F
236
237
1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ,
@@ -253,7 +254,7 @@ static UTF8_CHAR_WIDTH: [u8; 256] = [
253
254
#[ unstable( feature = "str_internals" , issue = "none" ) ]
254
255
#[ must_use]
255
256
#[ inline]
256
- pub fn utf8_char_width ( b : u8 ) -> usize {
257
+ pub const fn utf8_char_width ( b : u8 ) -> usize {
257
258
UTF8_CHAR_WIDTH [ b as usize ] as usize
258
259
}
259
260
0 commit comments