Skip to main content

vyre_reference/dual_impls/scan/literal/
reference.rs

1use crate::{dual_impls::common, workgroup::Memory};
2use vyre_primitives::PatternMatchLiteral;
3
4impl common::ReferenceEvaluator for PatternMatchLiteral {
5    fn evaluate(&self, inputs: &[Memory]) -> Result<Memory, common::EvalError> {
6        let haystack = common::one_input(inputs, "scan_literal")?;
7        if self.literal.is_empty() {
8            return Err(common::EvalError::new(
9                "primitive `scan_literal` has empty literal. Fix: pass a non-empty literal.",
10            ));
11        }
12        let mut offsets = Vec::new();
13        for offset in 0..=haystack.len().saturating_sub(self.literal.len()) {
14            if haystack[offset..].starts_with(&self.literal) {
15                offsets.push(u32::try_from(offset).map_err(|_| {
16                    common::EvalError::new(
17                        "primitive `scan_literal` offset exceeds u32. Fix: split haystacks before 4 GiB.",
18                    )
19                })?);
20            }
21        }
22        Ok(common::write_u32s(offsets))
23    }
24}