use crate::hir::Hir;
use super::{ShiftOr, ShiftOrInterpreter};
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
use super::jit::JitShiftOr;
pub struct ShiftOrEngine {
shift_or: ShiftOr,
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
jit: Option<JitShiftOr>,
}
impl std::fmt::Debug for ShiftOrEngine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("ShiftOrEngine")
.field("position_count", &self.shift_or.state_count())
.finish()
}
}
impl ShiftOrEngine {
pub fn from_hir(hir: &Hir) -> Option<Self> {
let shift_or = ShiftOr::from_hir(hir)?;
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
let jit = JitShiftOr::compile(&shift_or);
Some(Self {
shift_or,
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
jit,
})
}
pub fn new(shift_or: ShiftOr) -> Self {
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
let jit = JitShiftOr::compile(&shift_or);
Self {
shift_or,
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
jit,
}
}
#[inline]
pub fn is_match(&self, input: &[u8]) -> bool {
self.find(input).is_some()
}
#[inline]
pub fn find(&self, input: &[u8]) -> Option<(usize, usize)> {
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
if let Some(ref jit) = self.jit {
return jit.find(input);
}
ShiftOrInterpreter::new(&self.shift_or).find(input)
}
#[inline]
pub fn find_at(&self, input: &[u8], pos: usize) -> Option<(usize, usize)> {
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
if let Some(ref jit) = self.jit {
return jit.find_at(input, pos);
}
ShiftOrInterpreter::new(&self.shift_or).find_at(input, pos)
}
#[inline]
pub fn try_match_at(&self, input: &[u8], pos: usize) -> Option<(usize, usize)> {
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
if let Some(ref jit) = self.jit {
return jit.try_match_at(input, pos);
}
ShiftOrInterpreter::new(&self.shift_or).try_match_at(input, pos)
}
pub fn state_count(&self) -> usize {
self.shift_or.state_count()
}
pub fn shift_or(&self) -> &ShiftOr {
&self.shift_or
}
#[cfg(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64")))]
pub fn is_jit(&self) -> bool {
self.jit.is_some()
}
#[cfg(not(all(feature = "jit", any(target_arch = "x86_64", target_arch = "aarch64"))))]
pub fn is_jit(&self) -> bool {
false
}
}