bitbox_api/
util.rs

1// SPDX-License-Identifier: Apache-2.0
2
3pub fn remove_leading_zeroes(list: &[u8]) -> Vec<u8> {
4    if let Some(first_non_zero) = list.iter().position(|&x| x != 0) {
5        list[first_non_zero..].to_vec()
6    } else {
7        Vec::new()
8    }
9}
10
11#[cfg(feature = "multithreaded")]
12pub trait Threading: Sync + Send {}
13
14#[cfg(not(feature = "multithreaded"))]
15pub trait Threading {}
16
17#[cfg(test)]
18mod tests {
19    use super::*;
20    #[test]
21    fn test_remove_leading_zeroes() {
22        // Test with leading zeroes
23        let data = &[0, 0, 0, 1, 2, 3, 0, 4];
24        let result = remove_leading_zeroes(data);
25        assert_eq!(result, vec![1, 2, 3, 0, 4]);
26
27        // Test with no leading zeroes
28        let data = &[1, 0, 0, 1, 2, 3, 0, 4];
29        let result = remove_leading_zeroes(data);
30        assert_eq!(result, vec![1, 0, 0, 1, 2, 3, 0, 4]);
31
32        // Test with all zeroes
33        let data = &[0, 0, 0, 0, 0];
34        let result = remove_leading_zeroes(data);
35        assert_eq!(result, Vec::<u8>::new());
36
37        // Test with an empty list
38        let data = &[];
39        let result = remove_leading_zeroes(data);
40        assert_eq!(result, Vec::<u8>::new());
41    }
42}