-
Notifications
You must be signed in to change notification settings - Fork 116
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
noahboa27
wants to merge
2
commits into
trifectatechfoundation:main
Choose a base branch
from
noahboa27:wildcard-match
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
@@ -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; | ||
let mut is_negated = false; | ||
let mut last_dash = None; | ||
|
||
while let Some(p) = pattern.get(pattern_index) { | ||
if *p == b'[' || *p == b']' { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
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] | ||
|
@@ -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 | ||
); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 andVec<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]
.