update str find-replace to str replace (#5120)

This commit is contained in:
Darren Schroeder
2022-04-07 08:41:09 -05:00
committed by GitHub
parent 690ec9abfa
commit 4129f15eb9
10 changed files with 97 additions and 25 deletions

View File

@ -4,10 +4,10 @@ mod collect;
mod contains;
mod downcase;
mod ends_with;
mod find_replace;
mod index_of;
mod length;
mod lpad;
mod replace;
mod reverse;
mod rpad;
mod starts_with;
@ -21,10 +21,10 @@ pub use collect::*;
pub use contains::SubCommand as StrContains;
pub use downcase::SubCommand as StrDowncase;
pub use ends_with::SubCommand as StrEndswith;
pub use find_replace::SubCommand as StrFindReplace;
pub use index_of::SubCommand as StrIndexOf;
pub use length::SubCommand as StrLength;
pub use lpad::SubCommand as StrLpad;
pub use replace::SubCommand as StrReplace;
pub use reverse::SubCommand as StrReverse;
pub use rpad::SubCommand as StrRpad;
pub use starts_with::SubCommand as StrStartsWith;

View File

@ -1,11 +1,10 @@
use nu_engine::CallExt;
use nu_protocol::ast::Call;
use nu_protocol::ast::CellPath;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::Category;
use nu_protocol::Spanned;
use nu_protocol::{Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value};
use regex::Regex;
use nu_protocol::{
ast::{Call, CellPath},
engine::{Command, EngineState, Stack},
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
};
use regex::{NoExpand, Regex};
use std::sync::Arc;
struct Arguments {
@ -13,6 +12,7 @@ struct Arguments {
find: String,
replace: String,
column_paths: Vec<CellPath>,
literal_replace: bool,
}
#[derive(Clone)]
@ -20,11 +20,11 @@ pub struct SubCommand;
impl Command for SubCommand {
fn name(&self) -> &str {
"str find-replace"
"str replace"
}
fn signature(&self) -> Signature {
Signature::build("str find-replace")
Signature::build("str replace")
.required("find", SyntaxShape::String, "the pattern to find")
.required("replace", SyntaxShape::String, "the replacement pattern")
.rest(
@ -33,6 +33,11 @@ impl Command for SubCommand {
"optionally find and replace text by column paths",
)
.switch("all", "replace all occurrences of find string", Some('a'))
.switch(
"no-expand",
"do not expand the replace parameter as a regular expression",
Some('n'),
)
.category(Category::Strings)
}
@ -54,7 +59,7 @@ impl Command for SubCommand {
vec![
Example {
description: "Find and replace contents with capture group",
example: "'my_library.rb' | str find-replace '(.+).rb' '$1.nu'",
example: "'my_library.rb' | str replace '(.+).rb' '$1.nu'",
result: Some(Value::String {
val: "my_library.nu".to_string(),
span: Span::test_data(),
@ -62,7 +67,7 @@ impl Command for SubCommand {
},
Example {
description: "Find and replace all occurrences of find string",
example: "'abc abc abc' | str find-replace -a 'b' 'z'",
example: "'abc abc abc' | str replace -a 'b' 'z'",
result: Some(Value::String {
val: "azc azc azc".to_string(),
span: Span::test_data(),
@ -71,7 +76,7 @@ impl Command for SubCommand {
Example {
description: "Find and replace all occurrences of find string in table",
example:
"[[ColA ColB ColC]; [abc abc ads]] | str find-replace -a 'b' 'z' ColA ColC",
"[[ColA ColB ColC]; [abc abc ads]] | str replace -a 'b' 'z' ColA ColC",
result: Some(Value::List {
vals: vec![Value::Record {
cols: vec!["ColA".to_string(), "ColB".to_string(), "ColC".to_string()],
@ -94,6 +99,15 @@ impl Command for SubCommand {
span: Span::test_data(),
}),
},
Example {
description: "Find and replace contents without using the replace parameter as a regular expression",
example: r#"'dogs_$1_cats' | str replace '\$1' '$2' -n"#,
result: Some(Value::String {
val: "dogs_$2_cats".to_string(),
span: Span::test_data(),
}),
},
]
}
}
@ -106,12 +120,14 @@ fn operate(
) -> Result<PipelineData, ShellError> {
let find: Spanned<String> = call.req(engine_state, stack, 0)?;
let replace: Spanned<String> = call.req(engine_state, stack, 1)?;
let literal_replace = call.has_flag("no-expand");
let options = Arc::new(Arguments {
all: call.has_flag("all"),
find: find.item,
replace: replace.item,
column_paths: call.rest(engine_state, stack, 2)?,
literal_replace,
});
let head = call.head;
input.map(
@ -142,7 +158,11 @@ struct FindReplace<'a>(&'a str, &'a str);
fn action(
input: &Value,
Arguments {
find, replace, all, ..
find,
replace,
all,
literal_replace,
..
}: &Arguments,
head: Span,
) -> Value {
@ -155,12 +175,24 @@ fn action(
Ok(re) => {
if *all {
Value::String {
val: re.replace_all(val, replacement).to_string(),
val: {
if *literal_replace {
re.replace_all(val, NoExpand(replacement)).to_string()
} else {
re.replace_all(val, replacement).to_string()
}
},
span: head,
}
} else {
Value::String {
val: re.replace(val, replacement).to_string(),
val: {
if *literal_replace {
re.replace(val, NoExpand(replacement)).to_string()
} else {
re.replace(val, replacement).to_string()
}
},
span: head,
}
}
@ -206,6 +238,7 @@ mod tests {
find: String::from("Cargo.(.+)"),
replace: String::from("Carga.$1"),
column_paths: vec![],
literal_replace: false,
all: false,
};