@@ -87,14 +87,12 @@ where
8787 match mode {
8888 Mode :: Char | Mode :: Byte => {
8989 let mut chars = src. chars ( ) ;
90- let res = unescape_char_or_byte ( & mut chars, mode == Mode :: Byte ) ;
90+ let res = unescape_char_or_byte ( & mut chars, mode) ;
9191 callback ( 0 ..( src. len ( ) - chars. as_str ( ) . len ( ) ) , res) ;
9292 }
9393 Mode :: Str | Mode :: ByteStr => unescape_str_common ( src, mode, callback) ,
9494
95- Mode :: RawStr | Mode :: RawByteStr => {
96- unescape_raw_str_or_raw_byte_str ( src, mode == Mode :: RawByteStr , callback)
97- }
95+ Mode :: RawStr | Mode :: RawByteStr => unescape_raw_str_or_raw_byte_str ( src, mode, callback) ,
9896 Mode :: CStr | Mode :: RawCStr => unreachable ! ( ) ,
9997 }
10098}
@@ -122,11 +120,9 @@ where
122120 F : FnMut ( Range < usize > , Result < CStrUnit , EscapeError > ) ,
123121{
124122 if mode == Mode :: RawCStr {
125- unescape_raw_str_or_raw_byte_str (
126- src,
127- mode. characters_should_be_ascii ( ) ,
128- & mut |r, result| callback ( r, result. map ( CStrUnit :: Char ) ) ,
129- ) ;
123+ unescape_raw_str_or_raw_byte_str ( src, mode, & mut |r, result| {
124+ callback ( r, result. map ( CStrUnit :: Char ) )
125+ } ) ;
130126 } else {
131127 unescape_str_common ( src, mode, callback) ;
132128 }
@@ -135,13 +131,13 @@ where
135131/// Takes a contents of a char literal (without quotes), and returns an
136132/// unescaped char or an error.
137133pub fn unescape_char ( src : & str ) -> Result < char , EscapeError > {
138- unescape_char_or_byte ( & mut src. chars ( ) , false )
134+ unescape_char_or_byte ( & mut src. chars ( ) , Mode :: Char )
139135}
140136
141137/// Takes a contents of a byte literal (without quotes), and returns an
142138/// unescaped byte or an error.
143139pub fn unescape_byte ( src : & str ) -> Result < u8 , EscapeError > {
144- unescape_char_or_byte ( & mut src. chars ( ) , true ) . map ( byte_from_char)
140+ unescape_char_or_byte ( & mut src. chars ( ) , Mode :: Byte ) . map ( byte_from_char)
145141}
146142
147143/// What kind of literal do we parse.
@@ -180,7 +176,8 @@ impl Mode {
180176 }
181177
182178 /// Whether characters within the literal must be within the ASCII range
183- fn characters_should_be_ascii ( self ) -> bool {
179+ #[ inline]
180+ fn chars_should_be_ascii ( self ) -> bool {
184181 match self {
185182 Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
186183 Mode :: Char | Mode :: Str | Mode :: RawStr | Mode :: CStr | Mode :: RawCStr => false ,
@@ -299,22 +296,21 @@ fn scan_unicode(
299296}
300297
301298#[ inline]
302- fn ascii_check ( c : char , characters_should_be_ascii : bool ) -> Result < char , EscapeError > {
303- if characters_should_be_ascii && !c. is_ascii ( ) {
304- // Byte literal can't be a non-ascii character.
299+ fn ascii_check ( c : char , chars_should_be_ascii : bool ) -> Result < char , EscapeError > {
300+ if chars_should_be_ascii && !c. is_ascii ( ) {
305301 Err ( EscapeError :: NonAsciiCharInByte )
306302 } else {
307303 Ok ( c)
308304 }
309305}
310306
311- fn unescape_char_or_byte ( chars : & mut Chars < ' _ > , is_byte : bool ) -> Result < char , EscapeError > {
307+ fn unescape_char_or_byte ( chars : & mut Chars < ' _ > , mode : Mode ) -> Result < char , EscapeError > {
312308 let c = chars. next ( ) . ok_or ( EscapeError :: ZeroChars ) ?;
313309 let res = match c {
314- '\\' => scan_escape ( chars, if is_byte { Mode :: Byte } else { Mode :: Char } ) ,
310+ '\\' => scan_escape ( chars, mode ) ,
315311 '\n' | '\t' | '\'' => Err ( EscapeError :: EscapeOnlyChar ) ,
316312 '\r' => Err ( EscapeError :: BareCarriageReturn ) ,
317- _ => ascii_check ( c, is_byte ) ,
313+ _ => ascii_check ( c, mode . chars_should_be_ascii ( ) ) ,
318314 } ?;
319315 if chars. next ( ) . is_some ( ) {
320316 return Err ( EscapeError :: MoreThanOneChar ) ;
@@ -329,6 +325,7 @@ where
329325 F : FnMut ( Range < usize > , Result < T , EscapeError > ) ,
330326{
331327 let mut chars = src. chars ( ) ;
328+ let chars_should_be_ascii = mode. chars_should_be_ascii ( ) ; // get this outside the loop
332329
333330 // The `start` and `end` computation here is complicated because
334331 // `skip_ascii_whitespace` makes us to skip over chars without counting
@@ -353,7 +350,7 @@ where
353350 }
354351 '"' => Err ( EscapeError :: EscapeOnlyChar ) ,
355352 '\r' => Err ( EscapeError :: BareCarriageReturn ) ,
356- _ => ascii_check ( c, mode . characters_should_be_ascii ( ) ) . map ( Into :: into) ,
353+ _ => ascii_check ( c, chars_should_be_ascii ) . map ( Into :: into) ,
357354 } ;
358355 let end = src. len ( ) - chars. as_str ( ) . len ( ) ;
359356 callback ( start..end, res. map ( Into :: into) ) ;
@@ -390,11 +387,12 @@ where
390387/// sequence of characters or errors.
391388/// NOTE: Raw strings do not perform any explicit character escaping, here we
392389/// only produce errors on bare CR.
393- fn unescape_raw_str_or_raw_byte_str < F > ( src : & str , is_byte : bool , callback : & mut F )
390+ fn unescape_raw_str_or_raw_byte_str < F > ( src : & str , mode : Mode , callback : & mut F )
394391where
395392 F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
396393{
397394 let mut chars = src. chars ( ) ;
395+ let chars_should_be_ascii = mode. chars_should_be_ascii ( ) ; // get this outside the loop
398396
399397 // The `start` and `end` computation here matches the one in
400398 // `unescape_str_common` for consistency, even though this function
@@ -403,7 +401,7 @@ where
403401 let start = src. len ( ) - chars. as_str ( ) . len ( ) - c. len_utf8 ( ) ;
404402 let res = match c {
405403 '\r' => Err ( EscapeError :: BareCarriageReturnInRawString ) ,
406- _ => ascii_check ( c, is_byte ) ,
404+ _ => ascii_check ( c, chars_should_be_ascii ) ,
407405 } ;
408406 let end = src. len ( ) - chars. as_str ( ) . len ( ) ;
409407 callback ( start..end, res) ;
0 commit comments