use std::collections::{HashSet};
use bimap::BiHashMap;
use crate::engine::Engine;
pub struct Builder {
custom: Vec<& 'static str>,
custom_spaces: bool,
}
impl Default for Builder {
fn default() -> Self {
Self {
custom: vec!["http://", "https://", ".com", "\n\r\n", "\r\n\r", "C:\\", ".co.uk"],
custom_spaces: false,
}
}
}
impl Builder {
pub fn empty() -> Self {
Self {
custom: Vec::new(),
custom_spaces: false,
}
}
pub fn set_custom(& mut self, list: Vec<& 'static str>) -> & mut Self {
self.custom = list;
self
}
pub fn set_custom_spaces(& mut self, spaces: bool) -> & mut Self {
self.custom_spaces = spaces;
self
}
pub fn push_custom(& mut self, custom: & 'static str) -> & mut Self {
self.custom.push(custom);
self
}
pub fn clear_custom(& mut self) -> & mut Self {
self.custom.clear();
self
}
pub fn len_custom(& self) -> usize {
self.custom.len()
}
pub fn engine(&self) -> Engine {
let max_len = if self.custom_spaces { 16 } else { 32 };
let v = if self.custom.len() <= max_len { self.custom.clone() } else { (&self.custom[0..max_len]).to_vec() };
let mut map = BiHashMap::new();
let mut lengths = HashSet::new();
for (i, string) in v.iter().enumerate() {
map.insert(string.as_bytes(), i);
lengths.insert(string.len());
}
let mut lengths: Vec<_> = lengths.iter().map(|x| *x).collect();
lengths.sort();
lengths.reverse();
Engine {
custom_spaces: self.custom_spaces,
custom_map: map,
lengths,
}
}
}