@@ -58,69 +58,57 @@ pub enum EscapeError {
58
58
NonAsciiCharInByteString ,
59
59
}
60
60
61
- /// Takes a contents of a char literal (without quotes), and returns an
62
- /// unescaped char or an error
63
- pub fn unescape_char ( literal_text : & str ) -> Result < char , ( usize , EscapeError ) > {
64
- let mut chars = literal_text. chars ( ) ;
65
- unescape_char_or_byte ( & mut chars, Mode :: Char )
66
- . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
67
- }
68
-
69
- /// Takes a contents of a byte literal (without quotes), and returns an
70
- /// unescaped byte or an error.
71
- pub fn unescape_byte ( literal_text : & str ) -> Result < u8 , ( usize , EscapeError ) > {
72
- let mut chars = literal_text. chars ( ) ;
73
- unescape_char_or_byte ( & mut chars, Mode :: Byte )
74
- . map ( byte_from_char)
75
- . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
76
- }
77
-
78
- /// Takes a contents of a string literal (without quotes) and produces a
61
+ /// Takes a contents of a literal (without quotes) and produces a
79
62
/// sequence of escaped characters or errors.
80
63
/// Values are returned through invoking of the provided callback.
81
- pub fn unescape_str < F > ( literal_text : & str , callback : & mut F )
64
+ pub fn unescape_literal < F > ( literal_text : & str , mode : Mode , callback : & mut F )
82
65
where
83
66
F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
84
67
{
85
- unescape_str_or_byte_str ( literal_text, Mode :: Str , callback)
68
+ match mode {
69
+ Mode :: Char | Mode :: Byte => {
70
+ let mut chars = literal_text. chars ( ) ;
71
+ let result = unescape_char_or_byte ( & mut chars, mode) ;
72
+ // The Chars iterator moved forward.
73
+ callback ( 0 ..( literal_text. len ( ) - chars. as_str ( ) . len ( ) ) , result) ;
74
+ }
75
+ Mode :: Str | Mode :: ByteStr => unescape_str_or_byte_str ( literal_text, mode, callback) ,
76
+ // NOTE: Raw strings do not perform any explicit character escaping, here we
77
+ // only translate CRLF to LF and produce errors on bare CR.
78
+ Mode :: RawStr | Mode :: RawByteStr => {
79
+ unescape_raw_str_or_byte_str ( literal_text, mode, callback)
80
+ }
81
+ }
86
82
}
87
83
88
- /// Takes a contents of a byte string literal (without quotes) and produces a
89
- /// sequence of bytes or errors.
84
+ /// Takes a contents of a byte, byte string or raw byte string (without quotes)
85
+ /// and produces a sequence of bytes or errors.
90
86
/// Values are returned through invoking of the provided callback.
91
- pub fn unescape_byte_str < F > ( literal_text : & str , callback : & mut F )
87
+ pub fn unescape_byte_literal < F > ( literal_text : & str , mode : Mode , callback : & mut F )
92
88
where
93
89
F : FnMut ( Range < usize > , Result < u8 , EscapeError > ) ,
94
90
{
95
- unescape_str_or_byte_str ( literal_text, Mode :: ByteStr , & mut |range, char| {
96
- callback ( range, char. map ( byte_from_char) )
91
+ assert ! ( mode. is_bytes( ) ) ;
92
+ unescape_literal ( literal_text, mode, & mut |range, result| {
93
+ callback ( range, result. map ( byte_from_char) ) ;
97
94
} )
98
95
}
99
96
100
- /// Takes a contents of a raw string literal (without quotes) and produces a
101
- /// sequence of characters or errors.
102
- /// Values are returned through invoking of the provided callback.
103
- /// NOTE: Raw strings do not perform any explicit character escaping, here we
104
- /// only translate CRLF to LF and produce errors on bare CR.
105
- pub fn unescape_raw_str < F > ( literal_text : & str , callback : & mut F )
106
- where
107
- F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
108
- {
109
- unescape_raw_str_or_byte_str ( literal_text, Mode :: Str , callback)
97
+ /// Takes a contents of a char literal (without quotes), and returns an
98
+ /// unescaped char or an error
99
+ pub fn unescape_char ( literal_text : & str ) -> Result < char , ( usize , EscapeError ) > {
100
+ let mut chars = literal_text. chars ( ) ;
101
+ unescape_char_or_byte ( & mut chars, Mode :: Char )
102
+ . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
110
103
}
111
104
112
- /// Takes a contents of a raw byte string literal (without quotes) and produces a
113
- /// sequence of bytes or errors.
114
- /// Values are returned through invoking of the provided callback.
115
- /// NOTE: Raw strings do not perform any explicit character escaping, here we
116
- /// only translate CRLF to LF and produce errors on bare CR.
117
- pub fn unescape_raw_byte_str < F > ( literal_text : & str , callback : & mut F )
118
- where
119
- F : FnMut ( Range < usize > , Result < u8 , EscapeError > ) ,
120
- {
121
- unescape_raw_str_or_byte_str ( literal_text, Mode :: ByteStr , & mut |range, char| {
122
- callback ( range, char. map ( byte_from_char) )
123
- } )
105
+ /// Takes a contents of a byte literal (without quotes), and returns an
106
+ /// unescaped byte or an error.
107
+ pub fn unescape_byte ( literal_text : & str ) -> Result < u8 , ( usize , EscapeError ) > {
108
+ let mut chars = literal_text. chars ( ) ;
109
+ unescape_char_or_byte ( & mut chars, Mode :: Byte )
110
+ . map ( byte_from_char)
111
+ . map_err ( |err| ( literal_text. len ( ) - chars. as_str ( ) . len ( ) , err) )
124
112
}
125
113
126
114
/// What kind of literal do we parse.
@@ -130,13 +118,15 @@ pub enum Mode {
130
118
Str ,
131
119
Byte ,
132
120
ByteStr ,
121
+ RawStr ,
122
+ RawByteStr ,
133
123
}
134
124
135
125
impl Mode {
136
126
pub fn in_single_quotes ( self ) -> bool {
137
127
match self {
138
128
Mode :: Char | Mode :: Byte => true ,
139
- Mode :: Str | Mode :: ByteStr => false ,
129
+ Mode :: Str | Mode :: ByteStr | Mode :: RawStr | Mode :: RawByteStr => false ,
140
130
}
141
131
}
142
132
@@ -146,8 +136,8 @@ impl Mode {
146
136
147
137
pub fn is_bytes ( self ) -> bool {
148
138
match self {
149
- Mode :: Byte | Mode :: ByteStr => true ,
150
- Mode :: Char | Mode :: Str => false ,
139
+ Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
140
+ Mode :: Char | Mode :: Str | Mode :: RawStr => false ,
151
141
}
152
142
}
153
143
}
@@ -345,7 +335,7 @@ where
345
335
346
336
fn byte_from_char ( c : char ) -> u8 {
347
337
let res = c as u32 ;
348
- assert ! ( res <= u8 :: max_value( ) as u32 , "guaranteed because of Mode::Byte(Str) " ) ;
338
+ assert ! ( res <= u8 :: max_value( ) as u32 , "guaranteed because of Mode::ByteStr " ) ;
349
339
res as u8
350
340
}
351
341
0 commit comments