mitex_glob/
lib.rs

1//! Track Upstream, Licensed by MIT
2//! See <https://github.com/devongovett/glob-match/blob/v0.2.1/src/lib.rs>
3//! git diff  --no-index -- ./crates/mitex-glob/src/origin ./crates/mitex-glob/src/lib.rs
4#![cfg_attr(rustfmt, rustfmt_skip)]
5#![allow(clippy::all)]
6#![allow(dead_code)]
7
8use std::{ops::Range, path::is_separator};
9
10#[derive(Clone, Copy, Debug, Default)]
11struct State {
12  // These store character indices into the glob and path strings.
13  path_index: usize,
14  glob_index: usize,
15
16  // The current index into the captures list.
17  capture_index: usize,
18
19  // When we hit a * or **, we store the state for backtracking.
20  wildcard: Wildcard,
21  globstar: Wildcard,
22}
23
24#[derive(Clone, Copy, Debug, Default)]
25struct Wildcard {
26  // Using u32 rather than usize for these results in 10% faster performance.
27  glob_index: u32,
28  path_index: u32,
29  capture_index: u32,
30}
31
32type Capture = Range<usize>;
33
34pub fn glob_match_prefix(glob: &str, path: &str) -> bool {
35  glob_match_internal(glob, path, None, true)
36}
37
38pub fn glob_match(glob: &str, path: &str) -> bool {
39  glob_match_internal(glob, path, None, false)
40}
41
42pub fn glob_match_with_captures<'a>(glob: &str, path: &'a str) -> Option<Vec<Capture>> {
43  let mut captures = Vec::new();
44  if glob_match_internal(glob, path, Some(&mut captures), false) {
45    return Some(captures);
46  }
47  None
48}
49
50// #[inline(always)]
51fn glob_match_internal<'a>(
52  glob: &str,
53  path: &'a str,
54  mut captures: Option<&mut Vec<Capture>>,
55  is_prefix: bool,
56) -> bool {
57  // This algorithm is based on https://research.swtch.com/glob
58  let glob = glob.as_bytes();
59  let path = path.as_bytes();
60
61  let mut state = State::default();
62
63  // Store the state when we see an opening '{' brace in a stack.
64  // Up to 10 nested braces are supported.
65  let mut brace_stack = BraceStack::default();
66
67  // First, check if the pattern is negated with a leading '!' character.
68  // Multiple negations can occur.
69  let mut negated = false;
70  while state.glob_index < glob.len() && glob[state.glob_index] == b'!' {
71    negated = !negated;
72    state.glob_index += 1;
73  }
74
75  while state.glob_index < glob.len() || state.path_index < path.len() {
76    if state.glob_index < glob.len() {
77      match glob[state.glob_index] {
78        b'*' => {
79          let is_globstar = state.glob_index + 1 < glob.len() && glob[state.glob_index + 1] == b'*';
80          if is_globstar {
81            // Coalesce multiple ** segments into one.
82            state.glob_index = skip_globstars(glob, state.glob_index + 2) - 2;
83          }
84
85          // If we are on a different glob index than before, start a new capture.
86          // Otherwise, extend the active one.
87          if captures.is_some()
88            && (captures.as_ref().unwrap().is_empty()
89              || state.glob_index != state.wildcard.glob_index as usize)
90          {
91            state.wildcard.capture_index = state.capture_index as u32;
92            state.begin_capture(&mut captures, state.path_index..state.path_index);
93          } else {
94            state.extend_capture(&mut captures);
95          }
96
97          state.wildcard.glob_index = state.glob_index as u32;
98          state.wildcard.path_index = state.path_index as u32 + 1;
99
100          // ** allows path separators, whereas * does not.
101          // However, ** must be a full path component, i.e. a/**/b not a**b.
102          if is_globstar {
103            state.glob_index += 2;
104
105            if glob.len() == state.glob_index {
106              // A trailing ** segment without a following separator.
107              state.globstar = state.wildcard;
108            } else if (state.glob_index < 3 || glob[state.glob_index - 3] == b'/')
109              && glob[state.glob_index] == b'/'
110            {
111              // Matched a full /**/ segment. If the last character in the path was a separator,
112              // skip the separator in the glob so we search for the next character.
113              // In effect, this makes the whole segment optional so that a/**/b matches a/b.
114              if state.path_index == 0
115                || (state.path_index < path.len()
116                  && is_separator(path[state.path_index - 1] as char))
117              {
118                state.end_capture(&mut captures);
119                state.glob_index += 1;
120              }
121
122              // The allows_sep flag allows separator characters in ** matches.
123              // one is a '/', which prevents a/**/b from matching a/bb.
124              state.globstar = state.wildcard;
125            }
126          } else {
127            state.glob_index += 1;
128          }
129
130          // If we are in a * segment and hit a separator,
131          // either jump back to a previous ** or end the wildcard.
132          if state.globstar.path_index != state.wildcard.path_index
133            && state.path_index < path.len()
134            && is_separator(path[state.path_index] as char)
135          {
136            // Special case: don't jump back for a / at the end of the glob.
137            if state.globstar.path_index > 0 && state.path_index + 1 < path.len() {
138              state.glob_index = state.globstar.glob_index as usize;
139              state.capture_index = state.globstar.capture_index as usize;
140              state.wildcard.glob_index = state.globstar.glob_index;
141              state.wildcard.capture_index = state.globstar.capture_index;
142            } else {
143              state.wildcard.path_index = 0;
144            }
145          }
146
147          // If the next char is a special brace separator,
148          // skip to the end of the braces so we don't try to match it.
149          if brace_stack.length > 0
150            && state.glob_index < glob.len()
151            && matches!(glob[state.glob_index], b',' | b'}')
152          {
153            if state.skip_braces(glob, &mut captures, false) == BraceState::Invalid {
154              // invalid pattern!
155              return false;
156            }
157          }
158
159          continue;
160        }
161        b'?' if state.path_index < path.len() => {
162          if !is_separator(path[state.path_index] as char) {
163            state.add_char_capture(&mut captures);
164            state.glob_index += 1;
165            state.path_index += 1;
166            continue;
167          }
168        }
169        b'[' if state.path_index < path.len() => {
170          state.glob_index += 1;
171          let c = path[state.path_index];
172
173          // Check if the character class is negated.
174          let mut negated = false;
175          if state.glob_index < glob.len() && matches!(glob[state.glob_index], b'^' | b'!') {
176            negated = true;
177            state.glob_index += 1;
178          }
179
180          // Try each range.
181          let mut first = true;
182          let mut is_match = false;
183          while state.glob_index < glob.len() && (first || glob[state.glob_index] != b']') {
184            let mut low = glob[state.glob_index];
185            if !unescape(&mut low, glob, &mut state.glob_index) {
186              // Invalid pattern!
187              return false;
188            }
189            state.glob_index += 1;
190
191            // If there is a - and the following character is not ], read the range end character.
192            let high = if state.glob_index + 1 < glob.len()
193              && glob[state.glob_index] == b'-'
194              && glob[state.glob_index + 1] != b']'
195            {
196              state.glob_index += 1;
197              let mut high = glob[state.glob_index];
198              if !unescape(&mut high, glob, &mut state.glob_index) {
199                // Invalid pattern!
200                return false;
201              }
202              state.glob_index += 1;
203              high
204            } else {
205              low
206            };
207
208            if low <= c && c <= high {
209              is_match = true;
210            }
211            first = false;
212          }
213          if state.glob_index >= glob.len() {
214            // invalid pattern!
215            return false;
216          }
217          state.glob_index += 1;
218          if is_match != negated {
219            state.add_char_capture(&mut captures);
220            state.path_index += 1;
221            continue;
222          }
223        }
224        b'{' if state.path_index < path.len() => {
225          if brace_stack.length as usize >= brace_stack.stack.len() {
226            // Invalid pattern! Too many nested braces.
227            return false;
228          }
229
230          state.end_capture(&mut captures);
231          state.begin_capture(&mut captures, state.path_index..state.path_index);
232
233          // Push old state to the stack, and reset current state.
234          state = brace_stack.push(&state);
235          continue;
236        }
237        b'}' if brace_stack.length > 0 => {
238          // If we hit the end of the braces, we matched the last option.
239          brace_stack.longest_brace_match =
240            brace_stack.longest_brace_match.max(state.path_index as i32);
241          state.glob_index += 1;
242          state = brace_stack.pop(&state, &mut captures);
243          continue;
244        }
245        b',' if brace_stack.length > 0 => {
246          // If we hit a comma, we matched one of the options!
247          // But we still need to check the others in case there is a longer match.
248          brace_stack.longest_brace_match =
249            brace_stack.longest_brace_match.max(state.path_index as i32);
250          state.path_index = brace_stack.last().path_index;
251          state.glob_index += 1;
252          state.wildcard = Wildcard::default();
253          state.globstar = Wildcard::default();
254          continue;
255        }
256        mut c if state.path_index < path.len() => {
257          // Match escaped characters as literals.
258          if !unescape(&mut c, glob, &mut state.glob_index) {
259            // Invalid pattern!
260            return false;
261          }
262
263          let is_match = if c == b'/' {
264            is_separator(path[state.path_index] as char)
265          } else {
266            path[state.path_index] == c
267          };
268
269          if is_match {
270            state.end_capture(&mut captures);
271
272            if brace_stack.length > 0 && state.glob_index > 0 && glob[state.glob_index - 1] == b'}'
273            {
274              brace_stack.longest_brace_match = state.path_index as i32;
275              state = brace_stack.pop(&state, &mut captures);
276            }
277            state.glob_index += 1;
278            state.path_index += 1;
279
280            // If this is not a separator, lock in the previous globstar.
281            if c != b'/' {
282              state.globstar.path_index = 0;
283            }
284            continue;
285          }
286        }
287        _ => {}
288      }
289    }
290
291    // If we didn't match, restore state to the previous star pattern.
292    if state.wildcard.path_index > 0 && state.wildcard.path_index as usize <= path.len() {
293      state.backtrack();
294      continue;
295    }
296
297    if brace_stack.length > 0 {
298      // If in braces, find next option and reset path to index where we saw the '{'
299      match state.skip_braces(glob, &mut captures, true) {
300        BraceState::Invalid => return false,
301        BraceState::Comma => {
302          state.path_index = brace_stack.last().path_index;
303          continue;
304        }
305        BraceState::EndBrace => {}
306      }
307
308      // Hit the end. Pop the stack.
309      // If we matched a previous option, use that.
310      if brace_stack.longest_brace_match >= 0 {
311        state = brace_stack.pop(&state, &mut captures);
312        continue;
313      } else {
314        // Didn't match. Restore state, and check if we need to jump back to a star pattern.
315        state = *brace_stack.last();
316        brace_stack.length -= 1;
317        if let Some(captures) = &mut captures {
318          captures.truncate(state.capture_index);
319        }
320        if state.wildcard.path_index > 0 && state.wildcard.path_index as usize <= path.len() {
321          state.backtrack();
322          continue;
323        }
324      }
325    }
326
327    return if is_prefix {
328      if negated {
329        state.path_index != path.len()
330      } else {
331        state.path_index == path.len()
332      }
333    } else {
334      negated
335    }
336  }
337
338  if brace_stack.length > 0 && state.glob_index > 0 && glob[state.glob_index - 1] == b'}' {
339    brace_stack.longest_brace_match = state.path_index as i32;
340    brace_stack.pop(&state, &mut captures);
341  }
342
343  !negated
344}
345
346#[inline(always)]
347fn unescape(c: &mut u8, glob: &[u8], glob_index: &mut usize) -> bool {
348  if *c == b'\\' {
349    *glob_index += 1;
350    if *glob_index >= glob.len() {
351      // Invalid pattern!
352      return false;
353    }
354    *c = match glob[*glob_index] {
355      b'a' => b'\x61',
356      b'b' => b'\x08',
357      b'n' => b'\n',
358      b'r' => b'\r',
359      b't' => b'\t',
360      c => c,
361    }
362  }
363  true
364}
365
366#[derive(PartialEq)]
367enum BraceState {
368  Invalid,
369  Comma,
370  EndBrace,
371}
372
373impl State {
374  #[inline(always)]
375  fn backtrack(&mut self) {
376    self.glob_index = self.wildcard.glob_index as usize;
377    self.path_index = self.wildcard.path_index as usize;
378    self.capture_index = self.wildcard.capture_index as usize;
379  }
380
381  #[inline(always)]
382  fn begin_capture(&self, captures: &mut Option<&mut Vec<Capture>>, capture: Capture) {
383    if let Some(captures) = captures {
384      if self.capture_index < captures.len() {
385        captures[self.capture_index] = capture;
386      } else {
387        captures.push(capture);
388      }
389    }
390  }
391
392  #[inline(always)]
393  fn extend_capture(&self, captures: &mut Option<&mut Vec<Capture>>) {
394    if let Some(captures) = captures {
395      if self.capture_index < captures.len() {
396        captures[self.capture_index].end = self.path_index;
397      }
398    }
399  }
400
401  #[inline(always)]
402  fn end_capture(&mut self, captures: &mut Option<&mut Vec<Capture>>) {
403    if let Some(captures) = captures {
404      if self.capture_index < captures.len() {
405        self.capture_index += 1;
406      }
407    }
408  }
409
410  #[inline(always)]
411  fn add_char_capture(&mut self, captures: &mut Option<&mut Vec<Capture>>) {
412    self.end_capture(captures);
413    self.begin_capture(captures, self.path_index..self.path_index + 1);
414    self.capture_index += 1;
415  }
416
417  fn skip_braces(
418    &mut self,
419    glob: &[u8],
420    captures: &mut Option<&mut Vec<Capture>>,
421    stop_on_comma: bool,
422  ) -> BraceState {
423    let mut braces = 1;
424    let mut in_brackets = false;
425    let mut capture_index = self.capture_index + 1;
426    while self.glob_index < glob.len() && braces > 0 {
427      match glob[self.glob_index] {
428        // Skip nested braces.
429        b'{' if !in_brackets => braces += 1,
430        b'}' if !in_brackets => braces -= 1,
431        b',' if stop_on_comma && braces == 1 && !in_brackets => {
432          self.glob_index += 1;
433          return BraceState::Comma;
434        }
435        c @ (b'*' | b'?' | b'[') if !in_brackets => {
436          if c == b'[' {
437            in_brackets = true;
438          }
439          if let Some(captures) = captures {
440            if capture_index < captures.len() {
441              captures[capture_index] = self.path_index..self.path_index;
442            } else {
443              captures.push(self.path_index..self.path_index);
444            }
445            capture_index += 1;
446          }
447          if c == b'*' {
448            if self.glob_index + 1 < glob.len() && glob[self.glob_index + 1] == b'*' {
449              self.glob_index = skip_globstars(glob, self.glob_index + 2) - 2;
450              self.glob_index += 1;
451            }
452          }
453        }
454        b']' => in_brackets = false,
455        b'\\' => {
456          self.glob_index += 1;
457        }
458        _ => {}
459      }
460      self.glob_index += 1;
461    }
462
463    if braces != 0 {
464      return BraceState::Invalid;
465    }
466
467    BraceState::EndBrace
468  }
469}
470
471#[inline(always)]
472fn skip_globstars(glob: &[u8], mut glob_index: usize) -> usize {
473  // Coalesce multiple ** segments into one.
474  let needle = b"/**";
475  while glob[glob_index..].starts_with(needle) {
476    glob_index += needle.len();
477  }
478  glob_index
479}
480
481struct BraceStack {
482  stack: [State; 10],
483  length: u32,
484  longest_brace_match: i32,
485}
486
487impl Default for BraceStack {
488  #[inline]
489  fn default() -> Self {
490    // Manual implementation is faster than the automatically derived one.
491    BraceStack {
492      stack: [State::default(); 10],
493      length: 0,
494      longest_brace_match: -1,
495    }
496  }
497}
498
499impl BraceStack {
500  #[inline(always)]
501  fn push(&mut self, state: &State) -> State {
502    // Push old state to the stack, and reset current state.
503    self.stack[self.length as usize] = *state;
504    self.length += 1;
505    State {
506      path_index: state.path_index,
507      glob_index: state.glob_index + 1,
508      capture_index: state.capture_index + 1,
509      ..State::default()
510    }
511  }
512
513  #[inline(always)]
514  fn pop(&mut self, state: &State, captures: &mut Option<&mut Vec<Capture>>) -> State {
515    self.length -= 1;
516    let mut state = State {
517      path_index: self.longest_brace_match as usize,
518      glob_index: state.glob_index,
519      // But restore star state if needed later.
520      wildcard: self.stack[self.length as usize].wildcard,
521      globstar: self.stack[self.length as usize].globstar,
522      capture_index: self.stack[self.length as usize].capture_index,
523    };
524    if self.length == 0 {
525      self.longest_brace_match = -1;
526    }
527    state.extend_capture(captures);
528    if let Some(captures) = captures {
529      state.capture_index = captures.len();
530    }
531
532    state
533  }
534
535  #[inline(always)]
536  fn last(&self) -> &State {
537    &self.stack[self.length as usize - 1]
538  }
539}
540
541#[cfg(feature = "never-used")]
542#[cfg(test)]
543mod tests {
544  use super::*;
545
546  #[test]
547  fn basic() {
548    assert!(glob_match("abc", "abc"));
549    assert!(glob_match("*", "abc"));
550    assert!(glob_match("*", ""));
551    assert!(glob_match("**", ""));
552    assert!(glob_match("*c", "abc"));
553    assert!(!glob_match("*b", "abc"));
554    assert!(glob_match("a*", "abc"));
555    assert!(!glob_match("b*", "abc"));
556    assert!(glob_match("a*", "a"));
557    assert!(glob_match("*a", "a"));
558    assert!(glob_match("a*b*c*d*e*", "axbxcxdxe"));
559    assert!(glob_match("a*b*c*d*e*", "axbxcxdxexxx"));
560    assert!(glob_match("a*b?c*x", "abxbbxdbxebxczzx"));
561    assert!(!glob_match("a*b?c*x", "abxbbxdbxebxczzy"));
562
563    assert!(glob_match("a/*/test", "a/foo/test"));
564    assert!(!glob_match("a/*/test", "a/foo/bar/test"));
565    assert!(glob_match("a/**/test", "a/foo/test"));
566    assert!(glob_match("a/**/test", "a/foo/bar/test"));
567    assert!(glob_match("a/**/b/c", "a/foo/bar/b/c"));
568    assert!(glob_match("a\\*b", "a*b"));
569    assert!(!glob_match("a\\*b", "axb"));
570
571    assert!(glob_match("[abc]", "a"));
572    assert!(glob_match("[abc]", "b"));
573    assert!(glob_match("[abc]", "c"));
574    assert!(!glob_match("[abc]", "d"));
575    assert!(glob_match("x[abc]x", "xax"));
576    assert!(glob_match("x[abc]x", "xbx"));
577    assert!(glob_match("x[abc]x", "xcx"));
578    assert!(!glob_match("x[abc]x", "xdx"));
579    assert!(!glob_match("x[abc]x", "xay"));
580    assert!(glob_match("[?]", "?"));
581    assert!(!glob_match("[?]", "a"));
582    assert!(glob_match("[*]", "*"));
583    assert!(!glob_match("[*]", "a"));
584
585    assert!(glob_match("[a-cx]", "a"));
586    assert!(glob_match("[a-cx]", "b"));
587    assert!(glob_match("[a-cx]", "c"));
588    assert!(!glob_match("[a-cx]", "d"));
589    assert!(glob_match("[a-cx]", "x"));
590
591    assert!(!glob_match("[^abc]", "a"));
592    assert!(!glob_match("[^abc]", "b"));
593    assert!(!glob_match("[^abc]", "c"));
594    assert!(glob_match("[^abc]", "d"));
595    assert!(!glob_match("[!abc]", "a"));
596    assert!(!glob_match("[!abc]", "b"));
597    assert!(!glob_match("[!abc]", "c"));
598    assert!(glob_match("[!abc]", "d"));
599    assert!(glob_match("[\\!]", "!"));
600
601    assert!(glob_match("a*b*[cy]*d*e*", "axbxcxdxexxx"));
602    assert!(glob_match("a*b*[cy]*d*e*", "axbxyxdxexxx"));
603    assert!(glob_match("a*b*[cy]*d*e*", "axbxxxyxdxexxx"));
604
605    assert!(glob_match("test.{jpg,png}", "test.jpg"));
606    assert!(glob_match("test.{jpg,png}", "test.png"));
607    assert!(glob_match("test.{j*g,p*g}", "test.jpg"));
608    assert!(glob_match("test.{j*g,p*g}", "test.jpxxxg"));
609    assert!(glob_match("test.{j*g,p*g}", "test.jxg"));
610    assert!(!glob_match("test.{j*g,p*g}", "test.jnt"));
611    assert!(glob_match("test.{j*g,j*c}", "test.jnc"));
612    assert!(glob_match("test.{jpg,p*g}", "test.png"));
613    assert!(glob_match("test.{jpg,p*g}", "test.pxg"));
614    assert!(!glob_match("test.{jpg,p*g}", "test.pnt"));
615    assert!(glob_match("test.{jpeg,png}", "test.jpeg"));
616    assert!(!glob_match("test.{jpeg,png}", "test.jpg"));
617    assert!(glob_match("test.{jpeg,png}", "test.png"));
618    assert!(glob_match("test.{jp\\,g,png}", "test.jp,g"));
619    assert!(!glob_match("test.{jp\\,g,png}", "test.jxg"));
620    assert!(glob_match("test/{foo,bar}/baz", "test/foo/baz"));
621    assert!(glob_match("test/{foo,bar}/baz", "test/bar/baz"));
622    assert!(!glob_match("test/{foo,bar}/baz", "test/baz/baz"));
623    assert!(glob_match("test/{foo*,bar*}/baz", "test/foooooo/baz"));
624    assert!(glob_match("test/{foo*,bar*}/baz", "test/barrrrr/baz"));
625    assert!(glob_match("test/{*foo,*bar}/baz", "test/xxxxfoo/baz"));
626    assert!(glob_match("test/{*foo,*bar}/baz", "test/xxxxbar/baz"));
627    assert!(glob_match("test/{foo/**,bar}/baz", "test/bar/baz"));
628    assert!(!glob_match("test/{foo/**,bar}/baz", "test/bar/test/baz"));
629
630    assert!(!glob_match("*.txt", "some/big/path/to/the/needle.txt"));
631    assert!(glob_match(
632      "some/**/needle.{js,tsx,mdx,ts,jsx,txt}",
633      "some/a/bigger/path/to/the/crazy/needle.txt"
634    ));
635    assert!(glob_match(
636      "some/**/{a,b,c}/**/needle.txt",
637      "some/foo/a/bigger/path/to/the/crazy/needle.txt"
638    ));
639    assert!(!glob_match(
640      "some/**/{a,b,c}/**/needle.txt",
641      "some/foo/d/bigger/path/to/the/crazy/needle.txt"
642    ));
643    assert!(glob_match("a/{a{a,b},b}", "a/aa"));
644    assert!(glob_match("a/{a{a,b},b}", "a/ab"));
645    assert!(!glob_match("a/{a{a,b},b}", "a/ac"));
646    assert!(glob_match("a/{a{a,b},b}", "a/b"));
647    assert!(!glob_match("a/{a{a,b},b}", "a/c"));
648    assert!(glob_match("a/{b,c[}]*}", "a/b"));
649    assert!(glob_match("a/{b,c[}]*}", "a/c}xx"));
650  }
651
652  // The below tests are based on Bash and micromatch.
653  // https://github.com/micromatch/picomatch/blob/master/test/bash.js
654  // Converted using the following find and replace regex:
655  // find: assert\(([!])?isMatch\('(.*?)', ['"](.*?)['"]\)\);
656  // replace: assert!($1glob_match("$3", "$2"));
657
658  #[test]
659  fn bash() {
660    assert!(!glob_match("a*", "*"));
661    assert!(!glob_match("a*", "**"));
662    assert!(!glob_match("a*", "\\*"));
663    assert!(!glob_match("a*", "a/*"));
664    assert!(!glob_match("a*", "b"));
665    assert!(!glob_match("a*", "bc"));
666    assert!(!glob_match("a*", "bcd"));
667    assert!(!glob_match("a*", "bdir/"));
668    assert!(!glob_match("a*", "Beware"));
669    assert!(glob_match("a*", "a"));
670    assert!(glob_match("a*", "ab"));
671    assert!(glob_match("a*", "abc"));
672
673    assert!(!glob_match("\\a*", "*"));
674    assert!(!glob_match("\\a*", "**"));
675    assert!(!glob_match("\\a*", "\\*"));
676
677    assert!(glob_match("\\a*", "a"));
678    assert!(!glob_match("\\a*", "a/*"));
679    assert!(glob_match("\\a*", "abc"));
680    assert!(glob_match("\\a*", "abd"));
681    assert!(glob_match("\\a*", "abe"));
682    assert!(!glob_match("\\a*", "b"));
683    assert!(!glob_match("\\a*", "bb"));
684    assert!(!glob_match("\\a*", "bcd"));
685    assert!(!glob_match("\\a*", "bdir/"));
686    assert!(!glob_match("\\a*", "Beware"));
687    assert!(!glob_match("\\a*", "c"));
688    assert!(!glob_match("\\a*", "ca"));
689    assert!(!glob_match("\\a*", "cb"));
690    assert!(!glob_match("\\a*", "d"));
691    assert!(!glob_match("\\a*", "dd"));
692    assert!(!glob_match("\\a*", "de"));
693  }
694
695  #[test]
696  fn bash_directories() {
697    assert!(!glob_match("b*/", "*"));
698    assert!(!glob_match("b*/", "**"));
699    assert!(!glob_match("b*/", "\\*"));
700    assert!(!glob_match("b*/", "a"));
701    assert!(!glob_match("b*/", "a/*"));
702    assert!(!glob_match("b*/", "abc"));
703    assert!(!glob_match("b*/", "abd"));
704    assert!(!glob_match("b*/", "abe"));
705    assert!(!glob_match("b*/", "b"));
706    assert!(!glob_match("b*/", "bb"));
707    assert!(!glob_match("b*/", "bcd"));
708    assert!(glob_match("b*/", "bdir/"));
709    assert!(!glob_match("b*/", "Beware"));
710    assert!(!glob_match("b*/", "c"));
711    assert!(!glob_match("b*/", "ca"));
712    assert!(!glob_match("b*/", "cb"));
713    assert!(!glob_match("b*/", "d"));
714    assert!(!glob_match("b*/", "dd"));
715    assert!(!glob_match("b*/", "de"));
716  }
717
718  #[test]
719  fn bash_escaping() {
720    assert!(!glob_match("\\^", "*"));
721    assert!(!glob_match("\\^", "**"));
722    assert!(!glob_match("\\^", "\\*"));
723    assert!(!glob_match("\\^", "a"));
724    assert!(!glob_match("\\^", "a/*"));
725    assert!(!glob_match("\\^", "abc"));
726    assert!(!glob_match("\\^", "abd"));
727    assert!(!glob_match("\\^", "abe"));
728    assert!(!glob_match("\\^", "b"));
729    assert!(!glob_match("\\^", "bb"));
730    assert!(!glob_match("\\^", "bcd"));
731    assert!(!glob_match("\\^", "bdir/"));
732    assert!(!glob_match("\\^", "Beware"));
733    assert!(!glob_match("\\^", "c"));
734    assert!(!glob_match("\\^", "ca"));
735    assert!(!glob_match("\\^", "cb"));
736    assert!(!glob_match("\\^", "d"));
737    assert!(!glob_match("\\^", "dd"));
738    assert!(!glob_match("\\^", "de"));
739
740    assert!(glob_match("\\*", "*"));
741    // assert!(glob_match("\\*", "\\*"));
742    assert!(!glob_match("\\*", "**"));
743    assert!(!glob_match("\\*", "a"));
744    assert!(!glob_match("\\*", "a/*"));
745    assert!(!glob_match("\\*", "abc"));
746    assert!(!glob_match("\\*", "abd"));
747    assert!(!glob_match("\\*", "abe"));
748    assert!(!glob_match("\\*", "b"));
749    assert!(!glob_match("\\*", "bb"));
750    assert!(!glob_match("\\*", "bcd"));
751    assert!(!glob_match("\\*", "bdir/"));
752    assert!(!glob_match("\\*", "Beware"));
753    assert!(!glob_match("\\*", "c"));
754    assert!(!glob_match("\\*", "ca"));
755    assert!(!glob_match("\\*", "cb"));
756    assert!(!glob_match("\\*", "d"));
757    assert!(!glob_match("\\*", "dd"));
758    assert!(!glob_match("\\*", "de"));
759
760    assert!(!glob_match("a\\*", "*"));
761    assert!(!glob_match("a\\*", "**"));
762    assert!(!glob_match("a\\*", "\\*"));
763    assert!(!glob_match("a\\*", "a"));
764    assert!(!glob_match("a\\*", "a/*"));
765    assert!(!glob_match("a\\*", "abc"));
766    assert!(!glob_match("a\\*", "abd"));
767    assert!(!glob_match("a\\*", "abe"));
768    assert!(!glob_match("a\\*", "b"));
769    assert!(!glob_match("a\\*", "bb"));
770    assert!(!glob_match("a\\*", "bcd"));
771    assert!(!glob_match("a\\*", "bdir/"));
772    assert!(!glob_match("a\\*", "Beware"));
773    assert!(!glob_match("a\\*", "c"));
774    assert!(!glob_match("a\\*", "ca"));
775    assert!(!glob_match("a\\*", "cb"));
776    assert!(!glob_match("a\\*", "d"));
777    assert!(!glob_match("a\\*", "dd"));
778    assert!(!glob_match("a\\*", "de"));
779
780    assert!(glob_match("*q*", "aqa"));
781    assert!(glob_match("*q*", "aaqaa"));
782    assert!(!glob_match("*q*", "*"));
783    assert!(!glob_match("*q*", "**"));
784    assert!(!glob_match("*q*", "\\*"));
785    assert!(!glob_match("*q*", "a"));
786    assert!(!glob_match("*q*", "a/*"));
787    assert!(!glob_match("*q*", "abc"));
788    assert!(!glob_match("*q*", "abd"));
789    assert!(!glob_match("*q*", "abe"));
790    assert!(!glob_match("*q*", "b"));
791    assert!(!glob_match("*q*", "bb"));
792    assert!(!glob_match("*q*", "bcd"));
793    assert!(!glob_match("*q*", "bdir/"));
794    assert!(!glob_match("*q*", "Beware"));
795    assert!(!glob_match("*q*", "c"));
796    assert!(!glob_match("*q*", "ca"));
797    assert!(!glob_match("*q*", "cb"));
798    assert!(!glob_match("*q*", "d"));
799    assert!(!glob_match("*q*", "dd"));
800    assert!(!glob_match("*q*", "de"));
801
802    assert!(glob_match("\\**", "*"));
803    assert!(glob_match("\\**", "**"));
804    assert!(!glob_match("\\**", "\\*"));
805    assert!(!glob_match("\\**", "a"));
806    assert!(!glob_match("\\**", "a/*"));
807    assert!(!glob_match("\\**", "abc"));
808    assert!(!glob_match("\\**", "abd"));
809    assert!(!glob_match("\\**", "abe"));
810    assert!(!glob_match("\\**", "b"));
811    assert!(!glob_match("\\**", "bb"));
812    assert!(!glob_match("\\**", "bcd"));
813    assert!(!glob_match("\\**", "bdir/"));
814    assert!(!glob_match("\\**", "Beware"));
815    assert!(!glob_match("\\**", "c"));
816    assert!(!glob_match("\\**", "ca"));
817    assert!(!glob_match("\\**", "cb"));
818    assert!(!glob_match("\\**", "d"));
819    assert!(!glob_match("\\**", "dd"));
820    assert!(!glob_match("\\**", "de"));
821  }
822
823  #[test]
824  fn bash_classes() {
825    assert!(!glob_match("a*[^c]", "*"));
826    assert!(!glob_match("a*[^c]", "**"));
827    assert!(!glob_match("a*[^c]", "\\*"));
828    assert!(!glob_match("a*[^c]", "a"));
829    assert!(!glob_match("a*[^c]", "a/*"));
830    assert!(!glob_match("a*[^c]", "abc"));
831    assert!(glob_match("a*[^c]", "abd"));
832    assert!(glob_match("a*[^c]", "abe"));
833    assert!(!glob_match("a*[^c]", "b"));
834    assert!(!glob_match("a*[^c]", "bb"));
835    assert!(!glob_match("a*[^c]", "bcd"));
836    assert!(!glob_match("a*[^c]", "bdir/"));
837    assert!(!glob_match("a*[^c]", "Beware"));
838    assert!(!glob_match("a*[^c]", "c"));
839    assert!(!glob_match("a*[^c]", "ca"));
840    assert!(!glob_match("a*[^c]", "cb"));
841    assert!(!glob_match("a*[^c]", "d"));
842    assert!(!glob_match("a*[^c]", "dd"));
843    assert!(!glob_match("a*[^c]", "de"));
844    assert!(!glob_match("a*[^c]", "baz"));
845    assert!(!glob_match("a*[^c]", "bzz"));
846    assert!(!glob_match("a*[^c]", "BZZ"));
847    assert!(!glob_match("a*[^c]", "beware"));
848    assert!(!glob_match("a*[^c]", "BewAre"));
849
850    assert!(glob_match("a[X-]b", "a-b"));
851    assert!(glob_match("a[X-]b", "aXb"));
852
853    assert!(!glob_match("[a-y]*[^c]", "*"));
854    assert!(glob_match("[a-y]*[^c]", "a*"));
855    assert!(!glob_match("[a-y]*[^c]", "**"));
856    assert!(!glob_match("[a-y]*[^c]", "\\*"));
857    assert!(!glob_match("[a-y]*[^c]", "a"));
858    assert!(glob_match("[a-y]*[^c]", "a123b"));
859    assert!(!glob_match("[a-y]*[^c]", "a123c"));
860    assert!(glob_match("[a-y]*[^c]", "ab"));
861    assert!(!glob_match("[a-y]*[^c]", "a/*"));
862    assert!(!glob_match("[a-y]*[^c]", "abc"));
863    assert!(glob_match("[a-y]*[^c]", "abd"));
864    assert!(glob_match("[a-y]*[^c]", "abe"));
865    assert!(!glob_match("[a-y]*[^c]", "b"));
866    assert!(glob_match("[a-y]*[^c]", "bd"));
867    assert!(glob_match("[a-y]*[^c]", "bb"));
868    assert!(glob_match("[a-y]*[^c]", "bcd"));
869    assert!(glob_match("[a-y]*[^c]", "bdir/"));
870    assert!(!glob_match("[a-y]*[^c]", "Beware"));
871    assert!(!glob_match("[a-y]*[^c]", "c"));
872    assert!(glob_match("[a-y]*[^c]", "ca"));
873    assert!(glob_match("[a-y]*[^c]", "cb"));
874    assert!(!glob_match("[a-y]*[^c]", "d"));
875    assert!(glob_match("[a-y]*[^c]", "dd"));
876    assert!(glob_match("[a-y]*[^c]", "dd"));
877    assert!(glob_match("[a-y]*[^c]", "dd"));
878    assert!(glob_match("[a-y]*[^c]", "de"));
879    assert!(glob_match("[a-y]*[^c]", "baz"));
880    assert!(glob_match("[a-y]*[^c]", "bzz"));
881    assert!(glob_match("[a-y]*[^c]", "bzz"));
882    // assert(!isMatch('bzz', '[a-y]*[^c]', { regex: true }));
883    assert!(!glob_match("[a-y]*[^c]", "BZZ"));
884    assert!(glob_match("[a-y]*[^c]", "beware"));
885    assert!(!glob_match("[a-y]*[^c]", "BewAre"));
886
887    assert!(glob_match("a\\*b/*", "a*b/ooo"));
888    assert!(glob_match("a\\*?/*", "a*b/ooo"));
889
890    assert!(!glob_match("a[b]c", "*"));
891    assert!(!glob_match("a[b]c", "**"));
892    assert!(!glob_match("a[b]c", "\\*"));
893    assert!(!glob_match("a[b]c", "a"));
894    assert!(!glob_match("a[b]c", "a/*"));
895    assert!(glob_match("a[b]c", "abc"));
896    assert!(!glob_match("a[b]c", "abd"));
897    assert!(!glob_match("a[b]c", "abe"));
898    assert!(!glob_match("a[b]c", "b"));
899    assert!(!glob_match("a[b]c", "bb"));
900    assert!(!glob_match("a[b]c", "bcd"));
901    assert!(!glob_match("a[b]c", "bdir/"));
902    assert!(!glob_match("a[b]c", "Beware"));
903    assert!(!glob_match("a[b]c", "c"));
904    assert!(!glob_match("a[b]c", "ca"));
905    assert!(!glob_match("a[b]c", "cb"));
906    assert!(!glob_match("a[b]c", "d"));
907    assert!(!glob_match("a[b]c", "dd"));
908    assert!(!glob_match("a[b]c", "de"));
909    assert!(!glob_match("a[b]c", "baz"));
910    assert!(!glob_match("a[b]c", "bzz"));
911    assert!(!glob_match("a[b]c", "BZZ"));
912    assert!(!glob_match("a[b]c", "beware"));
913    assert!(!glob_match("a[b]c", "BewAre"));
914
915    assert!(!glob_match("a[\"b\"]c", "*"));
916    assert!(!glob_match("a[\"b\"]c", "**"));
917    assert!(!glob_match("a[\"b\"]c", "\\*"));
918    assert!(!glob_match("a[\"b\"]c", "a"));
919    assert!(!glob_match("a[\"b\"]c", "a/*"));
920    assert!(glob_match("a[\"b\"]c", "abc"));
921    assert!(!glob_match("a[\"b\"]c", "abd"));
922    assert!(!glob_match("a[\"b\"]c", "abe"));
923    assert!(!glob_match("a[\"b\"]c", "b"));
924    assert!(!glob_match("a[\"b\"]c", "bb"));
925    assert!(!glob_match("a[\"b\"]c", "bcd"));
926    assert!(!glob_match("a[\"b\"]c", "bdir/"));
927    assert!(!glob_match("a[\"b\"]c", "Beware"));
928    assert!(!glob_match("a[\"b\"]c", "c"));
929    assert!(!glob_match("a[\"b\"]c", "ca"));
930    assert!(!glob_match("a[\"b\"]c", "cb"));
931    assert!(!glob_match("a[\"b\"]c", "d"));
932    assert!(!glob_match("a[\"b\"]c", "dd"));
933    assert!(!glob_match("a[\"b\"]c", "de"));
934    assert!(!glob_match("a[\"b\"]c", "baz"));
935    assert!(!glob_match("a[\"b\"]c", "bzz"));
936    assert!(!glob_match("a[\"b\"]c", "BZZ"));
937    assert!(!glob_match("a[\"b\"]c", "beware"));
938    assert!(!glob_match("a[\"b\"]c", "BewAre"));
939
940    assert!(!glob_match("a[\\\\b]c", "*"));
941    assert!(!glob_match("a[\\\\b]c", "**"));
942    assert!(!glob_match("a[\\\\b]c", "\\*"));
943    assert!(!glob_match("a[\\\\b]c", "a"));
944    assert!(!glob_match("a[\\\\b]c", "a/*"));
945    assert!(glob_match("a[\\\\b]c", "abc"));
946    assert!(!glob_match("a[\\\\b]c", "abd"));
947    assert!(!glob_match("a[\\\\b]c", "abe"));
948    assert!(!glob_match("a[\\\\b]c", "b"));
949    assert!(!glob_match("a[\\\\b]c", "bb"));
950    assert!(!glob_match("a[\\\\b]c", "bcd"));
951    assert!(!glob_match("a[\\\\b]c", "bdir/"));
952    assert!(!glob_match("a[\\\\b]c", "Beware"));
953    assert!(!glob_match("a[\\\\b]c", "c"));
954    assert!(!glob_match("a[\\\\b]c", "ca"));
955    assert!(!glob_match("a[\\\\b]c", "cb"));
956    assert!(!glob_match("a[\\\\b]c", "d"));
957    assert!(!glob_match("a[\\\\b]c", "dd"));
958    assert!(!glob_match("a[\\\\b]c", "de"));
959    assert!(!glob_match("a[\\\\b]c", "baz"));
960    assert!(!glob_match("a[\\\\b]c", "bzz"));
961    assert!(!glob_match("a[\\\\b]c", "BZZ"));
962    assert!(!glob_match("a[\\\\b]c", "beware"));
963    assert!(!glob_match("a[\\\\b]c", "BewAre"));
964
965    assert!(!glob_match("a[\\b]c", "*"));
966    assert!(!glob_match("a[\\b]c", "**"));
967    assert!(!glob_match("a[\\b]c", "\\*"));
968    assert!(!glob_match("a[\\b]c", "a"));
969    assert!(!glob_match("a[\\b]c", "a/*"));
970    assert!(!glob_match("a[\\b]c", "abc"));
971    assert!(!glob_match("a[\\b]c", "abd"));
972    assert!(!glob_match("a[\\b]c", "abe"));
973    assert!(!glob_match("a[\\b]c", "b"));
974    assert!(!glob_match("a[\\b]c", "bb"));
975    assert!(!glob_match("a[\\b]c", "bcd"));
976    assert!(!glob_match("a[\\b]c", "bdir/"));
977    assert!(!glob_match("a[\\b]c", "Beware"));
978    assert!(!glob_match("a[\\b]c", "c"));
979    assert!(!glob_match("a[\\b]c", "ca"));
980    assert!(!glob_match("a[\\b]c", "cb"));
981    assert!(!glob_match("a[\\b]c", "d"));
982    assert!(!glob_match("a[\\b]c", "dd"));
983    assert!(!glob_match("a[\\b]c", "de"));
984    assert!(!glob_match("a[\\b]c", "baz"));
985    assert!(!glob_match("a[\\b]c", "bzz"));
986    assert!(!glob_match("a[\\b]c", "BZZ"));
987    assert!(!glob_match("a[\\b]c", "beware"));
988    assert!(!glob_match("a[\\b]c", "BewAre"));
989
990    assert!(!glob_match("a[b-d]c", "*"));
991    assert!(!glob_match("a[b-d]c", "**"));
992    assert!(!glob_match("a[b-d]c", "\\*"));
993    assert!(!glob_match("a[b-d]c", "a"));
994    assert!(!glob_match("a[b-d]c", "a/*"));
995    assert!(glob_match("a[b-d]c", "abc"));
996    assert!(!glob_match("a[b-d]c", "abd"));
997    assert!(!glob_match("a[b-d]c", "abe"));
998    assert!(!glob_match("a[b-d]c", "b"));
999    assert!(!glob_match("a[b-d]c", "bb"));
1000    assert!(!glob_match("a[b-d]c", "bcd"));
1001    assert!(!glob_match("a[b-d]c", "bdir/"));
1002    assert!(!glob_match("a[b-d]c", "Beware"));
1003    assert!(!glob_match("a[b-d]c", "c"));
1004    assert!(!glob_match("a[b-d]c", "ca"));
1005    assert!(!glob_match("a[b-d]c", "cb"));
1006    assert!(!glob_match("a[b-d]c", "d"));
1007    assert!(!glob_match("a[b-d]c", "dd"));
1008    assert!(!glob_match("a[b-d]c", "de"));
1009    assert!(!glob_match("a[b-d]c", "baz"));
1010    assert!(!glob_match("a[b-d]c", "bzz"));
1011    assert!(!glob_match("a[b-d]c", "BZZ"));
1012    assert!(!glob_match("a[b-d]c", "beware"));
1013    assert!(!glob_match("a[b-d]c", "BewAre"));
1014
1015    assert!(!glob_match("a?c", "*"));
1016    assert!(!glob_match("a?c", "**"));
1017    assert!(!glob_match("a?c", "\\*"));
1018    assert!(!glob_match("a?c", "a"));
1019    assert!(!glob_match("a?c", "a/*"));
1020    assert!(glob_match("a?c", "abc"));
1021    assert!(!glob_match("a?c", "abd"));
1022    assert!(!glob_match("a?c", "abe"));
1023    assert!(!glob_match("a?c", "b"));
1024    assert!(!glob_match("a?c", "bb"));
1025    assert!(!glob_match("a?c", "bcd"));
1026    assert!(!glob_match("a?c", "bdir/"));
1027    assert!(!glob_match("a?c", "Beware"));
1028    assert!(!glob_match("a?c", "c"));
1029    assert!(!glob_match("a?c", "ca"));
1030    assert!(!glob_match("a?c", "cb"));
1031    assert!(!glob_match("a?c", "d"));
1032    assert!(!glob_match("a?c", "dd"));
1033    assert!(!glob_match("a?c", "de"));
1034    assert!(!glob_match("a?c", "baz"));
1035    assert!(!glob_match("a?c", "bzz"));
1036    assert!(!glob_match("a?c", "BZZ"));
1037    assert!(!glob_match("a?c", "beware"));
1038    assert!(!glob_match("a?c", "BewAre"));
1039
1040    assert!(glob_match("*/man*/bash.*", "man/man1/bash.1"));
1041
1042    assert!(glob_match("[^a-c]*", "*"));
1043    assert!(glob_match("[^a-c]*", "**"));
1044    assert!(!glob_match("[^a-c]*", "a"));
1045    assert!(!glob_match("[^a-c]*", "a/*"));
1046    assert!(!glob_match("[^a-c]*", "abc"));
1047    assert!(!glob_match("[^a-c]*", "abd"));
1048    assert!(!glob_match("[^a-c]*", "abe"));
1049    assert!(!glob_match("[^a-c]*", "b"));
1050    assert!(!glob_match("[^a-c]*", "bb"));
1051    assert!(!glob_match("[^a-c]*", "bcd"));
1052    assert!(!glob_match("[^a-c]*", "bdir/"));
1053    assert!(glob_match("[^a-c]*", "Beware"));
1054    assert!(glob_match("[^a-c]*", "Beware"));
1055    assert!(!glob_match("[^a-c]*", "c"));
1056    assert!(!glob_match("[^a-c]*", "ca"));
1057    assert!(!glob_match("[^a-c]*", "cb"));
1058    assert!(glob_match("[^a-c]*", "d"));
1059    assert!(glob_match("[^a-c]*", "dd"));
1060    assert!(glob_match("[^a-c]*", "de"));
1061    assert!(!glob_match("[^a-c]*", "baz"));
1062    assert!(!glob_match("[^a-c]*", "bzz"));
1063    assert!(glob_match("[^a-c]*", "BZZ"));
1064    assert!(!glob_match("[^a-c]*", "beware"));
1065    assert!(glob_match("[^a-c]*", "BewAre"));
1066  }
1067
1068  #[test]
1069  fn bash_wildmatch() {
1070    assert!(!glob_match("a[]-]b", "aab"));
1071    assert!(!glob_match("[ten]", "ten"));
1072    assert!(glob_match("]", "]"));
1073    assert!(glob_match("a[]-]b", "a-b"));
1074    assert!(glob_match("a[]-]b", "a]b"));
1075    assert!(glob_match("a[]]b", "a]b"));
1076    assert!(glob_match("a[\\]a\\-]b", "aab"));
1077    assert!(glob_match("t[a-g]n", "ten"));
1078    assert!(glob_match("t[^a-g]n", "ton"));
1079  }
1080
1081  #[test]
1082  fn bash_slashmatch() {
1083    // assert!(!glob_match("f[^eiu][^eiu][^eiu][^eiu][^eiu]r", "foo/bar"));
1084    assert!(glob_match("foo[/]bar", "foo/bar"));
1085    assert!(glob_match("f[^eiu][^eiu][^eiu][^eiu][^eiu]r", "foo-bar"));
1086  }
1087
1088  #[test]
1089  fn bash_extra_stars() {
1090    assert!(!glob_match("a**c", "bbc"));
1091    assert!(glob_match("a**c", "abc"));
1092    assert!(!glob_match("a**c", "bbd"));
1093
1094    assert!(!glob_match("a***c", "bbc"));
1095    assert!(glob_match("a***c", "abc"));
1096    assert!(!glob_match("a***c", "bbd"));
1097
1098    assert!(!glob_match("a*****?c", "bbc"));
1099    assert!(glob_match("a*****?c", "abc"));
1100    assert!(!glob_match("a*****?c", "bbc"));
1101
1102    assert!(glob_match("?*****??", "bbc"));
1103    assert!(glob_match("?*****??", "abc"));
1104
1105    assert!(glob_match("*****??", "bbc"));
1106    assert!(glob_match("*****??", "abc"));
1107
1108    assert!(glob_match("?*****?c", "bbc"));
1109    assert!(glob_match("?*****?c", "abc"));
1110
1111    assert!(glob_match("?***?****c", "bbc"));
1112    assert!(glob_match("?***?****c", "abc"));
1113    assert!(!glob_match("?***?****c", "bbd"));
1114
1115    assert!(glob_match("?***?****?", "bbc"));
1116    assert!(glob_match("?***?****?", "abc"));
1117
1118    assert!(glob_match("?***?****", "bbc"));
1119    assert!(glob_match("?***?****", "abc"));
1120
1121    assert!(glob_match("*******c", "bbc"));
1122    assert!(glob_match("*******c", "abc"));
1123
1124    assert!(glob_match("*******?", "bbc"));
1125    assert!(glob_match("*******?", "abc"));
1126
1127    assert!(glob_match("a*cd**?**??k", "abcdecdhjk"));
1128    assert!(glob_match("a**?**cd**?**??k", "abcdecdhjk"));
1129    assert!(glob_match("a**?**cd**?**??k***", "abcdecdhjk"));
1130    assert!(glob_match("a**?**cd**?**??***k", "abcdecdhjk"));
1131    assert!(glob_match("a**?**cd**?**??***k**", "abcdecdhjk"));
1132    assert!(glob_match("a****c**?**??*****", "abcdecdhjk"));
1133  }
1134
1135  #[test]
1136  fn stars() {
1137    assert!(!glob_match("*.js", "a/b/c/z.js"));
1138    assert!(!glob_match("*.js", "a/b/z.js"));
1139    assert!(!glob_match("*.js", "a/z.js"));
1140    assert!(glob_match("*.js", "z.js"));
1141
1142    // assert!(!glob_match("*/*", "a/.ab"));
1143    // assert!(!glob_match("*", ".ab"));
1144
1145    assert!(glob_match("z*.js", "z.js"));
1146    assert!(glob_match("*/*", "a/z"));
1147    assert!(glob_match("*/z*.js", "a/z.js"));
1148    assert!(glob_match("a/z*.js", "a/z.js"));
1149
1150    assert!(glob_match("*", "ab"));
1151    assert!(glob_match("*", "abc"));
1152
1153    assert!(!glob_match("f*", "bar"));
1154    assert!(!glob_match("*r", "foo"));
1155    assert!(!glob_match("b*", "foo"));
1156    assert!(!glob_match("*", "foo/bar"));
1157    assert!(glob_match("*c", "abc"));
1158    assert!(glob_match("a*", "abc"));
1159    assert!(glob_match("a*c", "abc"));
1160    assert!(glob_match("*r", "bar"));
1161    assert!(glob_match("b*", "bar"));
1162    assert!(glob_match("f*", "foo"));
1163
1164    assert!(glob_match("*abc*", "one abc two"));
1165    assert!(glob_match("a*b", "a         b"));
1166
1167    assert!(!glob_match("*a*", "foo"));
1168    assert!(glob_match("*a*", "bar"));
1169    assert!(glob_match("*abc*", "oneabctwo"));
1170    assert!(!glob_match("*-bc-*", "a-b.c-d"));
1171    assert!(glob_match("*-*.*-*", "a-b.c-d"));
1172    assert!(glob_match("*-b*c-*", "a-b.c-d"));
1173    assert!(glob_match("*-b.c-*", "a-b.c-d"));
1174    assert!(glob_match("*.*", "a-b.c-d"));
1175    assert!(glob_match("*.*-*", "a-b.c-d"));
1176    assert!(glob_match("*.*-d", "a-b.c-d"));
1177    assert!(glob_match("*.c-*", "a-b.c-d"));
1178    assert!(glob_match("*b.*d", "a-b.c-d"));
1179    assert!(glob_match("a*.c*", "a-b.c-d"));
1180    assert!(glob_match("a-*.*-d", "a-b.c-d"));
1181    assert!(glob_match("*.*", "a.b"));
1182    assert!(glob_match("*.b", "a.b"));
1183    assert!(glob_match("a.*", "a.b"));
1184    assert!(glob_match("a.b", "a.b"));
1185
1186    assert!(!glob_match("**-bc-**", "a-b.c-d"));
1187    assert!(glob_match("**-**.**-**", "a-b.c-d"));
1188    assert!(glob_match("**-b**c-**", "a-b.c-d"));
1189    assert!(glob_match("**-b.c-**", "a-b.c-d"));
1190    assert!(glob_match("**.**", "a-b.c-d"));
1191    assert!(glob_match("**.**-**", "a-b.c-d"));
1192    assert!(glob_match("**.**-d", "a-b.c-d"));
1193    assert!(glob_match("**.c-**", "a-b.c-d"));
1194    assert!(glob_match("**b.**d", "a-b.c-d"));
1195    assert!(glob_match("a**.c**", "a-b.c-d"));
1196    assert!(glob_match("a-**.**-d", "a-b.c-d"));
1197    assert!(glob_match("**.**", "a.b"));
1198    assert!(glob_match("**.b", "a.b"));
1199    assert!(glob_match("a.**", "a.b"));
1200    assert!(glob_match("a.b", "a.b"));
1201
1202    assert!(glob_match("*/*", "/ab"));
1203    assert!(glob_match(".", "."));
1204    assert!(!glob_match("a/", "a/.b"));
1205    assert!(glob_match("/*", "/ab"));
1206    assert!(glob_match("/??", "/ab"));
1207    assert!(glob_match("/?b", "/ab"));
1208    assert!(glob_match("/*", "/cd"));
1209    assert!(glob_match("a", "a"));
1210    assert!(glob_match("a/.*", "a/.b"));
1211    assert!(glob_match("?/?", "a/b"));
1212    assert!(glob_match("a/**/j/**/z/*.md", "a/b/c/d/e/j/n/p/o/z/c.md"));
1213    assert!(glob_match("a/**/z/*.md", "a/b/c/d/e/z/c.md"));
1214    assert!(glob_match("a/b/c/*.md", "a/b/c/xyz.md"));
1215    assert!(glob_match("a/b/c/*.md", "a/b/c/xyz.md"));
1216    assert!(glob_match("a/*/z/.a", "a/b/z/.a"));
1217    assert!(!glob_match("bz", "a/b/z/.a"));
1218    assert!(glob_match("a/**/c/*.md", "a/bb.bb/aa/b.b/aa/c/xyz.md"));
1219    assert!(glob_match("a/**/c/*.md", "a/bb.bb/aa/bb/aa/c/xyz.md"));
1220    assert!(glob_match("a/*/c/*.md", "a/bb.bb/c/xyz.md"));
1221    assert!(glob_match("a/*/c/*.md", "a/bb/c/xyz.md"));
1222    assert!(glob_match("a/*/c/*.md", "a/bbbb/c/xyz.md"));
1223    assert!(glob_match("*", "aaa"));
1224    assert!(glob_match("*", "ab"));
1225    assert!(glob_match("ab", "ab"));
1226
1227    assert!(!glob_match("*/*/*", "aaa"));
1228    assert!(!glob_match("*/*/*", "aaa/bb/aa/rr"));
1229    assert!(!glob_match("aaa*", "aaa/bba/ccc"));
1230    // assert!(!glob_match("aaa**", "aaa/bba/ccc"));
1231    assert!(!glob_match("aaa/*", "aaa/bba/ccc"));
1232    assert!(!glob_match("aaa/*ccc", "aaa/bba/ccc"));
1233    assert!(!glob_match("aaa/*z", "aaa/bba/ccc"));
1234    assert!(!glob_match("*/*/*", "aaa/bbb"));
1235    assert!(!glob_match("*/*jk*/*i", "ab/zzz/ejkl/hi"));
1236    assert!(glob_match("*/*/*", "aaa/bba/ccc"));
1237    assert!(glob_match("aaa/**", "aaa/bba/ccc"));
1238    assert!(glob_match("aaa/*", "aaa/bbb"));
1239    assert!(glob_match("*/*z*/*/*i", "ab/zzz/ejkl/hi"));
1240    assert!(glob_match("*j*i", "abzzzejklhi"));
1241
1242    assert!(glob_match("*", "a"));
1243    assert!(glob_match("*", "b"));
1244    assert!(!glob_match("*", "a/a"));
1245    assert!(!glob_match("*", "a/a/a"));
1246    assert!(!glob_match("*", "a/a/b"));
1247    assert!(!glob_match("*", "a/a/a/a"));
1248    assert!(!glob_match("*", "a/a/a/a/a"));
1249
1250    assert!(!glob_match("*/*", "a"));
1251    assert!(glob_match("*/*", "a/a"));
1252    assert!(!glob_match("*/*", "a/a/a"));
1253
1254    assert!(!glob_match("*/*/*", "a"));
1255    assert!(!glob_match("*/*/*", "a/a"));
1256    assert!(glob_match("*/*/*", "a/a/a"));
1257    assert!(!glob_match("*/*/*", "a/a/a/a"));
1258
1259    assert!(!glob_match("*/*/*/*", "a"));
1260    assert!(!glob_match("*/*/*/*", "a/a"));
1261    assert!(!glob_match("*/*/*/*", "a/a/a"));
1262    assert!(glob_match("*/*/*/*", "a/a/a/a"));
1263    assert!(!glob_match("*/*/*/*", "a/a/a/a/a"));
1264
1265    assert!(!glob_match("*/*/*/*/*", "a"));
1266    assert!(!glob_match("*/*/*/*/*", "a/a"));
1267    assert!(!glob_match("*/*/*/*/*", "a/a/a"));
1268    assert!(!glob_match("*/*/*/*/*", "a/a/b"));
1269    assert!(!glob_match("*/*/*/*/*", "a/a/a/a"));
1270    assert!(glob_match("*/*/*/*/*", "a/a/a/a/a"));
1271    assert!(!glob_match("*/*/*/*/*", "a/a/a/a/a/a"));
1272
1273    assert!(!glob_match("a/*", "a"));
1274    assert!(glob_match("a/*", "a/a"));
1275    assert!(!glob_match("a/*", "a/a/a"));
1276    assert!(!glob_match("a/*", "a/a/a/a"));
1277    assert!(!glob_match("a/*", "a/a/a/a/a"));
1278
1279    assert!(!glob_match("a/*/*", "a"));
1280    assert!(!glob_match("a/*/*", "a/a"));
1281    assert!(glob_match("a/*/*", "a/a/a"));
1282    assert!(!glob_match("a/*/*", "b/a/a"));
1283    assert!(!glob_match("a/*/*", "a/a/a/a"));
1284    assert!(!glob_match("a/*/*", "a/a/a/a/a"));
1285
1286    assert!(!glob_match("a/*/*/*", "a"));
1287    assert!(!glob_match("a/*/*/*", "a/a"));
1288    assert!(!glob_match("a/*/*/*", "a/a/a"));
1289    assert!(glob_match("a/*/*/*", "a/a/a/a"));
1290    assert!(!glob_match("a/*/*/*", "a/a/a/a/a"));
1291
1292    assert!(!glob_match("a/*/*/*/*", "a"));
1293    assert!(!glob_match("a/*/*/*/*", "a/a"));
1294    assert!(!glob_match("a/*/*/*/*", "a/a/a"));
1295    assert!(!glob_match("a/*/*/*/*", "a/a/b"));
1296    assert!(!glob_match("a/*/*/*/*", "a/a/a/a"));
1297    assert!(glob_match("a/*/*/*/*", "a/a/a/a/a"));
1298
1299    assert!(!glob_match("a/*/a", "a"));
1300    assert!(!glob_match("a/*/a", "a/a"));
1301    assert!(glob_match("a/*/a", "a/a/a"));
1302    assert!(!glob_match("a/*/a", "a/a/b"));
1303    assert!(!glob_match("a/*/a", "a/a/a/a"));
1304    assert!(!glob_match("a/*/a", "a/a/a/a/a"));
1305
1306    assert!(!glob_match("a/*/b", "a"));
1307    assert!(!glob_match("a/*/b", "a/a"));
1308    assert!(!glob_match("a/*/b", "a/a/a"));
1309    assert!(glob_match("a/*/b", "a/a/b"));
1310    assert!(!glob_match("a/*/b", "a/a/a/a"));
1311    assert!(!glob_match("a/*/b", "a/a/a/a/a"));
1312
1313    assert!(!glob_match("*/**/a", "a"));
1314    assert!(!glob_match("*/**/a", "a/a/b"));
1315    assert!(glob_match("*/**/a", "a/a"));
1316    assert!(glob_match("*/**/a", "a/a/a"));
1317    assert!(glob_match("*/**/a", "a/a/a/a"));
1318    assert!(glob_match("*/**/a", "a/a/a/a/a"));
1319
1320    assert!(!glob_match("*/", "a"));
1321    assert!(!glob_match("*/*", "a"));
1322    assert!(!glob_match("a/*", "a"));
1323    // assert!(!glob_match("*/*", "a/"));
1324    // assert!(!glob_match("a/*", "a/"));
1325    assert!(!glob_match("*", "a/a"));
1326    assert!(!glob_match("*/", "a/a"));
1327    assert!(!glob_match("*/", "a/x/y"));
1328    assert!(!glob_match("*/*", "a/x/y"));
1329    assert!(!glob_match("a/*", "a/x/y"));
1330    // assert!(glob_match("*", "a/"));
1331    assert!(glob_match("*", "a"));
1332    assert!(glob_match("*/", "a/"));
1333    assert!(glob_match("*{,/}", "a/"));
1334    assert!(glob_match("*/*", "a/a"));
1335    assert!(glob_match("a/*", "a/a"));
1336
1337    assert!(!glob_match("a/**/*.txt", "a.txt"));
1338    assert!(glob_match("a/**/*.txt", "a/x/y.txt"));
1339    assert!(!glob_match("a/**/*.txt", "a/x/y/z"));
1340
1341    assert!(!glob_match("a/*.txt", "a.txt"));
1342    assert!(glob_match("a/*.txt", "a/b.txt"));
1343    assert!(!glob_match("a/*.txt", "a/x/y.txt"));
1344    assert!(!glob_match("a/*.txt", "a/x/y/z"));
1345
1346    assert!(glob_match("a*.txt", "a.txt"));
1347    assert!(!glob_match("a*.txt", "a/b.txt"));
1348    assert!(!glob_match("a*.txt", "a/x/y.txt"));
1349    assert!(!glob_match("a*.txt", "a/x/y/z"));
1350
1351    assert!(glob_match("*.txt", "a.txt"));
1352    assert!(!glob_match("*.txt", "a/b.txt"));
1353    assert!(!glob_match("*.txt", "a/x/y.txt"));
1354    assert!(!glob_match("*.txt", "a/x/y/z"));
1355
1356    assert!(!glob_match("a*", "a/b"));
1357    assert!(!glob_match("a/**/b", "a/a/bb"));
1358    assert!(!glob_match("a/**/b", "a/bb"));
1359
1360    assert!(!glob_match("*/**", "foo"));
1361    assert!(!glob_match("**/", "foo/bar"));
1362    assert!(!glob_match("**/*/", "foo/bar"));
1363    assert!(!glob_match("*/*/", "foo/bar"));
1364
1365    assert!(glob_match("**/..", "/home/foo/.."));
1366    assert!(glob_match("**/a", "a"));
1367    assert!(glob_match("**", "a/a"));
1368    assert!(glob_match("a/**", "a/a"));
1369    assert!(glob_match("a/**", "a/"));
1370    // assert!(glob_match("a/**", "a"));
1371    assert!(!glob_match("**/", "a/a"));
1372    // assert!(glob_match("**/a/**", "a"));
1373    // assert!(glob_match("a/**", "a"));
1374    assert!(!glob_match("**/", "a/a"));
1375    assert!(glob_match("*/**/a", "a/a"));
1376    // assert!(glob_match("a/**", "a"));
1377    assert!(glob_match("*/**", "foo/"));
1378    assert!(glob_match("**/*", "foo/bar"));
1379    assert!(glob_match("*/*", "foo/bar"));
1380    assert!(glob_match("*/**", "foo/bar"));
1381    assert!(glob_match("**/", "foo/bar/"));
1382    // assert!(glob_match("**/*", "foo/bar/"));
1383    assert!(glob_match("**/*/", "foo/bar/"));
1384    assert!(glob_match("*/**", "foo/bar/"));
1385    assert!(glob_match("*/*/", "foo/bar/"));
1386
1387    assert!(!glob_match("*/foo", "bar/baz/foo"));
1388    assert!(!glob_match("**/bar/*", "deep/foo/bar"));
1389    assert!(!glob_match("*/bar/**", "deep/foo/bar/baz/x"));
1390    assert!(!glob_match("/*", "ef"));
1391    assert!(!glob_match("foo?bar", "foo/bar"));
1392    assert!(!glob_match("**/bar*", "foo/bar/baz"));
1393    // assert!(!glob_match("**/bar**", "foo/bar/baz"));
1394    assert!(!glob_match("foo**bar", "foo/baz/bar"));
1395    assert!(!glob_match("foo*bar", "foo/baz/bar"));
1396    // assert!(glob_match("foo/**", "foo"));
1397    assert!(glob_match("/*", "/ab"));
1398    assert!(glob_match("/*", "/cd"));
1399    assert!(glob_match("/*", "/ef"));
1400    assert!(glob_match("a/**/j/**/z/*.md", "a/b/j/c/z/x.md"));
1401    assert!(glob_match("a/**/j/**/z/*.md", "a/j/z/x.md"));
1402
1403    assert!(glob_match("**/foo", "bar/baz/foo"));
1404    assert!(glob_match("**/bar/*", "deep/foo/bar/baz"));
1405    assert!(glob_match("**/bar/**", "deep/foo/bar/baz/"));
1406    assert!(glob_match("**/bar/*/*", "deep/foo/bar/baz/x"));
1407    assert!(glob_match("foo/**/**/bar", "foo/b/a/z/bar"));
1408    assert!(glob_match("foo/**/bar", "foo/b/a/z/bar"));
1409    assert!(glob_match("foo/**/**/bar", "foo/bar"));
1410    assert!(glob_match("foo/**/bar", "foo/bar"));
1411    assert!(glob_match("*/bar/**", "foo/bar/baz/x"));
1412    assert!(glob_match("foo/**/**/bar", "foo/baz/bar"));
1413    assert!(glob_match("foo/**/bar", "foo/baz/bar"));
1414    assert!(glob_match("**/foo", "XXX/foo"));
1415  }
1416
1417  #[test]
1418  fn globstars() {
1419    assert!(glob_match("**/*.js", "a/b/c/d.js"));
1420    assert!(glob_match("**/*.js", "a/b/c.js"));
1421    assert!(glob_match("**/*.js", "a/b.js"));
1422    assert!(glob_match("a/b/**/*.js", "a/b/c/d/e/f.js"));
1423    assert!(glob_match("a/b/**/*.js", "a/b/c/d/e.js"));
1424    assert!(glob_match("a/b/c/**/*.js", "a/b/c/d.js"));
1425    assert!(glob_match("a/b/**/*.js", "a/b/c/d.js"));
1426    assert!(glob_match("a/b/**/*.js", "a/b/d.js"));
1427    assert!(!glob_match("a/b/**/*.js", "a/d.js"));
1428    assert!(!glob_match("a/b/**/*.js", "d.js"));
1429
1430    assert!(!glob_match("**c", "a/b/c"));
1431    assert!(!glob_match("a/**c", "a/b/c"));
1432    assert!(!glob_match("a/**z", "a/b/c"));
1433    assert!(!glob_match("a/**b**/c", "a/b/c/b/c"));
1434    assert!(!glob_match("a/b/c**/*.js", "a/b/c/d/e.js"));
1435    assert!(glob_match("a/**/b/**/c", "a/b/c/b/c"));
1436    assert!(glob_match("a/**b**/c", "a/aba/c"));
1437    assert!(glob_match("a/**b**/c", "a/b/c"));
1438    assert!(glob_match("a/b/c**/*.js", "a/b/c/d.js"));
1439
1440    assert!(!glob_match("a/**/*", "a"));
1441    assert!(!glob_match("a/**/**/*", "a"));
1442    assert!(!glob_match("a/**/**/**/*", "a"));
1443    assert!(!glob_match("**/a", "a/"));
1444    assert!(!glob_match("a/**/*", "a/"));
1445    assert!(!glob_match("a/**/**/*", "a/"));
1446    assert!(!glob_match("a/**/**/**/*", "a/"));
1447    assert!(!glob_match("**/a", "a/b"));
1448    assert!(!glob_match("a/**/j/**/z/*.md", "a/b/c/j/e/z/c.txt"));
1449    assert!(!glob_match("a/**/b", "a/bb"));
1450    assert!(!glob_match("**/a", "a/c"));
1451    assert!(!glob_match("**/a", "a/b"));
1452    assert!(!glob_match("**/a", "a/x/y"));
1453    assert!(!glob_match("**/a", "a/b/c/d"));
1454    assert!(glob_match("**", "a"));
1455    assert!(glob_match("**/a", "a"));
1456    // assert!(glob_match("a/**", "a"));
1457    assert!(glob_match("**", "a/"));
1458    assert!(glob_match("**/a/**", "a/"));
1459    assert!(glob_match("a/**", "a/"));
1460    assert!(glob_match("a/**/**", "a/"));
1461    assert!(glob_match("**/a", "a/a"));
1462    assert!(glob_match("**", "a/b"));
1463    assert!(glob_match("*/*", "a/b"));
1464    assert!(glob_match("a/**", "a/b"));
1465    assert!(glob_match("a/**/*", "a/b"));
1466    assert!(glob_match("a/**/**/*", "a/b"));
1467    assert!(glob_match("a/**/**/**/*", "a/b"));
1468    assert!(glob_match("a/**/b", "a/b"));
1469    assert!(glob_match("**", "a/b/c"));
1470    assert!(glob_match("**/*", "a/b/c"));
1471    assert!(glob_match("**/**", "a/b/c"));
1472    assert!(glob_match("*/**", "a/b/c"));
1473    assert!(glob_match("a/**", "a/b/c"));
1474    assert!(glob_match("a/**/*", "a/b/c"));
1475    assert!(glob_match("a/**/**/*", "a/b/c"));
1476    assert!(glob_match("a/**/**/**/*", "a/b/c"));
1477    assert!(glob_match("**", "a/b/c/d"));
1478    assert!(glob_match("a/**", "a/b/c/d"));
1479    assert!(glob_match("a/**/*", "a/b/c/d"));
1480    assert!(glob_match("a/**/**/*", "a/b/c/d"));
1481    assert!(glob_match("a/**/**/**/*", "a/b/c/d"));
1482    assert!(glob_match("a/b/**/c/**/*.*", "a/b/c/d.e"));
1483    assert!(glob_match("a/**/f/*.md", "a/b/c/d/e/f/g.md"));
1484    assert!(glob_match("a/**/f/**/k/*.md", "a/b/c/d/e/f/g/h/i/j/k/l.md"));
1485    assert!(glob_match("a/b/c/*.md", "a/b/c/def.md"));
1486    assert!(glob_match("a/*/c/*.md", "a/bb.bb/c/ddd.md"));
1487    assert!(glob_match("a/**/f/*.md", "a/bb.bb/cc/d.d/ee/f/ggg.md"));
1488    assert!(glob_match("a/**/f/*.md", "a/bb.bb/cc/dd/ee/f/ggg.md"));
1489    assert!(glob_match("a/*/c/*.md", "a/bb/c/ddd.md"));
1490    assert!(glob_match("a/*/c/*.md", "a/bbbb/c/ddd.md"));
1491
1492    assert!(glob_match(
1493      "foo/bar/**/one/**/*.*",
1494      "foo/bar/baz/one/image.png"
1495    ));
1496    assert!(glob_match(
1497      "foo/bar/**/one/**/*.*",
1498      "foo/bar/baz/one/two/image.png"
1499    ));
1500    assert!(glob_match(
1501      "foo/bar/**/one/**/*.*",
1502      "foo/bar/baz/one/two/three/image.png"
1503    ));
1504    assert!(!glob_match("a/b/**/f", "a/b/c/d/"));
1505    // assert!(glob_match("a/**", "a"));
1506    assert!(glob_match("**", "a"));
1507    // assert!(glob_match("a{,/**}", "a"));
1508    assert!(glob_match("**", "a/"));
1509    assert!(glob_match("a/**", "a/"));
1510    assert!(glob_match("**", "a/b/c/d"));
1511    assert!(glob_match("**", "a/b/c/d/"));
1512    assert!(glob_match("**/**", "a/b/c/d/"));
1513    assert!(glob_match("**/b/**", "a/b/c/d/"));
1514    assert!(glob_match("a/b/**", "a/b/c/d/"));
1515    assert!(glob_match("a/b/**/", "a/b/c/d/"));
1516    assert!(glob_match("a/b/**/c/**/", "a/b/c/d/"));
1517    assert!(glob_match("a/b/**/c/**/d/", "a/b/c/d/"));
1518    assert!(glob_match("a/b/**/**/*.*", "a/b/c/d/e.f"));
1519    assert!(glob_match("a/b/**/*.*", "a/b/c/d/e.f"));
1520    assert!(glob_match("a/b/**/c/**/d/*.*", "a/b/c/d/e.f"));
1521    assert!(glob_match("a/b/**/d/**/*.*", "a/b/c/d/e.f"));
1522    assert!(glob_match("a/b/**/d/**/*.*", "a/b/c/d/g/e.f"));
1523    assert!(glob_match("a/b/**/d/**/*.*", "a/b/c/d/g/g/e.f"));
1524    assert!(glob_match("a/b-*/**/z.js", "a/b-c/z.js"));
1525    assert!(glob_match("a/b-*/**/z.js", "a/b-c/d/e/z.js"));
1526
1527    assert!(glob_match("*/*", "a/b"));
1528    assert!(glob_match("a/b/c/*.md", "a/b/c/xyz.md"));
1529    assert!(glob_match("a/*/c/*.md", "a/bb.bb/c/xyz.md"));
1530    assert!(glob_match("a/*/c/*.md", "a/bb/c/xyz.md"));
1531    assert!(glob_match("a/*/c/*.md", "a/bbbb/c/xyz.md"));
1532
1533    assert!(glob_match("**/*", "a/b/c"));
1534    assert!(glob_match("**/**", "a/b/c"));
1535    assert!(glob_match("*/**", "a/b/c"));
1536    assert!(glob_match("a/**/j/**/z/*.md", "a/b/c/d/e/j/n/p/o/z/c.md"));
1537    assert!(glob_match("a/**/z/*.md", "a/b/c/d/e/z/c.md"));
1538    assert!(glob_match("a/**/c/*.md", "a/bb.bb/aa/b.b/aa/c/xyz.md"));
1539    assert!(glob_match("a/**/c/*.md", "a/bb.bb/aa/bb/aa/c/xyz.md"));
1540    assert!(!glob_match("a/**/j/**/z/*.md", "a/b/c/j/e/z/c.txt"));
1541    assert!(!glob_match("a/b/**/c{d,e}/**/xyz.md", "a/b/c/xyz.md"));
1542    assert!(!glob_match("a/b/**/c{d,e}/**/xyz.md", "a/b/d/xyz.md"));
1543    assert!(!glob_match("a/**/", "a/b"));
1544    // assert!(!glob_match("**/*", "a/b/.js/c.txt"));
1545    assert!(!glob_match("a/**/", "a/b/c/d"));
1546    assert!(!glob_match("a/**/", "a/bb"));
1547    assert!(!glob_match("a/**/", "a/cb"));
1548    assert!(glob_match("/**", "/a/b"));
1549    assert!(glob_match("**/*", "a.b"));
1550    assert!(glob_match("**/*", "a.js"));
1551    assert!(glob_match("**/*.js", "a.js"));
1552    // assert!(glob_match("a/**/", "a/"));
1553    assert!(glob_match("**/*.js", "a/a.js"));
1554    assert!(glob_match("**/*.js", "a/a/b.js"));
1555    assert!(glob_match("a/**/b", "a/b"));
1556    assert!(glob_match("a/**b", "a/b"));
1557    assert!(glob_match("**/*.md", "a/b.md"));
1558    assert!(glob_match("**/*", "a/b/c.js"));
1559    assert!(glob_match("**/*", "a/b/c.txt"));
1560    assert!(glob_match("a/**/", "a/b/c/d/"));
1561    assert!(glob_match("**/*", "a/b/c/d/a.js"));
1562    assert!(glob_match("a/b/**/*.js", "a/b/c/z.js"));
1563    assert!(glob_match("a/b/**/*.js", "a/b/z.js"));
1564    assert!(glob_match("**/*", "ab"));
1565    assert!(glob_match("**/*", "ab/c"));
1566    assert!(glob_match("**/*", "ab/c/d"));
1567    assert!(glob_match("**/*", "abc.js"));
1568
1569    assert!(!glob_match("**/", "a"));
1570    assert!(!glob_match("**/a/*", "a"));
1571    assert!(!glob_match("**/a/*/*", "a"));
1572    assert!(!glob_match("*/a/**", "a"));
1573    assert!(!glob_match("a/**/*", "a"));
1574    assert!(!glob_match("a/**/**/*", "a"));
1575    assert!(!glob_match("**/", "a/b"));
1576    assert!(!glob_match("**/b/*", "a/b"));
1577    assert!(!glob_match("**/b/*/*", "a/b"));
1578    assert!(!glob_match("b/**", "a/b"));
1579    assert!(!glob_match("**/", "a/b/c"));
1580    assert!(!glob_match("**/**/b", "a/b/c"));
1581    assert!(!glob_match("**/b", "a/b/c"));
1582    assert!(!glob_match("**/b/*/*", "a/b/c"));
1583    assert!(!glob_match("b/**", "a/b/c"));
1584    assert!(!glob_match("**/", "a/b/c/d"));
1585    assert!(!glob_match("**/d/*", "a/b/c/d"));
1586    assert!(!glob_match("b/**", "a/b/c/d"));
1587    assert!(glob_match("**", "a"));
1588    assert!(glob_match("**/**", "a"));
1589    assert!(glob_match("**/**/*", "a"));
1590    assert!(glob_match("**/**/a", "a"));
1591    assert!(glob_match("**/a", "a"));
1592    // assert!(glob_match("**/a/**", "a"));
1593    // assert!(glob_match("a/**", "a"));
1594    assert!(glob_match("**", "a/b"));
1595    assert!(glob_match("**/**", "a/b"));
1596    assert!(glob_match("**/**/*", "a/b"));
1597    assert!(glob_match("**/**/b", "a/b"));
1598    assert!(glob_match("**/b", "a/b"));
1599    // assert!(glob_match("**/b/**", "a/b"));
1600    // assert!(glob_match("*/b/**", "a/b"));
1601    assert!(glob_match("a/**", "a/b"));
1602    assert!(glob_match("a/**/*", "a/b"));
1603    assert!(glob_match("a/**/**/*", "a/b"));
1604    assert!(glob_match("**", "a/b/c"));
1605    assert!(glob_match("**/**", "a/b/c"));
1606    assert!(glob_match("**/**/*", "a/b/c"));
1607    assert!(glob_match("**/b/*", "a/b/c"));
1608    assert!(glob_match("**/b/**", "a/b/c"));
1609    assert!(glob_match("*/b/**", "a/b/c"));
1610    assert!(glob_match("a/**", "a/b/c"));
1611    assert!(glob_match("a/**/*", "a/b/c"));
1612    assert!(glob_match("a/**/**/*", "a/b/c"));
1613    assert!(glob_match("**", "a/b/c/d"));
1614    assert!(glob_match("**/**", "a/b/c/d"));
1615    assert!(glob_match("**/**/*", "a/b/c/d"));
1616    assert!(glob_match("**/**/d", "a/b/c/d"));
1617    assert!(glob_match("**/b/**", "a/b/c/d"));
1618    assert!(glob_match("**/b/*/*", "a/b/c/d"));
1619    assert!(glob_match("**/d", "a/b/c/d"));
1620    assert!(glob_match("*/b/**", "a/b/c/d"));
1621    assert!(glob_match("a/**", "a/b/c/d"));
1622    assert!(glob_match("a/**/*", "a/b/c/d"));
1623    assert!(glob_match("a/**/**/*", "a/b/c/d"));
1624  }
1625
1626  #[test]
1627  fn utf8() {
1628    assert!(glob_match("フ*/**/*", "フォルダ/aaa.js"));
1629    assert!(glob_match("フォ*/**/*", "フォルダ/aaa.js"));
1630    assert!(glob_match("フォル*/**/*", "フォルダ/aaa.js"));
1631    assert!(glob_match("フ*ル*/**/*", "フォルダ/aaa.js"));
1632    assert!(glob_match("フォルダ/**/*", "フォルダ/aaa.js"));
1633  }
1634
1635  #[test]
1636  fn negation() {
1637    assert!(!glob_match("!*", "abc"));
1638    assert!(!glob_match("!abc", "abc"));
1639    assert!(!glob_match("*!.md", "bar.md"));
1640    assert!(!glob_match("foo!.md", "bar.md"));
1641    assert!(!glob_match("\\!*!*.md", "foo!.md"));
1642    assert!(!glob_match("\\!*!*.md", "foo!bar.md"));
1643    assert!(glob_match("*!*.md", "!foo!.md"));
1644    assert!(glob_match("\\!*!*.md", "!foo!.md"));
1645    assert!(glob_match("!*foo", "abc"));
1646    assert!(glob_match("!foo*", "abc"));
1647    assert!(glob_match("!xyz", "abc"));
1648    assert!(glob_match("*!*.*", "ba!r.js"));
1649    assert!(glob_match("*.md", "bar.md"));
1650    assert!(glob_match("*!*.*", "foo!.md"));
1651    assert!(glob_match("*!*.md", "foo!.md"));
1652    assert!(glob_match("*!.md", "foo!.md"));
1653    assert!(glob_match("*.md", "foo!.md"));
1654    assert!(glob_match("foo!.md", "foo!.md"));
1655    assert!(glob_match("*!*.md", "foo!bar.md"));
1656    assert!(glob_match("*b*.md", "foobar.md"));
1657
1658    assert!(!glob_match("a!!b", "a"));
1659    assert!(!glob_match("a!!b", "aa"));
1660    assert!(!glob_match("a!!b", "a/b"));
1661    assert!(!glob_match("a!!b", "a!b"));
1662    assert!(glob_match("a!!b", "a!!b"));
1663    assert!(!glob_match("a!!b", "a/!!/b"));
1664
1665    assert!(!glob_match("!a/b", "a/b"));
1666    assert!(glob_match("!a/b", "a"));
1667    assert!(glob_match("!a/b", "a.b"));
1668    assert!(glob_match("!a/b", "a/a"));
1669    assert!(glob_match("!a/b", "a/c"));
1670    assert!(glob_match("!a/b", "b/a"));
1671    assert!(glob_match("!a/b", "b/b"));
1672    assert!(glob_match("!a/b", "b/c"));
1673
1674    assert!(!glob_match("!abc", "abc"));
1675    assert!(glob_match("!!abc", "abc"));
1676    assert!(!glob_match("!!!abc", "abc"));
1677    assert!(glob_match("!!!!abc", "abc"));
1678    assert!(!glob_match("!!!!!abc", "abc"));
1679    assert!(glob_match("!!!!!!abc", "abc"));
1680    assert!(!glob_match("!!!!!!!abc", "abc"));
1681    assert!(glob_match("!!!!!!!!abc", "abc"));
1682
1683    // assert!(!glob_match("!(*/*)", "a/a"));
1684    // assert!(!glob_match("!(*/*)", "a/b"));
1685    // assert!(!glob_match("!(*/*)", "a/c"));
1686    // assert!(!glob_match("!(*/*)", "b/a"));
1687    // assert!(!glob_match("!(*/*)", "b/b"));
1688    // assert!(!glob_match("!(*/*)", "b/c"));
1689    // assert!(!glob_match("!(*/b)", "a/b"));
1690    // assert!(!glob_match("!(*/b)", "b/b"));
1691    // assert!(!glob_match("!(a/b)", "a/b"));
1692    assert!(!glob_match("!*", "a"));
1693    assert!(!glob_match("!*", "a.b"));
1694    assert!(!glob_match("!*/*", "a/a"));
1695    assert!(!glob_match("!*/*", "a/b"));
1696    assert!(!glob_match("!*/*", "a/c"));
1697    assert!(!glob_match("!*/*", "b/a"));
1698    assert!(!glob_match("!*/*", "b/b"));
1699    assert!(!glob_match("!*/*", "b/c"));
1700    assert!(!glob_match("!*/b", "a/b"));
1701    assert!(!glob_match("!*/b", "b/b"));
1702    assert!(!glob_match("!*/c", "a/c"));
1703    assert!(!glob_match("!*/c", "a/c"));
1704    assert!(!glob_match("!*/c", "b/c"));
1705    assert!(!glob_match("!*/c", "b/c"));
1706    assert!(!glob_match("!*a*", "bar"));
1707    assert!(!glob_match("!*a*", "fab"));
1708    // assert!(!glob_match("!a/(*)", "a/a"));
1709    // assert!(!glob_match("!a/(*)", "a/b"));
1710    // assert!(!glob_match("!a/(*)", "a/c"));
1711    // assert!(!glob_match("!a/(b)", "a/b"));
1712    assert!(!glob_match("!a/*", "a/a"));
1713    assert!(!glob_match("!a/*", "a/b"));
1714    assert!(!glob_match("!a/*", "a/c"));
1715    assert!(!glob_match("!f*b", "fab"));
1716    // assert!(glob_match("!(*/*)", "a"));
1717    // assert!(glob_match("!(*/*)", "a.b"));
1718    // assert!(glob_match("!(*/b)", "a"));
1719    // assert!(glob_match("!(*/b)", "a.b"));
1720    // assert!(glob_match("!(*/b)", "a/a"));
1721    // assert!(glob_match("!(*/b)", "a/c"));
1722    // assert!(glob_match("!(*/b)", "b/a"));
1723    // assert!(glob_match("!(*/b)", "b/c"));
1724    // assert!(glob_match("!(a/b)", "a"));
1725    // assert!(glob_match("!(a/b)", "a.b"));
1726    // assert!(glob_match("!(a/b)", "a/a"));
1727    // assert!(glob_match("!(a/b)", "a/c"));
1728    // assert!(glob_match("!(a/b)", "b/a"));
1729    // assert!(glob_match("!(a/b)", "b/b"));
1730    // assert!(glob_match("!(a/b)", "b/c"));
1731    assert!(glob_match("!*", "a/a"));
1732    assert!(glob_match("!*", "a/b"));
1733    assert!(glob_match("!*", "a/c"));
1734    assert!(glob_match("!*", "b/a"));
1735    assert!(glob_match("!*", "b/b"));
1736    assert!(glob_match("!*", "b/c"));
1737    assert!(glob_match("!*/*", "a"));
1738    assert!(glob_match("!*/*", "a.b"));
1739    assert!(glob_match("!*/b", "a"));
1740    assert!(glob_match("!*/b", "a.b"));
1741    assert!(glob_match("!*/b", "a/a"));
1742    assert!(glob_match("!*/b", "a/c"));
1743    assert!(glob_match("!*/b", "b/a"));
1744    assert!(glob_match("!*/b", "b/c"));
1745    assert!(glob_match("!*/c", "a"));
1746    assert!(glob_match("!*/c", "a.b"));
1747    assert!(glob_match("!*/c", "a/a"));
1748    assert!(glob_match("!*/c", "a/b"));
1749    assert!(glob_match("!*/c", "b/a"));
1750    assert!(glob_match("!*/c", "b/b"));
1751    assert!(glob_match("!*a*", "foo"));
1752    // assert!(glob_match("!a/(*)", "a"));
1753    // assert!(glob_match("!a/(*)", "a.b"));
1754    // assert!(glob_match("!a/(*)", "b/a"));
1755    // assert!(glob_match("!a/(*)", "b/b"));
1756    // assert!(glob_match("!a/(*)", "b/c"));
1757    // assert!(glob_match("!a/(b)", "a"));
1758    // assert!(glob_match("!a/(b)", "a.b"));
1759    // assert!(glob_match("!a/(b)", "a/a"));
1760    // assert!(glob_match("!a/(b)", "a/c"));
1761    // assert!(glob_match("!a/(b)", "b/a"));
1762    // assert!(glob_match("!a/(b)", "b/b"));
1763    // assert!(glob_match("!a/(b)", "b/c"));
1764    assert!(glob_match("!a/*", "a"));
1765    assert!(glob_match("!a/*", "a.b"));
1766    assert!(glob_match("!a/*", "b/a"));
1767    assert!(glob_match("!a/*", "b/b"));
1768    assert!(glob_match("!a/*", "b/c"));
1769    assert!(glob_match("!f*b", "bar"));
1770    assert!(glob_match("!f*b", "foo"));
1771
1772    assert!(!glob_match("!.md", ".md"));
1773    assert!(glob_match("!**/*.md", "a.js"));
1774    // assert!(!glob_match("!**/*.md", "b.md"));
1775    assert!(glob_match("!**/*.md", "c.txt"));
1776    assert!(glob_match("!*.md", "a.js"));
1777    assert!(!glob_match("!*.md", "b.md"));
1778    assert!(glob_match("!*.md", "c.txt"));
1779    assert!(!glob_match("!*.md", "abc.md"));
1780    assert!(glob_match("!*.md", "abc.txt"));
1781    assert!(!glob_match("!*.md", "foo.md"));
1782    assert!(glob_match("!.md", "foo.md"));
1783
1784    assert!(glob_match("!*.md", "a.js"));
1785    assert!(glob_match("!*.md", "b.txt"));
1786    assert!(!glob_match("!*.md", "c.md"));
1787    assert!(!glob_match("!a/*/a.js", "a/a/a.js"));
1788    assert!(!glob_match("!a/*/a.js", "a/b/a.js"));
1789    assert!(!glob_match("!a/*/a.js", "a/c/a.js"));
1790    assert!(!glob_match("!a/*/*/a.js", "a/a/a/a.js"));
1791    assert!(glob_match("!a/*/*/a.js", "b/a/b/a.js"));
1792    assert!(glob_match("!a/*/*/a.js", "c/a/c/a.js"));
1793    assert!(!glob_match("!a/a*.txt", "a/a.txt"));
1794    assert!(glob_match("!a/a*.txt", "a/b.txt"));
1795    assert!(glob_match("!a/a*.txt", "a/c.txt"));
1796    assert!(!glob_match("!a.a*.txt", "a.a.txt"));
1797    assert!(glob_match("!a.a*.txt", "a.b.txt"));
1798    assert!(glob_match("!a.a*.txt", "a.c.txt"));
1799    assert!(!glob_match("!a/*.txt", "a/a.txt"));
1800    assert!(!glob_match("!a/*.txt", "a/b.txt"));
1801    assert!(!glob_match("!a/*.txt", "a/c.txt"));
1802
1803    assert!(glob_match("!*.md", "a.js"));
1804    assert!(glob_match("!*.md", "b.txt"));
1805    assert!(!glob_match("!*.md", "c.md"));
1806    // assert!(!glob_match("!**/a.js", "a/a/a.js"));
1807    // assert!(!glob_match("!**/a.js", "a/b/a.js"));
1808    // assert!(!glob_match("!**/a.js", "a/c/a.js"));
1809    assert!(glob_match("!**/a.js", "a/a/b.js"));
1810    assert!(!glob_match("!a/**/a.js", "a/a/a/a.js"));
1811    assert!(glob_match("!a/**/a.js", "b/a/b/a.js"));
1812    assert!(glob_match("!a/**/a.js", "c/a/c/a.js"));
1813    assert!(glob_match("!**/*.md", "a/b.js"));
1814    assert!(glob_match("!**/*.md", "a.js"));
1815    assert!(!glob_match("!**/*.md", "a/b.md"));
1816    // assert!(!glob_match("!**/*.md", "a.md"));
1817    assert!(!glob_match("**/*.md", "a/b.js"));
1818    assert!(!glob_match("**/*.md", "a.js"));
1819    assert!(glob_match("**/*.md", "a/b.md"));
1820    assert!(glob_match("**/*.md", "a.md"));
1821    assert!(glob_match("!**/*.md", "a/b.js"));
1822    assert!(glob_match("!**/*.md", "a.js"));
1823    assert!(!glob_match("!**/*.md", "a/b.md"));
1824    // assert!(!glob_match("!**/*.md", "a.md"));
1825    assert!(glob_match("!*.md", "a/b.js"));
1826    assert!(glob_match("!*.md", "a.js"));
1827    assert!(glob_match("!*.md", "a/b.md"));
1828    assert!(!glob_match("!*.md", "a.md"));
1829    assert!(glob_match("!**/*.md", "a.js"));
1830    // assert!(!glob_match("!**/*.md", "b.md"));
1831    assert!(glob_match("!**/*.md", "c.txt"));
1832  }
1833
1834  #[test]
1835  fn question_mark() {
1836    assert!(glob_match("?", "a"));
1837    assert!(!glob_match("?", "aa"));
1838    assert!(!glob_match("?", "ab"));
1839    assert!(!glob_match("?", "aaa"));
1840    assert!(!glob_match("?", "abcdefg"));
1841
1842    assert!(!glob_match("??", "a"));
1843    assert!(glob_match("??", "aa"));
1844    assert!(glob_match("??", "ab"));
1845    assert!(!glob_match("??", "aaa"));
1846    assert!(!glob_match("??", "abcdefg"));
1847
1848    assert!(!glob_match("???", "a"));
1849    assert!(!glob_match("???", "aa"));
1850    assert!(!glob_match("???", "ab"));
1851    assert!(glob_match("???", "aaa"));
1852    assert!(!glob_match("???", "abcdefg"));
1853
1854    assert!(!glob_match("a?c", "aaa"));
1855    assert!(glob_match("a?c", "aac"));
1856    assert!(glob_match("a?c", "abc"));
1857    assert!(!glob_match("ab?", "a"));
1858    assert!(!glob_match("ab?", "aa"));
1859    assert!(!glob_match("ab?", "ab"));
1860    assert!(!glob_match("ab?", "ac"));
1861    assert!(!glob_match("ab?", "abcd"));
1862    assert!(!glob_match("ab?", "abbb"));
1863    assert!(glob_match("a?b", "acb"));
1864
1865    assert!(!glob_match("a/?/c/?/e.md", "a/bb/c/dd/e.md"));
1866    assert!(glob_match("a/??/c/??/e.md", "a/bb/c/dd/e.md"));
1867    assert!(!glob_match("a/??/c.md", "a/bbb/c.md"));
1868    assert!(glob_match("a/?/c.md", "a/b/c.md"));
1869    assert!(glob_match("a/?/c/?/e.md", "a/b/c/d/e.md"));
1870    assert!(!glob_match("a/?/c/???/e.md", "a/b/c/d/e.md"));
1871    assert!(glob_match("a/?/c/???/e.md", "a/b/c/zzz/e.md"));
1872    assert!(!glob_match("a/?/c.md", "a/bb/c.md"));
1873    assert!(glob_match("a/??/c.md", "a/bb/c.md"));
1874    assert!(glob_match("a/???/c.md", "a/bbb/c.md"));
1875    assert!(glob_match("a/????/c.md", "a/bbbb/c.md"));
1876  }
1877
1878  #[test]
1879  fn braces() {
1880    assert!(glob_match("{a,b,c}", "a"));
1881    assert!(glob_match("{a,b,c}", "b"));
1882    assert!(glob_match("{a,b,c}", "c"));
1883    assert!(!glob_match("{a,b,c}", "aa"));
1884    assert!(!glob_match("{a,b,c}", "bb"));
1885    assert!(!glob_match("{a,b,c}", "cc"));
1886
1887    assert!(glob_match("a/{a,b}", "a/a"));
1888    assert!(glob_match("a/{a,b}", "a/b"));
1889    assert!(!glob_match("a/{a,b}", "a/c"));
1890    assert!(!glob_match("a/{a,b}", "b/b"));
1891    assert!(!glob_match("a/{a,b,c}", "b/b"));
1892    assert!(glob_match("a/{a,b,c}", "a/c"));
1893    assert!(glob_match("a{b,bc}.txt", "abc.txt"));
1894
1895    assert!(glob_match("foo[{a,b}]baz", "foo{baz"));
1896
1897    assert!(!glob_match("a{,b}.txt", "abc.txt"));
1898    assert!(!glob_match("a{a,b,}.txt", "abc.txt"));
1899    assert!(!glob_match("a{b,}.txt", "abc.txt"));
1900    assert!(glob_match("a{,b}.txt", "a.txt"));
1901    assert!(glob_match("a{b,}.txt", "a.txt"));
1902    assert!(glob_match("a{a,b,}.txt", "aa.txt"));
1903    assert!(glob_match("a{a,b,}.txt", "aa.txt"));
1904    assert!(glob_match("a{,b}.txt", "ab.txt"));
1905    assert!(glob_match("a{b,}.txt", "ab.txt"));
1906
1907    // assert!(glob_match("{a/,}a/**", "a"));
1908    assert!(glob_match("a{a,b/}*.txt", "aa.txt"));
1909    assert!(glob_match("a{a,b/}*.txt", "ab/.txt"));
1910    assert!(glob_match("a{a,b/}*.txt", "ab/a.txt"));
1911    // assert!(glob_match("{a/,}a/**", "a/"));
1912    assert!(glob_match("{a/,}a/**", "a/a/"));
1913    // assert!(glob_match("{a/,}a/**", "a/a"));
1914    assert!(glob_match("{a/,}a/**", "a/a/a"));
1915    assert!(glob_match("{a/,}a/**", "a/a/"));
1916    assert!(glob_match("{a/,}a/**", "a/a/a/"));
1917    assert!(glob_match("{a/,}b/**", "a/b/a/"));
1918    assert!(glob_match("{a/,}b/**", "b/a/"));
1919    assert!(glob_match("a{,/}*.txt", "a.txt"));
1920    assert!(glob_match("a{,/}*.txt", "ab.txt"));
1921    assert!(glob_match("a{,/}*.txt", "a/b.txt"));
1922    assert!(glob_match("a{,/}*.txt", "a/ab.txt"));
1923
1924    assert!(glob_match("a{,.*{foo,db},\\(bar\\)}.txt", "a.txt"));
1925    assert!(!glob_match("a{,.*{foo,db},\\(bar\\)}.txt", "adb.txt"));
1926    assert!(glob_match("a{,.*{foo,db},\\(bar\\)}.txt", "a.db.txt"));
1927
1928    assert!(glob_match("a{,*.{foo,db},\\(bar\\)}.txt", "a.txt"));
1929    assert!(!glob_match("a{,*.{foo,db},\\(bar\\)}.txt", "adb.txt"));
1930    assert!(glob_match("a{,*.{foo,db},\\(bar\\)}.txt", "a.db.txt"));
1931
1932    // assert!(glob_match("a{,.*{foo,db},\\(bar\\)}", "a"));
1933    assert!(!glob_match("a{,.*{foo,db},\\(bar\\)}", "adb"));
1934    assert!(glob_match("a{,.*{foo,db},\\(bar\\)}", "a.db"));
1935
1936    // assert!(glob_match("a{,*.{foo,db},\\(bar\\)}", "a"));
1937    assert!(!glob_match("a{,*.{foo,db},\\(bar\\)}", "adb"));
1938    assert!(glob_match("a{,*.{foo,db},\\(bar\\)}", "a.db"));
1939
1940    assert!(!glob_match("{,.*{foo,db},\\(bar\\)}", "a"));
1941    assert!(!glob_match("{,.*{foo,db},\\(bar\\)}", "adb"));
1942    assert!(!glob_match("{,.*{foo,db},\\(bar\\)}", "a.db"));
1943    assert!(glob_match("{,.*{foo,db},\\(bar\\)}", ".db"));
1944
1945    assert!(!glob_match("{,*.{foo,db},\\(bar\\)}", "a"));
1946    assert!(glob_match("{*,*.{foo,db},\\(bar\\)}", "a"));
1947    assert!(!glob_match("{,*.{foo,db},\\(bar\\)}", "adb"));
1948    assert!(glob_match("{,*.{foo,db},\\(bar\\)}", "a.db"));
1949
1950    assert!(!glob_match("a/b/**/c{d,e}/**/xyz.md", "a/b/c/xyz.md"));
1951    assert!(!glob_match("a/b/**/c{d,e}/**/xyz.md", "a/b/d/xyz.md"));
1952    assert!(glob_match("a/b/**/c{d,e}/**/xyz.md", "a/b/cd/xyz.md"));
1953    assert!(glob_match("a/b/**/{c,d,e}/**/xyz.md", "a/b/c/xyz.md"));
1954    assert!(glob_match("a/b/**/{c,d,e}/**/xyz.md", "a/b/d/xyz.md"));
1955    assert!(glob_match("a/b/**/{c,d,e}/**/xyz.md", "a/b/e/xyz.md"));
1956
1957    assert!(glob_match("*{a,b}*", "xax"));
1958    assert!(glob_match("*{a,b}*", "xxax"));
1959    assert!(glob_match("*{a,b}*", "xbx"));
1960
1961    assert!(glob_match("*{*a,b}", "xba"));
1962    assert!(glob_match("*{*a,b}", "xb"));
1963
1964    assert!(!glob_match("*??", "a"));
1965    assert!(!glob_match("*???", "aa"));
1966    assert!(glob_match("*???", "aaa"));
1967    assert!(!glob_match("*****??", "a"));
1968    assert!(!glob_match("*****???", "aa"));
1969    assert!(glob_match("*****???", "aaa"));
1970
1971    assert!(!glob_match("a*?c", "aaa"));
1972    assert!(glob_match("a*?c", "aac"));
1973    assert!(glob_match("a*?c", "abc"));
1974
1975    assert!(glob_match("a**?c", "abc"));
1976    assert!(!glob_match("a**?c", "abb"));
1977    assert!(glob_match("a**?c", "acc"));
1978    assert!(glob_match("a*****?c", "abc"));
1979
1980    assert!(glob_match("*****?", "a"));
1981    assert!(glob_match("*****?", "aa"));
1982    assert!(glob_match("*****?", "abc"));
1983    assert!(glob_match("*****?", "zzz"));
1984    assert!(glob_match("*****?", "bbb"));
1985    assert!(glob_match("*****?", "aaaa"));
1986
1987    assert!(!glob_match("*****??", "a"));
1988    assert!(glob_match("*****??", "aa"));
1989    assert!(glob_match("*****??", "abc"));
1990    assert!(glob_match("*****??", "zzz"));
1991    assert!(glob_match("*****??", "bbb"));
1992    assert!(glob_match("*****??", "aaaa"));
1993
1994    assert!(!glob_match("?*****??", "a"));
1995    assert!(!glob_match("?*****??", "aa"));
1996    assert!(glob_match("?*****??", "abc"));
1997    assert!(glob_match("?*****??", "zzz"));
1998    assert!(glob_match("?*****??", "bbb"));
1999    assert!(glob_match("?*****??", "aaaa"));
2000
2001    assert!(glob_match("?*****?c", "abc"));
2002    assert!(!glob_match("?*****?c", "abb"));
2003    assert!(!glob_match("?*****?c", "zzz"));
2004
2005    assert!(glob_match("?***?****c", "abc"));
2006    assert!(!glob_match("?***?****c", "bbb"));
2007    assert!(!glob_match("?***?****c", "zzz"));
2008
2009    assert!(glob_match("?***?****?", "abc"));
2010    assert!(glob_match("?***?****?", "bbb"));
2011    assert!(glob_match("?***?****?", "zzz"));
2012
2013    assert!(glob_match("?***?****", "abc"));
2014    assert!(glob_match("*******c", "abc"));
2015    assert!(glob_match("*******?", "abc"));
2016    assert!(glob_match("a*cd**?**??k", "abcdecdhjk"));
2017    assert!(glob_match("a**?**cd**?**??k", "abcdecdhjk"));
2018    assert!(glob_match("a**?**cd**?**??k***", "abcdecdhjk"));
2019    assert!(glob_match("a**?**cd**?**??***k", "abcdecdhjk"));
2020    assert!(glob_match("a**?**cd**?**??***k**", "abcdecdhjk"));
2021    assert!(glob_match("a****c**?**??*****", "abcdecdhjk"));
2022
2023    assert!(!glob_match("a/?/c/?/*/e.md", "a/b/c/d/e.md"));
2024    assert!(glob_match("a/?/c/?/*/e.md", "a/b/c/d/e/e.md"));
2025    assert!(glob_match("a/?/c/?/*/e.md", "a/b/c/d/efghijk/e.md"));
2026    assert!(glob_match("a/?/**/e.md", "a/b/c/d/efghijk/e.md"));
2027    assert!(!glob_match("a/?/e.md", "a/bb/e.md"));
2028    assert!(glob_match("a/??/e.md", "a/bb/e.md"));
2029    assert!(!glob_match("a/?/**/e.md", "a/bb/e.md"));
2030    assert!(glob_match("a/?/**/e.md", "a/b/ccc/e.md"));
2031    assert!(glob_match("a/*/?/**/e.md", "a/b/c/d/efghijk/e.md"));
2032    assert!(glob_match("a/*/?/**/e.md", "a/b/c/d/efgh.ijk/e.md"));
2033    assert!(glob_match("a/*/?/**/e.md", "a/b.bb/c/d/efgh.ijk/e.md"));
2034    assert!(glob_match("a/*/?/**/e.md", "a/bbb/c/d/efgh.ijk/e.md"));
2035
2036    assert!(glob_match("a/*/ab??.md", "a/bbb/abcd.md"));
2037    assert!(glob_match("a/bbb/ab??.md", "a/bbb/abcd.md"));
2038    assert!(glob_match("a/bbb/ab???md", "a/bbb/abcd.md"));
2039  }
2040
2041  #[test]
2042  fn captures() {
2043    fn test_captures<'a>(glob: &str, path: &'a str) -> Option<Vec<&'a str>> {
2044      glob_match_with_captures(glob, path)
2045        .map(|v| v.into_iter().map(|capture| &path[capture]).collect())
2046    }
2047
2048    assert_eq!(test_captures("a/b", "a/b"), Some(vec![]));
2049    assert_eq!(test_captures("a/*/c", "a/bx/c"), Some(vec!["bx"]));
2050    assert_eq!(test_captures("a/*/c", "a/test/c"), Some(vec!["test"]));
2051    assert_eq!(
2052      test_captures("a/*/c/*/e", "a/b/c/d/e"),
2053      Some(vec!["b", "d"])
2054    );
2055    assert_eq!(
2056      test_captures("a/*/c/*/e", "a/b/c/d/e"),
2057      Some(vec!["b", "d"])
2058    );
2059    assert_eq!(test_captures("a/{b,x}/c", "a/b/c"), Some(vec!["b"]));
2060    assert_eq!(test_captures("a/{b,x}/c", "a/x/c"), Some(vec!["x"]));
2061    assert_eq!(test_captures("a/?/c", "a/b/c"), Some(vec!["b"]));
2062    assert_eq!(test_captures("a/*?x/c", "a/yybx/c"), Some(vec!["yy", "b"]));
2063    assert_eq!(
2064      test_captures("a/*[a-z]x/c", "a/yybx/c"),
2065      Some(vec!["yy", "b"])
2066    );
2067    assert_eq!(
2068      test_captures("a/{b*c,c}y", "a/bdcy"),
2069      Some(vec!["bdc", "d"])
2070    );
2071    assert_eq!(test_captures("a/{b*,c}y", "a/bdy"), Some(vec!["bd", "d"]));
2072    assert_eq!(test_captures("a/{b*c,c}", "a/bdc"), Some(vec!["bdc", "d"]));
2073    assert_eq!(test_captures("a/{b*,c}", "a/bd"), Some(vec!["bd", "d"]));
2074    assert_eq!(test_captures("a/{b*,c}", "a/c"), Some(vec!["c", ""]));
2075    assert_eq!(
2076      test_captures("a/{b{c,d},c}y", "a/bdy"),
2077      Some(vec!["bd", "d"])
2078    );
2079    assert_eq!(
2080      test_captures("a/{b*,c*}y", "a/bdy"),
2081      Some(vec!["bd", "d", ""])
2082    );
2083    assert_eq!(
2084      test_captures("a/{b*,c*}y", "a/cdy"),
2085      Some(vec!["cd", "", "d"])
2086    );
2087    assert_eq!(test_captures("a/{b,c}", "a/b"), Some(vec!["b"]));
2088    assert_eq!(test_captures("a/{b,c}", "a/c"), Some(vec!["c"]));
2089    assert_eq!(test_captures("a/{b,c[}]*}", "a/b"), Some(vec!["b", "", ""]));
2090    assert_eq!(
2091      test_captures("a/{b,c[}]*}", "a/c}xx"),
2092      Some(vec!["c}xx", "}", "xx"])
2093    );
2094
2095    // assert\.deepEqual\(([!])?capture\('(.*?)', ['"](.*?)['"]\), (.*)?\);
2096    // assert_eq!(test_captures("$2", "$3"), Some(vec!$4));
2097
2098    assert_eq!(test_captures("test/*", "test/foo"), Some(vec!["foo"]));
2099    assert_eq!(
2100      test_captures("test/*/bar", "test/foo/bar"),
2101      Some(vec!["foo"])
2102    );
2103    assert_eq!(
2104      test_captures("test/*/bar/*", "test/foo/bar/baz"),
2105      Some(vec!["foo", "baz"])
2106    );
2107    assert_eq!(test_captures("test/*.js", "test/foo.js"), Some(vec!["foo"]));
2108    assert_eq!(
2109      test_captures("test/*-controller.js", "test/foo-controller.js"),
2110      Some(vec!["foo"])
2111    );
2112
2113    assert_eq!(
2114      test_captures("test/**/*.js", "test/a.js"),
2115      Some(vec!["", "a"])
2116    );
2117    assert_eq!(
2118      test_captures("test/**/*.js", "test/dir/a.js"),
2119      Some(vec!["dir", "a"])
2120    );
2121    assert_eq!(
2122      test_captures("test/**/*.js", "test/dir/test/a.js"),
2123      Some(vec!["dir/test", "a"])
2124    );
2125    assert_eq!(
2126      test_captures("**/*.js", "test/dir/a.js"),
2127      Some(vec!["test/dir", "a"])
2128    );
2129    assert_eq!(
2130      test_captures("**/**/**/**/a", "foo/bar/baz/a"),
2131      Some(vec!["foo/bar/baz"])
2132    );
2133    assert_eq!(
2134      test_captures("a/{b/**/y,c/**/d}", "a/b/y"),
2135      Some(vec!["b/y", "", ""])
2136    );
2137    assert_eq!(
2138      test_captures("a/{b/**/y,c/**/d}", "a/b/x/x/y"),
2139      Some(vec!["b/x/x/y", "x/x", ""])
2140    );
2141    assert_eq!(
2142      test_captures("a/{b/**/y,c/**/d}", "a/c/x/x/d"),
2143      Some(vec!["c/x/x/d", "", "x/x"])
2144    );
2145    assert_eq!(
2146      test_captures("a/{b/**/**/y,c/**/**/d}", "a/b/x/x/x/x/x/y"),
2147      Some(vec!["b/x/x/x/x/x/y", "x/x/x/x/x", ""])
2148    );
2149    assert_eq!(
2150      test_captures("a/{b/**/**/y,c/**/**/d}", "a/c/x/x/x/x/x/d"),
2151      Some(vec!["c/x/x/x/x/x/d", "", "x/x/x/x/x"])
2152    );
2153    assert_eq!(
2154      test_captures(
2155        "some/**/{a,b,c}/**/needle.txt",
2156        "some/path/a/to/the/needle.txt"
2157      ),
2158      Some(vec!["path", "a", "to/the"])
2159    );
2160  }
2161
2162  #[test]
2163  fn fuzz_tests() {
2164    // https://github.com/devongovett/glob-match/issues/1
2165    let s = "{*{??*{??**,Uz*zz}w**{*{**a,z***b*[!}w??*azzzzzzzz*!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!z[za,z&zz}w**z*z*}";
2166    assert!(!glob_match(s, s));
2167    let s = "**** *{*{??*{??***\u{5} *{*{??*{??***\u{5},\0U\0}]*****\u{1},\0***\0,\0\0}w****,\0U\0}]*****\u{1},\0***\0,\0\0}w*****\u{1}***{}*.*\0\0*\0";
2168    assert!(!glob_match(s, s));
2169  }
2170}