Merge dc5fbc4026 into sapling-pr-archive-AdeAttwood
This commit is contained in:
commit
89889def72
3 changed files with 25 additions and 7 deletions
|
|
@ -17,7 +17,10 @@ fn concert_replacement(original: &str, replacement: &str) -> String {
|
|||
pub fn replace(search: &String, replace: String, input: String) -> String {
|
||||
let mut index = 0;
|
||||
let mut output = input;
|
||||
let search_pattern = Regex::new(&format!("(?i){search}")).unwrap();
|
||||
let search_pattern = match Regex::new(&format!("(?i){search}")) {
|
||||
Ok(pattern) => pattern,
|
||||
Err(_) => return output
|
||||
};
|
||||
|
||||
while let Some(search_match) = search_pattern.find_at(&output, index) {
|
||||
let start = search_match.start();
|
||||
|
|
|
|||
21
src/main.rs
21
src/main.rs
|
|
@ -11,17 +11,26 @@ struct Args {
|
|||
/// The replacement pattern.
|
||||
#[arg(short, long)]
|
||||
replace: String,
|
||||
|
||||
/// The input content to search and replace. If not provided, input will be read from stdin.
|
||||
#[arg(short, long)]
|
||||
input: Option<String>,
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let args = Args::parse();
|
||||
|
||||
let mut input = String::new();
|
||||
match std::io::stdin().read_to_string(&mut input) {
|
||||
Ok(_) => (),
|
||||
Err(err) => {
|
||||
eprintln!("Error reading from stdin: {}", err);
|
||||
return;
|
||||
let input = match args.input {
|
||||
Some(input) => input,
|
||||
None => {
|
||||
let mut input = String::new();
|
||||
match std::io::stdin().read_to_string(&mut input) {
|
||||
Ok(_) => input,
|
||||
Err(err) => {
|
||||
eprintln!("Error reading from stdin: {}", err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -17,3 +17,9 @@ Feature: Regex search and replace
|
|||
And Replace is 'Hello ${1}s'
|
||||
And Input is 'Hello world'
|
||||
Then Output is 'Hello worlds'
|
||||
|
||||
Scenario: You can search with an invalid regular expression
|
||||
Given Search is '(\w+'
|
||||
And Replace is 'new'
|
||||
And Input is 'this is a'
|
||||
Then Output is 'this is a'
|
||||
|
|
|
|||
Loading…
Reference in a new issue