diff options
author | Lukas Wirth <[email protected]> | 2021-01-30 15:31:19 +0000 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-01-30 15:31:19 +0000 |
commit | 6c2ce5515064011b2fde064ff18f178465383bce (patch) | |
tree | 742388df841bbeea6e6d7fcbfe65a16348c3575d /crates/syntax | |
parent | 557c1e36ddbb19cc76f7a1f04d1b327942aafcb9 (diff) |
Fix ast::String::value not properly escaping in some cases
Diffstat (limited to 'crates/syntax')
-rw-r--r-- | crates/syntax/src/ast/token_ext.rs | 21 |
1 files changed, 19 insertions, 2 deletions
diff --git a/crates/syntax/src/ast/token_ext.rs b/crates/syntax/src/ast/token_ext.rs index 5e07ec7d1..044e3e5e8 100644 --- a/crates/syntax/src/ast/token_ext.rs +++ b/crates/syntax/src/ast/token_ext.rs | |||
@@ -173,7 +173,7 @@ impl ast::String { | |||
173 | buf.capacity() == 0, | 173 | buf.capacity() == 0, |
174 | ) { | 174 | ) { |
175 | (Ok(c), false) => buf.push(c), | 175 | (Ok(c), false) => buf.push(c), |
176 | (Ok(c), true) if Some(c) == text_iter.next() => (), | 176 | (Ok(c), true) if char_range.len() == 1 && Some(c) == text_iter.next() => (), |
177 | (Ok(c), true) => { | 177 | (Ok(c), true) => { |
178 | buf.reserve_exact(text.len()); | 178 | buf.reserve_exact(text.len()); |
179 | buf.push_str(&text[..char_range.start]); | 179 | buf.push_str(&text[..char_range.start]); |
@@ -659,7 +659,7 @@ impl Radix { | |||
659 | 659 | ||
660 | #[cfg(test)] | 660 | #[cfg(test)] |
661 | mod tests { | 661 | mod tests { |
662 | use crate::ast::{make, FloatNumber, IntNumber}; | 662 | use crate::ast::{self, make, FloatNumber, IntNumber}; |
663 | 663 | ||
664 | fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) { | 664 | fn check_float_suffix<'a>(lit: &str, expected: impl Into<Option<&'a str>>) { |
665 | assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into()); | 665 | assert_eq!(FloatNumber { syntax: make::tokens::literal(lit) }.suffix(), expected.into()); |
@@ -692,4 +692,21 @@ mod tests { | |||
692 | check_int_suffix("0o11u32", "u32"); | 692 | check_int_suffix("0o11u32", "u32"); |
693 | check_int_suffix("0xffu32", "u32"); | 693 | check_int_suffix("0xffu32", "u32"); |
694 | } | 694 | } |
695 | |||
696 | fn check_string_value<'a>(lit: &str, expected: impl Into<Option<&'a str>>) { | ||
697 | assert_eq!( | ||
698 | ast::String { syntax: make::tokens::literal(&format!("\"{}\"", lit)) } | ||
699 | .value() | ||
700 | .as_deref(), | ||
701 | expected.into() | ||
702 | ); | ||
703 | } | ||
704 | |||
705 | #[test] | ||
706 | fn test_string_escape() { | ||
707 | check_string_value(r"foobar", "foobar"); | ||
708 | check_string_value(r"\foobar", None); | ||
709 | check_string_value(r"\nfoobar", "\nfoobar"); | ||
710 | check_string_value(r"C:\\Windows\\System32\\", "C:\\Windows\\System32\\"); | ||
711 | } | ||
695 | } | 712 | } |