@@ -134,71 +134,75 @@ pub fn unescape_for_errors(
134
134
}
135
135
}
136
136
137
+ /// Takes the contents of a raw string literal (without quotes)
138
+ /// and produces a sequence of characters or errors,
139
+ /// which are returned by invoking `callback`.
140
+ /// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
137
141
pub fn check_raw_str ( src : & str , mut callback : impl FnMut ( Range < usize > , Result < char , EscapeError > ) ) {
138
- unescape_unicode ( src, Mode :: RawStr , & mut callback)
142
+ check_raw_common ( src, Mode :: RawStr , & mut callback)
139
143
}
140
144
145
+ /// Takes the contents of a raw byte string literal (without quotes)
146
+ /// and produces a sequence of bytes or errors,
147
+ /// which are returned by invoking `callback`.
148
+ /// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
141
149
pub fn check_raw_byte_str (
142
150
src : & str ,
143
151
mut callback : impl FnMut ( Range < usize > , Result < u8 , EscapeError > ) ,
144
152
) {
145
- unescape_unicode ( src, Mode :: RawByteStr , & mut |r, res| {
153
+ check_raw_common ( src, Mode :: RawByteStr , & mut |r, res| {
146
154
callback ( r, res. map ( byte_from_char) )
147
155
} )
148
156
}
149
157
158
+ /// Takes the contents of a raw C string literal (without quotes)
159
+ /// and produces a sequence of characters or errors,
160
+ /// which are returned by invoking `callback`.
161
+ /// NOTE: Does no escaping, but produces errors for bare carriage return ('\r').
150
162
pub fn check_raw_c_str (
151
163
src : & str ,
152
164
mut callback : impl FnMut ( Range < usize > , Result < char , EscapeError > ) ,
153
165
) {
154
- unescape_unicode ( src, Mode :: RawCStr , & mut callback)
166
+ check_raw_common ( src, Mode :: RawCStr , & mut |r, mut result| {
167
+ if let Ok ( '\0' ) = result {
168
+ result = Err ( EscapeError :: NulInCStr ) ;
169
+ }
170
+ callback ( r, result)
171
+ } )
155
172
}
156
173
174
+ /// Takes the contents of a string literal (without quotes)
175
+ /// and produces a sequence of escaped characters or errors,
176
+ /// which are returned by invoking `callback`.
157
177
pub fn unescape_str ( src : & str , mut callback : impl FnMut ( Range < usize > , Result < char , EscapeError > ) ) {
158
- unescape_unicode ( src, Mode :: Str , & mut callback)
178
+ unescape_non_raw_common ( src, Mode :: Str , & mut callback)
159
179
}
160
180
181
+ /// Takes the contents of a byte string literal (without quotes)
182
+ /// and produces a sequence of escaped bytes or errors,
183
+ /// which are returned by invoking `callback`.
161
184
pub fn unescape_byte_str (
162
185
src : & str ,
163
186
mut callback : impl FnMut ( Range < usize > , Result < u8 , EscapeError > ) ,
164
187
) {
165
- unescape_unicode ( src, Mode :: ByteStr , & mut |r, res| {
188
+ unescape_non_raw_common ( src, Mode :: ByteStr , & mut |r, res| {
166
189
callback ( r, res. map ( byte_from_char) )
167
190
} )
168
191
}
169
192
193
+ /// Takes the contents of a C string literal (without quotes)
194
+ /// and produces a sequence of escaped MixedUnits or errors,
195
+ /// which are returned by invoking `callback`.
170
196
pub fn unescape_c_str (
171
197
src : & str ,
172
198
mut callback : impl FnMut ( Range < usize > , Result < MixedUnit , EscapeError > ) ,
173
199
) {
174
- unescape_mixed ( src, Mode :: CStr , & mut callback)
175
- }
176
-
177
- /// Takes the contents of a unicode-only (non-mixed-utf8) literal (without
178
- /// quotes) and produces a sequence of escaped characters or errors.
179
- ///
180
- /// Values are returned by invoking `callback`. For `Char` and `Byte` modes,
181
- /// the callback will be called exactly once.
182
- fn unescape_unicode < F > ( src : & str , mode : Mode , callback : & mut F )
183
- where
184
- F : FnMut ( Range < usize > , Result < char , EscapeError > ) ,
185
- {
186
- match mode {
187
- Char | Byte => {
188
- let mut chars = src. chars ( ) ;
189
- let res = unescape_char_or_byte ( & mut chars, mode) ;
190
- callback ( 0 ..( src. len ( ) - chars. as_str ( ) . len ( ) ) , res) ;
200
+ unescape_non_raw_common ( src, Mode :: CStr , & mut |r, mut result| {
201
+ if let Ok ( MixedUnit :: Char ( '\0' ) ) = result {
202
+ result = Err ( EscapeError :: NulInCStr ) ;
191
203
}
192
- Str | ByteStr => unescape_non_raw_common ( src, mode, callback) ,
193
- RawStr | RawByteStr => check_raw_common ( src, mode, callback) ,
194
- RawCStr => check_raw_common ( src, mode, & mut |r, mut result| {
195
- if let Ok ( '\0' ) = result {
196
- result = Err ( EscapeError :: NulInCStr ) ;
197
- }
198
- callback ( r, result)
199
- } ) ,
200
- CStr => unreachable ! ( ) ,
201
- }
204
+ callback ( r, result)
205
+ } )
202
206
}
203
207
204
208
/// Used for mixed utf8 string literals, i.e. those that allow both unicode
@@ -236,25 +240,6 @@ impl From<u8> for MixedUnit {
236
240
}
237
241
}
238
242
239
- /// Takes the contents of a mixed-utf8 literal (without quotes) and produces
240
- /// a sequence of escaped characters or errors.
241
- ///
242
- /// Values are returned by invoking `callback`.
243
- fn unescape_mixed < F > ( src : & str , mode : Mode , callback : & mut F )
244
- where
245
- F : FnMut ( Range < usize > , Result < MixedUnit , EscapeError > ) ,
246
- {
247
- match mode {
248
- CStr => unescape_non_raw_common ( src, mode, & mut |r, mut result| {
249
- if let Ok ( MixedUnit :: Char ( '\0' ) ) = result {
250
- result = Err ( EscapeError :: NulInCStr ) ;
251
- }
252
- callback ( r, result)
253
- } ) ,
254
- Char | Byte | Str | RawStr | ByteStr | RawByteStr | RawCStr => unreachable ! ( ) ,
255
- }
256
- }
257
-
258
243
/// Takes a contents of a char literal (without quotes), and returns an
259
244
/// unescaped char or an error.
260
245
pub fn unescape_char ( src : & str ) -> Result < char , EscapeError > {
0 commit comments