use crate::core::util::automation::automaton::Automaton;
use crate::core::util::automation::run_automaton::RunAutomaton;
use crate::core::util::error::lucene_error::Result;
use std::char::decode_utf16;
use std::hash::{Hash, Hasher};
pub struct CharacterRunAutomaton {
pub base: RunAutomaton,
}
#[cfg(test)]
impl Clone for CharacterRunAutomaton {
fn clone(&self) -> Self {
Self {
base: self.base.clone(),
}
}
}
impl CharacterRunAutomaton {
pub fn new(automaton: Automaton) -> Result<Self> {
Ok(Self {
base: RunAutomaton::new(automaton, char::MAX as usize + 1)?,
})
}
pub fn run_str(&self, s: &str) -> Result<bool> {
let mut state = 0;
for ch in s.chars() {
state = self.base.step(state, ch as i32);
if state == -1 {
return Ok(false);
}
}
self.base.is_accept(state)
}
pub fn run_chars(&self, chars: &[u16], offset: usize, length: usize) -> Result<bool> {
let mut state: i32 = 0;
let iter = decode_utf16(chars[offset..offset + length].iter().cloned());
for result in iter {
let code_point = match result {
Ok(ch) => ch as i32,
Err(error) => i32::from(error.unpaired_surrogate()),
};
state = self.base.step(state, code_point);
if state == -1 {
return Ok(false);
}
}
self.base.is_accept(state)
}
}
impl PartialEq for CharacterRunAutomaton {
fn eq(&self, other: &Self) -> bool {
self.base.eq(&other.base)
}
}
impl Eq for CharacterRunAutomaton {}
impl Hash for CharacterRunAutomaton {
fn hash<H>(&self, state: &mut H)
where
H: Hasher,
{
self.base.hash(state)
}
}