Skip to content

Commit

Permalink
Fix deleting two consecutive matches
Browse files Browse the repository at this point in the history
Summary: facebookarchive/codemod#33 also repros in fastmod. The problem was that the amount we need to advance needs to depend on the length of the substitution: if it's zero, we need to continue scanning from the exact same offset instead of advancing.

Reviewed By: modocache

Differential Revision: D7705574

fbshipit-source-id: e58e06f79b108e3493a7c09cdd504fc07fadc05e
  • Loading branch information
swolchok authored and facebook-github-bot committed Apr 20, 2018
1 parent 51d17c9 commit 501a8b9
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion src/main.rs
Expand Up @@ -247,7 +247,9 @@ impl Fastmod {
new_contents.push_str(&new_trailing_contents);
let (start_line, _) = index_to_row_col(&contents, mat.start() + offset);
let (end_line, _) = index_to_row_col(&contents, mat.end() + offset - 1);
offset = offset + mat.start() + 1;
// If the substitution is zero length, need to
// restart from the *same* position!
offset = offset + mat.start() + min(1, subst.len());
self.ask_about_patch(
path,
&contents,
Expand Down Expand Up @@ -784,4 +786,22 @@ mod tests {
.doesnt_contain("file5.c")
.unwrap();
}

#[test]
fn test_zero_length_replacement() {
let dir = TempDir::new("fastmodtest").unwrap();
let file_path = dir.path().join("foo.txt");
{
let mut f1 = File::create(file_path.clone()).unwrap();
f1.write_all(b"foofoo").unwrap();
f1.sync_all().unwrap();
}
let regex = RegexBuilder::new("foo").multi_line(true).build().unwrap();
let mut fm = Fastmod::new(true, false);
fm.present_and_apply_patches(&regex, "", &file_path, "foofoo".into()).unwrap();
let mut f1 = File::open(file_path).unwrap();
let mut contents = String::new();
f1.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "");
}
}

0 comments on commit 501a8b9

Please sign in to comment.