nu_command/bytes/
remove.rs

1use nu_cmd_base::input_handler::{CmdArgument, operate};
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()).map(|pipeline| {
77            // image/png with some bytes removed is likely not a valid image/png anymore
78            let metadata = pipeline.metadata().map(|m| m.with_content_type(None));
79            pipeline.set_metadata(metadata)
80        })
81    }
82
83    fn examples(&self) -> Vec<Example> {
84        vec![
85            Example {
86                description: "Remove contents",
87                example: "0x[10 AA FF AA FF] | bytes remove 0x[10 AA]",
88                result: Some(Value::test_binary(vec![0xFF, 0xAA, 0xFF])),
89            },
90            Example {
91                description: "Remove all occurrences of find binary in record field",
92                example: "{ data: 0x[10 AA 10 BB 10] } | bytes remove --all 0x[10] data",
93                result: Some(Value::test_record(record! {
94                    "data" => Value::test_binary(vec![0xAA, 0xBB])
95                })),
96            },
97            Example {
98                description: "Remove occurrences of find binary from end",
99                example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[10]",
100                result: Some(Value::test_binary(vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA])),
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(vec![
106                    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(vec![Value::test_record(record! {
113                    "ColA" => Value::test_binary ( vec![0x12, 0x13],),
114                    "ColB" => Value::test_binary ( vec![0x14, 0x15, 0x16],),
115                    "ColC" => Value::test_binary ( vec![0x17, 0x18, 0x19],),
116                })])),
117            },
118        ]
119    }
120}
121
122fn remove(val: &Value, args: &Arguments, span: Span) -> Value {
123    let val_span = val.span();
124    match val {
125        Value::Binary { val, .. } => remove_impl(val, args, val_span),
126        // Propagate errors by explicitly matching them before the final case.
127        Value::Error { .. } => val.clone(),
128        other => Value::error(
129            ShellError::OnlySupportsThisInputType {
130                exp_input_type: "binary".into(),
131                wrong_type: other.get_type().to_string(),
132                dst_span: span,
133                src_span: other.span(),
134            },
135            span,
136        ),
137    }
138}
139
140fn remove_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
141    let mut result = vec![];
142    let remove_all = arg.all;
143    let input_len = input.len();
144    let pattern_len = arg.pattern.len();
145
146    // Note:
147    // remove_all from start and end will generate the same result.
148    // so we'll put `remove_all` relative logic into else clause.
149    if arg.end && !remove_all {
150        let (mut left, mut right) = (
151            input.len() as isize - arg.pattern.len() as isize,
152            input.len() as isize,
153        );
154        while left >= 0 && input[left as usize..right as usize] != arg.pattern {
155            result.push(input[right as usize - 1]);
156            left -= 1;
157            right -= 1;
158        }
159        // append the remaining thing to result, this can be happening when
160        // we have something to remove and remove_all is False.
161        // check if the left is positive, if it is not, we don't need to append anything.
162        if left > 0 {
163            let mut remain = input[..left as usize].iter().copied().rev().collect();
164            result.append(&mut remain);
165        }
166        result = result.into_iter().rev().collect();
167        Value::binary(result, span)
168    } else {
169        let (mut left, mut right) = (0, arg.pattern.len());
170        while right <= input_len {
171            if input[left..right] == arg.pattern {
172                left += pattern_len;
173                right += pattern_len;
174                if !remove_all {
175                    break;
176                }
177            } else {
178                result.push(input[left]);
179                left += 1;
180                right += 1;
181            }
182        }
183        // append the remaining thing to result, this can happened when
184        // we have something to remove and remove_all is False.
185        let mut remain = input[left..].to_vec();
186        result.append(&mut remain);
187        Value::binary(result, span)
188    }
189}
190
191#[cfg(test)]
192mod tests {
193    use super::*;
194
195    #[test]
196    fn test_examples() {
197        use crate::test_examples;
198
199        test_examples(BytesRemove {})
200    }
201}