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