devela/text/parse/
byte_search.rs#[cfg(feature = "dep_memchr")]
use crate::_dep::memchr::*;
#[cfg(not(feature = "dep_memchr"))]
use crate::iif;
pub struct ByteSearch;
impl ByteSearch {
#[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])
}
#[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])
}
#[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]
})
}
#[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])
}
#[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])
}
#[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])
}
}