Skip to content

Commit

Permalink
Invalid characters fix: escape quote triplets in triple-quoted strings
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatsJustCheesy committed Oct 23, 2024
1 parent 30ae80c commit 705ac51
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 5 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -265,9 +265,16 @@ pub(crate) fn invalid_string_characters<'a>(
let replacement: &str = match c {
'\\' => "\\\\",
'\'' | '"' => {
// Quotes don't have to be escaped in triple-quoted strings,
// *except* at the very end (like """this: \"""").
if string_flags.is_triple_quoted() && column + 1 < string_content.len() {
// Quotes only have to be escaped in triple-quoted strings at the beginning
// of a triplet (like `\"""\"""` within the string, or `\""""` at the end).
// For simplicity, escape all quotes not followed by the same character
// (e.g., `r""" \""" \""""` becomes `""" \\\"\"" \""""`).
if string_flags.is_triple_quoted()
&& string_content
.as_bytes()
.get(column + 1)
.is_some_and(|c2| char::from(*c2) != c)
{
continue;
}
match (c, string_flags.quote_style()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ invalid_characters.py:63:35: PLE2510 [*] Invalid unescaped character backspace,
63 | raw_single_singlequote = r'\ \' " ␈'
| ^ PLE2510
64 | raw_triple_singlequote = r'''\ ' " '''
65 | raw_triple_singlequote_2 = r'''\''''
65 | raw_triple_singlequote_2 = r''' \''' \''''
|
= help: Replace with escape sequence

Expand All @@ -80,7 +80,7 @@ invalid_characters.py:63:35: PLE2510 [*] Invalid unescaped character backspace,
63 |-raw_single_singlequote = r'\ \' " ␈'
63 |+raw_single_singlequote = '\\ \\\' " \b'
64 64 | raw_triple_singlequote = r'''\ ' " '''
65 65 | raw_triple_singlequote_2 = r'''\''''
65 65 | raw_triple_singlequote_2 = r''' \''' \''''
66 66 | raw_single_doublequote = r"\ ' \""

invalid_characters.py:77:1: PLE2510 [*] Invalid unescaped character backspace, use "\b" instead
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 comments on commit 705ac51

Please sign in to comment.