@@ -472,6 +472,14 @@ fn format_comp(comp: &ComprehensionChunk) -> String {
472
472
}
473
473
}
474
474
475
+ fn format_float ( n : f64 ) -> String {
476
+ let mut s = n. to_string ( ) ;
477
+ if s. find ( '.' ) . is_none ( ) {
478
+ s. push_str ( "." ) ;
479
+ }
480
+ s
481
+ }
482
+
475
483
fn format_expr ( e : & Expression ) -> String {
476
484
match e {
477
485
Expression :: Ellipsis => "..." . to_string ( ) ,
@@ -481,11 +489,20 @@ fn format_expr(e: &Expression) -> String {
481
489
Expression :: Name ( ref n) => n. to_string ( ) ,
482
490
Expression :: Int ( ref n) => n. to_string ( ) ,
483
491
Expression :: ImaginaryInt ( ref n) => format ! ( "{}j" , n) ,
484
- Expression :: Float ( ref n) => n . to_string ( ) ,
485
- Expression :: ImaginaryFloat ( ref n) => format ! ( "{}j" , n ) ,
492
+ Expression :: Float ( ref n) => format_float ( * n ) ,
493
+ Expression :: ImaginaryFloat ( ref n) => format ! ( "{}j" , format_float ( * n ) ) ,
486
494
Expression :: String ( ref v) => {
487
495
space_join ( v. iter ( ) . map ( |PyString { prefix, content } |
488
- format ! ( "{}{:?}" , prefix, content) // FIXME: that's cheating
496
+ format ! ( "{}\" {}\" " , prefix, content. chars( ) . map( |c| match c {
497
+ '\r' => "\\ r" . to_string( ) ,
498
+ '\n' => "\\ n" . to_string( ) ,
499
+ '\t' => "\\ t" . to_string( ) ,
500
+ '\\' => "\\ \\ " . to_string( ) ,
501
+ '"' => "\\ \" " . to_string( ) ,
502
+ c if c. is_ascii( ) => c. to_string( ) ,
503
+ c if ( c as u32 ) <= 0xffff => format!( "\\ u{:04}" , c as u32 ) ,
504
+ c => format!( "\\ U{:08}" , c as u32 ) ,
505
+ } ) . collect:: <Vec <_>>( ) [ ..] . concat( ) )
489
506
) )
490
507
} ,
491
508
Expression :: Bytes ( ref b) => {
0 commit comments