Skip to content

Simple bracket expressions #1138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 73 additions & 1 deletion src/sudo/env/wildcard_match.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/// Match a test input with a pattern
/// Match a test input with a pattern
/// Only wildcard characters (*) in the pattern string have a special meaning: they match on zero or more characters
pub(super) fn wildcard_match(test: &[u8], pattern: &[u8]) -> bool {
let mut test_index = 0;
Expand Down Expand Up @@ -44,8 +44,45 @@ pub(super) fn wildcard_match(test: &[u8], pattern: &[u8]) -> bool {
}
}

#[allow(dead_code)]
pub(super) fn bracket_match(test: &[u8], pattern: &[u8]) -> bool {
let mut match_cases: Vec<u8> = Vec::new();
let mut pattern_index = 0;
Copy link
Member

@squell squell Jun 2, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using a Vec for a pattern wouldn't by my solution.

For range patterns like [a-z] it feels wasteful, but also a pattern like [ëä] should ideally be supported and Vec<u8> won't work very well there.

I don't think it's absolutely necessary in code like this to first build a full representation, and then check against the representation. I.e. first see if the first character is ! or ^, and then interpret it as a negated pattern. And then there are two cases: either it is a range or the pattern is simply "itself" (i.e. a slice of characters)

Maybe the wildcard_match function (and this one) should also take &str instead of &[u8].

let mut is_negated = false;
let mut last_dash = None;

while let Some(p) = pattern.get(pattern_index) {
if *p == b'[' || *p == b']' {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use an iterator instead of using an explicit index? I see a lot of += 1 below.

pattern_index += 1;
} else if *p == b'!' || *p == b'^' {
pattern_index += 1;
is_negated = true;
} else if *p == b'-' {
pattern_index += 1;
last_dash = Some(pattern_index);
} else if last_dash.is_some() {
let last_push = match_cases.last().unwrap();
for case in *last_push..=*p {
match_cases.push(case);
}
last_dash = None;
pattern_index += 1;
} else {
match_cases.push(*p);
pattern_index += 1;
}
}

if is_negated {
!test.iter().any(|c| match_cases.contains(c))
} else {
test.iter().any(|c| match_cases.contains(c))
}
}

#[cfg(test)]
mod tests {
use super::bracket_match;
use super::wildcard_match;

#[test]
Expand Down Expand Up @@ -83,4 +120,39 @@ mod tests {
);
}
}

#[test]
fn test_bracket_match() {
let tests = vec![
("foo", "[aeiou]", true),
("foo", "[xyz]", false),
("123", "[321]", true),
("123", "[456]", false),
("foo", "[xyz][fgh]", true),
("foo", "[AEIOU]", false),
("FOO", "[AEIOU]", true),
("foo", "[a-z]", true),
("foo", "[A-Z]", false),
("FOO", "[A-Z]", true),
("123", "[0-9]", true),
("foo", "[0-9]", false),
("foo", "[abc][123][e-j]", true),
("foo", "[^abc]", true),
("foo", "[!fgh]", false),
("foo", "[!a-c][!x-z]", true),
("123", "[^5-9]", true),
("foo bar", "[A-Za-z0-9]", true),
];

for (test, pattern, expected) in tests.into_iter() {
assert_eq!(
bracket_match(test.as_bytes(), pattern.as_bytes()),
expected,
"\"{}\" {} match {}",
test,
if expected { "should" } else { "should not" },
pattern
);
}
}
}
Loading