Fix the bug for "bytes remove --end" . (#11428)

This PR should close #11426 .

# Description
> ### Describe the bug
> When using the `--end` option of bytes remove, nushell panics if the
provided bytes don't exist. This doesn't seem to affect `bytes remove`
w/o flag or `bytes remove --all`.

# User-Facing Changes
Nushell doesn`t panic anymore.

# Tests + Formatting
Behavior before fixing the bug:
![nu-before
changes](https://github.com/UPB-CS-OpenSourceUpstream/nushell/assets/119429832/f9c26d88-8962-4f38-a373-ba436a26ca7c)
Behavior after fixing the bug:
![nu- after
changes](https://github.com/UPB-CS-OpenSourceUpstream/nushell/assets/119429832/0dd2b487-1696-45a6-9ea2-928cbd3a33a8)
This commit is contained in:
Ionel Sebastian 2023-12-28 01:01:55 +02:00 committed by GitHub
parent 9522052063
commit 15421dc45e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -105,6 +105,13 @@ impl Command for BytesRemove {
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA], vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA],
)), )),
}, },
Example {
description: "Remove find binary from end not found",
example: "0x[10 AA 10 BB CC AA 10] | bytes remove --end 0x[11]",
result: Some(Value::test_binary (
vec![0x10, 0xAA, 0x10, 0xBB, 0xCC, 0xAA, 0x10],
)),
},
Example { Example {
description: "Remove all occurrences of find binary in table", description: "Remove all occurrences of find binary in table",
example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC", example: "[[ColA ColB ColC]; [0x[11 12 13] 0x[14 15 16] 0x[17 18 19]]] | bytes remove 0x[11] ColA ColC",
@ -159,8 +166,11 @@ fn remove_impl(input: &[u8], arg: &Arguments, span: Span) -> Value {
} }
// append the remaining thing to result, this can be happening when // append the remaining thing to result, this can be happening when
// we have something to remove and remove_all is False. // we have something to remove and remove_all is False.
// check if the left is positive, if it is not, we don't need to append anything.
if left > 0 {
let mut remain = input[..left as usize].iter().copied().rev().collect(); let mut remain = input[..left as usize].iter().copied().rev().collect();
result.append(&mut remain); result.append(&mut remain);
}
result = result.into_iter().rev().collect(); result = result.into_iter().rev().collect();
Value::binary(result, span) Value::binary(result, span)
} else { } else {