Skip to content

Commit 856bf04

Browse files
committed
Merge pull request #726 from marcusklaas/string-backslashes
Prevent incorrect backslash removal in strings
2 parents c0b7de7 + 1e80fd2 commit 856bf04

File tree

3 files changed

+15
-5
lines changed

3 files changed

+15
-5
lines changed

src/string.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,17 @@ pub struct StringFormat<'a> {
3131
}
3232

3333
// FIXME: simplify this!
34-
pub fn rewrite_string<'a>(s: &str, fmt: &StringFormat<'a>) -> Option<String> {
34+
pub fn rewrite_string<'a>(orig: &str, fmt: &StringFormat<'a>) -> Option<String> {
3535
// Strip line breaks.
36-
let re = Regex::new(r"(\\[\n\r][:space:]*)").unwrap();
37-
let stripped_str = re.replace_all(s, "");
36+
let re = Regex::new(r"([^\\](\\\\)*)\\[\n\r][:space:]*").unwrap();
37+
let stripped_str = re.replace_all(orig, "$1");
3838

3939
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
4040
let indent = fmt.offset.to_string(fmt.config);
4141
let punctuation = ":,;.";
4242

4343
let mut cur_start = 0;
44-
let mut result = String::with_capacity(round_up_to_power_of_two(s.len()));
44+
let mut result = String::with_capacity(round_up_to_power_of_two(stripped_str.len()));
4545
result.push_str(fmt.opener);
4646

4747
let ender_length = fmt.line_end.len();
@@ -79,7 +79,7 @@ pub fn rewrite_string<'a>(s: &str, fmt: &StringFormat<'a>) -> Option<String> {
7979
}
8080
}
8181
// Make sure there is no whitespace to the right of the break.
82-
while cur_end < s.len() && graphemes[cur_end].trim().is_empty() {
82+
while cur_end < stripped_str.len() && graphemes[cur_end].trim().is_empty() {
8383
cur_end += 1;
8484
}
8585
let raw_line = graphemes[cur_start..cur_end].join("");

tests/source/string-lit.rs

+5
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,8 @@ fn issue682() {
3939
let a = "hello \\ o/";
4040
let b = a.replace("\\ ", "\\");
4141
}
42+
43+
fn issue716() {
44+
println!("forall x. mult(e(), x) = x /\\
45+
forall x. mult(x, x) = e()");
46+
}

tests/target/string-lit.rs

+5
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,8 @@ fn issue682() {
4545
let a = "hello \\ o/";
4646
let b = a.replace("\\ ", "\\");
4747
}
48+
49+
fn issue716() {
50+
println!("forall x. mult(e(), x) = x /\\
51+
forall x. mult(x, x) = e()");
52+
}

0 commit comments

Comments
 (0)