nu_cmd_extra/extra/bits/
shift_right.rs

1use super::{InputNumType, NumberBytes, get_input_num_type, get_number_bytes};
2use nu_cmd_base::input_handler::{CmdArgument, operate};
3use nu_engine::command_prelude::*;
4
5struct Arguments {
6    signed: bool,
7    bits: Spanned<usize>,
8    number_size: NumberBytes,
9}
10
11impl CmdArgument for Arguments {
12    fn take_cell_paths(&mut self) -> Option<Vec<CellPath>> {
13        None
14    }
15}
16
17#[derive(Clone)]
18pub struct BitsShr;
19
20impl Command for BitsShr {
21    fn name(&self) -> &str {
22        "bits shr"
23    }
24
25    fn signature(&self) -> Signature {
26        Signature::build("bits shr")
27            .input_output_types(vec![
28                (Type::Int, Type::Int),
29                (Type::Binary, Type::Binary),
30                (
31                    Type::List(Box::new(Type::Int)),
32                    Type::List(Box::new(Type::Int)),
33                ),
34                (
35                    Type::List(Box::new(Type::Binary)),
36                    Type::List(Box::new(Type::Binary)),
37                ),
38            ])
39            .allow_variants_without_examples(true)
40            .required("bits", SyntaxShape::Int, "Number of bits to shift right.")
41            .switch(
42                "signed",
43                "always treat input number as a signed number",
44                Some('s'),
45            )
46            .named(
47                "number-bytes",
48                SyntaxShape::Int,
49                "the word size in number of bytes. Must be `1`, `2`, `4`, or `8` (defaults to the smallest of those that fits the input number)",
50                Some('n'),
51            )
52            .category(Category::Bits)
53    }
54
55    fn description(&self) -> &str {
56        "Bitwise shift right for ints or binary values."
57    }
58
59    fn search_terms(&self) -> Vec<&str> {
60        vec!["shift right"]
61    }
62
63    fn run(
64        &self,
65        engine_state: &EngineState,
66        stack: &mut Stack,
67        call: &Call,
68        input: PipelineData,
69    ) -> Result<PipelineData, ShellError> {
70        let head = call.head;
71        // This restricts to a positive shift value (our underlying operations do not
72        // permit them)
73        let bits: Spanned<usize> = call.req(engine_state, stack, 0)?;
74        let signed = call.has_flag(engine_state, stack, "signed")?;
75        let number_bytes: Option<Spanned<usize>> =
76            call.get_flag(engine_state, stack, "number-bytes")?;
77        let number_size = get_number_bytes(number_bytes, head)?;
78
79        // This doesn't match explicit nulls
80        if let PipelineData::Empty = input {
81            return Err(ShellError::PipelineEmpty { dst_span: head });
82        }
83
84        let args = Arguments {
85            signed,
86            number_size,
87            bits,
88        };
89
90        operate(action, args, input, head, engine_state.signals())
91    }
92
93    fn examples(&self) -> Vec<Example<'_>> {
94        vec![
95            Example {
96                description: "Shift right a number with 2 bits",
97                example: "8 | bits shr 2",
98                result: Some(Value::test_int(2)),
99            },
100            Example {
101                description: "Shift right a list of numbers",
102                example: "[15 35 2] | bits shr 2",
103                result: Some(Value::list(
104                    vec![Value::test_int(3), Value::test_int(8), Value::test_int(0)],
105                    Span::test_data(),
106                )),
107            },
108            Example {
109                description: "Shift right a binary value",
110                example: "0x[4f f4] | bits shr 4",
111                result: Some(Value::binary(vec![0x04, 0xff], Span::test_data())),
112            },
113        ]
114    }
115}
116
117fn action(input: &Value, args: &Arguments, span: Span) -> Value {
118    let Arguments {
119        signed,
120        number_size,
121        bits,
122    } = *args;
123    let bits_span = bits.span;
124    let bits = bits.item;
125
126    match input {
127        Value::Int { val, .. } => {
128            use InputNumType::*;
129            let val = *val;
130            let bits = bits as u32;
131            let input_num_type = get_input_num_type(val, signed, number_size);
132
133            if !input_num_type.is_permitted_bit_shift(bits) {
134                return Value::error(
135                    ShellError::IncorrectValue {
136                        msg: format!(
137                            "Trying to shift by more than the available bits (permitted < {})",
138                            input_num_type.num_bits()
139                        ),
140                        val_span: bits_span,
141                        call_span: span,
142                    },
143                    span,
144                );
145            }
146            let int = match input_num_type {
147                One => ((val as u8) >> bits) as i64,
148                Two => ((val as u16) >> bits) as i64,
149                Four => ((val as u32) >> bits) as i64,
150                Eight => ((val as u64) >> bits) as i64,
151                SignedOne => ((val as i8) >> bits) as i64,
152                SignedTwo => ((val as i16) >> bits) as i64,
153                SignedFour => ((val as i32) >> bits) as i64,
154                SignedEight => val >> bits,
155            };
156
157            Value::int(int, span)
158        }
159        Value::Binary { val, .. } => {
160            let byte_shift = bits / 8;
161            let bit_shift = bits % 8;
162
163            let len = val.len();
164            // This check is done for symmetry with the int case and the previous
165            // implementation would overflow byte indices leading to unexpected output
166            // lengths
167            if bits > len * 8 {
168                return Value::error(
169                    ShellError::IncorrectValue {
170                        msg: format!(
171                            "Trying to shift by more than the available bits ({})",
172                            len * 8
173                        ),
174                        val_span: bits_span,
175                        call_span: span,
176                    },
177                    span,
178                );
179            }
180            let bytes = if bit_shift == 0 {
181                shift_bytes_right(val, byte_shift)
182            } else {
183                shift_bytes_and_bits_right(val, byte_shift, bit_shift)
184            };
185
186            Value::binary(bytes, span)
187        }
188        // Propagate errors by explicitly matching them before the final case.
189        Value::Error { .. } => input.clone(),
190        other => Value::error(
191            ShellError::OnlySupportsThisInputType {
192                exp_input_type: "int or binary".into(),
193                wrong_type: other.get_type().to_string(),
194                dst_span: span,
195                src_span: other.span(),
196            },
197            span,
198        ),
199    }
200}
201fn shift_bytes_right(data: &[u8], byte_shift: usize) -> Vec<u8> {
202    let len = data.len();
203    let mut output = vec![0; len];
204    output[byte_shift..].copy_from_slice(&data[..len - byte_shift]);
205    output
206}
207
208fn shift_bytes_and_bits_right(data: &[u8], byte_shift: usize, bit_shift: usize) -> Vec<u8> {
209    debug_assert!(
210        bit_shift > 0 && bit_shift < 8,
211        "bit_shift should be in the range (0, 8)"
212    );
213    let len = data.len();
214    let mut output = vec![0; len];
215
216    for i in byte_shift..len {
217        let shifted_bits = data[i - byte_shift] >> bit_shift;
218        let carried_bits = if i > byte_shift {
219            data[i - byte_shift - 1] << (8 - bit_shift)
220        } else {
221            0
222        };
223        let shifted_byte = shifted_bits | carried_bits;
224
225        output[i] = shifted_byte;
226    }
227
228    output
229}
230
231#[cfg(test)]
232mod test {
233    use super::*;
234
235    #[test]
236    fn test_examples() {
237        use crate::test_examples;
238
239        test_examples(BitsShr {})
240    }
241}