4
4
use std:: ops:: Range ;
5
5
use std:: str:: Chars ;
6
6
7
+ use Mode :: * ;
8
+
7
9
#[ cfg( test) ]
8
10
mod tests;
9
11
@@ -84,15 +86,14 @@ where
84
86
F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
85
87
{
86
88
match mode {
87
- Mode :: Char | Mode :: Byte => {
89
+ Char | Byte => {
88
90
let mut chars = src. chars ( ) ;
89
91
let res = unescape_char_or_byte ( & mut chars, mode) ;
90
92
callback ( 0 ..( src. len ( ) - chars. as_str ( ) . len ( ) ) , res) ;
91
93
}
92
- Mode :: Str | Mode :: ByteStr => unescape_str_common ( src, mode, callback) ,
93
-
94
- Mode :: RawStr | Mode :: RawByteStr => unescape_raw_str_or_raw_byte_str ( src, mode, callback) ,
95
- Mode :: CStr | Mode :: RawCStr => unreachable ! ( ) ,
94
+ Str | ByteStr => unescape_str_common ( src, mode, callback) ,
95
+ RawStr | RawByteStr => unescape_raw_str_or_raw_byte_str ( src, mode, callback) ,
96
+ CStr | RawCStr => unreachable ! ( ) ,
96
97
}
97
98
}
98
99
@@ -118,84 +119,87 @@ pub fn unescape_c_string<F>(src: &str, mode: Mode, callback: &mut F)
118
119
where
119
120
F : FnMut ( Range < usize > , Result < CStrUnit , EscapeError > ) ,
120
121
{
121
- if mode == Mode :: RawCStr {
122
- unescape_raw_str_or_raw_byte_str ( src, mode, & mut |r, result| {
123
- callback ( r, result. map ( CStrUnit :: Char ) )
124
- } ) ;
125
- } else {
126
- unescape_str_common ( src, mode, callback) ;
122
+ match mode {
123
+ CStr => {
124
+ unescape_str_common ( src, mode, callback) ;
125
+ }
126
+ RawCStr => {
127
+ unescape_raw_str_or_raw_byte_str ( src, mode, & mut |r, result| {
128
+ callback ( r, result. map ( CStrUnit :: Char ) )
129
+ } ) ;
130
+ }
131
+ Char | Byte | Str | RawStr | ByteStr | RawByteStr => unreachable ! ( ) ,
127
132
}
128
133
}
129
134
130
135
/// Takes a contents of a char literal (without quotes), and returns an
131
136
/// unescaped char or an error.
132
137
pub fn unescape_char ( src : & str ) -> Result < char , EscapeError > {
133
- unescape_char_or_byte ( & mut src. chars ( ) , Mode :: Char )
138
+ unescape_char_or_byte ( & mut src. chars ( ) , Char )
134
139
}
135
140
136
141
/// Takes a contents of a byte literal (without quotes), and returns an
137
142
/// unescaped byte or an error.
138
143
pub fn unescape_byte ( src : & str ) -> Result < u8 , EscapeError > {
139
- unescape_char_or_byte ( & mut src. chars ( ) , Mode :: Byte ) . map ( byte_from_char)
144
+ unescape_char_or_byte ( & mut src. chars ( ) , Byte ) . map ( byte_from_char)
140
145
}
141
146
142
147
/// What kind of literal do we parse.
143
148
#[ derive( Debug , Clone , Copy , PartialEq ) ]
144
149
pub enum Mode {
145
150
Char ,
146
- Str ,
151
+
147
152
Byte ,
148
- ByteStr ,
153
+
154
+ Str ,
149
155
RawStr ,
156
+
157
+ ByteStr ,
150
158
RawByteStr ,
159
+
151
160
CStr ,
152
161
RawCStr ,
153
162
}
154
163
155
164
impl Mode {
156
165
pub fn in_double_quotes ( self ) -> bool {
157
166
match self {
158
- Mode :: Str
159
- | Mode :: ByteStr
160
- | Mode :: RawStr
161
- | Mode :: RawByteStr
162
- | Mode :: CStr
163
- | Mode :: RawCStr => true ,
164
- Mode :: Char | Mode :: Byte => false ,
167
+ Str | RawStr | ByteStr | RawByteStr | CStr | RawCStr => true ,
168
+ Char | Byte => false ,
165
169
}
166
170
}
167
171
168
172
/// Non-byte literals should have `\xXX` escapes that are within the ASCII range.
169
173
fn ascii_escapes_should_be_ascii ( self ) -> bool {
170
174
match self {
171
- Mode :: Char | Mode :: Str => true ,
172
- Mode :: Byte | Mode :: ByteStr | Mode :: CStr => false ,
173
- Mode :: RawStr | Mode :: RawByteStr | Mode :: RawCStr => unreachable ! ( ) ,
175
+ Char | Str => true ,
176
+ Byte | ByteStr | CStr => false ,
177
+ RawStr | RawByteStr | RawCStr => unreachable ! ( ) ,
174
178
}
175
179
}
176
180
177
- /// Whether characters within the literal must be within the ASCII range
181
+ /// Whether characters within the literal must be within the ASCII range.
178
182
#[ inline]
179
183
fn chars_should_be_ascii ( self ) -> bool {
180
184
match self {
181
- Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
182
- Mode :: Char | Mode :: Str | Mode :: RawStr | Mode :: CStr | Mode :: RawCStr => false ,
185
+ Byte | ByteStr | RawByteStr => true ,
186
+ Char | Str | RawStr | CStr | RawCStr => false ,
183
187
}
184
188
}
185
189
186
190
/// Byte literals do not allow unicode escape.
187
191
fn is_unicode_escape_disallowed ( self ) -> bool {
188
192
match self {
189
- Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
190
- Mode :: Char | Mode :: Str | Mode :: RawStr | Mode :: CStr | Mode :: RawCStr => false ,
193
+ Byte | ByteStr | RawByteStr => true ,
194
+ Char | Str | RawStr | CStr | RawCStr => false ,
191
195
}
192
196
}
193
197
194
198
pub fn prefix_noraw ( self ) -> & ' static str {
195
199
match self {
196
- Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => "b " ,
197
- Mode :: CStr | Mode :: RawCStr => "c " ,
198
- Mode :: Char | Mode :: Str | Mode :: RawStr => "" ,
200
+ Char | Str | RawStr => "" ,
201
+ Byte | ByteStr | RawByteStr => "b " ,
202
+ CStr | RawCStr => "c " ,
199
203
}
200
204
}
201
205
}
@@ -410,7 +414,7 @@ where
410
414
#[ inline]
411
415
pub fn byte_from_char ( c : char ) -> u8 {
412
416
let res = c as u32 ;
413
- debug_assert ! ( res <= u8 :: MAX as u32 , "guaranteed because of Mode:: ByteStr" ) ;
417
+ debug_assert ! ( res <= u8 :: MAX as u32 , "guaranteed because of ByteStr" ) ;
414
418
res as u8
415
419
}
416
420
0 commit comments