@@ -264,6 +264,8 @@ pub trait CharExt {
264264 fn escape_unicode ( self ) -> EscapeUnicode ;
265265 #[ stable( feature = "core" , since = "1.6.0" ) ]
266266 fn escape_default ( self ) -> EscapeDefault ;
267+ #[ unstable( feature = "char_escape" , issue = "0" ) ]
268+ fn escape ( self ) -> Escape ;
267269 #[ stable( feature = "core" , since = "1.6.0" ) ]
268270 fn len_utf8 ( self ) -> usize ;
269271 #[ stable( feature = "core" , since = "1.6.0" ) ]
@@ -316,6 +318,19 @@ impl CharExt for char {
316318
317319 #[ inline]
318320 fn escape_default ( self ) -> EscapeDefault {
321+ let init_state = match self {
322+ '\t' => EscapeDefaultState :: Backslash ( 't' ) ,
323+ '\r' => EscapeDefaultState :: Backslash ( 'r' ) ,
324+ '\n' => EscapeDefaultState :: Backslash ( 'n' ) ,
325+ '\\' | '\'' | '"' => EscapeDefaultState :: Backslash ( self ) ,
326+ '\x20' ... '\x7e' => EscapeDefaultState :: Char ( self ) ,
327+ _ => EscapeDefaultState :: Unicode ( self . escape_unicode ( ) )
328+ } ;
329+ EscapeDefault { state : init_state }
330+ }
331+
332+ #[ inline]
333+ fn escape ( self ) -> Escape {
319334 let init_state = match self {
320335 '\t' => EscapeDefaultState :: Backslash ( 't' ) ,
321336 '\r' => EscapeDefaultState :: Backslash ( 'r' ) ,
@@ -324,7 +339,7 @@ impl CharExt for char {
324339 c if is_printable ( c) => EscapeDefaultState :: Char ( c) ,
325340 c => EscapeDefaultState :: Unicode ( c. escape_unicode ( ) ) ,
326341 } ;
327- EscapeDefault { state : init_state }
342+ Escape ( EscapeDefault { state : init_state } )
328343 }
329344
330345 #[ inline]
@@ -601,6 +616,27 @@ impl ExactSizeIterator for EscapeDefault {
601616 }
602617}
603618
619+ /// An iterator that yields the literal escape code of a `char`.
620+ ///
621+ /// This `struct` is created by the [`escape()`] method on [`char`]. See its
622+ /// documentation for more.
623+ ///
624+ /// [`escape()`]: ../../std/primitive.char.html#method.escape
625+ /// [`char`]: ../../std/primitive.char.html
626+ #[ unstable( feature = "char_escape" , issue = "0" ) ]
627+ #[ derive( Clone , Debug ) ]
628+ pub struct Escape ( EscapeDefault ) ;
629+
630+ #[ unstable( feature = "char_escape" , issue = "0" ) ]
631+ impl Iterator for Escape {
632+ type Item = char ;
633+ fn next ( & mut self ) -> Option < char > { self . 0 . next ( ) }
634+ fn size_hint ( & self ) -> ( usize , Option < usize > ) { self . 0 . size_hint ( ) }
635+ }
636+
637+ #[ unstable( feature = "char_escape" , issue = "0" ) ]
638+ impl ExactSizeIterator for Escape { }
639+
604640/// An iterator over `u8` entries represending the UTF-8 encoding of a `char`
605641/// value.
606642///
0 commit comments