@@ -958,12 +958,20 @@ impl Literal {
958958 pub fn string ( t : & str ) -> Literal {
959959 let mut repr = String :: with_capacity ( t. len ( ) + 2 ) ;
960960 repr. push ( '"' ) ;
961- for c in t. chars ( ) {
962- if c == '\'' {
961+ let mut chars = t. chars ( ) ;
962+ while let Some ( ch) = chars. next ( ) {
963+ if ch == '\0'
964+ && chars
965+ . as_str ( )
966+ . starts_with ( |next| '0' <= next && next <= '7' )
967+ {
968+ // circumvent clippy::octal_escapes lint
969+ repr. push_str ( "\\ x00" ) ;
970+ } else if ch == '\'' {
963971 // escape_debug turns this into "\'" which is unnecessary.
964- repr. push ( c ) ;
972+ repr. push ( ch ) ;
965973 } else {
966- repr. extend ( c . escape_debug ( ) ) ;
974+ repr. extend ( ch . escape_debug ( ) ) ;
967975 }
968976 }
969977 repr. push ( '"' ) ;
@@ -985,16 +993,21 @@ impl Literal {
985993
986994 pub fn byte_string ( bytes : & [ u8 ] ) -> Literal {
987995 let mut escaped = "b\" " . to_string ( ) ;
988- for b in bytes {
996+ let mut bytes = bytes. iter ( ) ;
997+ while let Some ( & b) = bytes. next ( ) {
989998 #[ allow( clippy:: match_overlapping_arm) ]
990- match * b {
991- b'\0' => escaped. push_str ( r"\0" ) ,
999+ match b {
1000+ b'\0' => escaped. push_str ( match bytes. as_slice ( ) . first ( ) {
1001+ // circumvent clippy::octal_escapes lint
1002+ Some ( b'0' ..=b'7' ) => r"\x00" ,
1003+ _ => r"\0" ,
1004+ } ) ,
9921005 b'\t' => escaped. push_str ( r"\t" ) ,
9931006 b'\n' => escaped. push_str ( r"\n" ) ,
9941007 b'\r' => escaped. push_str ( r"\r" ) ,
9951008 b'"' => escaped. push_str ( "\\ \" " ) ,
9961009 b'\\' => escaped. push_str ( "\\ \\ " ) ,
997- b'\x20' ..=b'\x7E' => escaped. push ( * b as char ) ,
1010+ b'\x20' ..=b'\x7E' => escaped. push ( b as char ) ,
9981011 _ => {
9991012 let _ = write ! ( escaped, "\\ x{:02X}" , b) ;
10001013 }
0 commit comments