@@ -44,7 +44,7 @@ pub use self::methods::encode_utf8_raw; // perma-unstable
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 , MaybeEscaped > ) ;
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
@@ -192,7 +192,7 @@ impl Iterator for EscapeUnicode {
192
192
193
193
#[ inline]
194
194
fn last ( mut self ) -> Option < char > {
195
- self . 0 . next_back ( ) . map ( char :: from )
195
+ self . 0 . next_back ( )
196
196
}
197
197
198
198
#[ inline]
@@ -214,8 +214,9 @@ impl FusedIterator for EscapeUnicode {}
214
214
215
215
#[ stable( feature = "char_struct_display" , since = "1.16.0" ) ]
216
216
impl fmt:: Display for EscapeUnicode {
217
+ #[ inline]
217
218
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
218
- f . write_str ( self . 0 . as_str ( ) )
219
+ fmt :: Display :: fmt ( & self . 0 , f )
219
220
}
220
221
}
221
222
@@ -227,22 +228,22 @@ impl fmt::Display for EscapeUnicode {
227
228
/// [`escape_default`]: char::escape_default
228
229
#[ derive( Clone , Debug ) ]
229
230
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
230
- pub struct EscapeDefault ( escape :: EscapeIterInner < 10 > ) ;
231
+ pub struct EscapeDefault ( EscapeIterInner < 10 , AlwaysEscaped > ) ;
231
232
232
233
impl EscapeDefault {
233
234
#[ inline]
234
235
const fn printable ( c : ascii:: Char ) -> Self {
235
- Self ( escape :: EscapeIterInner :: ascii ( c. to_u8 ( ) ) )
236
+ Self ( EscapeIterInner :: ascii ( c. to_u8 ( ) ) )
236
237
}
237
238
238
239
#[ inline]
239
240
const fn backslash ( c : ascii:: Char ) -> Self {
240
- Self ( escape :: EscapeIterInner :: backslash ( c) )
241
+ Self ( EscapeIterInner :: backslash ( c) )
241
242
}
242
243
243
244
#[ inline]
244
245
const fn unicode ( c : char ) -> Self {
245
- Self ( escape :: EscapeIterInner :: unicode ( c) )
246
+ Self ( EscapeIterInner :: unicode ( c) )
246
247
}
247
248
}
248
249
@@ -290,8 +291,9 @@ impl FusedIterator for EscapeDefault {}
290
291
291
292
#[ stable( feature = "char_struct_display" , since = "1.16.0" ) ]
292
293
impl fmt:: Display for EscapeDefault {
294
+ #[ inline]
293
295
fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
294
- f . write_str ( self . 0 . as_str ( ) )
296
+ fmt :: Display :: fmt ( & self . 0 , f )
295
297
}
296
298
}
297
299
@@ -303,37 +305,22 @@ impl fmt::Display for EscapeDefault {
303
305
/// [`escape_debug`]: char::escape_debug
304
306
#[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
305
307
#[ 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
- }
308
+ pub struct EscapeDebug ( EscapeIterInner < 10 , MaybeEscaped > ) ;
317
309
318
310
impl EscapeDebug {
319
311
#[ inline]
320
312
const fn printable ( chr : char ) -> Self {
321
- Self ( EscapeDebugInner :: Char ( chr) )
313
+ Self ( EscapeIterInner :: printable ( chr) )
322
314
}
323
315
324
316
#[ inline]
325
317
const fn backslash ( c : ascii:: Char ) -> Self {
326
- Self ( EscapeDebugInner :: Bytes ( escape :: EscapeIterInner :: backslash ( c) ) )
318
+ Self ( EscapeIterInner :: backslash ( c) )
327
319
}
328
320
329
321
#[ inline]
330
322
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 ( ) ) ;
323
+ Self ( EscapeIterInner :: unicode ( c) )
337
324
}
338
325
}
339
326
@@ -343,13 +330,7 @@ impl Iterator for EscapeDebug {
343
330
344
331
#[ inline]
345
332
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
- }
333
+ self . 0 . next ( )
353
334
}
354
335
355
336
#[ inline]
@@ -366,11 +347,9 @@ impl Iterator for EscapeDebug {
366
347
367
348
#[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
368
349
impl ExactSizeIterator for EscapeDebug {
350
+ #[ inline]
369
351
fn len ( & self ) -> usize {
370
- match & self . 0 {
371
- EscapeDebugInner :: Bytes ( bytes) => bytes. len ( ) ,
372
- EscapeDebugInner :: Char ( _) => 1 ,
373
- }
352
+ self . 0 . len ( )
374
353
}
375
354
}
376
355
@@ -379,11 +358,9 @@ impl FusedIterator for EscapeDebug {}
379
358
380
359
#[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
381
360
impl fmt:: Display for EscapeDebug {
361
+ #[ inline]
382
362
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
- }
363
+ fmt:: Display :: fmt ( & self . 0 , f)
387
364
}
388
365
}
389
366
@@ -480,6 +457,7 @@ macro_rules! casemappingiter_impls {
480
457
481
458
#[ stable( feature = "char_struct_display" , since = "1.16.0" ) ]
482
459
impl fmt:: Display for $ITER_NAME {
460
+ #[ inline]
483
461
fn fmt( & self , f: & mut fmt:: Formatter <' _>) -> fmt:: Result {
484
462
fmt:: Display :: fmt( & self . 0 , f)
485
463
}
0 commit comments