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 {
|
pub fn replace(search: &String, replace: String, input: String) -> String {
|
||||||
let mut index = 0;
|
let mut index = 0;
|
||||||
let mut output = input;
|
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) {
|
while let Some(search_match) = search_pattern.find_at(&output, index) {
|
||||||
let start = search_match.start();
|
let start = search_match.start();
|
||||||
|
|
|
||||||
21
src/main.rs
21
src/main.rs
|
|
@ -11,17 +11,26 @@ struct Args {
|
||||||
/// The replacement pattern.
|
/// The replacement pattern.
|
||||||
#[arg(short, long)]
|
#[arg(short, long)]
|
||||||
replace: String,
|
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() {
|
fn main() {
|
||||||
let args = Args::parse();
|
let args = Args::parse();
|
||||||
|
|
||||||
let mut input = String::new();
|
let input = match args.input {
|
||||||
match std::io::stdin().read_to_string(&mut input) {
|
Some(input) => input,
|
||||||
Ok(_) => (),
|
None => {
|
||||||
Err(err) => {
|
let mut input = String::new();
|
||||||
eprintln!("Error reading from stdin: {}", err);
|
match std::io::stdin().read_to_string(&mut input) {
|
||||||
return;
|
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 Replace is 'Hello ${1}s'
|
||||||
And Input is 'Hello world'
|
And Input is 'Hello world'
|
||||||
Then Output is 'Hello worlds'
|
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