Skip to content

Commit

Permalink
Escape regex metacharacters in substitution string (#45)
Browse files Browse the repository at this point in the history
Summary:
`fastmod -F 'something' '$foo.bar'`

was replacing with `.bar`, not `$foo.bar` as intended.  Now it works.

Docs for replacement string escaping:
https://docs.rs/regex/1.9.6/regex/struct.Regex.html#replacement-string-syntax

Pull Request resolved: #45

Reviewed By: zertosh

Differential Revision: D51635478

Pulled By: swolchok

fbshipit-source-id: 1833228369d730575141dc47a1fc5b5ef1883e50
  • Loading branch information
ahupp authored and facebook-github-bot committed Nov 30, 2023
1 parent 605aa88 commit 8070231
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -873,10 +873,11 @@ compatibility with the original codemod.",
let hidden = matches.is_present("hidden");
let print_changed_files = matches.is_present("print_changed_files");
let regex_str = matches.value_of("match").expect("match is required!");
let maybe_escaped_regex = if matches.is_present("fixed_strings") {
regex::escape(regex_str)
let subst = matches.value_of("subst").expect("subst is required!");
let (maybe_escaped_regex, subst) = if matches.is_present("fixed_strings") {
(regex::escape(regex_str), subst.replace("$", "$$"))
} else {
regex_str.to_string()
(regex_str.to_string(), subst.to_string())
};
let regex = RegexBuilder::new(&maybe_escaped_regex)
.case_insensitive(ignore_case)
Expand All @@ -896,21 +897,20 @@ not what you want. Press Enter to continue anyway or Ctrl-C to quit.",
.multi_line(true)
.dot_matches_new_line(multiline)
.build(&maybe_escaped_regex)?;
let subst = matches.value_of("subst").expect("subst is required!");

if accept_all {
Fastmod::run_fast(
&regex,
&matcher,
subst,
&subst,
dirs,
file_set,
hidden,
print_changed_files,
)
} else {
Fastmod::new(accept_all, hidden, print_changed_files)
.run_interactive(&regex, &matcher, subst, dirs, file_set)
.run_interactive(&regex, &matcher, &subst, dirs, file_set)
}
}

Expand Down

0 comments on commit 8070231

Please sign in to comment.