vyre 0.4.0

GPU compute intermediate representation with a standard operation library
Documentation
// Boyer-Moore find operation module.



// Backend-specific lowering marker.



// CPU reference kernel for `string_matching.boyer_moore_find`.

use crate::ops::string_matching::search_contract::{to_u32_offset, validate_search_inputs, MatchError, NOT_FOUND};

/// Return the first Boyer-Moore-Horspool match offset, or [`NOT_FOUND`].
///
/// # Errors
///
/// Returns `Fix: ...` when either input exceeds the documented T47 cap.
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)
}