@@ -71,44 +71,28 @@ where
71
71
/// sequence of characters or errors.
72
72
/// NOTE: Raw strings do not perform any explicit character escaping, here we
73
73
/// 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 )
75
75
where
76
76
F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
77
77
{
78
78
let mut byte_offset: usize = 0 ;
79
79
80
80
let mut chars = literal_text. chars ( ) . peekable ( ) ;
81
81
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 ] ) ,
86
92
} ;
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;
112
96
}
113
97
}
114
98
0 commit comments