Skip to content

Commit

Permalink
Implement new advancement policy
Browse files Browse the repository at this point in the history
Summary: Fixes #3.

Reviewed By: d16r

Differential Revision: D17214029

fbshipit-source-id: 496cd4e82d83bf4bc7477873e3ab642706253704
  • Loading branch information
swolchok authored and facebook-github-bot committed Sep 6, 2019
1 parent 85da761 commit 767f874
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,12 +303,10 @@ impl Fastmod {
&new_contents,
)?;
if accepted {
// If the substitution is zero length, need to
// restart from the *same* position!
offset = offset + mat.start() + min(1, subst.len());
offset = offset + mat.start() + subst.len();
} else {
// Need to advance even if the substitution is zero length.
offset = offset + mat.start() + 1;
// Advance to the next character after the match.
offset = offset + mat.end() + 1;
}
}
}
Expand Down Expand Up @@ -954,4 +952,23 @@ mod tests {
f1.read_to_string(&mut contents).unwrap();
assert_eq!(contents, "");
}

#[test]
fn test_replacement_matches_search() {
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"foo").unwrap();
f1.sync_all().unwrap();
}
let regex = RegexBuilder::new("foo").build().unwrap();
let mut fm = Fastmod::new(true, false);
fm.present_and_apply_patches(&regex, "foofoo", &file_path, "foo".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, "foofoo");
}
}

0 comments on commit 767f874

Please sign in to comment.