use crate::nfa::AnchorEnvironment;
pub(crate) trait AnchorPolicy: Copy + Clone {
fn anchor_environment(&self) -> Option<AnchorEnvironment>;
fn update_row(&mut self, row: usize);
}
#[derive(Copy, Clone)]
pub(crate) struct IgnoreAnchors;
impl AnchorPolicy for IgnoreAnchors {
#[inline(always)]
fn anchor_environment(&self) -> Option<AnchorEnvironment> {
None
}
#[inline(always)]
fn update_row(&mut self, _row: usize) {}
}
#[derive(Copy, Clone)]
pub(crate) struct CheckAnchors {
pub(crate) virt_row: usize,
pub(crate) total_virt_rows: usize,
pub(crate) absolute_partition_start: usize,
pub(crate) has_virtual_end: bool,
}
impl AnchorPolicy for CheckAnchors {
#[inline(always)]
fn anchor_environment(&self) -> Option<AnchorEnvironment> {
let at_start = self.virt_row == self.absolute_partition_start;
if self.has_virtual_end
&& self.virt_row.checked_add(1) == Some(self.total_virt_rows)
{
return Some(if at_start {
AnchorEnvironment::VirtualStartEndEmpty
} else {
AnchorEnvironment::VirtualEnd
});
}
Some(if at_start {
AnchorEnvironment::PhysicalStart
} else {
AnchorEnvironment::PhysicalInterior
})
}
#[inline(always)]
fn update_row(&mut self, row: usize) {
self.virt_row = row;
}
}
#[cfg(test)]
mod tests {
use super::{AnchorPolicy, CheckAnchors};
use crate::nfa::AnchorEnvironment;
#[test]
fn end_anchor_comparison_is_exact_at_usize_boundary() {
let mut policy = CheckAnchors {
virt_row: usize::MAX - 1,
total_virt_rows: usize::MAX,
absolute_partition_start: 0,
has_virtual_end: true,
};
assert_eq!(
policy.anchor_environment(),
Some(AnchorEnvironment::VirtualEnd)
);
policy.update_row(usize::MAX);
assert_eq!(
policy.anchor_environment(),
Some(AnchorEnvironment::PhysicalInterior)
);
}
}