Debian Codegraph @

src/sudo/env/wildcard_match.rs

raw symbols SCIP
1 /// Match a test input with a pattern
2 /// Only wildcard characters (*) in the pattern string have a special meaning: they match on zero or more characters
3 pub(superrust-analyzer cargo sudo-rs 0.2.14 sudo/env/) fn wildcard_matchrust-analyzer cargo sudo-rs 0.2.14 sudo/env/wildcard_match/wildcard_match().(test: &[u8], pattern: &[u8]) -> bool {
4 let mut test_index = 0;
5 let mut pattern_index = 0;
6 let mut last_star = Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#;
7
8 loop {
9 match (pattern.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(pattern_index), test.getrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]get().(test_index)) {
10 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(p), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(t)) => {
11 if *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().p rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[u8][`PartialEq<Self>`]eq().b'*' {
12 pattern_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
13 last_star = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((test_index, pattern_index));
14 } else if p 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().t {
15 pattern_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
16 test_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
17 } else if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((t_index, p_index)) = last_star {
18 test_index = t_index 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().1;
19 pattern_index = p_index;
20 last_star = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((test_index, pattern_index));
21 } else {
22 return false;
23 }
24 }
25 (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(_)) => {
26 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((t_index, p_index)) = last_star {
27 test_index = t_index 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().1;
28 pattern_index = p_index;
29 last_star = Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#((test_index, pattern_index));
30 } else {
31 return false;
32 }
33 }
34 (Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(b'*'), Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => {
35 pattern_index rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().1;
36 }
37 (Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#) => {
38 return true;
39 }
40 _ => {
41 return false;
42 }
43 }
44 }
45 }
46
47 #[cfg(test)]
48 mod testsrust-analyzer cargo sudo-rs 0.2.14 sudo/env/wildcard_match/tests/ {
49 use superrust-analyzer cargo sudo-rs 0.2.14 sudo/env/wildcard_match/::wildcard_matchrust-analyzer cargo sudo-rs 0.2.14 sudo/env/wildcard_match/wildcard_match().;
50
51 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
52 fn test_wildcard_matchrust-analyzer cargo sudo-rs 0.2.14 sudo/env/wildcard_match/tests/test_wildcard_match().() {
53 let tests = vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc macros/vec!![
54 ("foo bar", "foo *", true),
55 ("foo bar", "foo ba*", true),
56 ("foo bar", "foo *ar", true),
57 ("foo bar", "foo *r", true),
58 ("foo bar", "foo *ab", false),
59 ("foo bar", "foo r*", false),
60 ("foo bar", "*oo bar", true),
61 ("foo bar", "*f* bar", true),
62 ("foo bar", "*f bar", false),
63 ("foo ", "foo *", true),
64 ("foo", "foo *", false),
65 ("foo", "foo*", true),
66 ("foo bar", "f*******r", true),
67 ("foo******bar", "f*r", true),
68 ("foo********bar", "foo bar", false),
69 ("#%^$V@#TYH%&rot13%#@$%#$%", "#%^$V@#*t13%#@$%#$%", true),
70 ("#%^$V@#TYH%&rot13%#@$%#$%", "*%^*%&rot*%#$%", true),
71 ("#%^$V@#TYH%&rot13%#@$%#$%", "#%^$V@#TYH%&r*%#@$#$%", false),
72 ("#%^$V@#TYH%&rot13%#@$%#$%", "#%^$V@#*******@$%#$%", true),
73 ];
74
75 for (test, pattern, expected) in tests.into_iterrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`][IntoIterator]into_iter().() {
76 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
77 wildcard_matchrust-analyzer cargo sudo-rs 0.2.14 sudo/env/wildcard_match/wildcard_match().(test.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().(), pattern.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().()),
78 expected,
79 "\"{}\" {} match {}",
80 test,
81 if expected { "should" } else { "should not" },
82 pattern
83 );
84 }
85 }
86 }