Skip to content

Commit

Permalink
Fix crash on escaped backslashes in rf-string (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
jakkdl authored May 17, 2023
1 parent 889ce56 commit ea19578
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 2 additions & 2 deletions native/libcst/src/tokenizer/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -973,8 +973,8 @@ impl<'t> TokState<'t> {
}
(Some('\\'), _) if is_raw_string => {
self.text_pos.next();
if let Some('"' | '\'') = self.text_pos.peek() {
// these aren't end of string markers, skip them
// skip escaped end-of-string marker or backslash
if let Some('"' | '\'' | '\\') = self.text_pos.peek() {
self.text_pos.next();
}
}
Expand Down
12 changes: 12 additions & 0 deletions native/libcst/src/tokenizer/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -529,6 +529,10 @@ fn test_string_prefix() {
tokenize_all(r#"r"\"""#, &default_config()),
Ok(vec![(TokType::String, r#"r"\"""#)]),
);
assert_eq!(
tokenize_all(r#"r'\\'"#, &default_config()),
Ok(vec![(TokType::String, r#"r'\\'"#)]),
);
let config = TokConfig {
split_fstring: true,
..default_config()
Expand All @@ -549,6 +553,14 @@ fn test_string_prefix() {
(TokType::FStringEnd, "\""),
]),
);
assert_eq!(
tokenize_all(r#"rf'\\'"#, &config),
Ok(vec![
(TokType::FStringStart, "rf'"),
(TokType::FStringString, r#"\\"#),
(TokType::FStringEnd, "'"),
]),
);
}

#[test]
Expand Down

0 comments on commit ea19578

Please sign in to comment.