devela/text/parse/
byte_search.rs
1#[cfg(feature = "dep_memchr")]
7use crate::_dep::memchr::*;
8#[cfg(not(feature = "dep_memchr"))]
9use crate::iif;
10
11#[doc = crate::TAG_NAMESPACE!()]
12pub struct ByteSearch;
18
19impl ByteSearch {
20 #[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 #[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 #[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 #[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 #[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 #[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}