@@ -403,16 +403,20 @@ impl char {
403
403
}
404
404
405
405
/// 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.
408
410
#[ 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 {
410
412
let init_state = match self {
411
413
'\t' => EscapeDefaultState :: Backslash ( 't' ) ,
412
414
'\r' => EscapeDefaultState :: Backslash ( 'r' ) ,
413
415
'\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 ( ) => {
416
420
EscapeDefaultState :: Unicode ( self . escape_unicode ( ) )
417
421
}
418
422
_ if is_printable ( self ) => EscapeDefaultState :: Char ( self ) ,
@@ -458,7 +462,7 @@ impl char {
458
462
#[ stable( feature = "char_escape_debug" , since = "1.20.0" ) ]
459
463
#[ inline]
460
464
pub fn escape_debug ( self ) -> EscapeDebug {
461
- self . escape_debug_ext ( true )
465
+ self . escape_debug_ext ( EscapeDebugExtArgs :: ESCAPE_ALL )
462
466
}
463
467
464
468
/// Returns an iterator that yields the literal escape code of a character
@@ -1565,6 +1569,25 @@ impl char {
1565
1569
}
1566
1570
}
1567
1571
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
+
1568
1591
#[ inline]
1569
1592
const fn len_utf8 ( code : u32 ) -> usize {
1570
1593
if code < MAX_ONE_B {
0 commit comments