@@ -71,29 +71,24 @@ where
7171/// sequence of characters or errors.
7272/// NOTE: Raw strings do not perform any explicit character escaping, here we
7373/// only translate CRLF to LF and produce errors on bare CR.
74- pub ( crate ) fn unescape_raw_str < F > ( literal_text : & str , mode : Mode , callback : & mut F )
74+ pub ( crate ) fn unescape_raw_str < F > ( literal_text : & str , callback : & mut F )
7575where
7676 F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
7777{
78- let mut byte_offset: usize = 0 ;
78+ unescape_raw_str_or_byte_str ( literal_text, Mode :: Str , callback)
79+ }
7980
80- let mut chars = literal_text. chars ( ) . peekable ( ) ;
81- while let Some ( curr) = chars. next ( ) {
82- let ( result, scanned) = match ( curr, chars. peek ( ) ) {
83- ( '\r' , Some ( '\n' ) ) => {
84- chars. next ( ) ;
85- ( Ok ( '\n' ) , [ Some ( '\r' ) , Some ( '\n' ) ] )
86- } ,
87- ( '\r' , _) =>
88- ( Err ( EscapeError :: BareCarriageReturn ) , [ Some ( '\r' ) , None ] ) ,
89- ( c, _) if mode. is_bytes ( ) && c > '\x7F' =>
90- ( Err ( EscapeError :: NonAsciiCharInByteString ) , [ Some ( c) , None ] ) ,
91- ( c, _) => ( Ok ( c) , [ Some ( c) , None ] ) ,
92- } ;
93- let len_utf8: usize = scanned. iter ( ) . filter_map ( |& x| x) . map ( char:: len_utf8) . sum ( ) ;
94- callback ( byte_offset..( byte_offset + len_utf8) , result) ;
95- byte_offset += len_utf8;
96- }
81+ /// Takes a contents of a string literal (without quotes) and produces a
82+ /// sequence of characters or errors.
83+ /// NOTE: Raw strings do not perform any explicit character escaping, here we
84+ /// only translate CRLF to LF and produce errors on bare CR.
85+ pub ( crate ) fn unescape_raw_byte_str < F > ( literal_text : & str , callback : & mut F )
86+ where
87+ F : FnMut ( Range < usize > , Result < u8 , EscapeError > ) ,
88+ {
89+ unescape_raw_str_or_byte_str ( literal_text, Mode :: ByteStr , & mut |range, char| {
90+ callback ( range, char. map ( byte_from_char) )
91+ } )
9792}
9893
9994#[ derive( Debug , Clone , Copy ) ]
@@ -284,9 +279,38 @@ where
284279 }
285280}
286281
282+ /// Takes a contents of a string literal (without quotes) and produces a
283+ /// sequence of characters or errors.
284+ /// NOTE: Raw strings do not perform any explicit character escaping, here we
285+ /// only translate CRLF to LF and produce errors on bare CR.
286+ fn unescape_raw_str_or_byte_str < F > ( literal_text : & str , mode : Mode , callback : & mut F )
287+ where
288+ F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
289+ {
290+ let mut byte_offset: usize = 0 ;
291+
292+ let mut chars = literal_text. chars ( ) . peekable ( ) ;
293+ while let Some ( curr) = chars. next ( ) {
294+ let ( result, scanned) = match ( curr, chars. peek ( ) ) {
295+ ( '\r' , Some ( '\n' ) ) => {
296+ chars. next ( ) ;
297+ ( Ok ( '\n' ) , [ Some ( '\r' ) , Some ( '\n' ) ] )
298+ } ,
299+ ( '\r' , _) =>
300+ ( Err ( EscapeError :: BareCarriageReturn ) , [ Some ( '\r' ) , None ] ) ,
301+ ( c, _) if mode. is_bytes ( ) && !c. is_ascii ( ) =>
302+ ( Err ( EscapeError :: NonAsciiCharInByteString ) , [ Some ( c) , None ] ) ,
303+ ( c, _) => ( Ok ( c) , [ Some ( c) , None ] ) ,
304+ } ;
305+ let len_utf8: usize = scanned. iter ( ) . filter_map ( |& x| x) . map ( char:: len_utf8) . sum ( ) ;
306+ callback ( byte_offset..( byte_offset + len_utf8) , result) ;
307+ byte_offset += len_utf8;
308+ }
309+ }
310+
287311fn byte_from_char ( c : char ) -> u8 {
288312 let res = c as u32 ;
289- assert ! ( res <= u8 :: max_value( ) as u32 , "guaranteed because of Mode::Byte" ) ;
313+ assert ! ( res <= u8 :: max_value( ) as u32 , "guaranteed because of Mode::Byte(Str) " ) ;
290314 res as u8
291315}
292316
0 commit comments