Add a TODO to string_literal (#3181)

This commit is contained in:
Charlie Marsh 2023-02-23 12:46:20 -05:00 committed by GitHub
parent f5241451d8
commit 08be7bd285
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -52,6 +52,8 @@ impl Format<ASTFormatContext<'_>> for StringLiteralPart {
f.write_element(FormatElement::StaticText { text: "b" })?;
}
// TODO(charlie): Avoid allocating if there's nothing to escape. In other words, if we can
// use the string body directly, do so!
if trailing_quote.len() == 1 {
// Single-quoted string.
if dquotes == 0 || squotes > 0 {
@ -178,6 +180,7 @@ fn double_escape(text: &str) -> String {
escaped.push('\'');
} else if *next == '"' {
chars.next();
escaped.push('\\');
escaped.push('"');
} else if *next == '\\' {
chars.next();
@ -210,6 +213,7 @@ fn single_escape(text: &str) -> String {
escaped.push('"');
} else if *next == '\'' {
chars.next();
escaped.push('\\');
escaped.push('\'');
} else if *next == '\\' {
chars.next();
@ -241,6 +245,8 @@ mod tests {
#[test]
fn test_single_escape() {
assert_eq!(single_escape(r#"It's \"mine\""#), r#"It\'s "mine""#);
assert_eq!(single_escape(r#"It's mine"#), r#"It\'s mine"#);
assert_eq!(single_escape(r#"It\'s "mine""#), r#"It\'s "mine""#);
assert_eq!(single_escape(r#"It\\'s mine"#), r#"It\\\'s mine"#);
}
}