1pub fn matches(pattern: &str, haystack: &str) -> bool {
4 if pattern.is_empty() {
5 return true;
6 }
7 let pattern_lower: Vec<char> = pattern.chars().flat_map(|c| c.to_lowercase()).collect();
8 let mut pi = 0usize;
9 for hc in haystack.chars().flat_map(|c| c.to_lowercase()) {
10 if pi < pattern_lower.len() && hc == pattern_lower[pi] {
11 pi += 1;
12 if pi == pattern_lower.len() {
13 return true;
14 }
15 }
16 }
17 pi == pattern_lower.len()
18}
19
20#[cfg(test)]
21mod tests {
22 use super::*;
23
24 #[test]
25 fn substring_matches() {
26 assert!(matches("bil", "billing-svc"));
27 }
28
29 #[test]
30 fn case_insensitive() {
31 assert!(matches("BSV", "billing-svc"));
32 assert!(matches("mAiN", "MAIN"));
33 }
34
35 #[test]
36 fn empty_pattern_matches_anything() {
37 assert!(matches("", "anything"));
38 assert!(matches("", ""));
39 }
40
41 #[test]
42 fn non_match_returns_false() {
43 assert!(!matches("xyz", "billing-svc"));
44 }
45
46 #[test]
47 fn order_is_enforced() {
48 assert!(!matches("cba", "abc"));
49 }
50
51 #[test]
52 fn non_ascii() {
53 assert!(matches("café", "Café Latté"));
54 }
55}