1use fancy_regex::{NoExpand, Regex};
2use nu_cmd_base::input_handler::{CmdArgument, operate};
3use nu_engine::command_prelude::*;
4
5struct Arguments {
6 all: bool,
7 find: Spanned<String>,
8 replace: Spanned<String>,
9 cell_paths: Option<Vec<CellPath>>,
10 literal_replace: bool,
11 no_regex: bool,
12 multiline: bool,
13}
14
15impl CmdArgument for Arguments {
16 fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
17 self.cell_paths.take()
18 }
19}
20
21#[derive(Clone)]
22pub struct StrReplace;
23
24impl Command for StrReplace {
25 fn name(&self) -> &str {
26 "str replace"
27 }
28
29 fn signature(&self) -> Signature {
30 Signature::build("str replace")
31 .input_output_types(vec![
32 (Type::String, Type::String),
33 (Type::table(), Type::table()),
35 (Type::record(), Type::record()),
36 (
37 Type::List(Box::new(Type::String)),
38 Type::List(Box::new(Type::String)),
39 ),
40 ])
41 .required("find", SyntaxShape::String, "The pattern to find.")
42 .required("replace", SyntaxShape::String, "The replacement string.")
43 .rest(
44 "rest",
45 SyntaxShape::CellPath,
46 "For a data structure input, operate on strings at the given cell paths.",
47 )
48 .switch("all", "replace all occurrences of the pattern", Some('a'))
49 .switch(
50 "no-expand",
51 "do not expand capture groups (like $name) in the replacement string",
52 Some('n'),
53 )
54 .switch(
55 "regex",
56 "match the pattern as a regular expression in the input, instead of a substring",
57 Some('r'),
58 )
59 .switch(
60 "multiline",
61 "multi-line regex mode (implies --regex): ^ and $ match begin/end of line; equivalent to (?m)",
62 Some('m'),
63 )
64 .allow_variants_without_examples(true)
65 .category(Category::Strings)
66 }
67
68 fn description(&self) -> &str {
69 "Find and replace text."
70 }
71
72 fn search_terms(&self) -> Vec<&str> {
73 vec!["search", "shift", "switch", "regex"]
74 }
75
76 fn is_const(&self) -> bool {
77 true
78 }
79
80 fn run(
81 &self,
82 engine_state: &EngineState,
83 stack: &mut Stack,
84 call: &Call,
85 input: PipelineData,
86 ) -> Result<PipelineData, ShellError> {
87 let find: Spanned<String> = call.req(engine_state, stack, 0)?;
88 let replace: Spanned<String> = call.req(engine_state, stack, 1)?;
89 let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 2)?;
90 let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
91 let literal_replace = call.has_flag(engine_state, stack, "no-expand")?;
92 let no_regex = !call.has_flag(engine_state, stack, "regex")?
93 && !call.has_flag(engine_state, stack, "multiline")?;
94 let multiline = call.has_flag(engine_state, stack, "multiline")?;
95
96 let args = Arguments {
97 all: call.has_flag(engine_state, stack, "all")?,
98 find,
99 replace,
100 cell_paths,
101 literal_replace,
102 no_regex,
103 multiline,
104 };
105 operate(action, args, input, call.head, engine_state.signals())
106 }
107
108 fn run_const(
109 &self,
110 working_set: &StateWorkingSet,
111 call: &Call,
112 input: PipelineData,
113 ) -> Result<PipelineData, ShellError> {
114 let find: Spanned<String> = call.req_const(working_set, 0)?;
115 let replace: Spanned<String> = call.req_const(working_set, 1)?;
116 let cell_paths: Vec<CellPath> = call.rest_const(working_set, 2)?;
117 let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
118 let literal_replace = call.has_flag_const(working_set, "no-expand")?;
119 let no_regex = !call.has_flag_const(working_set, "regex")?
120 && !call.has_flag_const(working_set, "multiline")?;
121 let multiline = call.has_flag_const(working_set, "multiline")?;
122
123 let args = Arguments {
124 all: call.has_flag_const(working_set, "all")?,
125 find,
126 replace,
127 cell_paths,
128 literal_replace,
129 no_regex,
130 multiline,
131 };
132 operate(
133 action,
134 args,
135 input,
136 call.head,
137 working_set.permanent().signals(),
138 )
139 }
140
141 fn examples(&self) -> Vec<Example> {
142 vec![
143 Example {
144 description: "Find and replace the first occurrence of a substring",
145 example: r"'c:\some\cool\path' | str replace 'c:\some\cool' '~'",
146 result: Some(Value::test_string("~\\path")),
147 },
148 Example {
149 description: "Find and replace all occurrences of a substring",
150 example: r#"'abc abc abc' | str replace --all 'b' 'z'"#,
151 result: Some(Value::test_string("azc azc azc")),
152 },
153 Example {
154 description: "Find and replace contents with capture group using regular expression",
155 example: "'my_library.rb' | str replace -r '(.+).rb' '$1.nu'",
156 result: Some(Value::test_string("my_library.nu")),
157 },
158 Example {
159 description: "Find and replace contents with capture group using regular expression, with escapes",
160 example: "'hello=world' | str replace -r '\\$?(?<varname>.*)=(?<value>.*)' '$$$varname = $value'",
161 result: Some(Value::test_string("$hello = world")),
162 },
163 Example {
164 description: "Find and replace all occurrences of found string using regular expression",
165 example: "'abc abc abc' | str replace --all --regex 'b' 'z'",
166 result: Some(Value::test_string("azc azc azc")),
167 },
168 Example {
169 description: "Find and replace all occurrences of found string in table using regular expression",
170 example: "[[ColA ColB ColC]; [abc abc ads]] | str replace --all --regex 'b' 'z' ColA ColC",
171 result: Some(Value::test_list(vec![Value::test_record(record! {
172 "ColA" => Value::test_string("azc"),
173 "ColB" => Value::test_string("abc"),
174 "ColC" => Value::test_string("ads"),
175 })])),
176 },
177 Example {
178 description: "Find and replace all occurrences of found string in record using regular expression",
179 example: "{ KeyA: abc, KeyB: abc, KeyC: ads } | str replace --all --regex 'b' 'z' KeyA KeyC",
180 result: Some(Value::test_record(record! {
181 "KeyA" => Value::test_string("azc"),
182 "KeyB" => Value::test_string("abc"),
183 "KeyC" => Value::test_string("ads"),
184 })),
185 },
186 Example {
187 description: "Find and replace contents without using the replace parameter as a regular expression",
188 example: r"'dogs_$1_cats' | str replace -r '\$1' '$2' -n",
189 result: Some(Value::test_string("dogs_$2_cats")),
190 },
191 Example {
192 description: "Use captures to manipulate the input text using regular expression",
193 example: r#""abc-def" | str replace -r "(.+)-(.+)" "${2}_${1}""#,
194 result: Some(Value::test_string("def_abc")),
195 },
196 Example {
197 description: "Find and replace with fancy-regex using regular expression",
198 example: r"'a successful b' | str replace -r '\b([sS])uc(?:cs|s?)e(ed(?:ed|ing|s?)|ss(?:es|ful(?:ly)?|i(?:ons?|ve(?:ly)?)|ors?)?)\b' '${1}ucce$2'",
199 result: Some(Value::test_string("a successful b")),
200 },
201 Example {
202 description: "Find and replace with fancy-regex using regular expression",
203 example: r#"'GHIKK-9+*' | str replace -r '[*[:xdigit:]+]' 'z'"#,
204 result: Some(Value::test_string("GHIKK-z+*")),
205 },
206 Example {
207 description: "Find and replace on individual lines using multiline regular expression",
208 example: r#""non-matching line\n123. one line\n124. another line\n" | str replace --all --multiline '^[0-9]+\. ' ''"#,
209 result: Some(Value::test_string(
210 "non-matching line\none line\nanother line\n",
211 )),
212 },
213 ]
214 }
215}
216
217struct FindReplace<'a>(&'a str, &'a str);
218
219fn action(
220 input: &Value,
221 Arguments {
222 find,
223 replace,
224 all,
225 literal_replace,
226 no_regex,
227 multiline,
228 ..
229 }: &Arguments,
230 head: Span,
231) -> Value {
232 match input {
233 Value::String { val, .. } => {
234 let FindReplace(find_str, replace_str) = FindReplace(&find.item, &replace.item);
235 if *no_regex {
236 if *all {
238 Value::string(val.replace(find_str, replace_str), head)
239 } else {
240 Value::string(val.replacen(find_str, replace_str, 1), head)
241 }
242 } else {
243 let flags = match multiline {
245 true => "(?m)",
246 false => "",
247 };
248 let regex_string = flags.to_string() + find_str;
249 let regex = Regex::new(®ex_string);
250
251 match regex {
252 Ok(re) => {
253 if *all {
254 Value::string(
255 {
256 if *literal_replace {
257 re.replace_all(val, NoExpand(replace_str)).to_string()
258 } else {
259 re.replace_all(val, replace_str).to_string()
260 }
261 },
262 head,
263 )
264 } else {
265 Value::string(
266 {
267 if *literal_replace {
268 re.replace(val, NoExpand(replace_str)).to_string()
269 } else {
270 re.replace(val, replace_str).to_string()
271 }
272 },
273 head,
274 )
275 }
276 }
277 Err(e) => Value::error(
278 ShellError::IncorrectValue {
279 msg: format!("Regex error: {e}"),
280 val_span: find.span,
281 call_span: head,
282 },
283 find.span,
284 ),
285 }
286 }
287 }
288 Value::Error { .. } => input.clone(),
289 _ => Value::error(
290 ShellError::OnlySupportsThisInputType {
291 exp_input_type: "string".into(),
292 wrong_type: input.get_type().to_string(),
293 dst_span: head,
294 src_span: input.span(),
295 },
296 head,
297 ),
298 }
299}
300
301#[cfg(test)]
302mod tests {
303 use super::*;
304 use super::{Arguments, StrReplace, action};
305
306 fn test_spanned_string(val: &str) -> Spanned<String> {
307 Spanned {
308 item: String::from(val),
309 span: Span::test_data(),
310 }
311 }
312
313 #[test]
314 fn test_examples() {
315 use crate::test_examples;
316
317 test_examples(StrReplace {})
318 }
319
320 #[test]
321 fn can_have_capture_groups() {
322 let word = Value::test_string("Cargo.toml");
323
324 let options = Arguments {
325 find: test_spanned_string("Cargo.(.+)"),
326 replace: test_spanned_string("Carga.$1"),
327 cell_paths: None,
328 literal_replace: false,
329 all: false,
330 no_regex: false,
331 multiline: false,
332 };
333
334 let actual = action(&word, &options, Span::test_data());
335 assert_eq!(actual, Value::test_string("Carga.toml"));
336 }
337}