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
93
/*!
This module defines simple wrapper routines for the memchr functions from the
`memchr` crate. Basically, when the `memchr` crate is available, we use it,
otherwise we use a naive implementation which is still pretty fast.
*/

pub(crate) use self::inner::*;

#[cfg(feature = "perf-literal-substring")]
pub(super) mod inner {
    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
        memchr::memchr(n1, haystack)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
        memchr::memchr2(n1, n2, haystack)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memchr3(
        n1: u8,
        n2: u8,
        n3: u8,
        haystack: &[u8],
    ) -> Option<usize> {
        memchr::memchr3(n1, n2, n3, haystack)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
        memchr::memrchr(n1, haystack)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
        memchr::memrchr2(n1, n2, haystack)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memrchr3(
        n1: u8,
        n2: u8,
        n3: u8,
        haystack: &[u8],
    ) -> Option<usize> {
        memchr::memrchr3(n1, n2, n3, haystack)
    }
}

#[cfg(not(feature = "perf-literal-substring"))]
pub(super) mod inner {
    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memchr(n1: u8, haystack: &[u8]) -> Option<usize> {
        haystack.iter().position(|&b| b == n1)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
        haystack.iter().position(|&b| b == n1 || b == n2)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memchr3(
        n1: u8,
        n2: u8,
        n3: u8,
        haystack: &[u8],
    ) -> Option<usize> {
        haystack.iter().position(|&b| b == n1 || b == n2 || b == n3)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memrchr(n1: u8, haystack: &[u8]) -> Option<usize> {
        haystack.iter().rposition(|&b| b == n1)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memrchr2(n1: u8, n2: u8, haystack: &[u8]) -> Option<usize> {
        haystack.iter().rposition(|&b| b == n1 || b == n2)
    }

    #[cfg_attr(feature = "perf-inline", inline(always))]
    pub(crate) fn memrchr3(
        n1: u8,
        n2: u8,
        n3: u8,
        haystack: &[u8],
    ) -> Option<usize> {
        haystack.iter().rposition(|&b| b == n1 || b == n2 || b == n3)
    }
}