use super::anchor::find_best_anchor_sequence;
#[derive(Debug, Clone)]
pub struct Pattern {
pub bytes: Vec<u8>,
pub mask: Vec<bool>,
pub mask_bytes: Vec<u8>,
pub anchor_sequence: Option<Vec<(usize, u8)>>,
}
impl Pattern {
pub fn from_str(pattern_str: &str) -> Result<Self, String> {
let mut bytes = Vec::new();
let mut mask = Vec::new();
let mut mask_bytes = Vec::new();
for token in pattern_str.split_whitespace() {
if token == "??" || token == "?" {
bytes.push(0);
mask.push(false);
mask_bytes.push(0x00);
} else {
match u8::from_str_radix(token, 16) {
Ok(b) => {
bytes.push(b);
mask.push(true);
mask_bytes.push(0xFF);
}
Err(_) => return Err(format!("Invalid hex byte: {}", token)),
}
}
}
if bytes.is_empty() {
return Err("Pattern cannot be empty".to_string());
}
let anchor_sequence = find_best_anchor_sequence(&bytes, &mask);
Ok(Self {
bytes,
mask,
mask_bytes,
anchor_sequence,
})
}
}