@@ -233,29 +233,66 @@ impl<const N: usize, ESCAPING> EscapeIterInner<N, ESCAPING> {
233
233
usize:: from ( self . alive . end - self . alive . start )
234
234
}
235
235
236
+ #[ inline]
236
237
pub ( crate ) fn advance_by ( & mut self , n : usize ) -> Result < ( ) , NonZero < usize > > {
237
238
self . alive . advance_by ( n)
238
239
}
239
240
241
+ #[ inline]
240
242
pub ( crate ) fn advance_back_by ( & mut self , n : usize ) -> Result < ( ) , NonZero < usize > > {
241
243
self . alive . advance_back_by ( n)
242
244
}
245
+
246
+ /// Returns a `char` if `self.data` contains one in its `literal` variant.
247
+ #[ inline]
248
+ const fn to_char ( & self ) -> Option < char > {
249
+ if self . alive . end > Self :: LITERAL_ESCAPE_START {
250
+ // SAFETY: We just checked that `self.data` contains a `char` in
251
+ // its `literal` variant.
252
+ return Some ( unsafe { self . data . literal } ) ;
253
+ }
254
+
255
+ None
256
+ }
257
+
258
+ /// Returns a string of printable ASCII characters in `self.data.escape_seq`.
259
+ ///
260
+ /// # Safety
261
+ ///
262
+ /// - `self.data` must contain printable ASCII characters in its `escape_seq` variant.
263
+ /// - `self.alive` must be a valid range for `self.data.escape_seq`.
264
+ #[ inline]
265
+ unsafe fn to_str_unchecked ( & self ) -> & str {
266
+ // SAFETY: The caller guarantees `self.data` contains printable ASCII
267
+ // characters in its `escape_seq` variant, and `self.alive` is
268
+ // a valid range for `self.data.escape_seq`.
269
+ unsafe {
270
+ self . data
271
+ . escape_seq
272
+ . get_unchecked ( usize:: from ( self . alive . start ) ..usize:: from ( self . alive . end ) )
273
+ . as_str ( )
274
+ }
275
+ }
243
276
}
244
277
245
278
impl < const N : usize > EscapeIterInner < N , AlwaysEscaped > {
246
279
pub ( crate ) fn next ( & mut self ) -> Option < u8 > {
247
280
let i = self . alive . next ( ) ?;
248
281
249
- // SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid,
250
- // and `i` is guaranteed to be a valid index for `self.data.escape_seq`.
282
+ // SAFETY: The `AlwaysEscaped` marker guarantees that `self.data`
283
+ // contains printable ASCII characters in its `escape_seq`
284
+ // variant, and `i` is guaranteed to be a valid index for
285
+ // `self.data.escape_seq`.
251
286
unsafe { Some ( self . data . escape_seq . get_unchecked ( usize:: from ( i) ) . to_u8 ( ) ) }
252
287
}
253
288
254
289
pub ( crate ) fn next_back ( & mut self ) -> Option < u8 > {
255
290
let i = self . alive . next_back ( ) ?;
256
291
257
- // SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid,
258
- // and `i` is guaranteed to be a valid index for `self.data.escape_seq`.
292
+ // SAFETY: The `AlwaysEscaped` marker guarantees that `self.data`
293
+ // contains printable ASCII characters in its `escape_seq`
294
+ // variant, and `i` is guaranteed to be a valid index for
295
+ // `self.data.escape_seq`.
259
296
unsafe { Some ( self . data . escape_seq . get_unchecked ( usize:: from ( i) ) . to_u8 ( ) ) }
260
297
}
261
298
}
@@ -276,63 +313,47 @@ impl<const N: usize> EscapeIterInner<N, MaybeEscaped> {
276
313
pub ( crate ) fn next ( & mut self ) -> Option < char > {
277
314
let i = self . alive . next ( ) ?;
278
315
279
- if self . alive . end > Self :: LITERAL_ESCAPE_START {
280
- // SAFETY: We just checked that `self.data.literal` is valid.
281
- return Some ( unsafe { self . data . literal } ) ;
316
+ if let Some ( c) = self . to_char ( ) {
317
+ return Some ( c) ;
282
318
}
283
319
284
- // SAFETY: At this point, `self.data.escape_seq` must be valid, and
285
- // `i` is guaranteed to be a valid index for `self.data.escape_seq`.
320
+ // SAFETY: At this point, `self.data` must contain printable ASCII
321
+ // characters in its `escape_seq` variant, and `i` is
322
+ // guaranteed to be a valid index for `self.data.escape_seq`.
286
323
Some ( char:: from ( unsafe { self . data . escape_seq . get_unchecked ( usize:: from ( i) ) . to_u8 ( ) } ) )
287
324
}
288
325
}
289
326
290
327
impl < const N : usize > fmt:: Display for EscapeIterInner < N , AlwaysEscaped > {
291
328
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
292
- // SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid,
293
- // and `self.alive` is guaranteed to be a valid range for `self.data.escape_seq`.
294
- f. write_str ( unsafe {
295
- self . data . escape_seq [ usize:: from ( self . alive . start ) ..usize:: from ( self . alive . end ) ]
296
- . as_str ( )
297
- } )
329
+ // SAFETY: The `AlwaysEscaped` marker guarantees that `self.data`
330
+ // contains printable ASCII chars, and `self.alive` is
331
+ // guaranteed to be a valid range for `self.data`.
332
+ f. write_str ( unsafe { self . to_str_unchecked ( ) } )
298
333
}
299
334
}
300
335
301
336
impl < const N : usize > fmt:: Display for EscapeIterInner < N , MaybeEscaped > {
302
337
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
303
- if self . alive . end > Self :: LITERAL_ESCAPE_START {
304
- // SAFETY: We just checked that `self.data.literal` is valid.
305
- return f. write_char ( unsafe { self . data . literal } ) ;
338
+ if let Some ( c) = self . to_char ( ) {
339
+ return f. write_char ( c) ;
306
340
}
307
341
308
- // SAFETY: At this point, `self.data.escape_seq` must be valid,
309
- // and `self.alive` is guaranteed to be a valid range for `self.data.escape_seq`.
310
- f. write_str ( unsafe {
311
- self . data . escape_seq [ usize:: from ( self . alive . start ) ..usize:: from ( self . alive . end ) ]
312
- . as_str ( )
313
- } )
342
+ // SAFETY: At this point, `self.data` must contain printable ASCII
343
+ // characters in its `escape_seq` variant, and `self.alive`
344
+ // is guaranteed to be a valid range for `self.data`.
345
+ f. write_str ( unsafe { self . to_str_unchecked ( ) } )
314
346
}
315
347
}
316
348
317
349
impl < const N : usize > fmt:: Debug for EscapeIterInner < N , AlwaysEscaped > {
318
350
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
319
- f. debug_tuple ( "EscapeIterInner" )
320
- // SAFETY: The `AlwaysEscaped` marker guarantees that `self.data.escape_seq` is valid.
321
- . field ( unsafe { & self . data . escape_seq } )
322
- . finish ( )
351
+ f. debug_tuple ( "EscapeIterInner" ) . field ( & format_args ! ( "{:?}" , self ) ) . finish ( )
323
352
}
324
353
}
325
354
326
355
impl < const N : usize > fmt:: Debug for EscapeIterInner < N , MaybeEscaped > {
327
356
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
328
- let mut d = f. debug_tuple ( "EscapeIterInner" ) ;
329
- if self . alive . end > Self :: LITERAL_ESCAPE_START {
330
- // SAFETY: We just checked that `self.data.literal` is valid.
331
- d. field ( unsafe { & self . data . literal } ) ;
332
- } else {
333
- // SAFETY: At this point, `self.data.escape_seq` must be valid.
334
- d. field ( unsafe { & self . data . escape_seq } ) ;
335
- }
336
- d. finish ( )
357
+ f. debug_tuple ( "EscapeIterInner" ) . field ( & format_args ! ( "{:?}" , self ) ) . finish ( )
337
358
}
338
359
}
0 commit comments