@@ -71,29 +71,24 @@ 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 , mode : Mode , callback : & mut F )
74
+ pub ( crate ) fn unescape_raw_str < F > ( literal_text : & str , callback : & mut F )
75
75
where
76
76
F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
77
77
{
78
- let mut byte_offset: usize = 0 ;
78
+ unescape_raw_str_or_byte_str ( literal_text, Mode :: Str , callback)
79
+ }
79
80
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
+ } )
97
92
}
98
93
99
94
#[ derive( Debug , Clone , Copy ) ]
@@ -284,9 +279,38 @@ where
284
279
}
285
280
}
286
281
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
+
287
311
fn byte_from_char ( c : char ) -> u8 {
288
312
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) " ) ;
290
314
res as u8
291
315
}
292
316
0 commit comments