@@ -86,14 +86,12 @@ where
8686 match mode {
8787 Mode :: Char | Mode :: Byte => {
8888 let mut chars = src. chars ( ) ;
89- let res = unescape_char_or_byte ( & mut chars, mode == Mode :: Byte ) ;
89+ let res = unescape_char_or_byte ( & mut chars, mode) ;
9090 callback ( 0 ..( src. len ( ) - chars. as_str ( ) . len ( ) ) , res) ;
9191 }
9292 Mode :: Str | Mode :: ByteStr => unescape_str_common ( src, mode, callback) ,
9393
94- Mode :: RawStr | Mode :: RawByteStr => {
95- unescape_raw_str_or_raw_byte_str ( src, mode == Mode :: RawByteStr , callback)
96- }
94+ Mode :: RawStr | Mode :: RawByteStr => unescape_raw_str_or_raw_byte_str ( src, mode, callback) ,
9795 Mode :: CStr | Mode :: RawCStr => unreachable ! ( ) ,
9896 }
9997}
@@ -121,11 +119,9 @@ where
121119 F : FnMut ( Range < usize > , Result < CStrUnit , EscapeError > ) ,
122120{
123121 if mode == Mode :: RawCStr {
124- unescape_raw_str_or_raw_byte_str (
125- src,
126- mode. characters_should_be_ascii ( ) ,
127- & mut |r, result| callback ( r, result. map ( CStrUnit :: Char ) ) ,
128- ) ;
122+ unescape_raw_str_or_raw_byte_str ( src, mode, & mut |r, result| {
123+ callback ( r, result. map ( CStrUnit :: Char ) )
124+ } ) ;
129125 } else {
130126 unescape_str_common ( src, mode, callback) ;
131127 }
@@ -134,13 +130,13 @@ where
134130/// Takes a contents of a char literal (without quotes), and returns an
135131/// unescaped char or an error.
136132pub fn unescape_char ( src : & str ) -> Result < char , EscapeError > {
137- unescape_char_or_byte ( & mut src. chars ( ) , false )
133+ unescape_char_or_byte ( & mut src. chars ( ) , Mode :: Char )
138134}
139135
140136/// Takes a contents of a byte literal (without quotes), and returns an
141137/// unescaped byte or an error.
142138pub fn unescape_byte ( src : & str ) -> Result < u8 , EscapeError > {
143- unescape_char_or_byte ( & mut src. chars ( ) , true ) . map ( byte_from_char)
139+ unescape_char_or_byte ( & mut src. chars ( ) , Mode :: Byte ) . map ( byte_from_char)
144140}
145141
146142/// What kind of literal do we parse.
@@ -179,7 +175,8 @@ impl Mode {
179175 }
180176
181177 /// Whether characters within the literal must be within the ASCII range
182- fn characters_should_be_ascii ( self ) -> bool {
178+ #[ inline]
179+ fn chars_should_be_ascii ( self ) -> bool {
183180 match self {
184181 Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
185182 Mode :: Char | Mode :: Str | Mode :: RawStr | Mode :: CStr | Mode :: RawCStr => false ,
@@ -298,22 +295,21 @@ fn scan_unicode(
298295}
299296
300297#[ inline]
301- fn ascii_check ( c : char , characters_should_be_ascii : bool ) -> Result < char , EscapeError > {
302- if characters_should_be_ascii && !c. is_ascii ( ) {
303- // Byte literal can't be a non-ascii character.
298+ fn ascii_check ( c : char , chars_should_be_ascii : bool ) -> Result < char , EscapeError > {
299+ if chars_should_be_ascii && !c. is_ascii ( ) {
304300 Err ( EscapeError :: NonAsciiCharInByte )
305301 } else {
306302 Ok ( c)
307303 }
308304}
309305
310- fn unescape_char_or_byte ( chars : & mut Chars < ' _ > , is_byte : bool ) -> Result < char , EscapeError > {
306+ fn unescape_char_or_byte ( chars : & mut Chars < ' _ > , mode : Mode ) -> Result < char , EscapeError > {
311307 let c = chars. next ( ) . ok_or ( EscapeError :: ZeroChars ) ?;
312308 let res = match c {
313- '\\' => scan_escape ( chars, if is_byte { Mode :: Byte } else { Mode :: Char } ) ,
309+ '\\' => scan_escape ( chars, mode ) ,
314310 '\n' | '\t' | '\'' => Err ( EscapeError :: EscapeOnlyChar ) ,
315311 '\r' => Err ( EscapeError :: BareCarriageReturn ) ,
316- _ => ascii_check ( c, is_byte ) ,
312+ _ => ascii_check ( c, mode . chars_should_be_ascii ( ) ) ,
317313 } ?;
318314 if chars. next ( ) . is_some ( ) {
319315 return Err ( EscapeError :: MoreThanOneChar ) ;
@@ -328,6 +324,7 @@ where
328324 F : FnMut ( Range < usize > , Result < T , EscapeError > ) ,
329325{
330326 let mut chars = src. chars ( ) ;
327+ let chars_should_be_ascii = mode. chars_should_be_ascii ( ) ; // get this outside the loop
331328
332329 // The `start` and `end` computation here is complicated because
333330 // `skip_ascii_whitespace` makes us to skip over chars without counting
@@ -352,7 +349,7 @@ where
352349 }
353350 '"' => Err ( EscapeError :: EscapeOnlyChar ) ,
354351 '\r' => Err ( EscapeError :: BareCarriageReturn ) ,
355- _ => ascii_check ( c, mode . characters_should_be_ascii ( ) ) . map ( Into :: into) ,
352+ _ => ascii_check ( c, chars_should_be_ascii ) . map ( Into :: into) ,
356353 } ;
357354 let end = src. len ( ) - chars. as_str ( ) . len ( ) ;
358355 callback ( start..end, res. map ( Into :: into) ) ;
@@ -389,11 +386,12 @@ where
389386/// sequence of characters or errors.
390387/// NOTE: Raw strings do not perform any explicit character escaping, here we
391388/// only produce errors on bare CR.
392- fn unescape_raw_str_or_raw_byte_str < F > ( src : & str , is_byte : bool , callback : & mut F )
389+ fn unescape_raw_str_or_raw_byte_str < F > ( src : & str , mode : Mode , callback : & mut F )
393390where
394391 F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
395392{
396393 let mut chars = src. chars ( ) ;
394+ let chars_should_be_ascii = mode. chars_should_be_ascii ( ) ; // get this outside the loop
397395
398396 // The `start` and `end` computation here matches the one in
399397 // `unescape_str_common` for consistency, even though this function
@@ -402,7 +400,7 @@ where
402400 let start = src. len ( ) - chars. as_str ( ) . len ( ) - c. len_utf8 ( ) ;
403401 let res = match c {
404402 '\r' => Err ( EscapeError :: BareCarriageReturnInRawString ) ,
405- _ => ascii_check ( c, is_byte ) ,
403+ _ => ascii_check ( c, chars_should_be_ascii ) ,
406404 } ;
407405 let end = src. len ( ) - chars. as_str ( ) . len ( ) ;
408406 callback ( start..end, res) ;
0 commit comments