@@ -403,16 +403,20 @@ impl char {
403403 }
404404
405405 /// An extended version of `escape_debug` that optionally permits escaping
406- /// Extended Grapheme codepoints. This allows us to format characters like
407- /// nonspacing marks better when they're at the start of a string.
406+ /// Extended Grapheme codepoints, single quotes, and double quotes. This
407+ /// allows us to format characters like nonspacing marks better when they're
408+ /// at the start of a string, and allows escaping single quotes in
409+ /// characters, and double quotes in strings.
408410 #[ inline]
409- pub ( crate ) fn escape_debug_ext ( self , escape_grapheme_extended : bool ) -> EscapeDebug {
411+ pub ( crate ) fn escape_debug_ext ( self , args : EscapeDebugExtArgs ) -> EscapeDebug {
410412 let init_state = match self {
411413 '\t' => EscapeDefaultState :: Backslash ( 't' ) ,
412414 '\r' => EscapeDefaultState :: Backslash ( 'r' ) ,
413415 '\n' => EscapeDefaultState :: Backslash ( 'n' ) ,
414- '\\' | '\'' | '"' => EscapeDefaultState :: Backslash ( self ) ,
415- _ if escape_grapheme_extended && self . is_grapheme_extended ( ) => {
416+ '\\' => EscapeDefaultState :: Backslash ( self ) ,
417+ '"' if args. escape_double_quote => EscapeDefaultState :: Backslash ( self ) ,
418+ '\'' if args. escape_single_quote => EscapeDefaultState :: Backslash ( self ) ,
419+ _ if args. escape_grapheme_extended && self . is_grapheme_extended ( ) => {
416420 EscapeDefaultState :: Unicode ( self . escape_unicode ( ) )
417421 }
418422 _ if is_printable ( self ) => EscapeDefaultState :: Char ( self ) ,
@@ -458,7 +462,7 @@ impl char {
458462 #[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
459463 #[ inline]
460464 pub fn escape_debug ( self ) -> EscapeDebug {
461- self . escape_debug_ext ( true )
465+ self . escape_debug_ext ( EscapeDebugExtArgs :: ESCAPE_ALL )
462466 }
463467
464468 /// Returns an iterator that yields the literal escape code of a character
@@ -1565,6 +1569,25 @@ impl char {
15651569 }
15661570}
15671571
1572+ pub ( crate ) struct EscapeDebugExtArgs {
1573+ /// Escape Extended Grapheme codepoints?
1574+ pub ( crate ) escape_grapheme_extended : bool ,
1575+
1576+ /// Escape single quotes?
1577+ pub ( crate ) escape_single_quote : bool ,
1578+
1579+ /// Escape double quotes?
1580+ pub ( crate ) escape_double_quote : bool ,
1581+ }
1582+
1583+ impl EscapeDebugExtArgs {
1584+ pub ( crate ) const ESCAPE_ALL : Self = Self {
1585+ escape_grapheme_extended : true ,
1586+ escape_single_quote : true ,
1587+ escape_double_quote : true ,
1588+ } ;
1589+ }
1590+
15681591#[ inline]
15691592const fn len_utf8 ( code : u32 ) -> usize {
15701593 if code < MAX_ONE_B {
0 commit comments