Skip to content

Prevent incorrect backslash removal in strings #726

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 26, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ pub struct StringFormat<'a> {
}

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

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

let mut cur_start = 0;
let mut result = String::with_capacity(round_up_to_power_of_two(s.len()));
let mut result = String::with_capacity(round_up_to_power_of_two(stripped_str.len()));
result.push_str(fmt.opener);

let ender_length = fmt.line_end.len();
Expand Down Expand Up @@ -79,7 +79,7 @@ pub fn rewrite_string<'a>(s: &str, fmt: &StringFormat<'a>) -> Option<String> {
}
}
// Make sure there is no whitespace to the right of the break.
while cur_end < s.len() && graphemes[cur_end].trim().is_empty() {
while cur_end < stripped_str.len() && graphemes[cur_end].trim().is_empty() {
cur_end += 1;
}
let raw_line = graphemes[cur_start..cur_end].join("");
Expand Down
5 changes: 5 additions & 0 deletions tests/source/string-lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,8 @@ fn issue682() {
let a = "hello \\ o/";
let b = a.replace("\\ ", "\\");
}

fn issue716() {
println!("forall x. mult(e(), x) = x /\\
forall x. mult(x, x) = e()");
}
5 changes: 5 additions & 0 deletions tests/target/string-lit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,8 @@ fn issue682() {
let a = "hello \\ o/";
let b = a.replace("\\ ", "\\");
}

fn issue716() {
println!("forall x. mult(e(), x) = x /\\
forall x. mult(x, x) = e()");
}