-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Add parse_glob
module and update du
to use parse_glob
#3754
Merged
Merged
Changes from all commits
Commits
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
This file contains 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,2 +1,3 @@ | ||
pub mod parse_glob; | ||
pub mod parse_size; | ||
pub mod parse_time; |
This file contains 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 |
---|---|---|
@@ -0,0 +1,109 @@ | ||
//! Parsing a glob Pattern from a string. | ||
//! | ||
//! Use the [`from_str`] function to parse a [`Pattern`] from a string. | ||
|
||
// cSpell:words fnmatch | ||
|
||
use glob::{Pattern, PatternError}; | ||
|
||
fn fix_negation(glob: &str) -> String { | ||
let mut chars = glob.chars().collect::<Vec<_>>(); | ||
|
||
let mut i = 0; | ||
while i < chars.len() { | ||
if chars[i] == '[' && i + 4 <= glob.len() && chars[i + 1] == '^' { | ||
match chars[i + 3..].iter().position(|x| *x == ']') { | ||
None => (), | ||
Some(j) => { | ||
chars[i + 1] = '!'; | ||
i += j + 4; | ||
continue; | ||
} | ||
} | ||
} | ||
|
||
i += 1; | ||
} | ||
|
||
chars.into_iter().collect::<String>() | ||
} | ||
|
||
/// Parse a glob Pattern from a string. | ||
/// | ||
/// This function amends the input string to replace any caret or circumflex | ||
/// character (^) used to negate a set of characters with an exclamation mark | ||
/// (!), which adapts rust's glob matching to function the way the GNU utils' | ||
/// fnmatch does. | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```rust | ||
/// use std::time::Duration; | ||
/// use uucore::parse_glob::from_str; | ||
/// assert!(!from_str("[^abc]").unwrap().matches("a")); | ||
/// assert!(from_str("[^abc]").unwrap().matches("x")); | ||
/// ``` | ||
pub fn from_str(glob: &str) -> Result<Pattern, PatternError> { | ||
Pattern::new(&fix_negation(glob)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn test_from_str() { | ||
assert_eq!(from_str("[^abc]").unwrap(), Pattern::new("[!abc]").unwrap()); | ||
} | ||
|
||
#[test] | ||
fn test_fix_negation() { | ||
// Happy/Simple case | ||
assert_eq!(fix_negation("[^abc]"), "[!abc]"); | ||
|
||
// Should fix negations in a long regex | ||
assert_eq!(fix_negation("foo[abc] bar[^def]"), "foo[abc] bar[!def]"); | ||
|
||
// Should fix multiple negations in a regex | ||
assert_eq!(fix_negation("foo[^abc]bar[^def]"), "foo[!abc]bar[!def]"); | ||
|
||
// Should fix negation of the single character ] | ||
assert_eq!(fix_negation("[^]]"), "[!]]"); | ||
|
||
// Should fix negation of the single character ^ | ||
assert_eq!(fix_negation("[^^]"), "[!^]"); | ||
|
||
// Should fix negation of the space character | ||
assert_eq!(fix_negation("[^ ]"), "[! ]"); | ||
|
||
// Complicated patterns | ||
assert_eq!(fix_negation("[^][]"), "[!][]"); | ||
assert_eq!(fix_negation("[^[]]"), "[![]]"); | ||
|
||
// More complex patterns that should be replaced | ||
assert_eq!(fix_negation("[[]] [^a]"), "[[]] [!a]"); | ||
assert_eq!(fix_negation("[[] [^a]"), "[[] [!a]"); | ||
assert_eq!(fix_negation("[]] [^a]"), "[]] [!a]"); | ||
} | ||
|
||
#[test] | ||
fn test_fix_negation_should_not_amend() { | ||
assert_eq!(fix_negation("abc"), "abc"); | ||
|
||
// Regex specifically matches either [ or ^ | ||
assert_eq!(fix_negation("[[^]"), "[[^]"); | ||
|
||
// Regex that specifically matches either space or ^ | ||
assert_eq!(fix_negation("[ ^]"), "[ ^]"); | ||
|
||
// Regex that specifically matches either [, space or ^ | ||
assert_eq!(fix_negation("[[ ^]"), "[[ ^]"); | ||
assert_eq!(fix_negation("[ [^]"), "[ [^]"); | ||
|
||
// Invalid globs (according to rust's glob implementation) will remain unamended | ||
assert_eq!(fix_negation("[^]"), "[^]"); | ||
assert_eq!(fix_negation("[^"), "[^"); | ||
assert_eq!(fix_negation("[][^]"), "[][^]"); | ||
} | ||
} |
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.
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.
Could you please add a comment explaining what this block is doing? It isn't super obvious reading the code
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.
Please see newest changes!