mirror of
https://github.com/nushell/nushell.git
synced 2025-04-14 08:18:17 +02:00
# Description - enables `bits` commands to operate on binary data, where both inputs are binary and can vary in length - adds an `--endian` flag to `bits and`, `or`, `xor` for specifying endianness (for binary values of different lengths) # User-Facing Changes - `bits` commands will no longer error for non-int inputs - the default for `--number-bytes` is now `auto` (infer int size; changed from 8) # Tests + Formatting > addendum: first PR, please inform if any changes are needed
139 lines
4.3 KiB
Rust
139 lines
4.3 KiB
Rust
use super::binary_op;
|
|
use nu_engine::CallExt;
|
|
use nu_protocol::ast::Call;
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
use nu_protocol::{
|
|
Category, Example, PipelineData, ShellError, Signature, Spanned, SyntaxShape, Type, Value,
|
|
};
|
|
|
|
#[derive(Clone)]
|
|
pub struct BitsXor;
|
|
|
|
impl Command for BitsXor {
|
|
fn name(&self) -> &str {
|
|
"bits xor"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("bits xor")
|
|
.input_output_types(vec![
|
|
(Type::Int, Type::Int),
|
|
(Type::Binary, Type::Binary),
|
|
(
|
|
Type::List(Box::new(Type::Int)),
|
|
Type::List(Box::new(Type::Int)),
|
|
),
|
|
(
|
|
Type::List(Box::new(Type::Binary)),
|
|
Type::List(Box::new(Type::Binary)),
|
|
),
|
|
])
|
|
.allow_variants_without_examples(true)
|
|
.required(
|
|
"target",
|
|
SyntaxShape::OneOf(vec![SyntaxShape::Binary, SyntaxShape::Int]),
|
|
"right-hand side of the operation",
|
|
)
|
|
.named(
|
|
"endian",
|
|
SyntaxShape::String,
|
|
"byte encode endian, available options: native(default), little, big",
|
|
Some('e'),
|
|
)
|
|
.category(Category::Bits)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Performs bitwise xor for ints or binary values."
|
|
}
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
vec!["logic xor"]
|
|
}
|
|
|
|
fn run(
|
|
&self,
|
|
engine_state: &EngineState,
|
|
stack: &mut Stack,
|
|
call: &Call,
|
|
input: PipelineData,
|
|
) -> Result<PipelineData, ShellError> {
|
|
let head = call.head;
|
|
let target: Value = call.req(engine_state, stack, 0)?;
|
|
let endian = call.get_flag::<Spanned<String>>(engine_state, stack, "endian")?;
|
|
|
|
let little_endian = if let Some(endian) = endian {
|
|
match endian.item.as_str() {
|
|
"native" => cfg!(target_endian = "little"),
|
|
"little" => true,
|
|
"big" => false,
|
|
_ => {
|
|
return Err(ShellError::TypeMismatch {
|
|
err_message: "Endian must be one of native, little, big".to_string(),
|
|
span: endian.span,
|
|
})
|
|
}
|
|
}
|
|
} else {
|
|
cfg!(target_endian = "little")
|
|
};
|
|
|
|
// This doesn't match explicit nulls
|
|
if matches!(input, PipelineData::Empty) {
|
|
return Err(ShellError::PipelineEmpty { dst_span: head });
|
|
}
|
|
|
|
input.map(
|
|
move |value| binary_op(&value, &target, little_endian, |(l, r)| l ^ r, head),
|
|
engine_state.ctrlc.clone(),
|
|
)
|
|
}
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
vec![
|
|
Example {
|
|
description: "Apply bits xor to two numbers",
|
|
example: "2 | bits xor 2",
|
|
result: Some(Value::test_int(0)),
|
|
},
|
|
Example {
|
|
description: "Apply bitwise xor to a list of numbers",
|
|
example: "[8 3 2] | bits xor 2",
|
|
result: Some(Value::test_list(vec![
|
|
Value::test_int(10),
|
|
Value::test_int(1),
|
|
Value::test_int(0),
|
|
])),
|
|
},
|
|
Example {
|
|
description: "Apply bitwise xor to binary data",
|
|
example: "0x[ca fe] | bits xor 0x[ba be]",
|
|
result: Some(Value::test_binary(vec![0x70, 0x40])),
|
|
},
|
|
Example {
|
|
description:
|
|
"Apply bitwise xor to binary data of varying lengths with specified endianness",
|
|
example: "0x[ca fe] | bits xor 0x[aa] --endian big",
|
|
result: Some(Value::test_binary(vec![0xca, 0x54])),
|
|
},
|
|
Example {
|
|
description: "Apply bitwise xor to input binary data smaller than the operand",
|
|
example: "0x[ff] | bits xor 0x[12 34 56] --endian little",
|
|
result: Some(Value::test_binary(vec![0xed, 0x34, 0x56])),
|
|
},
|
|
]
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod test {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn test_examples() {
|
|
use crate::test_examples;
|
|
|
|
test_examples(BitsXor {})
|
|
}
|
|
}
|