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,86 @@ 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 | ByteStr | RawStr | 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 ,
153
+
154
+ Str ,
148
155
ByteStr ,
156
+ CStr ,
157
+
149
158
RawStr ,
150
159
RawByteStr ,
151
- CStr ,
152
160
RawCStr ,
153
161
}
154
162
155
163
impl Mode {
156
164
pub fn in_double_quotes ( self ) -> bool {
157
165
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 ,
166
+ Str | ByteStr | CStr | RawStr | RawByteStr | RawCStr => true ,
167
+ Char | Byte => false ,
165
168
}
166
169
}
167
170
168
171
/// Non-byte literals should have `\xXX` escapes that are within the ASCII range.
169
172
fn ascii_escapes_should_be_ascii ( self ) -> bool {
170
173
match self {
171
- Mode :: Char | Mode :: Str => true ,
172
- Mode :: Byte | Mode :: ByteStr | Mode :: CStr => false ,
173
- Mode :: RawStr | Mode :: RawByteStr | Mode :: RawCStr => unreachable ! ( ) ,
174
+ Char | Str => true ,
175
+ Byte | ByteStr | CStr => false ,
176
+ RawStr | RawByteStr | RawCStr => unreachable ! ( ) ,
174
177
}
175
178
}
176
179
177
- /// Whether characters within the literal must be within the ASCII range
180
+ /// Whether characters within the literal must be within the ASCII range.
178
181
#[ inline]
179
182
fn chars_should_be_ascii ( self ) -> bool {
180
183
match self {
181
- Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
182
- Mode :: Char | Mode :: Str | Mode :: RawStr | Mode :: CStr | Mode :: RawCStr => false ,
184
+ Byte | ByteStr | RawByteStr => true ,
185
+ Char | Str | CStr | RawStr | RawCStr => false ,
183
186
}
184
187
}
185
188
186
189
/// Byte literals do not allow unicode escape.
187
190
fn is_unicode_escape_disallowed ( self ) -> bool {
188
191
match self {
189
- Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => true ,
190
- Mode :: Char | Mode :: Str | Mode :: RawStr | Mode :: CStr | Mode :: RawCStr => false ,
192
+ Byte | ByteStr | RawByteStr => true ,
193
+ Char | Str | CStr | RawStr | RawCStr => false ,
191
194
}
192
195
}
193
196
194
197
pub fn prefix_noraw ( self ) -> & ' static str {
195
198
match self {
196
- Mode :: Byte | Mode :: ByteStr | Mode :: RawByteStr => "b " ,
197
- Mode :: CStr | Mode :: RawCStr => "c " ,
198
- Mode :: Char | Mode :: Str | Mode :: RawStr => "" ,
199
+ Char | Str | RawStr => "" ,
200
+ Byte | ByteStr | RawByteStr => "b " ,
201
+ CStr | RawCStr => "c " ,
199
202
}
200
203
}
201
204
}
@@ -410,7 +413,7 @@ where
410
413
#[ inline]
411
414
pub fn byte_from_char ( c : char ) -> u8 {
412
415
let res = c as u32 ;
413
- debug_assert ! ( res <= u8 :: MAX as u32 , "guaranteed because of Mode:: ByteStr" ) ;
416
+ debug_assert ! ( res <= u8 :: MAX as u32 , "guaranteed because of ByteStr" ) ;
414
417
res as u8
415
418
}
416
419
0 commit comments