@@ -44,7 +44,7 @@ pub use self::methods::{encode_utf8_raw, encode_utf8_raw_unchecked}; // perma-un
44
44
use crate :: ascii;
45
45
pub ( crate ) use self :: methods:: EscapeDebugExtArgs ;
46
46
use crate :: error:: Error ;
47
- use crate :: escape;
47
+ use crate :: escape:: { AlwaysEscaped , EscapeIterInner , MaybeEscaped } ;
48
48
use crate :: fmt:: { self , Write } ;
49
49
use crate :: iter:: { FusedIterator , TrustedLen , TrustedRandomAccess , TrustedRandomAccessNoCoerce } ;
50
50
use crate :: num:: NonZero ;
@@ -161,12 +161,12 @@ pub const fn from_digit(num: u32, radix: u32) -> Option<char> {
161
161
/// [`escape_unicode`]: char::escape_unicode
162
162
#[ derive( Clone , Debug ) ]
163
163
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
164
- pub struct EscapeUnicode ( escape :: EscapeIterInner < 10 > ) ;
164
+ pub struct EscapeUnicode ( EscapeIterInner < 10 , AlwaysEscaped > ) ;
165
165
166
166
impl EscapeUnicode {
167
167
#[ inline]
168
168
const fn new ( c : char ) -> Self {
169
- Self ( escape :: EscapeIterInner :: unicode ( c) )
169
+ Self ( EscapeIterInner :: unicode ( c) )
170
170
}
171
171
}
172
172
@@ -215,7 +215,7 @@ impl FusedIterator for EscapeUnicode {}
215
215
#[ stable( feature = "char_struct_display" , since = "1.16.0" ) ]
216
216
impl fmt:: Display for EscapeUnicode {
217
217
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
218
- f . write_str ( self . 0 . as_str ( ) )
218
+ fmt :: Display :: fmt ( & self . 0 , f )
219
219
}
220
220
}
221
221
@@ -227,22 +227,22 @@ impl fmt::Display for EscapeUnicode {
227
227
/// [`escape_default`]: char::escape_default
228
228
#[ derive( Clone , Debug ) ]
229
229
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
230
- pub struct EscapeDefault ( escape :: EscapeIterInner < 10 > ) ;
230
+ pub struct EscapeDefault ( EscapeIterInner < 10 , AlwaysEscaped > ) ;
231
231
232
232
impl EscapeDefault {
233
233
#[ inline]
234
234
const fn printable ( c : ascii:: Char ) -> Self {
235
- Self ( escape :: EscapeIterInner :: ascii ( c. to_u8 ( ) ) )
235
+ Self ( EscapeIterInner :: ascii ( c. to_u8 ( ) ) )
236
236
}
237
237
238
238
#[ inline]
239
239
const fn backslash ( c : ascii:: Char ) -> Self {
240
- Self ( escape :: EscapeIterInner :: backslash ( c) )
240
+ Self ( EscapeIterInner :: backslash ( c) )
241
241
}
242
242
243
243
#[ inline]
244
244
const fn unicode ( c : char ) -> Self {
245
- Self ( escape :: EscapeIterInner :: unicode ( c) )
245
+ Self ( EscapeIterInner :: unicode ( c) )
246
246
}
247
247
}
248
248
@@ -290,8 +290,9 @@ impl FusedIterator for EscapeDefault {}
290
290
291
291
#[ stable( feature = "char_struct_display" , since = "1.16.0" ) ]
292
292
impl fmt:: Display for EscapeDefault {
293
+ #[ inline]
293
294
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
294
- f . write_str ( self . 0 . as_str ( ) )
295
+ fmt :: Display :: fmt ( & self . 0 , f )
295
296
}
296
297
}
297
298
@@ -303,37 +304,22 @@ impl fmt::Display for EscapeDefault {
303
304
/// [`escape_debug`]: char::escape_debug
304
305
#[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
305
306
#[ derive( Clone , Debug ) ]
306
- pub struct EscapeDebug ( EscapeDebugInner ) ;
307
-
308
- #[ derive( Clone , Debug ) ]
309
- // Note: It’s possible to manually encode the EscapeDebugInner inside of
310
- // EscapeIterInner (e.g. with alive=254..255 indicating that data[0..4] holds
311
- // a char) which would likely result in a more optimised code. For now we use
312
- // the option easier to implement.
313
- enum EscapeDebugInner {
314
- Bytes ( escape:: EscapeIterInner < 10 > ) ,
315
- Char ( char ) ,
316
- }
307
+ pub struct EscapeDebug ( EscapeIterInner < 10 , MaybeEscaped > ) ;
317
308
318
309
impl EscapeDebug {
319
310
#[ inline]
320
311
const fn printable ( chr : char ) -> Self {
321
- Self ( EscapeDebugInner :: Char ( chr) )
312
+ Self ( EscapeIterInner :: printable ( chr) )
322
313
}
323
314
324
315
#[ inline]
325
316
const fn backslash ( c : ascii:: Char ) -> Self {
326
- Self ( EscapeDebugInner :: Bytes ( escape :: EscapeIterInner :: backslash ( c) ) )
317
+ Self ( EscapeIterInner :: backslash ( c) )
327
318
}
328
319
329
320
#[ inline]
330
321
const fn unicode ( c : char ) -> Self {
331
- Self ( EscapeDebugInner :: Bytes ( escape:: EscapeIterInner :: unicode ( c) ) )
332
- }
333
-
334
- #[ inline]
335
- fn clear ( & mut self ) {
336
- self . 0 = EscapeDebugInner :: Bytes ( escape:: EscapeIterInner :: empty ( ) ) ;
322
+ Self ( EscapeIterInner :: unicode ( c) )
337
323
}
338
324
}
339
325
@@ -343,13 +329,7 @@ impl Iterator for EscapeDebug {
343
329
344
330
#[ inline]
345
331
fn next ( & mut self ) -> Option < char > {
346
- match self . 0 {
347
- EscapeDebugInner :: Bytes ( ref mut bytes) => bytes. next ( ) . map ( char:: from) ,
348
- EscapeDebugInner :: Char ( chr) => {
349
- self . clear ( ) ;
350
- Some ( chr)
351
- }
352
- }
332
+ self . 0 . next ( )
353
333
}
354
334
355
335
#[ inline]
@@ -367,10 +347,7 @@ impl Iterator for EscapeDebug {
367
347
#[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
368
348
impl ExactSizeIterator for EscapeDebug {
369
349
fn len ( & self ) -> usize {
370
- match & self . 0 {
371
- EscapeDebugInner :: Bytes ( bytes) => bytes. len ( ) ,
372
- EscapeDebugInner :: Char ( _) => 1 ,
373
- }
350
+ self . 0 . len ( )
374
351
}
375
352
}
376
353
@@ -379,11 +356,9 @@ impl FusedIterator for EscapeDebug {}
379
356
380
357
#[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
381
358
impl fmt:: Display for EscapeDebug {
359
+ #[ inline]
382
360
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
383
- match & self . 0 {
384
- EscapeDebugInner :: Bytes ( bytes) => f. write_str ( bytes. as_str ( ) ) ,
385
- EscapeDebugInner :: Char ( chr) => f. write_char ( * chr) ,
386
- }
361
+ fmt:: Display :: fmt ( & self . 0 , f)
387
362
}
388
363
}
389
364
@@ -480,6 +455,7 @@ macro_rules! casemappingiter_impls {
480
455
481
456
#[ stable( feature = "char_struct_display" , since = "1.16.0" ) ]
482
457
impl fmt:: Display for $ITER_NAME {
458
+ #[ inline]
483
459
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
484
460
fmt:: Display :: fmt( & self . 0 , f)
485
461
}
0 commit comments