Skip to content

Commit

Permalink
Raw string to avoid escaped backslashes where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dtolnay committed Apr 14, 2024
1 parent bcac1fd commit 5bdf9be
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/fallback.rs
Expand Up @@ -1019,9 +1019,9 @@ impl Literal {
.starts_with(|next| '0' <= next && next <= '7')
{
// circumvent clippy::octal_escapes lint
"\\x00"
r"\x00"
} else {
"\\0"
r"\0"
},
);
} else if ch == '\'' {
Expand Down Expand Up @@ -1063,10 +1063,10 @@ impl Literal {
b'\n' => repr.push_str(r"\n"),
b'\r' => repr.push_str(r"\r"),
b'"' => repr.push_str("\\\""),
b'\\' => repr.push_str("\\\\"),
b'\\' => repr.push_str(r"\\"),
b'\x20'..=b'\x7E' => repr.push(b as char),
_ => {
let _ = write!(repr, "\\x{:02X}", b);
let _ = write!(repr, r"\x{:02X}", b);
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions tests/test.rs
Expand Up @@ -189,7 +189,7 @@ fn literal_c_string() {
#[test]
fn literal_character() {
assert_eq!(Literal::character('x').to_string(), "'x'");
assert_eq!(Literal::character('\'').to_string(), "'\\''");
assert_eq!(Literal::character('\'').to_string(), r"'\''");
assert_eq!(Literal::character('"').to_string(), "'\"'");
}

Expand Down Expand Up @@ -378,7 +378,7 @@ fn roundtrip() {
roundtrip("'a");
roundtrip("'_");
roundtrip("'static");
roundtrip("'\\u{10__FFFF}'");
roundtrip(r"'\u{10__FFFF}'");
roundtrip("\"\\u{10_F0FF__}foo\\u{1_0_0_0__}\"");
}

Expand Down

0 comments on commit 5bdf9be

Please sign in to comment.