pathpatterns 0.1.2

Include/exclude path list implementation with git-like globbing support.
Documentation
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! This implements the pattern matching algorithm found in git's `wildmatch.c`

use std::fmt;
use std::mem;
use std::ops::RangeInclusive;

use bitflags::bitflags;

/// A character class can be a list of characters `[abc]`, ranges of characters `[a-z]`, or named
/// classes `[[:digit:]]`, or a combination of them all. Additionally they can be negated with a
/// `^` at the beginning.
#[derive(Clone, Debug, Default)]
struct CharacterClass {
    negated: bool,
    named: Vec<fn(u8) -> bool>,
    listed: Vec<u8>,
    ranges: Vec<RangeInclusive<u8>>,
}

impl CharacterClass {
    /// Check if a byte match this character class.
    pub fn matches(&self, ch: u8) -> bool {
        self.matches_do(ch) != self.negated
    }

    fn matches_do(&self, ch: u8) -> bool {
        self.listed.contains(&ch)
            || self.ranges.iter().any(|range| range.contains(&ch))
            || self.named.iter().any(|func| func(ch))
    }
}

/// One component of a pattern.
#[derive(Clone, Debug)]
enum Component {
    /// A literal match. The `/a` and `.txt` in `/a*.txt`.
    Literal(Vec<u8>),

    /// A question mark should match exactly one byte. If desired it may also match slashes, but
    /// this property is part of the whole pattern, not the component.
    QuestionMark,

    /// A "star" normally matches everything except when matching path names, where it does not
    /// match slashes.
    Star,

    /// A double star always matches everything, even slashes.
    StarStar,

    // SlashStarStarSlash, // maybe?
    /// A character class matches one byte out of a set of allowed or disallowed bytes.
    Class(CharacterClass),
}

impl Component {
    /// Check if this is a literal component ending with a slash.
    fn ends_with_slash(&self) -> bool {
        match self {
            Component::Literal(lit) => lit.last().copied() == Some(b'/'),
            _ => false,
        }
    }

    /// Check if this is a literal component starting with a slash.
    fn starts_with_slash(&self) -> bool {
        match self {
            Component::Literal(lit) => {
                lit.first().copied() == Some(b'/')
                    || (lit.first().copied() == Some(b'\\') && lit.get(1).copied() == Some(b'/'))
            }
            _ => false,
        }
    }
}

bitflags! {
    /// Flags affecting how a pattern should match.
    pub struct PatternFlag: u8 {
        /// Ignore upper/lower case on the pattern. Note that this only affects ascii characters.
        /// We do not normalize/casefold unicode here. If you need this, case-fold your input
        /// strings and patterns first.
        const IGNORE_CASE = 0x01;

        /// This pattern is used for paths, meaning that `*` and `?` do not match slashes. Only
        /// explicit slashes and `**` can match slashes.
        const PATH_NAME = 0x02;
    }
}

/// Error cases which may happen while parsing a pattern.
#[derive(Clone, Debug)]
pub enum ParseError {
    EmptyPattern,
    NulByteError,
    TrailingBackslash,
    UnclosedCharacterClass(usize),
    MalformedNamedCharacterClass(usize),
}

impl std::error::Error for ParseError {}

impl fmt::Display for ParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            ParseError::EmptyPattern => write!(f, "empty pattern"),
            ParseError::NulByteError => write!(f, "null-byte in pattern"),
            ParseError::TrailingBackslash => write!(f, "trailing backslash in pattern"),
            ParseError::UnclosedCharacterClass(begin) => write!(
                f,
                "unclosed character class in pattern, starting at byte {}",
                begin
            ),
            ParseError::MalformedNamedCharacterClass(begin) => write!(
                f,
                "malformed named character class in pattern, starting at byte {}",
                begin
            ),
        }
    }
}

/// See the match function for the algorithm.
enum MatchResult {
    Match,
    NoMatch,
    AbortAll,
    AbortToStarStar,
}

/// An `fnmatch`-like pattern working like in `git`, with support for `*` vs `**` distinction for
/// paths.
///
/// Note that patterns are treated as ASCII strings, so unicode characters have no special effect.
/// You can use it for UTF-8 paths, but there's no direct support for `PatternFlag::IGNORE_CASE` on
/// unicode text.
#[derive(Clone, Debug)]
pub struct Pattern {
    /// Original pattern the user provided.
    pattern: std::ffi::CString,

    /// Matching components we parsed out of the string.
    components: Vec<Component>,

    /// Flags used for the pattern.
    ///
    /// In the future we may want to optimize the `components` based on this, so `IGNORE_CASE`
    /// happens at "compile time" already.
    flags: PatternFlag,
}

impl Pattern {
    /// Get the original input pattern.
    pub fn pattern(&self) -> &std::ffi::CStr {
        &self.pattern
    }

    /// Create a new pattern.
    pub fn new<T: AsRef<[u8]>>(pattern: T, flags: PatternFlag) -> Result<Self, ParseError> {
        Self::new_do(pattern.as_ref(), flags)
    }

    /// Convenience shortcut to create a new pattern with `PatternFlag::PATH_NAME`.
    pub fn path<T: AsRef<[u8]>>(pattern: T) -> Result<Self, ParseError> {
        Self::new_do(pattern.as_ref(), PatternFlag::PATH_NAME)
    }

    fn new_do(pattern: &[u8], flags: PatternFlag) -> Result<Self, ParseError> {
        if pattern.is_empty() {
            return Err(ParseError::EmptyPattern);
        }

        // strip trailing slashes:
        let pattern = match pattern.iter().rposition(|&b| b != b'/') {
            Some(pos) => &pattern[..=pos],
            None => b"/",
        };

        let c_pattern = std::ffi::CString::new(pattern).map_err(|_| ParseError::NulByteError)?;

        let mut components = Vec::<Component>::new();
        let mut literal = Vec::<u8>::new();

        fn push_literal(
            literal: &mut Vec<u8>,
            components: &mut Vec<Component>,
            flags: PatternFlag,
        ) {
            if !literal.is_empty() {
                if flags.intersects(PatternFlag::IGNORE_CASE) {
                    for b in &mut literal[..] {
                        *b = b.to_ascii_lowercase();
                    }
                }
                components.push(Component::Literal(mem::take(literal)));
            }
        }

        let mut i = 0;
        while i != pattern.len() {
            match pattern[i] {
                0 => return Err(ParseError::NulByteError),
                b'\\' => {
                    i += 1;
                    let mut ch = *pattern.get(i).ok_or(ParseError::TrailingBackslash)?;
                    if flags.intersects(PatternFlag::IGNORE_CASE) {
                        ch = ch.to_ascii_lowercase()
                    }
                    literal.push(ch);
                }
                b'?' => {
                    push_literal(&mut literal, &mut components, flags);
                    components.push(Component::QuestionMark);
                }
                b'*' => {
                    push_literal(&mut literal, &mut components, flags);
                    if pattern.get(i + 1).copied() == Some(b'*') {
                        let beg = i;
                        i += 1;
                        // swallow following stars as well:
                        while pattern.get(i + 1).copied() == Some(b'*') {
                            i += 1;
                        }

                        // git doesn't allow `**` attached to anything other than slashes to match
                        // subdirectories, so only `**`, `.../**`, `.../**/...` and `**/...` are
                        // valid.
                        if (beg == 0 || pattern[beg - 1] == b'/')
                            && ((i + 1) == pattern.len() || pattern[i + 1] == b'/')
                        {
                            components.push(Component::StarStar)
                        } else {
                            components.push(Component::Star)
                        }
                    } else {
                        components.push(Component::Star)
                    }
                }
                b'[' => {
                    push_literal(&mut literal, &mut components, flags);
                    let (component, new_i) = Self::parse_char_class(pattern, i, flags)?;
                    i = new_i;
                    components.push(component);
                }
                ch => literal.push(if flags.intersects(PatternFlag::IGNORE_CASE) {
                    ch.to_ascii_lowercase()
                } else {
                    ch
                }),
            }

            i += 1;
        }

        push_literal(&mut literal, &mut components, flags);
        Ok(Self {
            pattern: c_pattern,
            components,
            flags,
        })
    }

    fn parse_char_class(
        pattern: &[u8],
        begin_i: usize,
        flags: PatternFlag,
    ) -> Result<(Component, usize), ParseError> {
        let mut i = begin_i + 1;

        let negated = if pattern.get(i).copied() == Some(b'^') {
            i += 1;
            true
        } else {
            false
        };

        if i == pattern.len() {
            return Err(ParseError::UnclosedCharacterClass(begin_i));
        }

        let mut class = CharacterClass {
            negated,
            ..Default::default()
        };
        let mut prev = None;
        while i != pattern.len() {
            let mut new_prev = None;
            match pattern[i] {
                0 => return Err(ParseError::NulByteError),
                b'[' if pattern[(i + 1)..].starts_with(b":alnum:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_alphanumeric());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":alpha:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_alphabetic());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":blank:]") => {
                    i += 8;
                    class.named.push(|b| b == b' ' || b == b'\t');
                }
                b'[' if pattern[(i + 1)..].starts_with(b":cntrl:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_control());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":digit:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_digit());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":graph:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_graphic());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":lower:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_lowercase());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":print:]") => {
                    i += 8;
                    class.named.push(|b| b >= 0x20 && b <= 0x7f);
                }
                b'[' if pattern[(i + 1)..].starts_with(b":punct:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_punctuation());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":space:]") => {
                    i += 8;
                    class.named.push(|b| b.is_ascii_whitespace());
                }
                b'[' if pattern[(i + 1)..].starts_with(b":upper:]") => {
                    i += 8;
                    if flags.intersects(PatternFlag::IGNORE_CASE) {
                        class.named.push(|b| b.is_ascii_lowercase());
                    } else {
                        class.named.push(|b| b.is_ascii_uppercase());
                    }
                }
                b'[' if pattern[(i + 1)..].starts_with(b":xdigit:]") => {
                    i += 9;
                    class.named.push(|b| b.is_ascii_hexdigit());
                }
                b'[' if pattern.get(i + 1).copied() == Some(b':') => {
                    return Err(ParseError::MalformedNamedCharacterClass(begin_i));
                }
                b']' => break,
                b'\\' => {
                    i += 1;
                    let ch = *pattern.get(i).ok_or(ParseError::TrailingBackslash)?;
                    class.listed.push(ch);
                    new_prev = Some(ch);
                }
                b'-' => match prev {
                    None => {
                        new_prev = Some(b'-');
                        class.listed.push(b'-');
                    }
                    Some(beg) => {
                        // The previous character was also pushed to `class.listed`, so remove it:
                        class.listed.pop();

                        i += 1;
                        let mut end = *pattern
                            .get(i)
                            .ok_or(ParseError::UnclosedCharacterClass(begin_i))?;
                        if end == b'\\' {
                            i += 1;
                            end = *pattern
                                .get(i)
                                .ok_or(ParseError::UnclosedCharacterClass(begin_i))?;
                        }

                        if flags.intersects(PatternFlag::IGNORE_CASE) {
                            end = end.to_ascii_lowercase();
                        }

                        if beg <= end {
                            class.ranges.push(beg..=end);
                        } else {
                            class.ranges.push(end..=beg);
                        }
                    }
                },
                mut ch => {
                    if flags.intersects(PatternFlag::IGNORE_CASE) {
                        ch = ch.to_ascii_lowercase();
                    }
                    new_prev = Some(ch);
                    class.listed.push(ch);
                }
            }
            prev = new_prev;
            i += 1;
        }

        Ok((Component::Class(class), i))
    }

    /// Check whether this pattern matches a text.
    pub fn matches<T: AsRef<[u8]>>(&self, text: T) -> bool {
        match self.do_matches(0, text.as_ref(), false) {
            MatchResult::Match => true,
            _ => false,
        }
    }

    // The algorithm is ported from git's wildmatch.c.
    fn do_matches(
        &self,
        mut ci: usize,
        mut text: &[u8],
        mut skip_slash_in_literal: bool,
    ) -> MatchResult {
        let components = &self.components[..];

        if self.flags.intersects(PatternFlag::PATH_NAME) {
            // If we match a path then we want the pattern `"foo"` to match the path `"/foo"`.
        }

        while ci != components.len() {
            let skip_slash_in_literal = mem::replace(&mut skip_slash_in_literal, false);
            //eprintln!("Matching: {:?} at text: {:?}", components[ci], unsafe {
            //    std::str::from_utf8_unchecked(text)
            //},);
            match &components[ci] {
                Component::Literal(literal) => {
                    if text.is_empty() {
                        // The '*' implementation is NON-greedy, so if the text is empty, we
                        // already tried all shorter possible matches, so anything other than a '*'
                        // match can `AbortAll` if the text is empty.
                        return MatchResult::AbortAll;
                    }

                    let literal = if skip_slash_in_literal {
                        &literal[1..]
                    } else {
                        literal
                    };

                    if !starts_with(text, &literal, self.flags) {
                        return MatchResult::NoMatch;
                    }

                    text = &text[literal.len()..];
                }
                Component::QuestionMark => {
                    if text.is_empty() {
                        // See Literal case
                        return MatchResult::AbortAll;
                    }

                    if text[0] == b'/' && self.flags.intersects(PatternFlag::PATH_NAME) {
                        return MatchResult::NoMatch;
                    }

                    text = &text[1..];
                }
                Component::Class(class) => {
                    if text.is_empty() {
                        // See Literal case
                        return MatchResult::AbortAll;
                    }

                    let mut ch = text[0];
                    if self.flags.intersects(PatternFlag::IGNORE_CASE) {
                        ch = ch.to_ascii_lowercase();
                    }
                    if !class.matches(ch) {
                        return MatchResult::NoMatch;
                    }

                    text = &text[1..];
                }
                Component::Star if self.flags.intersects(PatternFlag::PATH_NAME) => {
                    // FIXME: Optimization: Instead of .contains, fast-skip to its index, like git
                    // does.
                    if (ci + 1) == components.len() && !text.contains(&b'/') {
                        return MatchResult::Match;
                    }

                    loop {
                        if text.is_empty() {
                            // We still abort all here, but git has some optimizations we could
                            // do instead before reaching this.
                            return MatchResult::AbortAll;
                        }

                        // FIXME: Optimization: Add the "try to advance faster" optimization from
                        // git here.

                        match self.do_matches(ci + 1, text, false) {
                            MatchResult::NoMatch => {
                                if text[0] == b'/' {
                                    return MatchResult::AbortToStarStar;
                                }
                            }
                            other => return other,
                        }

                        text = &text[1..];
                    }
                }
                Component::Star | Component::StarStar => {
                    if (ci + 1) == components.len() {
                        return MatchResult::Match;
                    }

                    if let Component::StarStar = components[ci] {
                        if ((ci > 0 && components[ci - 1].ends_with_slash()) || ci == 0)
                            && ((ci + 1) == components.len()
                                || components[ci + 1].starts_with_slash())
                        {
                            // Assuming we matched `foo/` and are at `/` `**` `/`, see if we can let
                            // it match nothing, so that `foo/` `**` `/bar` can match `foo/bar`.
                            #[allow(clippy::single_match)]
                            match self.do_matches(ci + 1, text, true) {
                                MatchResult::Match => return MatchResult::Match,
                                _ => (), // or just continue regularly
                            }
                        }
                    }

                    loop {
                        if text.is_empty() {
                            // See Literal case
                            return MatchResult::AbortAll;
                        }

                        match self.do_matches(ci + 1, text, false) {
                            MatchResult::NoMatch => (),
                            MatchResult::AbortToStarStar => (), // continue from here
                            other => return other,
                        }

                        text = &text[1..];
                    }
                }
            }
            ci += 1;
        }

        if text.is_empty() {
            MatchResult::Match
        } else {
            MatchResult::NoMatch
        }
    }
}

fn starts_with(text: &[u8], with: &[u8], flags: PatternFlag) -> bool {
    if flags.intersects(PatternFlag::IGNORE_CASE) {
        starts_with_caseless(text, with)
    } else {
        text.starts_with(with)
    }
}

fn starts_with_caseless(text: &[u8], with: &[u8]) -> bool {
    if text.len() < with.len() {
        return false;
    }

    for i in 0..with.len() {
        if text[i].to_ascii_lowercase() != with[i].to_ascii_lowercase() {
            return false;
        }
    }

    true
}

#[test]
fn test() {
    let pattern = Pattern::new("/hey/*/you", PatternFlag::PATH_NAME).unwrap();
    assert!(pattern.matches("/hey/asdf/you"));
    assert!(!pattern.matches("/hey/asdf/more/you"));
    assert!(!pattern.matches("/heyasdf/you"));
    assert!(!pattern.matches("/heyasdfyou"));
    assert!(!pattern.matches("/hey/asdfyou"));
    assert!(!pattern.matches("/hey/you"));
    assert!(pattern.matches("/hey//you"));

    let pattern = Pattern::new("/hey/*/you", PatternFlag::empty()).unwrap();
    assert!(pattern.matches("/hey/asdf/you"));
    assert!(pattern.matches("/hey/asdf/more/you")); // different to PATH_NAME
    assert!(!pattern.matches("/heyasdf/you"));
    assert!(!pattern.matches("/heyasdfyou"));
    assert!(!pattern.matches("/hey/asdfyou"));
    assert!(!pattern.matches("/hey/you"));
    assert!(pattern.matches("/hey//you"));

    let pattern = Pattern::new("/hey/**/you", PatternFlag::PATH_NAME).unwrap();
    assert!(pattern.matches("/hey/asdf/you"));
    assert!(pattern.matches("/hey/asdf/more/you"));
    assert!(!pattern.matches("/heyasdf/you"));
    assert!(!pattern.matches("/heyasdfyou"));
    assert!(!pattern.matches("/hey/asdfyou"));
    assert!(pattern.matches("/hey/you"));
    assert!(pattern.matches("/hey//you"));

    let pattern = Pattern::new("/he[yx]/**/you", PatternFlag::PATH_NAME).unwrap();
    assert!(pattern.matches("/hey/asdf/you"));
    assert!(pattern.matches("/hey/asdf/more/you"));
    assert!(!pattern.matches("/heyasdf/you"));
    assert!(!pattern.matches("/heyasdfyou"));
    assert!(!pattern.matches("/hey/asdfyou"));
    assert!(pattern.matches("/hey/you"));
    assert!(pattern.matches("/hey//you"));

    assert!(pattern.matches("/hex/asdf/you"));
    assert!(pattern.matches("/hex/asdf/more/you"));
    assert!(!pattern.matches("/hexasdf/you"));
    assert!(!pattern.matches("/hexasdfyou"));
    assert!(!pattern.matches("/hex/asdfyou"));
    assert!(pattern.matches("/hex/you"));
    assert!(pattern.matches("/hex//you"));

    assert!(!pattern.matches("/hez/asdf/you"));
    assert!(!pattern.matches("/hez/asdf/more/you"));
    assert!(!pattern.matches("/hezasdf/you"));
    assert!(!pattern.matches("/hezasdfyou"));
    assert!(!pattern.matches("/hez/asdfyou"));
    assert!(!pattern.matches("/hez/you"));
    assert!(!pattern.matches("/hez//you"));

    let pattern = Pattern::new("/he[^yx]/**/you", PatternFlag::PATH_NAME).unwrap();
    assert!(!pattern.matches("/hey/asdf/you"));
    assert!(!pattern.matches("/hey/asdf/more/you"));
    assert!(!pattern.matches("/heyasdf/you"));
    assert!(!pattern.matches("/heyasdfyou"));
    assert!(!pattern.matches("/hey/asdfyou"));
    assert!(!pattern.matches("/hey/you"));
    assert!(!pattern.matches("/hey//you"));

    assert!(!pattern.matches("/hex/asdf/you"));
    assert!(!pattern.matches("/hex/asdf/more/you"));
    assert!(!pattern.matches("/hexasdf/you"));
    assert!(!pattern.matches("/hexasdfyou"));
    assert!(!pattern.matches("/hex/asdfyou"));
    assert!(!pattern.matches("/hex/you"));
    assert!(!pattern.matches("/hex//you"));

    assert!(pattern.matches("/hez/asdf/you"));
    assert!(pattern.matches("/hez/asdf/more/you"));
    assert!(!pattern.matches("/hezasdf/you"));
    assert!(!pattern.matches("/hezasdfyou"));
    assert!(!pattern.matches("/hez/asdfyou"));
    assert!(pattern.matches("/hez/you"));
    assert!(pattern.matches("/hez//you"));

    let wrong = b"/hez/";
    for i in 0..wrong.len() {
        assert!(!pattern.matches(&wrong[..i]));
    }

    let pattern = Pattern::new("/tes[a-t]", PatternFlag::PATH_NAME).unwrap();
    assert!(!pattern.matches("/testoolong"));
    assert!(!pattern.matches("/tes"));
    assert!(!pattern.matches("/t"));
    assert!(!pattern.matches("/"));
    assert!(!pattern.matches(""));
    assert!(pattern.matches("/tesa"));
    assert!(pattern.matches("/test"));
    assert!(!pattern.matches("/tesu"));

    let pattern_path = Pattern::new("/tes[a-t]/a?a", PatternFlag::PATH_NAME).unwrap();
    let pattern_nopath = Pattern::new("/tes[a-t]/a?a", PatternFlag::empty()).unwrap();
    assert!(!pattern_path.matches("/tesu"));
    assert!(!pattern_nopath.matches("/tesu"));
    assert!(!pattern_path.matches("/tesu/aaa"));
    assert!(!pattern_nopath.matches("/tesu/aaa"));
    assert!(!pattern_path.matches("/tesu/xax"));
    assert!(!pattern_nopath.matches("/tesu/xax"));
    assert!(!pattern_path.matches("/test/xax"));
    assert!(!pattern_nopath.matches("/test/xax"));
    assert!(!pattern_path.matches("/test/a"));
    assert!(!pattern_nopath.matches("/test/a"));
    assert!(!pattern_path.matches("/test/ab"));
    assert!(!pattern_nopath.matches("/test/ab"));
    assert!(pattern_path.matches("/test/aba"));
    assert!(pattern_nopath.matches("/test/aba"));
    assert!(pattern_path.matches("/test/aaa"));
    assert!(pattern_nopath.matches("/test/aaa"));
    assert!(pattern_path.matches("/test/aba"));
    assert!(pattern_nopath.matches("/test/aba"));
    // the difference is here:
    assert!(!pattern_path.matches("/test/a/a"));
    assert!(pattern_nopath.matches("/test/a/a"));

    let pattern = Pattern::new("a*b*c", PatternFlag::PATH_NAME).unwrap();
    assert!(pattern.matches("axxbxxc"));
    assert!(!pattern.matches("axxbxxcxx"));
    assert!(pattern.matches("axxbxxbxxc"));
    assert!(!pattern.matches("axxbxxbxxcxx"));
    assert!(pattern.matches("axxbxxbxxcxxc"));
    assert!(!pattern.matches("axxbxxbxxcxxcxx"));

    let pattern = Pattern::new("a*b*c*", PatternFlag::PATH_NAME).unwrap();
    assert!(pattern.matches("axxbxxc"));
    assert!(pattern.matches("axxbxxcxx"));
    assert!(pattern.matches("axxbxxbxxc"));
    assert!(pattern.matches("axxbxxbxxcxx"));
    assert!(pattern.matches("axxbxxbxxcxxc"));
    assert!(pattern.matches("axxbxxbxxcxxcxx"));

    let pattern = Pattern::new(
        "aB[c-fX-Z][[:upper:]][[:lower:]][[:digit:]k]",
        PatternFlag::PATH_NAME | PatternFlag::IGNORE_CASE,
    )
    .unwrap();
    assert!(pattern.matches("aBcUl3"));
    assert!(pattern.matches("AbCuL9"));
    assert!(!pattern.matches("aBgUl3"));
    assert!(!pattern.matches("aBgUl3"));
    assert!(!pattern.matches("aBcUlx"));
    assert!(pattern.matches("abculk"));
    assert!(pattern.matches("abxulk"));
    assert!(!pattern.matches("abxul"));

    let pattern = Pattern::new("a/b**/c", PatternFlag::PATH_NAME).unwrap();
    assert!(pattern.matches("a/bxx/c"));
    assert!(!pattern.matches("a/bxx/yy/c"));

    let pattern = Pattern::new("**/lost+found", PatternFlag::PATH_NAME).unwrap();
    assert!(pattern.matches("/foo/lost+found"));
    assert!(pattern.matches("foo/lost+found"));
    assert!(pattern.matches("/lost+found"));
    assert!(pattern.matches("///lost+found"));
    assert!(pattern.matches("lost+found"));
    assert!(!pattern.matches("lost+found2"));
    assert!(!pattern.matches("lost+found/"));
    assert!(!pattern.matches("xlost+found"));
    assert!(!pattern.matches("xlost+found/"));
}