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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
//! `fast-glob` is a high-performance glob matching crate for Rust, originally forked from [`devongovett/glob-match`](https://github.com/devongovett/glob-match).
//! This crate provides efficient glob pattern matching with support for multi-pattern matching and brace expansion.
//!
//! ## Key Features
//!
//! - Up to 60% performance improvement.
//! - Support for more complex and efficient brace expansion.
//! - Fixed matching issues with wildcard and globstar [`glob-match/issues#9`](https://github.com/devongovett/glob-match/issues/9).
//!
//! ## Examples
//!
//! ### Simple Match
//!
//! Note that simple matching does not support `brace expansion`, but all other syntaxes do.
//!
//! ```rust
//! use fast_glob::glob_match;
//!
//! let glob = "some/**/n*d[k-m]e?txt";
//! let path = "some/a/bigger/path/to/the/crazy/needle.txt";
//!
//! assert!(glob_match(glob, path));
//! ```
//!
//! ### Brace Expansion
//!
//! Brace expansion is supported using `glob_match_with_brace`, allowing for more complex matching patterns:
//!
//! ```rust
//! use fast_glob::glob_match_with_brace;
//!
//! let glob = "some/**/{the,crazy}/?*.{png,txt}";
//! let path = "some/a/bigger/path/to/the/crazy/needle.txt";
//!
//! assert!(glob_match_with_brace(glob, path));
//! ```
//!
//! ### Multi-Pattern Matching
//!
//! `Glob` instances can handle multiple patterns efficiently:
//!
//! ```rust
//! use fast_glob::Glob;
//!
//! let mut glob = Glob::default();
//! assert!(glob.add("*.txt"));
//! assert!(glob.is_match("file.txt"));
//! ```
//!
//! ## Syntax
//!
//! `fast-glob` supports the following glob pattern syntax:
//!
//! | Syntax  | Meaning                                                                                                                                                                                             |
//! | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
//! | `?`     | Matches any single character.                                                                                                                                                                       |
//! | `*`     | Matches zero or more characters, except for path separators (e.g., `/`).                                                                                                                             |
//! | `**`    | Matches zero or more characters, including path separators. Must match a complete path segment (i.e., followed by a `/` or the end of the pattern).                                                  |
//! | `[ab]`  | Matches one of the characters contained in the brackets. Character ranges, e.g., `[a-z]`, are also supported. Use `[!ab]` or `[^ab]` to match any character _except_ those contained in the brackets. |
//! | `{a,b}` | Matches one of the patterns contained in the braces. Any of the wildcard characters can be used in the sub-patterns. Braces may be nested up to 10 levels deep.                                     |
//! | `!`     | When at the start of the glob, this negates the result. Multiple `!` characters negate the glob multiple times.                                                                                     |
//! | `\`     | A backslash character may be used to escape any of the above special characters.                                                                                                                    |
//!
//! ---
//!
//! For detailed usage and API reference, refer to the specific function and struct documentation.
//!
//! For any issues or contributions, please visit the [GitHub repository](https://github.com/shulaoda/fast-glob).
//!
mod brace;
mod glob;

use brace::Pattern;
use glob::glob_match_normal;

/// `Glob` represents a glob pattern matcher with support for multi-pattern matching.
#[derive(Debug, Default)]
pub struct Glob {
  glob: Vec<u8>,
  pattern: Pattern,
}

impl Glob {
  /// Creates a new `Glob` instance from a given glob pattern.
  ///
  /// Returns `Some(Glob)` if successful, `None` otherwise.
  ///
  /// # Example
  ///
  /// ```
  /// use fast_glob::Glob;
  ///
  /// let glob = Glob::new("*.txt");
  /// assert!(glob.is_some());
  /// ```
  pub fn new(glob: &str) -> Option<Self> {
    let mut value = Vec::with_capacity(glob.len() + 2);
    value.push(b'{');
    value.extend(glob.as_bytes());
    value.push(b'}');

    if let Some(pattern) = Pattern::new(&value) {
      return Some(Glob {
        glob: value,
        pattern,
      });
    }
    None
  }

  /// Adds a new glob pattern to match against.
  ///
  /// Returns `true` if the pattern was successfully added, `false` otherwise.
  ///
  /// # Example
  ///
  /// ```
  /// use fast_glob::Glob;
  ///
  /// let mut glob = Glob::default();
  /// assert!(glob.add("*.txt"));
  /// ```
  pub fn add(&mut self, glob: &str) -> bool {
    if self.glob.len() == 0 {
      if let Some(c) = Self::new(glob) {
        *self = c;
        return true;
      }
      return false;
    }

    let glob = glob.as_bytes();
    if let Some(branch) = Pattern::parse(glob) {
      self.pattern.branch[0].1 += 1;
      self.pattern.branch.extend(branch);
      self.glob.reserve_exact(glob.len() + 1);

      let index = self.glob.len() - 1;
      self.glob[index] = b',';
      self.glob.extend(glob);
      self.glob.push(b'}');

      return true;
    }
    false
  }

  /// Checks if any of the glob patterns matches the given path.
  ///
  /// Returns `true` if a match is found, `false` otherwise.
  ///
  /// # Example
  ///
  /// ```
  /// use fast_glob::Glob;
  ///
  /// let mut glob = Glob::new("*.txt").unwrap();
  /// assert!(glob.is_match("file.txt"));
  /// ```
  pub fn is_match(&mut self, path: &str) -> bool {
    let mut flag = false;
    loop {
      let (result, longest_index) = glob_match_normal(&self.pattern.value, path.as_bytes());
      if result || !self.pattern.trigger(&self.glob, longest_index) {
        if flag {
          self.pattern.restore();
          self.pattern.track(&self.glob);
        }
        return result;
      }
      flag = true;
    }
  }
}

/// Performs glob pattern matching for a simple glob pattern.
///
/// Returns `true` if `glob` matches `path`, `false` otherwise.
///
/// # Example
///
/// ```
/// use fast_glob::glob_match;
///
/// let glob = "**/*.txt";
/// let path = "file.txt";
///
/// assert!(glob_match(glob, path));
/// ```
pub fn glob_match(glob: &str, path: &str) -> bool {
  glob_match_normal(glob.as_bytes(), path.as_bytes()).0
}

/// Performs glob pattern matching for a glob pattern with brace expansion.
///
/// Returns `true` if `glob` matches `path`, `false` otherwise.
///
/// # Example
///
/// ```
/// use fast_glob::glob_match_with_brace;
///
/// let glob = "some/**/{the,crazy}/?*.{png,txt}";
/// let path = "some/a/bigger/path/to/the/crazy/needle.txt";
///
/// assert!(glob_match_with_brace(glob, path));
/// ```
pub fn glob_match_with_brace(glob: &str, path: &str) -> bool {
  let glob = glob.as_bytes();
  let path = path.as_bytes();

  if let Some(pattern) = &mut Pattern::new(glob) {
    if pattern.branch.is_empty() {
      return glob_match_normal(glob, path).0;
    }

    loop {
      let (result, longest_index) = glob_match_normal(&pattern.value, path);

      if result || !pattern.trigger(glob, longest_index) {
        return result;
      }
    }
  }
  false
}