| 1 | /// Match a target against a pattern using make-style wildcard matching. |
| 2 | /// |
| 3 | /// Supports `%` as a wildcard that matches any sequence of characters. |
| 4 | /// For example, `%.o` matches `foo.o`, `bar.o`, etc. |
| 5 | /// |
| 6 | /// # Arguments |
| 7 | /// * `pattern` - The pattern to match against (e.g., "%.o") |
| 8 | /// * `target` - The target name to check (e.g., "foo.o") |
| 9 | /// |
| 10 | /// # Returns |
| 11 | /// `true` if the target matches the pattern, `false` otherwise |
| 12 | pub(crate) fn matches_pattern(pattern: &str, target: &str) -> bool { |
| 13 | // No wildcard means exact match |
| 14 | if !pattern.contains('%') { |
| 15 | return pattern == target; |
| 16 | } |
| 17 | |
| 18 | // GNU make supports exactly one '%' which matches any NON-EMPTY substring |
| 19 | let parts: Vec<&str> = pattern.split('%').collect(); |
| 20 | |
| 21 | // Only handle single % (GNU make doesn't support multiple %) |
| 22 | if parts.len() != 2 { |
| 23 | // Multiple % or malformed pattern - just do exact match as fallback |
| 24 | return pattern == target; |
| 25 | } |
| 26 | |
| 27 | let prefix = parts[0]; |
| 28 | let suffix = parts[1]; |
| 29 | |
| 30 | // Target must be longer than prefix + suffix to have a non-empty stem |
| 31 | if target.len() <= prefix.len() + suffix.len() { |
| 32 | return false; |
| 33 | } |
| 34 | |
| 35 | // Check that target starts with prefix and ends with suffix |
| 36 | target.starts_with(prefix) && target.ends_with(suffix) |
| 37 | } |
| 38 | |
| 39 | #[cfg(test)] |
| 40 | mod tests { |
| 41 | use super::*; |
| 42 | |
| 43 | #[test] |
| 44 | fn test_matches_pattern_exact() { |
| 45 | assert!(matches_pattern("foo.o", "foo.o")); |
| 46 | assert!(!matches_pattern("foo.o", "bar.o")); |
| 47 | } |
| 48 | |
| 49 | #[test] |
| 50 | fn test_matches_pattern_wildcard_only() { |
| 51 | assert!(matches_pattern("%", "a")); |
| 52 | assert!(matches_pattern("%", "anything")); |
| 53 | assert!(!matches_pattern("%", "")); // % requires non-empty stem |
| 54 | } |
| 55 | |
| 56 | #[test] |
| 57 | fn test_matches_pattern_suffix() { |
| 58 | assert!(matches_pattern("%.o", "foo.o")); |
| 59 | assert!(matches_pattern("%.o", "bar.o")); |
| 60 | assert!(!matches_pattern("%.o", "foo.c")); |
| 61 | assert!(!matches_pattern("%.o", ".o")); // % requires non-empty stem |
| 62 | } |
| 63 | |
| 64 | #[test] |
| 65 | fn test_matches_pattern_prefix() { |
| 66 | assert!(matches_pattern("test_%", "test_foo")); |
| 67 | assert!(matches_pattern("test_%", "test_bar")); |
| 68 | assert!(!matches_pattern("test_%", "other_foo")); |
| 69 | assert!(!matches_pattern("test_%", "test_")); // % requires non-empty stem |
| 70 | } |
| 71 | |
| 72 | #[test] |
| 73 | fn test_matches_pattern_middle() { |
| 74 | assert!(matches_pattern("foo%bar", "fooBARbar")); |
| 75 | assert!(matches_pattern("foo%bar", "foo123bar")); |
| 76 | assert!(!matches_pattern("foo%bar", "foobar")); // % requires non-empty stem |
| 77 | } |
| 78 | |
| 79 | #[test] |
| 80 | fn test_matches_pattern_empty_stem() { |
| 81 | // % must match at least one character (non-empty stem) |
| 82 | assert!(!matches_pattern("%.o", ".o")); |
| 83 | assert!(!matches_pattern("foo%", "foo")); |
| 84 | assert!(!matches_pattern("%bar", "bar")); |
| 85 | } |
| 86 | |
| 87 | #[test] |
| 88 | fn test_matches_pattern_multiple_wildcards_not_supported() { |
| 89 | // Multiple % not supported - fallback to exact match |
| 90 | assert!(matches_pattern("%.%.o", "%.%.o")); |
| 91 | assert!(!matches_pattern("%.%.o", "foo.bar.o")); |
| 92 | } |
| 93 | } |