devela/text/parse/
byte_search.rs

1// devela::text::parse::byte_search
2//
3//! Naive versions of `memchr` fns
4//
5
6#[cfg(feature = "dep_memchr")]
7use crate::_dep::memchr::*;
8#[cfg(not(feature = "dep_memchr"))]
9use crate::iif;
10
11#[doc = crate::TAG_NAMESPACE!()]
12/// A utility struct for searching bytes in slices.
13///
14/// # Features
15/// - Supports efficient search for bytes with `dep_memchr` enabled.
16/// - Provides fallback naive implementations otherwise.
17pub struct ByteSearch;
18
19impl ByteSearch {
20    /// Search for the first occurrence of a byte in a slice.
21    ///
22    /// # Features
23    /// Makes use of the `dep_memchr` dependency if enabled. See [memchr],
24    ///
25    /// [memchr]: fn@memchr
26    #[must_use] #[rustfmt::skip]
27    pub fn first1(needle: u8, haystack: &[u8]) -> Option<usize> {
28        #[cfg(feature = "dep_memchr")]
29        return memchr(needle, haystack);
30        #[cfg(not(feature = "dep_memchr"))]
31        haystack.iter().enumerate()
32            .find_map(|(index, &byte)| iif![byte == needle; Some(index); None])
33    }
34
35    /// Search for the first occurrence of two possible bytes in a haystack.
36    ///
37    /// # Features
38    /// Makes use of the `dep_memchr` dependency if enabled. See [memchr2],
39    #[must_use] #[rustfmt::skip]
40    pub fn first2(needle1: u8, needle2: u8, haystack: &[u8]) -> Option<usize> {
41        #[cfg(feature = "dep_memchr")]
42        return memchr2(needle1, needle2, haystack);
43        #[cfg(not(feature = "dep_memchr"))]
44        haystack.iter().enumerate()
45            .find_map(|(index, &byte)| iif![byte == needle1 || byte == needle2; Some(index); None])
46    }
47
48    /// Search for the first occurrence of three possible bytes in a haystack.
49    ///
50    /// # Features
51    /// Makes use of the `dep_memchr` dependency if enabled. See [memchr3],
52    #[must_use]
53    pub fn first3(needle1: u8, needle2: u8, needle3: u8, haystack: &[u8]) -> Option<usize> {
54        #[cfg(feature = "dep_memchr")]
55        return memchr3(needle1, needle2, needle3, haystack);
56        #[cfg(not(feature = "dep_memchr"))]
57        haystack.iter().enumerate().find_map(|(index, &byte)| {
58            iif![byte == needle1 || byte == needle2 || byte == needle3; Some(index); None]
59        })
60    }
61
62    /// Search for the last occurrence of a byte in a slice.
63    ///
64    /// # Features
65    /// Makes use of the `dep_memchr` dependency if enabled. See [memrchr],
66    #[must_use] #[rustfmt::skip]
67    pub fn last1(needle: u8, haystack: &[u8]) -> Option<usize> {
68        #[cfg(feature = "dep_memchr")]
69        return memrchr(needle, haystack);
70        #[cfg(not(feature = "dep_memchr"))]
71        haystack.iter().enumerate().rev()
72            .find_map(|(index, &byte)| iif![byte == needle; Some(index); None])
73    }
74
75    /// Search for the last occurrence of two possible bytes in a haystack.
76    ///
77    /// # Features
78    /// Makes use of the `dep_memchr` dependency if enabled. See [memrchr2],
79    #[must_use] #[rustfmt::skip]
80    pub fn last2(needle1: u8, needle2: u8, haystack: &[u8]) -> Option<usize> {
81        #[cfg(feature = "dep_memchr")]
82        return memrchr2(needle1, needle2, haystack);
83        #[cfg(not(feature = "dep_memchr"))]
84        haystack.iter().enumerate().rev()
85            .find_map(|(index, &byte)| iif![byte == needle1 || byte == needle2; Some(index); None])
86    }
87
88    /// Search for the last occurrence of three possible bytes in a haystack.
89    ///
90    /// # Features
91    /// Makes use of the `dep_memchr` dependency if enabled. See [memrchr3],
92    #[must_use]
93    pub fn last3(needle1: u8, needle2: u8, needle3: u8, haystack: &[u8]) -> Option<usize> {
94        #[cfg(feature = "dep_memchr")]
95        return memrchr3(needle1, needle2, needle3, haystack);
96        #[cfg(not(feature = "dep_memchr"))]
97        haystack.iter().enumerate().rev()
98            .find_map(|(index, &byte)|
99                iif![byte == needle1 || byte == needle2 || byte == needle3; Some(index); None])
100    }
101}