@@ -958,12 +958,20 @@ impl Literal {
958
958
pub fn string ( t : & str ) -> Literal {
959
959
let mut repr = String :: with_capacity ( t. len ( ) + 2 ) ;
960
960
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 == '\'' {
963
971
// escape_debug turns this into "\'" which is unnecessary.
964
- repr. push ( c ) ;
972
+ repr. push ( ch ) ;
965
973
} else {
966
- repr. extend ( c . escape_debug ( ) ) ;
974
+ repr. extend ( ch . escape_debug ( ) ) ;
967
975
}
968
976
}
969
977
repr. push ( '"' ) ;
@@ -985,16 +993,21 @@ impl Literal {
985
993
986
994
pub fn byte_string ( bytes : & [ u8 ] ) -> Literal {
987
995
let mut escaped = "b\" " . to_string ( ) ;
988
- for b in bytes {
996
+ let mut bytes = bytes. iter ( ) ;
997
+ while let Some ( & b) = bytes. next ( ) {
989
998
#[ 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
+ } ) ,
992
1005
b'\t' => escaped. push_str ( r"\t" ) ,
993
1006
b'\n' => escaped. push_str ( r"\n" ) ,
994
1007
b'\r' => escaped. push_str ( r"\r" ) ,
995
1008
b'"' => escaped. push_str ( "\\ \" " ) ,
996
1009
b'\\' => escaped. push_str ( "\\ \\ " ) ,
997
- b'\x20' ..=b'\x7E' => escaped. push ( * b as char ) ,
1010
+ b'\x20' ..=b'\x7E' => escaped. push ( b as char ) ,
998
1011
_ => {
999
1012
let _ = write ! ( escaped, "\\ x{:02X}" , b) ;
1000
1013
}
0 commit comments