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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
//! `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
//!
//! ```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));
//! ```
//!
//! ## 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).

/**
 * The following code is modified based on
 * https://github.com/devongovett/glob-match/blob/d5a6c67/src/lib.rs
 *
 * MIT Licensed
 * Copyright (c) 2023 Devon Govett
 * https://github.com/devongovett/glob-match/tree/main/LICENSE
 */
use std::path::is_separator;

#[derive(Clone, Copy, Debug, Default)]
struct State {
  path_index: usize,
  glob_index: usize,

  wildcard: Wildcard,
  globstar: Wildcard,
}

#[derive(Clone, Copy, Debug, Default)]
struct Wildcard {
  glob_index: usize,
  path_index: usize,
}

pub fn glob_match(glob: &str, path: &str) -> bool {
  let glob = glob.as_bytes();
  let path = path.as_bytes();

  let mut state = State::default();

  let mut negated = false;
  while state.glob_index < glob.len() && glob[state.glob_index] == b'!' {
    negated = !negated;
    state.glob_index += 1;
  }

  let matched = state.glob_match_from(glob, path);
  if negated {
    !matched
  } else {
    matched
  }
}

#[inline(always)]
fn unescape(c: &mut u8, glob: &[u8], state: &mut State) -> bool {
  if *c == b'\\' {
    state.glob_index += 1;
    if state.glob_index >= glob.len() {
      return false;
    }
    *c = match glob[state.glob_index] {
      b'a' => b'\x61',
      b'b' => b'\x08',
      b'n' => b'\n',
      b'r' => b'\r',
      b't' => b'\t',
      c => c,
    }
  }
  true
}

impl State {
  #[inline(always)]
  fn backtrack(&mut self) {
    self.glob_index = self.wildcard.glob_index;
    self.path_index = self.wildcard.path_index;
  }

  #[inline(always)]
  fn skip_globstars(&mut self, glob: &[u8]) {
    let mut glob_index = self.glob_index + 2;

    while glob_index + 4 <= glob.len() && &glob[glob_index..glob_index + 4] == b"/**/" {
      glob_index += 3;
    }

    if glob_index + 3 == glob.len() && &glob[glob_index..] == b"/**" {
      glob_index += 3;
    }

    self.glob_index = glob_index - 2;
  }

  #[inline(always)]
  fn skip_to_separator(&mut self, path: &[u8], is_end_invalid: bool) {
    if self.path_index == path.len() {
      self.wildcard.path_index += 1;
      return;
    }

    let mut path_index = self.path_index;
    while path_index < path.len() && !is_separator(path[path_index] as char) {
      path_index += 1;
    }

    if is_end_invalid || path_index != path.len() {
      path_index += 1;
    }

    self.wildcard.path_index = path_index;
    self.globstar = self.wildcard;
  }

  fn match_brace_branch(
    &self,
    glob: &[u8],
    path: &[u8],
    open_brace_index: usize,
    close_brace_index: usize,
    branch_index: usize,
    buffer: &mut Vec<u8>,
  ) -> bool {
    buffer.extend_from_slice(&glob[..open_brace_index]);
    buffer.extend_from_slice(&glob[branch_index..self.glob_index]);
    buffer.extend_from_slice(&glob[close_brace_index + 1..]);

    let mut branch_state = self.clone();
    branch_state.glob_index = open_brace_index;
    let matched = branch_state.glob_match_from(&buffer, path);
    buffer.clear();
    matched
  }

  fn match_brace(&mut self, glob: &[u8], path: &[u8]) -> bool {
    let mut brace_depth = 0;
    let mut in_brackets = false;

    let mut close_brace_index = 0;
    let mut glob_index = self.glob_index;

    while glob_index < glob.len() {
      match glob[glob_index] {
        b'{' if !in_brackets => brace_depth += 1,
        b'}' if !in_brackets => {
          brace_depth -= 1;
          if brace_depth == 0 {
            close_brace_index = glob_index;
            break;
          }
        }
        b'[' if !in_brackets => in_brackets = true,
        b']' => in_brackets = false,
        b'\\' => glob_index += 1,
        _ => (),
      }
      glob_index += 1;
    }

    if brace_depth != 0 {
      // Invalid pattern!
      return false;
    }

    let open_brace_index = self.glob_index;

    let mut buffer = Vec::with_capacity(glob.len());
    let mut branch_index = 0;

    while self.glob_index < glob.len() {
      match glob[self.glob_index] {
        b'{' if !in_brackets => {
          brace_depth += 1;
          if brace_depth == 1 {
            branch_index = self.glob_index + 1;
          }
        }
        b'}' if !in_brackets => {
          brace_depth -= 1;
          if brace_depth == 0 {
            if self.match_brace_branch(
              glob,
              path,
              open_brace_index,
              close_brace_index,
              branch_index,
              &mut buffer,
            ) {
              return true;
            }
            break;
          }
        }
        b',' if brace_depth == 1 => {
          if self.match_brace_branch(
            glob,
            path,
            open_brace_index,
            close_brace_index,
            branch_index,
            &mut buffer,
          ) {
            return true;
          }
          branch_index = self.glob_index + 1;
        }
        b'[' if !in_brackets => in_brackets = true,
        b']' => in_brackets = false,
        b'\\' => self.glob_index += 1,
        _ => (),
      }
      self.glob_index += 1;
    }

    return false;
  }

  #[inline(always)]
  fn glob_match_from(&mut self, glob: &[u8], path: &[u8]) -> bool {
    while self.glob_index < glob.len() || self.path_index < path.len() {
      if self.glob_index < glob.len() {
        match glob[self.glob_index] {
          b'*' => {
            let is_globstar = self.glob_index + 1 < glob.len() && glob[self.glob_index + 1] == b'*';
            if is_globstar {
              self.skip_globstars(glob);
            }

            self.wildcard.glob_index = self.glob_index;
            self.wildcard.path_index = self.path_index + 1;

            let mut in_globstar = false;
            if is_globstar {
              self.glob_index += 2;

              let is_end_invalid = self.glob_index != glob.len();

              if (self.glob_index < 3 || glob[self.glob_index - 3] == b'/')
                && (!is_end_invalid || glob[self.glob_index] == b'/')
              {
                if is_end_invalid {
                  self.glob_index += 1;
                }

                self.skip_to_separator(path, is_end_invalid);
                in_globstar = true;
              }
            } else {
              self.glob_index += 1;
            }

            if !in_globstar
              && self.path_index < path.len()
              && is_separator(path[self.path_index] as char)
            {
              self.wildcard = self.globstar;
            }

            continue;
          }
          b'?' if self.path_index < path.len() => {
            if !is_separator(path[self.path_index] as char) {
              self.glob_index += 1;
              self.path_index += 1;
              continue;
            }
          }
          b'[' if self.path_index < path.len() => {
            self.glob_index += 1;

            let mut negated = false;
            if self.glob_index < glob.len() && matches!(glob[self.glob_index], b'^' | b'!') {
              negated = true;
              self.glob_index += 1;
            }

            let mut first = true;
            let mut is_match = false;
            let c = path[self.path_index];
            while self.glob_index < glob.len() && (first || glob[self.glob_index] != b']') {
              let mut low = glob[self.glob_index];
              if !unescape(&mut low, glob, self) {
                return false;
              }

              self.glob_index += 1;

              let high = if self.glob_index + 1 < glob.len()
                && glob[self.glob_index] == b'-'
                && glob[self.glob_index + 1] != b']'
              {
                self.glob_index += 1;

                let mut high = glob[self.glob_index];
                if !unescape(&mut high, glob, self) {
                  return false;
                }

                self.glob_index += 1;
                high
              } else {
                low
              };

              if low <= c && c <= high {
                is_match = true;
              }

              first = false;
            }

            if self.glob_index >= glob.len() {
              return false;
            }

            self.glob_index += 1;
            if is_match != negated {
              self.path_index += 1;
              continue;
            }
          }
          b'{' if self.path_index < path.len() => {
            return self.match_brace(glob, path);
          }
          mut c if self.path_index < path.len() => {
            if !unescape(&mut c, glob, self) {
              return false;
            }

            let is_match = if c == b'/' {
              is_separator(path[self.path_index] as char)
            } else {
              path[self.path_index] == c
            };

            if is_match {
              self.glob_index += 1;
              self.path_index += 1;

              if c == b'/' {
                self.wildcard = self.globstar;
              }

              continue;
            }
          }
          _ => {}
        }
      }

      if self.wildcard.path_index > 0 && self.wildcard.path_index <= path.len() {
        self.backtrack();
        continue;
      }

      return false;
    }

    return true;
  }
}