use crate::ops::string_matching::search_contract::{to_u32_offset, validate_search_inputs, MatchError, NOT_FOUND};
pub fn boyer_moore_find(haystack: &[u8], needle: &[u8]) -> Result<u32, MatchError> {
validate_search_inputs(haystack, needle)?;
if needle.is_empty() {
return Ok(0);
}
if needle.len() > haystack.len() {
return Ok(NOT_FOUND);
}
let mut shift = [needle.len(); 256];
for (index, &byte) in needle[..needle.len() - 1].iter().enumerate() {
shift[usize::from(byte)] = needle.len() - 1 - index;
}
let mut pos = 0usize;
while pos + needle.len() <= haystack.len() {
let mut matched = needle.len();
while matched > 0 && needle[matched - 1] == haystack[pos + matched - 1] {
matched -= 1;
}
if matched == 0 {
return to_u32_offset(pos);
}
pos += shift[usize::from(haystack[pos + needle.len() - 1])].max(1);
}
Ok(NOT_FOUND)
}