File tree 2 files changed +25
-7
lines changed
2 files changed +25
-7
lines changed Original file line number Diff line number Diff line change @@ -147,6 +147,7 @@ pub trait CharExt {
147
147
fn to_digit ( self , radix : u32 ) -> Option < u32 > ;
148
148
fn escape_unicode ( self ) -> EscapeUnicode ;
149
149
fn escape_default ( self ) -> EscapeDefault ;
150
+ fn needs_escape_default ( self ) -> bool ;
150
151
fn len_utf8 ( self ) -> usize ;
151
152
fn len_utf16 ( self ) -> usize ;
152
153
fn encode_utf8 ( self , dst : & mut [ u8 ] ) -> Option < usize > ;
@@ -194,6 +195,15 @@ impl CharExt for char {
194
195
EscapeDefault { state : init_state }
195
196
}
196
197
198
+ #[ inline]
199
+ fn needs_escape_default ( self ) -> bool {
200
+ match self {
201
+ '\\' | '\'' | '"' => true ,
202
+ '\x20' ... '\x7e' => false ,
203
+ _ => true
204
+ }
205
+ }
206
+
197
207
#[ inline]
198
208
fn len_utf8 ( self ) -> usize {
199
209
let code = self as u32 ;
Original file line number Diff line number Diff line change @@ -1310,11 +1310,20 @@ impl Display for bool {
1310
1310
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1311
1311
impl Debug for str {
1312
1312
fn fmt ( & self , f : & mut Formatter ) -> Result {
1313
- try!( write ! ( f, "\" " ) ) ;
1314
- for c in self . chars ( ) . flat_map ( |c| c. escape_default ( ) ) {
1315
- try!( f. write_char ( c) )
1313
+ try!( f. write_char ( '"' ) ) ;
1314
+ let mut from = 0 ;
1315
+ for ( i, c) in self . char_indices ( ) {
1316
+ // If char needs escaping, flush backlog so far and write, else skip
1317
+ if c. needs_escape_default ( ) {
1318
+ try!( f. write_str ( & self [ from..i] ) ) ;
1319
+ for e in c. escape_default ( ) {
1320
+ try!( f. write_char ( e) ) ;
1321
+ }
1322
+ from = i + c. len_utf8 ( ) ;
1323
+ }
1316
1324
}
1317
- write ! ( f, "\" " )
1325
+ try!( f. write_str ( & self [ from..] ) ) ;
1326
+ f. write_char ( '"' )
1318
1327
}
1319
1328
}
1320
1329
@@ -1328,12 +1337,11 @@ impl Display for str {
1328
1337
#[ stable( feature = "rust1" , since = "1.0.0" ) ]
1329
1338
impl Debug for char {
1330
1339
fn fmt ( & self , f : & mut Formatter ) -> Result {
1331
- use char:: CharExt ;
1332
- try!( write ! ( f, "'" ) ) ;
1340
+ try!( f. write_char ( '\'' ) ) ;
1333
1341
for c in self . escape_default ( ) {
1334
1342
try!( f. write_char ( c) )
1335
1343
}
1336
- write ! ( f , "'" )
1344
+ f . write_char ( '\'' )
1337
1345
}
1338
1346
}
1339
1347
You can’t perform that action at this time.
0 commit comments