pub fn fuzzy(query: &str, text: &str) -> Option<u32> {
let text: Vec<char> = text.chars().flat_map(char::to_lowercase).collect();
let mut from = 0;
let mut score = 0;
for part in query.split_whitespace() {
let part: Vec<char> = part.chars().flat_map(char::to_lowercase).collect();
let (cost, end) = best(&part, &text, from)?;
score += cost;
from = end;
}
Some(score)
}
fn best(part: &[char], text: &[char], from: usize) -> Option<(u32, usize)> {
let first = *part.first()?;
let mut found: Option<(u32, usize)> = None;
for start in (from..text.len()).filter(|at| text[*at] == first) {
let mut at = start;
let mut gaps = 0;
let mut matched = true;
for letter in &part[1..] {
match text[at + 1..].iter().position(|c| c == letter) {
Some(skipped) => {
gaps += skipped as u32;
at += 1 + skipped;
}
None => {
matched = false;
break;
}
}
}
if !matched {
break;
}
let word_start = start == 0 || !text[start - 1].is_alphanumeric();
let cost = gaps * 2
+ if word_start {
0
} else {
3 + (start - from).min(20) as u32
};
if found.is_none_or(|(lowest, _)| cost < lowest) {
found = Some((cost, at + 1));
}
}
found
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn letters_in_order_match_also_with_gaps() {
assert!(fuzzy("chk", "Checkout").is_some());
assert!(fuzzy("check", "Checkout CHK").is_some());
assert!(fuzzy("COUT", "checkout").is_some());
assert!(fuzzy("mm bil", "module:make-model Billing").is_some());
assert!(fuzzy("", "anything").is_some());
assert!(fuzzy(" ", "anything").is_some());
}
#[test]
fn letters_out_of_order_or_missing_do_not_match() {
assert_eq!(fuzzy("zzq", "Checkout"), None);
assert_eq!(fuzzy("tuo", "Checkout"), None);
assert_eq!(fuzzy("bil mm", "module:make-model Billing"), None);
assert_eq!(fuzzy("a", ""), None);
}
#[test]
fn a_closer_match_scores_lower() {
let score = |text: &str| fuzzy("ship", text).unwrap();
assert!(score("Shipping") < score("Scholarship"));
assert!(score("Order shipping") < score("Fellowship"));
assert_eq!(fuzzy("check", "Checkout"), Some(0));
}
}