Skip to content

Commit 7efc1c4

Browse files
committed
Implement consistent behavior for Literal::string on all versions of Rust
1 parent 9c092a3 commit 7efc1c4

File tree

2 files changed

+16
-12
lines changed

2 files changed

+16
-12
lines changed

src/fallback.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -960,13 +960,18 @@ impl Literal {
960960
repr.push('"');
961961
let mut chars = t.chars();
962962
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");
963+
if ch == '\0' {
964+
repr.push_str(
965+
if chars
966+
.as_str()
967+
.starts_with(|next| '0' <= next && next <= '7')
968+
{
969+
// circumvent clippy::octal_escapes lint
970+
"\\x00"
971+
} else {
972+
"\\0"
973+
},
974+
);
970975
} else if ch == '\'' {
971976
// escape_debug turns this into "\'" which is unnecessary.
972977
repr.push(ch);

tests/test.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,10 @@ fn literal_string() {
115115
assert_eq!(Literal::string("foo").to_string(), "\"foo\"");
116116
assert_eq!(Literal::string("\"").to_string(), "\"\\\"\"");
117117
assert_eq!(Literal::string("didn't").to_string(), "\"didn't\"");
118-
119-
let repr = Literal::string("a\00b\07c\08d\0e\0").to_string();
120-
if repr != "\"a\\x000b\\x007c\\u{0}8d\\u{0}e\\u{0}\"" {
121-
assert_eq!(repr, "\"a\\x000b\\x007c\\08d\\0e\\0\"");
122-
}
118+
assert_eq!(
119+
Literal::string("a\00b\07c\08d\0e\0").to_string(),
120+
"\"a\\x000b\\x007c\\08d\\0e\\0\"",
121+
);
123122
}
124123

125124
#[test]

0 commit comments

Comments
 (0)