Skip to content

Commit

Permalink
Merge pull request #381 from dtolnay/escape0
Browse files Browse the repository at this point in the history
Implement consistent behavior for Literal::string on all versions of Rust
  • Loading branch information
dtolnay authored Apr 3, 2023
2 parents 9c092a3 + 7efc1c4 commit e82b074
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
19 changes: 12 additions & 7 deletions src/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -960,13 +960,18 @@ impl Literal {
repr.push('"');
let mut chars = t.chars();
while let Some(ch) = chars.next() {
if ch == '\0'
&& chars
.as_str()
.starts_with(|next| '0' <= next && next <= '7')
{
// circumvent clippy::octal_escapes lint
repr.push_str("\\x00");
if ch == '\0' {
repr.push_str(
if chars
.as_str()
.starts_with(|next| '0' <= next && next <= '7')
{
// circumvent clippy::octal_escapes lint
"\\x00"
} else {
"\\0"
},
);
} else if ch == '\'' {
// escape_debug turns this into "\'" which is unnecessary.
repr.push(ch);
Expand Down
9 changes: 4 additions & 5 deletions tests/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,10 @@ fn literal_string() {
assert_eq!(Literal::string("foo").to_string(), "\"foo\"");
assert_eq!(Literal::string("\"").to_string(), "\"\\\"\"");
assert_eq!(Literal::string("didn't").to_string(), "\"didn't\"");

let repr = Literal::string("a\00b\07c\08d\0e\0").to_string();
if repr != "\"a\\x000b\\x007c\\u{0}8d\\u{0}e\\u{0}\"" {
assert_eq!(repr, "\"a\\x000b\\x007c\\08d\\0e\\0\"");
}
assert_eq!(
Literal::string("a\00b\07c\08d\0e\0").to_string(),
"\"a\\x000b\\x007c\\08d\\0e\\0\"",
);
}

#[test]
Expand Down

0 comments on commit e82b074

Please sign in to comment.