Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions codex-rs/common/src/fuzzy_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,17 @@ pub fn fuzzy_indices(haystack: &str, needle: &str) -> Option<Vec<usize>> {
})
}

/// Returns true when `needle` fuzzy-matches `haystack`.
pub fn fuzzy_matches(haystack: &str, needle: &str) -> bool {
fuzzy_match(haystack, needle).is_some()
}

/// Returns the computed fuzzy score for `needle` against `haystack` if it
/// matches; smaller is better. Useful when a caller only needs the score.
pub fn fuzzy_score(haystack: &str, needle: &str) -> Option<i32> {
fuzzy_match(haystack, needle).map(|(_idx, score)| score)
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down Expand Up @@ -174,4 +185,15 @@ mod tests {
// Lowercasing 'İ' expands to two chars; contiguous prefix -> window 0 with bonus
assert_eq!(score, -100);
}

#[test]
fn helpers_match_and_score() {
assert!(fuzzy_matches("hello", "hl"));
assert!(!fuzzy_matches("hello", "xz"));

let score = fuzzy_score("hello", "hl").expect("should have score");
assert!(score < 0);

assert!(fuzzy_score("hello", "xz").is_none());
}
}