1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//! `fast-find` finds bytes fast.
//!
//! Fast-find uses SIMD instructions to make searching small byte arrays fast, or
//! falls back to linear search when not possible.

#[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
#[path = "arch/x64.rs"]
mod arch;

#[cfg(not(any(target_arch = "x86", target_arch = "x86_64")))]
#[path = "fallback.rs"]
mod arch;

mod fallback;

/// Returns the index of the first element in the haystack that matches needle, up-to len, or None
#[inline]
pub fn find_in_4(needle: u8, haystack: &[u8; 4], len: usize) -> Option<usize> {
    arch::find_in_4(needle, haystack, len)
}

/// Returns the index of the first element in the haystack that matches needle, up-to len, or None
#[inline]
pub fn find_in_16(needle: u8, haystack: &[u8; 16], len: usize) -> Option<usize> {
    arch::find_in_16(needle, haystack, len)
}

/// Returns the index of the first element in the haystack that matches needle, up-to len, or None
#[inline]
pub fn find_in_32(needle: u8, haystack: &[u8; 32], len: usize) -> Option<usize> {
    arch::find_in_32(needle, haystack, len)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_find_4() {
        let mut array = [0; 4];

        assert_eq!(find_in_4(4, &array, 4), None);

        array[1] = 4;
        array[3] = 10;

        assert_eq!(find_in_4(4, &array, 4), Some(1));
        assert_eq!(find_in_4(5, &array, 4), None);
        assert_eq!(find_in_4(10, &array, 4), Some(3));

        assert_eq!(find_in_4(4, &array, 2), Some(1));
        assert_eq!(find_in_4(5, &array, 2), None);
        assert_eq!(find_in_4(10, &array, 2), None);
    }

    #[test]
    fn test_find_16() {
        let mut array = [0; 16];

        assert_eq!(find_in_16(4, &array, 16), None);

        array[4] = 4;
        array[6] = 10;

        assert_eq!(find_in_16(4, &array, 16), Some(4));
        assert_eq!(find_in_16(5, &array, 16), None);
        assert_eq!(find_in_16(10, &array, 16), Some(6));

        assert_eq!(find_in_16(4, &array, 5), Some(4));
        assert_eq!(find_in_16(5, &array, 5), None);
        assert_eq!(find_in_16(10, &array, 5), None);
    }

    #[test]
    fn test_find_32() {
        let mut array = [0; 32];

        assert_eq!(find_in_32(4, &array, 16), None);

        array[4] = 4;
        array[6] = 10;
        array[20] = 5;

        assert_eq!(find_in_32(4, &array, 32), Some(4));
        assert_eq!(find_in_32(5, &array, 32), Some(20));
        assert_eq!(find_in_32(10, &array, 32), Some(6));

        assert_eq!(find_in_32(4, &array, 5), Some(4));
        assert_eq!(find_in_32(5, &array, 5), None);
        assert_eq!(find_in_32(10, &array, 5), None);
    }
}