nu_command/bytes/
remove.rs

1use nu_cmd_base::input_handler::{operate, CmdArgument};
2use nu_engine::command_prelude::*;
3
4struct Arguments {
5    pattern: Vec<u8>,
6    end: bool,
7    cell_paths: Option<Vec<CellPath>>,
8    all: bool,
9}
10
11impl CmdArgument for Arguments {
12    fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
13        self.cell_paths.take()
14    }
15}
16
17#[derive(Clone)]
18pub struct BytesRemove;
19
20impl Command for BytesRemove {
21    fn name(&self) -> &str {
22        "bytes remove"
23    }
24
25    fn signature(&self) -> Signature {
26        Signature::build("bytes remove")
27            .input_output_types(vec![
28                (Type::Binary, Type::Binary),
29                (Type::table(), Type::table()),
30                (Type::record(), Type::record()),
31            ])
32            .required("pattern", SyntaxShape::Binary, "The pattern to find.")
33            .rest(
34                "rest",
35                SyntaxShape::CellPath,
36                "For a data structure input, remove bytes from data at the given cell paths.",
37            )
38            .switch("end", "remove from end of binary", Some('e'))
39            .switch("all", "remove occurrences of finding binary", Some('a'))
40            .category(Category::Bytes)
41    }
42
43    fn description(&self) -> &str {
44        "Remove bytes."
45    }
46
47    fn search_terms(&self) -> Vec<&str> {
48        vec!["search", "shift", "switch"]
49    }
50
51    fn run(
52        &self,
53        engine_state: &EngineState,
54        stack: &mut Stack,
55        call: &Call,
56        input: PipelineData,
57    ) -> Result<PipelineData, ShellError> {
58        let cell_paths: Vec<CellPath> = call.rest(engine_state, stack, 1)?;
59        let cell_paths = (!cell_paths.is_empty()).then_some(cell_paths);
60        let pattern_to_remove = call.req::<Spanned<Vec<u8>>>(engine_state, stack, 0)?;
61        if pattern_to_remove.item.is_empty() {
62            return Err(ShellError::TypeMismatch {
63                err_message: "the pattern to remove cannot be empty".to_string(),
64                span: pattern_to_remove.span,
65            });
66        }
67
68        let pattern_to_remove: Vec<u8> = pattern_to_remove.item;
69        let arg = Arguments {
70            pattern: pattern_to_remove,
71            end: call.has_flag(engine_state, stack, "end")?,
72            cell_paths,
73            all: call.has_flag(engine_state, stack, "all")?,
74        };
75
76        operate(remove, arg, input, call.head, engine_state.signals())
77    }
78
79    fn examples(&self) -> Vec<Example> {
80        vec![
81            Example {
82                description: "Remove contents",
83                example: "0x[10 AA FF AA FF] | bytes remove 0x[10 AA]",
84                result: Some(Value::test_binary (
85                    vec![0xFF, 0xAA, 0xFF],
86                )),
87            },
88            Example {
89                description: "Remove all occurrences of find binary in record field",
90                example: "{ data: 0x[10 AA 10 BB 10] } | bytes remove --all 0x[10] data",
91                result: Some(Value::test_record(record! {
92                    "data" => Value::test_binary(vec![0xAA, 0xBB])
93                })),
94            },
95            Example {
96                description: "Remove occurrences of find binary from end",
97                example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[10]",
98                result: Some(Value::test_binary (
99                    vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
100                )),
101            },
102            Example {
103                description: "Remove find binary from end not found",
104                example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[11]",
105                result: Some(Value::test_binary (
106                    vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA, 0x10],
107                )),
108            },
109            Example {
110                description: "Remove all occurrences of find binary in table",
111                example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC",
112                result: Some(Value::test_list (
113                    vec![Value::test_record(record! {
114                        "ColA" => Value::test_binary ( vec![0x12, 0x13],),
115                        "ColB" => Value::test_binary ( vec![0x14, 0x15, 0x16],),
116                        "ColC" => Value::test_binary ( vec![0x17, 0x18, 0x19],),
117                    })],
118                )),
119            },
120        ]
121    }
122}
123
124fn remove(val: &Value, args: &Arguments, span: Span) -> Value {
125    let val_span = val.span();
126    match val {
127        Value::Binary { val, .. } => remove_impl(val, args, val_span),
128        // Propagate errors by explicitly matching them before the final case.
129        Value::Error { .. } => val.clone(),
130        other => Value::error(
131            ShellError::OnlySupportsThisInputType {
132                exp_input_type: "binary".into(),
133                wrong_type: other.get_type().to_string(),
134                dst_span: span,
135                src_span: other.span(),
136            },
137            span,
138        ),
139    }
140}
141
142fn remove_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
143    let mut result = vec![];
144    let remove_all = arg.all;
145    let input_len = input.len();
146    let pattern_len = arg.pattern.len();
147
148    // Note:
149    // remove_all from start and end will generate the same result.
150    // so we'll put `remove_all` relative logic into else clause.
151    if arg.end && !remove_all {
152        let (mut left, mut right) = (
153            input.len() as isize - arg.pattern.len() as isize,
154            input.len() as isize,
155        );
156        while left >= 0 && input[left as usize..right as usize] != arg.pattern {
157            result.push(input[right as usize - 1]);
158            left -= 1;
159            right -= 1;
160        }
161        // append the remaining thing to result, this can be happening when
162        // we have something to remove and remove_all is False.
163        // check if the left is positive, if it is not, we don't need to append anything.
164        if left > 0 {
165            let mut remain = input[..left as usize].iter().copied().rev().collect();
166            result.append(&mut remain);
167        }
168        result = result.into_iter().rev().collect();
169        Value::binary(result, span)
170    } else {
171        let (mut left, mut right) = (0, arg.pattern.len());
172        while right <= input_len {
173            if input[left..right] == arg.pattern {
174                left += pattern_len;
175                right += pattern_len;
176                if !remove_all {
177                    break;
178                }
179            } else {
180                result.push(input[left]);
181                left += 1;
182                right += 1;
183            }
184        }
185        // append the remaining thing to result, this can happened when
186        // we have something to remove and remove_all is False.
187        let mut remain = input[left..].to_vec();
188        result.append(&mut remain);
189        Value::binary(result, span)
190    }
191}
192
193#[cfg(test)]
194mod tests {
195    use super::*;
196
197    #[test]
198    fn test_examples() {
199        use crate::test_examples;
200
201        test_examples(BytesRemove {})
202    }
203}