1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// This file is part of the uutils coreutils package.
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//! 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;
    // Add 3 to prevent out of bounds in loop
    while i + 3 < chars.len() {
        if chars[i] == '[' && chars[i + 1] == '^' {
            match chars[i + 3..].iter().position(|x| *x == ']') {
                None => {
                    // if closing square bracket not found, stop looking for it
                    // again
                    break;
                }
                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 that we don't look for closing square brackets unnecessarily
        // Verifies issue #5584
        let chars = "^[".repeat(174571);
        assert_eq!(fix_negation(chars.as_str()), chars);
    }

    #[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("[][^]"), "[][^]");

        // Issue #4479
        assert_eq!(fix_negation("ààà[^"), "ààà[^");
    }
}