@@ -8,25 +8,25 @@ use super::Utf8Error;
88/// The first byte is special, only want bottom 5 bits for width 2, 4 bits
99/// for width 3, and 3 bits for width 4.
1010#[ inline]
11- fn utf8_first_byte ( byte : u8 , width : u32 ) -> u32 {
11+ const fn utf8_first_byte ( byte : u8 , width : u32 ) -> u32 {
1212 ( byte & ( 0x7F >> width) ) as u32
1313}
1414
1515/// Returns the value of `ch` updated with continuation byte `byte`.
1616#[ inline]
17- fn utf8_acc_cont_byte ( ch : u32 , byte : u8 ) -> u32 {
17+ const fn utf8_acc_cont_byte ( ch : u32 , byte : u8 ) -> u32 {
1818 ( ch << 6 ) | ( byte & CONT_MASK ) as u32
1919}
2020
2121/// Checks whether the byte is a UTF-8 continuation byte (i.e., starts with the
2222/// bits `10`).
2323#[ inline]
24- pub ( super ) fn utf8_is_cont_byte ( byte : u8 ) -> bool {
24+ pub ( super ) const fn utf8_is_cont_byte ( byte : u8 ) -> bool {
2525 ( byte as i8 ) < -64
2626}
2727
2828#[ inline]
29- fn unwrap_or_0 ( opt : Option < & u8 > ) -> u8 {
29+ const fn unwrap_or_0 ( opt : Option < & u8 > ) -> u8 {
3030 match opt {
3131 Some ( & byte) => byte,
3232 None => 0 ,
@@ -105,14 +105,15 @@ const NONASCII_MASK: usize = 0x80808080_80808080u64 as usize;
105105
106106/// Returns `true` if any byte in the word `x` is nonascii (>= 128).
107107#[ inline]
108- fn contains_nonascii ( x : usize ) -> bool {
108+ const fn contains_nonascii ( x : usize ) -> bool {
109109 ( x & NONASCII_MASK ) != 0
110110}
111111
112112/// Walks through `v` checking that it's a valid UTF-8 sequence,
113113/// returning `Ok(())` in that case, or, if it is invalid, `Err(err)`.
114114#[ 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 > {
116117 let mut index = 0 ;
117118 let len = v. len ( ) ;
118119
@@ -142,7 +143,7 @@ pub(super) fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
142143
143144 let first = v[ index] ;
144145 if first >= 128 {
145- let w = UTF8_CHAR_WIDTH [ first as usize ] ;
146+ let w = utf8_char_width ( first) ;
146147 // 2-byte encoding is for codepoints \u{0080} to \u{07ff}
147148 // first C2 80 last DF BF
148149 // 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> {
230231}
231232
232233// https://tools.ietf.org/html/rfc3629
233- static UTF8_CHAR_WIDTH : [ u8 ; 256 ] = [
234+ const UTF8_CHAR_WIDTH : & [ u8 ; 256 ] = & [
234235 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 ,
235236 1 , // 0x1F
236237 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] = [
253254#[ unstable( feature = "str_internals" , issue = "none" ) ]
254255#[ must_use]
255256#[ inline]
256- pub fn utf8_char_width ( b : u8 ) -> usize {
257+ pub const fn utf8_char_width ( b : u8 ) -> usize {
257258 UTF8_CHAR_WIDTH [ b as usize ] as usize
258259}
259260
0 commit comments