@@ -71,44 +71,28 @@ 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 , callback : & mut F )
74+ pub ( crate ) fn unescape_raw_str < F > ( literal_text : & str , mode : Mode , callback : & mut F )
7575where
7676 F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
7777{
7878 let mut byte_offset: usize = 0 ;
7979
8080 let mut chars = literal_text. chars ( ) . peekable ( ) ;
8181 while let Some ( curr) = chars. next ( ) {
82- let result = match ( curr, chars. peek ( ) ) {
83- ( '\r' , Some ( '\n' ) ) => Ok ( curr) ,
84- ( '\r' , _) => Err ( EscapeError :: BareCarriageReturn ) ,
85- _ => Ok ( curr) ,
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 ] ) ,
8692 } ;
87- callback ( byte_offset..( byte_offset + curr. len_utf8 ( ) ) , result) ;
88- byte_offset += curr. len_utf8 ( ) ;
89- }
90- }
91-
92- /// Takes a contents of a string literal (without quotes) and produces a
93- /// sequence of characters or errors.
94- /// NOTE: Raw strings do not perform any explicit character escaping, here we
95- /// only translate CRLF to LF and produce errors on bare CR.
96- pub ( crate ) fn unescape_raw_byte_str < F > ( literal_text : & str , callback : & mut F )
97- where
98- F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
99- {
100- let mut byte_offset: usize = 0 ;
101-
102- let mut chars = literal_text. chars ( ) . peekable ( ) ;
103- while let Some ( curr) = chars. next ( ) {
104- let result = match ( curr, chars. peek ( ) ) {
105- ( '\r' , Some ( '\n' ) ) => Ok ( curr) ,
106- ( '\r' , _) => Err ( EscapeError :: BareCarriageReturn ) ,
107- ( c, _) if c > '\x7F' => Err ( EscapeError :: NonAsciiCharInByteString ) ,
108- _ => Ok ( curr) ,
109- } ;
110- callback ( byte_offset..( byte_offset + curr. len_utf8 ( ) ) , result) ;
111- byte_offset += curr. len_utf8 ( ) ;
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;
11296 }
11397}
11498
0 commit comments