|
| 1 | +/// Returns true if `path` matches all `keywords` using the zoxide matching algorithm: |
| 2 | +/// * All terms must be present within the path, in order. |
| 3 | +/// * The last component of the last keyword must be contained in the last component of the path. |
| 4 | +#[must_use] |
| 5 | +pub fn matches_zoxide_algo(path: &str, keywords: &[String]) -> bool { |
| 6 | + if keywords.is_empty() { |
| 7 | + return true; |
| 8 | + } |
| 9 | + |
| 10 | + let path_lower = path.to_lowercase(); |
| 11 | + let mut search_start = 0; |
| 12 | + |
| 13 | + for keyword in keywords { |
| 14 | + let kw_lower = keyword.to_lowercase(); |
| 15 | + if let Some(found_offset) = path_lower[search_start..].find(&kw_lower) { |
| 16 | + search_start += found_offset + kw_lower.len(); |
| 17 | + } else { |
| 18 | + return false; |
| 19 | + } |
| 20 | + } |
| 21 | + |
| 22 | + let last_keyword = keywords.last().expect("keywords is non-empty"); |
| 23 | + let last_kw_lower = last_keyword.to_lowercase(); |
| 24 | + let kw_last_component = last_kw_lower |
| 25 | + .split('/') |
| 26 | + .next_back() |
| 27 | + .unwrap_or(&last_kw_lower); |
| 28 | + let path_last_component = path_lower.split('/').next_back().unwrap_or(&path_lower); |
| 29 | + |
| 30 | + path_last_component.contains(kw_last_component) |
| 31 | +} |
| 32 | + |
| 33 | +#[allow(clippy::unwrap_used, reason = "unwrap() OK inside tests")] |
| 34 | +#[cfg(test)] |
| 35 | +mod tests { |
| 36 | + use super::*; |
| 37 | + use proptest::prelude::*; |
| 38 | + |
| 39 | + #[test] |
| 40 | + fn test_matches_zoxide_empty_keywords() { |
| 41 | + assert!(matches_zoxide_algo("/foo/bar", &[])); |
| 42 | + } |
| 43 | + |
| 44 | + #[test] |
| 45 | + fn test_matches_zoxide_basic() { |
| 46 | + assert!(matches_zoxide_algo("/foo/bar", &["bar".to_owned()])); |
| 47 | + } |
| 48 | + |
| 49 | + #[test] |
| 50 | + fn test_matches_zoxide_last_component_rule() { |
| 51 | + // "bar" must appear in last component of path |
| 52 | + assert!(!matches_zoxide_algo("/bar/foo", &["bar".to_owned()])); |
| 53 | + } |
| 54 | + |
| 55 | + #[test] |
| 56 | + fn test_matches_zoxide_case_insensitive() { |
| 57 | + assert!(matches_zoxide_algo("/FOO/BAR", &["bar".to_owned()])); |
| 58 | + assert!(matches_zoxide_algo("/foo/bar", &["BAR".to_owned()])); |
| 59 | + } |
| 60 | + |
| 61 | + #[test] |
| 62 | + fn test_matches_zoxide_multiple_keywords_ordered() { |
| 63 | + assert!(matches_zoxide_algo( |
| 64 | + "/foo/bar", |
| 65 | + &["fo".to_owned(), "ba".to_owned()] |
| 66 | + )); |
| 67 | + // reversed order should not match |
| 68 | + assert!(!matches_zoxide_algo( |
| 69 | + "/foo/bar", |
| 70 | + &["ba".to_owned(), "fo".to_owned()] |
| 71 | + )); |
| 72 | + } |
| 73 | + |
| 74 | + #[test] |
| 75 | + fn test_matches_zoxide_slash_in_keyword() { |
| 76 | + // z foo/bar matches /foo/bar but not /foo/bar/baz |
| 77 | + assert!(matches_zoxide_algo("/foo/bar", &["foo/bar".to_owned()])); |
| 78 | + assert!(!matches_zoxide_algo( |
| 79 | + "/foo/bar/baz", |
| 80 | + &["foo/bar".to_owned()] |
| 81 | + )); |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_matches_zoxide_slash_separated_keywords() { |
| 86 | + // z fo / ba matches /foo/bar but not /foobar |
| 87 | + assert!(matches_zoxide_algo( |
| 88 | + "/foo/bar", |
| 89 | + &["fo".to_owned(), "/".to_owned(), "ba".to_owned()] |
| 90 | + )); |
| 91 | + assert!(!matches_zoxide_algo( |
| 92 | + "/foobar", |
| 93 | + &["fo".to_owned(), "/".to_owned(), "ba".to_owned()] |
| 94 | + )); |
| 95 | + } |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn test_matches_zoxide_file_last_component() { |
| 99 | + // Keyword matching against a file path — last component is the filename |
| 100 | + assert!(matches_zoxide_algo( |
| 101 | + "/home/user/docs/report.pdf", |
| 102 | + &["docs".to_owned(), "rep".to_owned()] |
| 103 | + )); |
| 104 | + assert!(matches_zoxide_algo( |
| 105 | + "/home/user/docs/report.pdf", |
| 106 | + &["rep".to_owned()] |
| 107 | + )); |
| 108 | + // "docs" must be in last component (filename), not a directory |
| 109 | + assert!(!matches_zoxide_algo( |
| 110 | + "/home/user/docs/report.pdf", |
| 111 | + &["docs".to_owned()] |
| 112 | + )); |
| 113 | + } |
| 114 | + |
| 115 | + proptest! { |
| 116 | + #[test] |
| 117 | + fn prop_keyword_in_last_component_matches( |
| 118 | + prefix in "[a-z]{1,8}", |
| 119 | + keyword in "[a-z]{2,8}", |
| 120 | + suffix in "[a-z]{0,5}", |
| 121 | + ) { |
| 122 | + // Construct a path where the keyword is embedded in the last component. |
| 123 | + let path = format!("/{prefix}/{keyword}{suffix}"); |
| 124 | + prop_assert!(matches_zoxide_algo(&path, core::slice::from_ref(&keyword)), |
| 125 | + "path={path} should match keyword={keyword}"); |
| 126 | + } |
| 127 | + |
| 128 | + #[test] |
| 129 | + fn prop_keyword_only_in_non_last_component_doesnt_match( |
| 130 | + keyword in "[a-z]{4,8}", |
| 131 | + last_component in "[a-z]{4,8}", |
| 132 | + ) { |
| 133 | + // The last component must not contain the keyword. |
| 134 | + prop_assume!(!last_component.contains(keyword.as_str())); |
| 135 | + let path = format!("/{keyword}/{last_component}"); |
| 136 | + prop_assert!(!matches_zoxide_algo(&path, core::slice::from_ref(&keyword)), |
| 137 | + "keyword={keyword} should not match when absent from last component of path={path}"); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments