Debian Codegraph @

src/pattern.rs

raw symbols SCIP
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(craterust-analyzer cargo makefile-lossless 0.3.40 crate/) fn matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().(pattern: &str, target: &str) -> bool {
13 // No wildcard means exact match
14 if !rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().pattern.containsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]contains().('%') {
15 return pattern rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().target;
16 }
17
18 // GNU make supports exactly one '%' which matches any NON-EMPTY substring
19 let parts: Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<&str> = pattern.splitrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]split().('%').collectrust-analyzer cargo core https://github.com/rust-lang/rust/library/core iter/traits/iterator/Iterator#collect().();
20
21 // Only handle single % (GNU make doesn't support multiple %)
22 if parts.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().!=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]ne().2 {
23 // Multiple % or malformed pattern - just do exact match as fallback
24 return pattern rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[`&A`][`PartialEq<&B>`]eq().target;
25 }
26
27 let prefix = parts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().0]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().;
28 let suffix = parts[rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().1]rust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][`Index<I>`]index().;
29
30 // Target must be longer than prefix + suffix to have a non-empty stem
31 if target.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().<=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]le().prefix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().suffix.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().() {
32 return false;
33 }
34
35 // Check that target starts with prefix and ends with suffix
36 target.starts_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]starts_with().(prefix) && target.ends_withrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]ends_with().(suffix)
37 }
38
39 #[cfg(test)]
40 mod testsrust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/ {
41 use superrust-analyzer cargo makefile-lossless 0.3.40 pattern/::*;
42
43 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
44 fn test_matches_pattern_exactrust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/test_matches_pattern_exact().() {
45 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("foo.o", "foo.o"));
46 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("foo.o", "bar.o"));
47 }
48
49 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
50 fn test_matches_pattern_wildcard_onlyrust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/test_matches_pattern_wildcard_only().() {
51 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%", "a"));
52 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%", "anything"));
53 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%", "")); // % requires non-empty stem
54 }
55
56 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
57 fn test_matches_pattern_suffixrust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/test_matches_pattern_suffix().() {
58 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%.o", "foo.o"));
59 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%.o", "bar.o"));
60 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%.o", "foo.c"));
61 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%.o", ".o")); // % requires non-empty stem
62 }
63
64 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
65 fn test_matches_pattern_prefixrust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/test_matches_pattern_prefix().() {
66 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("test_%", "test_foo"));
67 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("test_%", "test_bar"));
68 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("test_%", "other_foo"));
69 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("test_%", "test_")); // % requires non-empty stem
70 }
71
72 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
73 fn test_matches_pattern_middlerust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/test_matches_pattern_middle().() {
74 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("foo%bar", "fooBARbar"));
75 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("foo%bar", "foo123bar"));
76 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("foo%bar", "foobar")); // % requires non-empty stem
77 }
78
79 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
80 fn test_matches_pattern_empty_stemrust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/test_matches_pattern_empty_stem().() {
81 // % must match at least one character (non-empty stem)
82 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%.o", ".o"));
83 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("foo%", "foo"));
84 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%bar", "bar"));
85 }
86
87 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
88 fn test_matches_pattern_multiple_wildcards_not_supportedrust-analyzer cargo makefile-lossless 0.3.40 pattern/tests/test_matches_pattern_multiple_wildcards_not_supported().() {
89 // Multiple % not supported - fallback to exact match
90 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%.%.o", "%.%.o"));
91 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(!rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/bit/impl#[bool][Not]not().matches_patternrust-analyzer cargo makefile-lossless 0.3.40 pattern/matches_pattern().("%.%.o", "foo.bar.o"));
92 }
93 }