Prevent incorrect backslash removal in strings

This commit is contained in:
Marcus Klaas 2015-12-25 18:50:40 +01:00
parent c0b7de7c52
commit 1e80fd2dcd
3 changed files with 15 additions and 5 deletions

View File

@ -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();
@ -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("");

View File

@ -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()");
}

View File

@ -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()");
}