use topcoat::Result; use topcoat::context::Cx; use topcoat::view::{View, component, view}; use crate::i18n::Lang; use crate::i18n::s; pub const MAX_QUERY_CHARS: usize = 100; #[derive(Clone, Debug, Default)] pub struct Finder { pub q: String, pub exact: bool, pub case: bool, needle: String, words: Vec>, } impl Finder { pub fn new(q: Option<&str>, exact: Option<&str>, case: Option<&str>) -> Finder { let q: String = q .unwrap_or("") .trim() .chars() .take(MAX_QUERY_CHARS) .collect(); let exact = exact.is_some_and(|v| !v.is_empty()); let case = case.is_some_and(|v| !v.is_empty()); let needle = if case { q.clone() } else { q.to_lowercase() }; let words = needle .split(|c: char| !c.is_alphanumeric()) .filter(|w| !w.is_empty()) .map(|w| w.chars().collect()) .collect(); Finder { q, exact, case, needle, words, } } pub fn is_empty(&self) -> bool { self.needle.is_empty() } pub fn matches(&self, text: &str) -> bool { if self.is_empty() { return true; } let hay = if self.case { text.to_owned() } else { text.to_lowercase() }; if hay.contains(&self.needle) { return true; } if self.exact { return false; } let tokens: Vec> = hay .split(|c: char| !c.is_alphanumeric()) .filter(|t| !t.is_empty()) .map(|t| t.chars().collect()) .collect(); !self.words.is_empty() && self.words.iter().all(|word| { tokens .iter() .any(|token| token.starts_with(word) || (!self.case && near(word, token))) }) } pub fn matches_any(&self, fields: &[&str]) -> bool { self.is_empty() || self.matches(&fields.join(" \u{1f} ")) } pub fn params(&self) -> Vec<(&'static str, &str)> { let mut out = vec![("q", self.q.as_str())]; if self.exact { out.push(("exact", "1")); } if self.case { out.push(("case", "1")); } out } } fn slips_for(len: usize) -> usize { match len { 0..=3 => 0, 4..=7 => 1, _ => 2, } } fn near(word: &[char], token: &[char]) -> bool { let slips = slips_for(word.len()); if slips == 0 { return false; } if distance(word, token) <= slips { return true; } token.len() > word.len() && distance(word, &token[..word.len()]) <= slips } fn distance(a: &[char], b: &[char]) -> usize { if a.len().abs_diff(b.len()) > 2 { return usize::MAX; } let (n, m) = (a.len(), b.len()); let mut d = vec![vec![0usize; m + 1]; n + 1]; for (i, row) in d.iter_mut().enumerate() { row[0] = i; } for j in 0..=m { d[0][j] = j; } for i in 1..=n { for j in 1..=m { let cost = usize::from(a[i - 1] != b[j - 1]); let mut best = (d[i - 1][j] + 1) .min(d[i][j - 1] + 1) .min(d[i - 1][j - 1] + cost); if i > 1 && j > 1 && a[i - 1] == b[j - 2] && a[i - 2] == b[j - 1] { best = best.min(d[i - 2][j - 2] + 1); } d[i][j] = best; } } d[n][m] } #[component] pub async fn finder_field( cx: &Cx, lang: Lang, finder: Finder, placeholder: &str, #[default] id: Option<&str>, ) -> Result { let id = id.unwrap_or("find-q"); Ok(view! {
}) } #[cfg(test)] mod tests { use super::Finder; fn find(q: &str, exact: bool, case: bool) -> Finder { Finder::new(Some(q), exact.then_some("1"), case.then_some("1")) } #[test] fn forgives_typos_and_case_unless_asked() { let text = "Repository created · Hezarfen/hezarfen_landing"; assert!(find("", false, false).matches(text)); assert!(find("reposotory", false, false).matches(text)); assert!(find("landnig hezarfen", false, false).matches(text)); assert!(find("crea", false, false).matches(text)); assert!(!find("deleted", false, false).matches(text)); assert!(!find("reposotory", true, false).matches(text)); assert!(find("repository CREATED", true, false).matches(text)); assert!(!find("repository", false, true).matches(text)); assert!(find("Repository", false, true).matches(text)); assert!(!find("bulk 1", false, false).matches("tahinli/bulk-21")); assert!(find("bulk 1", false, false).matches("tahinli/bulk-12")); } }