devela/text/parse/
byte_search.rs

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
94
95
96
97
98
99
100
// devela::text::parse::byte_search
//
//! Naive versions of `memchr` fns
//

#[cfg(feature = "dep_memchr")]
use crate::_dep::memchr::*;
#[cfg(not(feature = "dep_memchr"))]
use crate::iif;

/// A utility struct for searching bytes in slices.
///
/// # Features
/// - Supports efficient search for bytes with `dep_memchr` enabled.
/// - Provides fallback naive implementations otherwise.
pub struct ByteSearch;

impl ByteSearch {
    /// Search for the first occurrence of a byte in a slice.
    ///
    /// # Features
    /// Makes use of the `dep_memchr` dependency if enabled. See [memchr],
    ///
    /// [memchr]: fn@memchr
    #[must_use] #[rustfmt::skip]
    pub fn first1(needle: u8, haystack: &[u8]) -> Option<usize> {
        #[cfg(feature = "dep_memchr")]
        return memchr(needle, haystack);
        #[cfg(not(feature = "dep_memchr"))]
        haystack.iter().enumerate()
            .find_map(|(index, &byte)| iif![byte == needle; Some(index); None])
    }

    /// Search for the first occurrence of two possible bytes in a haystack.
    ///
    /// # Features
    /// Makes use of the `dep_memchr` dependency if enabled. See [memchr2],
    #[must_use] #[rustfmt::skip]
    pub fn first2(needle1: u8, needle2: u8, haystack: &[u8]) -> Option<usize> {
        #[cfg(feature = "dep_memchr")]
        return memchr2(needle1, needle2, haystack);
        #[cfg(not(feature = "dep_memchr"))]
        haystack.iter().enumerate()
            .find_map(|(index, &byte)| iif![byte == needle1 || byte == needle2; Some(index); None])
    }

    /// Search for the first occurrence of three possible bytes in a haystack.
    ///
    /// # Features
    /// Makes use of the `dep_memchr` dependency if enabled. See [memchr3],
    #[must_use]
    pub fn first3(needle1: u8, needle2: u8, needle3: u8, haystack: &[u8]) -> Option<usize> {
        #[cfg(feature = "dep_memchr")]
        return memchr3(needle1, needle2, needle3, haystack);
        #[cfg(not(feature = "dep_memchr"))]
        haystack.iter().enumerate().find_map(|(index, &byte)| {
            iif![byte == needle1 || byte == needle2 || byte == needle3; Some(index); None]
        })
    }

    /// Search for the last occurrence of a byte in a slice.
    ///
    /// # Features
    /// Makes use of the `dep_memchr` dependency if enabled. See [memrchr],
    #[must_use] #[rustfmt::skip]
    pub fn last1(needle: u8, haystack: &[u8]) -> Option<usize> {
        #[cfg(feature = "dep_memchr")]
        return memrchr(needle, haystack);
        #[cfg(not(feature = "dep_memchr"))]
        haystack.iter().enumerate().rev()
            .find_map(|(index, &byte)| iif![byte == needle; Some(index); None])
    }

    /// Search for the last occurrence of two possible bytes in a haystack.
    ///
    /// # Features
    /// Makes use of the `dep_memchr` dependency if enabled. See [memrchr2],
    #[must_use] #[rustfmt::skip]
    pub fn last2(needle1: u8, needle2: u8, haystack: &[u8]) -> Option<usize> {
        #[cfg(feature = "dep_memchr")]
        return memrchr2(needle1, needle2, haystack);
        #[cfg(not(feature = "dep_memchr"))]
        haystack.iter().enumerate().rev()
            .find_map(|(index, &byte)| iif![byte == needle1 || byte == needle2; Some(index); None])
    }

    /// Search for the last occurrence of three possible bytes in a haystack.
    ///
    /// # Features
    /// Makes use of the `dep_memchr` dependency if enabled. See [memrchr3],
    #[must_use]
    pub fn last3(needle1: u8, needle2: u8, needle3: u8, haystack: &[u8]) -> Option<usize> {
        #[cfg(feature = "dep_memchr")]
        return memrchr3(needle1, needle2, needle3, haystack);
        #[cfg(not(feature = "dep_memchr"))]
        haystack.iter().enumerate().rev()
            .find_map(|(index, &byte)|
                iif![byte == needle1 || byte == needle2 || byte == needle3; Some(index); None])
    }
}