syd 3.52.0

rock-solid application kernel
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
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
//
// Syd: rock-solid application kernel
// src/wildmatch.rs: Shell-style pattern matching
//
// Copyright (c) 2024, 2025, 2026 Ali Polatel <alip@chesswob.org>
// Based in part upon rsync's lib/wildmatch.c which is:
//   Written by Rich $alz, mirror!rs, Wed Nov 26 19:03:17 EST 1986.
//   Rich $alz is now <rsalz@bbn.com>.
//   Modified by Wayne Davison to special-case '/' matching, to make '**'
//   work differently than '*', and to fix the character-class code.
//   SPDX-License-Identifier: GPL-3.0-or-later
// Based in part upon Kirk Krauss's FastWildCompare algorithm which is:
//   Copyright 2018 IBM Corporation
//   See: https://www.developforperformance.com/MatchingWildcards_AnImprovedAlgorithmForBigData.html
//   SPDX-License-Identifier: Apache-2.0
//
// Changes by alip:
// - Ported to Rust.
// - Added SIMD support.
// - Intuitive matching for consecutive slashes separated by double
//   star, e.g. /usr/**/bin/bash matches /usr/bin/bash.
// - Rewrite using Kirk Krauss's FastWildCompare algorithm,
//   extended with dual backup points for '*' and '**' wildcards.
//
// SPDX-License-Identifier: GPL-3.0

// SAFETY: This module has been liberated from unsafe code!
// Tests call fnmatch(3) to compare.
#![cfg_attr(not(test), forbid(unsafe_code))]

use std::{borrow::Cow, cmp::Ordering};

use memchr::{
    arch::all::{is_equal, is_prefix},
    memchr, memchr2, memchr3, memmem,
};
use nix::NixPath;

use crate::{
    likely,
    path::{XPath, XPathBuf},
    unlikely,
};

/// Match methods
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum MatchMethod {
    /// Literal match
    Literal,
    /// Prefix match
    Prefix,
    /// Glob match
    Glob,
}

/// Return true if haystack contains the substring needle.
pub fn contains(haystack: &[u8], needle: &[u8]) -> bool {
    memmem::find(haystack, needle).is_some()
}

/// Apply matching according to given type and return result.
pub fn globmatch(pattern: &[u8], path: &[u8], method: MatchMethod) -> bool {
    match method {
        MatchMethod::Literal => litmatch(pattern, path),
        MatchMethod::Prefix => prematch(pattern, path),
        MatchMethod::Glob => wildmatch(pattern, path),
    }
}

/// Convenience for glob matching of names.
///
/// Pattern is prefixed and suffixed with the `*` character
/// for literal, non-glob patterns.
///
/// Matching is done case-insensitively.
pub fn inamematch(pattern: &str, name: &str) -> bool {
    let glob = if !is_literal(pattern.as_bytes()) {
        Cow::Borrowed(pattern)
    } else {
        Cow::Owned(format!("*{pattern}*"))
    };

    wildmatch(
        glob.to_ascii_lowercase().as_bytes(),
        name.to_ascii_lowercase().as_bytes(),
    )
}

/// Return true if the pattern contains none of '*', '?', or '[',
/// indicating a literal string rather than a glob pattern.
#[inline]
pub fn is_literal(pattern: &[u8]) -> bool {
    memchr3(b'*', b'?', b'[', pattern).is_none()
}

/// Return Some(prefix) if the pattern can be reduced to a substring match.
pub fn get_prefix(pattern: &XPath) -> Option<XPathBuf> {
    if pattern.ends_with(b"/***") {
        // 1. Extract prefix (remove the slash).
        // 2. Check if the prefix is a literal string.
        let len = pattern.len();
        let pre = &pattern.as_bytes()[..len - "/***".len()];
        if is_literal(pre) {
            return Some(pre.into());
        }
    } else if pattern.ends_with(b"/**") {
        // 1. Extract prefix (keep the slash!)
        // 2. Check if the prefix is a literal string.
        let len = pattern.len();
        let pre = &pattern.as_bytes()[..len - "**".len()];
        if is_literal(pre) {
            return Some(pre.into());
        }
    }

    None
}

/// Determine match method for a pattern and handle `/***` splitting.
///
/// Returns `(method, optional_split_pattern)`:
/// - `method`: The match method to use (Prefix, Literal, or Glob)
/// - `split`: For `/***` patterns, contains the intermediate `foo/**` pattern
///
/// For `/***` patterns: `foo/***` is split into `foo/**` (returned in Option)
/// and `foo` (mutated in place).
#[expect(clippy::disallowed_methods)]
pub fn get_match_method(pat: &mut XPathBuf) -> (MatchMethod, Option<XPathBuf>) {
    if let Some(prefix) = get_prefix(pat) {
        *pat = prefix;
        (MatchMethod::Prefix, None)
    } else if is_literal(pat.as_bytes()) {
        (MatchMethod::Literal, None)
    } else if pat.ends_with(b"/***") {
        // Pattern followed by triple star: split into `foo/**` and `foo`.
        let len = pat.len();
        let len0 = len.checked_sub(b"*".len()).unwrap();
        let len1 = len.checked_sub(b"/***".len()).unwrap();
        pat.truncate(len0); // foo/*** -> foo/**
        let split = pat.clone();
        pat.truncate(len1); // foo/*** -> foo
        (MatchMethod::Glob, Some(split))
    } else {
        (MatchMethod::Glob, None)
    }
}

/// Match the "pattern" against the "path" literally.
///
/// This function performs simple string matching.
///
/// # Arguments
///
/// * `pattern` - The literal string to match.
/// * `path` - The path to match against the pattern.
///
/// # Returns
///
/// * `true` if the path matches the pattern.
/// * `false` otherwise.
pub fn litmatch(pattern: &[u8], path: &[u8]) -> bool {
    is_equal(path, pattern)
}

/// Match the "pattern" against the "path" using prefix match.
///
/// This function performs simple substring matching.
///
/// # Arguments
///
/// * `pattern` - The prefix to match.
/// * `path` - The path to match against the pattern.
///
/// # Returns
///
/// * `true` if the path matches the pattern.
/// * `false` otherwise.
pub fn prematch(pattern: &[u8], path: &[u8]) -> bool {
    let len = pattern.len();
    let ord = path.len().cmp(&len);
    (ord == Ordering::Equal
        || (ord == Ordering::Greater && (pattern.last() == Some(&b'/') || path[len] == b'/')))
        && is_prefix(path, pattern)
}

/// Match the "pattern" against the "text".
///
/// This function performs shell-style pattern matching, supporting ?, \, [], and * characters.
/// It is 8-bit clean and has special handling for '/' characters and '**' patterns.
///
/// # Arguments
///
/// * `pattern` - The glob pattern to match.
/// * `text` - The text to match against the pattern.
///
/// # Returns
///
/// * `true` if the path matches the pattern.
/// * `false` otherwise.
// Two-loop algorithm based on Kirk Krauss's FastWildCompare.
// Loop 1: Fast path for literals until '*' is found.
// Loop 2: Handle wildcards with dual backup points.
#[expect(clippy::cognitive_complexity)]
pub fn wildmatch(pattern: &[u8], text: &[u8]) -> bool {
    // Loop 1: Match literals until a special character is found.
    let mut idx = 0;
    for (&p_ch, &t_ch) in pattern.iter().zip(text.iter()) {
        if unlikely(matches!(p_ch, b'*' | b'[' | b'\\')) {
            // Special character detected, enter 2nd loop.
            break;
        }
        if unlikely((p_ch != b'?' && p_ch != t_ch) || (p_ch != b'/' && t_ch == b'/')) {
            // ?: Match any single character except '/'.
            // x: Literal match.
            return false;
        }
        idx += 1;
    }

    // Handle cases where one ran out before the other or both exhausted.
    let p_len = pattern.len();
    let t_len = text.len();
    if unlikely(idx >= p_len) {
        // Pattern exhausted: Only match if text also exhausted.
        return idx >= t_len;
    }

    if likely(idx >= t_len) {
        // Text exhausted:
        // 1. Pattern must be all wildcards.
        // 2. Handle intuitive '/**/' where trailing '/' is optional.
        let mut p_idx = idx;
        while let Some(p_ch) = pattern.get(p_idx) {
            // Skip all consecutive '*' characters.
            if p_ch == &b'*' {
                p_idx += 1;
                while pattern.get(p_idx) == Some(&b'*') {
                    p_idx += 1;
                }
            } else {
                return false;
            }

            // If we encounter '/', check the previous two characters to be '**'.
            if pattern.get(p_idx) == Some(&b'/') {
                for n in 1..=2 {
                    if p_idx
                        .checked_sub(n)
                        .map(|idx| pattern.get(idx) != Some(&b'*'))
                        .unwrap_or(false)
                    {
                        return false;
                    }
                }
                p_idx += 1;
            }
        }
        return true;
    }

    // Loop 2: Handle wildcards with dual backup points.
    let mut p_idx = idx;
    let mut t_idx = idx;

    // Backup points for backtracking:
    // star:     Backup for '*' which stops at '/'.
    // globstar: Backup for '**' which crosses '/'.
    struct BackupPoint {
        // Index into the pattern.
        p_idx: usize,
        // Index into the text.
        t_idx: usize,
    }
    let mut star_p: Option<BackupPoint> = None;
    let mut globstar_p: Option<BackupPoint> = None;
    let mut globstar_anchored = false;

    loop {
        if let Some(&p_ch) = pattern.get(p_idx) {
            match p_ch {
                b'*' => {
                    // Check for '**'.
                    let is_double = pattern.get(p_idx + 1).map(|&b| b == b'*').unwrap_or(false);

                    if is_double {
                        p_idx += 2; // Skip both stars.

                        // Check for intuitive '/**/' matching:
                        // If pattern is '/**/' make the trailing '/' optional.
                        let anchored = p_idx
                            .checked_sub(3)
                            .map(|idx| {
                                pattern.get(idx) == Some(&b'/') && pattern.get(p_idx) == Some(&b'/')
                            })
                            .unwrap_or(false);

                        if anchored {
                            p_idx += 1; // Skip the trailing '/'.
                            globstar_anchored = true;
                        } else {
                            globstar_anchored = false;
                        }

                        // Set globstar backup point.
                        globstar_p = Some(BackupPoint { p_idx, t_idx });

                        // Clear single star: globstar takes over.
                        star_p = None;
                    } else {
                        p_idx += 1; // Skip the star.

                        // SIMD optimization: If next pattern char is a literal,
                        // use memchr2 to skip to its first occurrence or a '/'.
                        match pattern.get(p_idx).copied() {
                            None | Some(b'*' | b'?' | b'[' | b'\\') => {
                                // Set star backup point for non-literal next chars.
                                star_p = Some(BackupPoint { p_idx, t_idx });
                            }
                            Some(next_p) => {
                                // Find next_p or '/' in remaining text.
                                star_p = if let Some(skip) = memchr2(next_p, b'/', &text[t_idx..]) {
                                    if text[t_idx + skip] != b'/' {
                                        // Found the literal: Skip directly to it.
                                        t_idx += skip;
                                    }
                                    // Else we hit slash barrier: Single star can't cross.
                                    // Set backup point and let backtracking handle it.
                                    Some(BackupPoint { p_idx, t_idx })
                                } else if globstar_p.is_some() {
                                    // globstar: Fall through to backtracking.
                                    Some(BackupPoint { p_idx, t_idx })
                                } else {
                                    // Literal not found, no slash, and no globstar.
                                    return false;
                                };
                                continue;
                            }
                        }
                    }

                    // Check for trailing wildcard.
                    if p_idx < p_len {
                        continue;
                    }

                    // '**' matches everything.
                    if is_double {
                        return true;
                    }

                    // '*' at end: Check if no more '/' in text.
                    // If there is a '/', fall through to try globstar backtracking.
                    if memchr(b'/', &text[t_idx..]).is_none() {
                        return true;
                    }

                    // There's a '/': If no globstar backup, fail.
                    // Otherwise fall through to backtracking.
                    if globstar_p.is_none() {
                        return false;
                    }
                    // Fall through to backtracking below.
                }

                b'?' => {
                    if text.get(t_idx).map(|&b| b != b'/').unwrap_or(false) {
                        p_idx += 1;
                        t_idx += 1;
                        continue;
                    }
                }

                b'[' => match text.get(t_idx) {
                    None | Some(&b'/') => {}
                    Some(&t_ch) => {
                        if let Some(new_p) = classmatch(pattern, p_idx + 1, t_ch) {
                            p_idx = new_p;
                            t_idx += 1;
                            continue;
                        }
                    }
                },

                b'\\' => {
                    p_idx += 1;
                    if pattern
                        .get(p_idx)
                        .map(|p_ch| text.get(t_idx) == Some(p_ch))
                        .unwrap_or(false)
                    {
                        p_idx += 1;
                        t_idx += 1;
                        continue;
                    }
                }

                // Literal match.
                _ => {
                    if text.get(t_idx) == Some(&p_ch) {
                        p_idx += 1;
                        t_idx += 1;
                        continue;
                    }
                }
            }
        }

        // Check if both exhausted which means success.
        if p_idx >= p_len && t_idx >= t_len {
            return true;
        }

        // Check for trailing wildcards in pattern.
        if t_idx >= t_len {
            while matches!(pattern.get(p_idx), Some(&b'*')) {
                p_idx += 1;
            }
            return p_idx >= p_len;
        }

        // Mismatch: Try backtracking.
        //
        // Step 1: Try single star '*' first, bounded by '/'.
        if let Some(BackupPoint {
            p_idx: sp,
            t_idx: st,
        }) = star_p
        {
            if text.get(st).map(|&b| b != b'/').unwrap_or(false) {
                p_idx = sp;
                t_idx = st + 1;
                star_p = Some(BackupPoint { p_idx, t_idx });
                continue;
            }
            // Hit '/' - star is exhausted: Fall through to globstar.
        }

        // Step 2: Try globstar '**' which crosses '/'.
        if let Some(BackupPoint {
            p_idx: gsp,
            t_idx: gst,
        }) = globstar_p
        {
            if gst < t_len {
                if globstar_anchored {
                    // Must resume at component boundary: Find next '/'.
                    if let Some(pos) = memchr(b'/', &text[gst..]) {
                        p_idx = gsp;
                        t_idx = gst + pos + 1;
                        star_p = None;
                        globstar_p = Some(BackupPoint { p_idx, t_idx });
                        continue;
                    }
                    // No more slashes: globstar is exhausted.
                } else {
                    // Non-anchored: Resume at any position.
                    p_idx = gsp;
                    t_idx = gst + 1;
                    star_p = None;
                    globstar_p = Some(BackupPoint { p_idx, t_idx });
                    continue;
                }
            }
        }

        // No backtracking options left.
        return false;
    }
}

// Match a character class [...]
// Returns the pattern index after ']' if matched, None otherwise.
#[expect(clippy::cognitive_complexity)]
#[inline]
fn classmatch(pattern: &[u8], mut p_idx: usize, t_ch: u8) -> Option<usize> {
    let mut matched = false;
    let mut negated = false;
    let mut prev_ch: u8 = 0;
    let mut first = true;

    loop {
        let p_ch = if let Some(&p_ch) = pattern.get(p_idx) {
            p_ch
        } else {
            // Unclosed bracker
            return None;
        };

        // Check for negation.
        if unlikely(first && !negated && matches!(p_ch, NEGATE_CLASS | NEGATE_CLASS2)) {
            negated = true;
            p_idx += 1;
            continue;
        }

        // Handle ']' - closes the class unless it's at the start.
        if unlikely(p_ch == b']' && !first) {
            break;
        }

        first = false;
        match p_ch {
            b'\\' => {
                // Escaped character
                p_idx += 1;
                let escaped = if let Some(&escaped) = pattern.get(p_idx) {
                    escaped
                } else {
                    return None;
                };
                if escaped == t_ch {
                    matched = true;
                }
                prev_ch = escaped;
                p_idx += 1;
            }
            b'-' if prev_ch != 0 && pattern.get(p_idx + 1).map(|&b| b != b']').unwrap_or(false) => {
                // Character range: prev_ch - next_ch
                p_idx += 1;
                let mut range_end = pattern[p_idx];
                if range_end == b'\\' {
                    p_idx += 1;
                    range_end = if let Some(&ch) = pattern.get(p_idx) {
                        ch
                    } else {
                        return None;
                    };
                }
                if t_ch >= prev_ch && t_ch <= range_end {
                    matched = true;
                }
                p_idx += 1;
                prev_ch = 0; // Reset after range.
            }
            b'[' if pattern.get(p_idx + 1).map(|&b| b == b':').unwrap_or(false) => {
                // POSIX character class [:name:]
                p_idx += 2;
                let class_start = p_idx;

                // Find closing ':]'.
                while let Some(ch) = pattern.get(p_idx) {
                    if ch == &b':' && pattern.get(p_idx + 1) == Some(&b']') {
                        break;
                    }
                    p_idx += 1;
                }

                if unlikely(pattern.get(p_idx).map(|&b| b != b':').unwrap_or(true)) {
                    // Didn't find ':', treat '[' as literal.
                    p_idx = class_start - 2;
                    if pattern[p_idx] == t_ch {
                        matched = true;
                    }
                    prev_ch = b'[';
                    p_idx += 1;
                    continue;
                }

                // Look up POSIX class and call respective closure.
                let class_name = &pattern[class_start..p_idx];
                if let Ok(pos) = POSIX_CLASSES.binary_search_by(|(name, _)| name.cmp(&class_name)) {
                    if POSIX_CLASSES[pos].1(t_ch) {
                        matched = true;
                    }
                } else {
                    // Unknown POSIX class.
                    return None;
                }

                p_idx += 2; // Skip ':]'
                prev_ch = 0; // Reset after character class.
            }
            _ => {
                // Literal character in class.
                if p_ch == t_ch {
                    matched = true;
                }
                p_idx += 1;
                prev_ch = p_ch;
            }
        }
    }

    // p_idx is now at ']'.
    if matched != negated {
        Some(p_idx + 1)
    } else {
        None
    }
}

const NEGATE_CLASS: u8 = b'!';
const NEGATE_CLASS2: u8 = b'^';

// Supported POSIX classes.
// This array must be sorted by name for binary search.
#[expect(clippy::type_complexity)]
const POSIX_CLASSES: &[(&[u8], fn(u8) -> bool)] = &[
    (b"alnum", |c| c.is_ascii_alphanumeric()),
    (b"alpha", |c| c.is_ascii_alphabetic()),
    (b"blank", |c| matches!(c, b' ' | b'\t')),
    (b"cntrl", |c| c.is_ascii_control()),
    (b"digit", |c| c.is_ascii_digit()),
    (b"graph", |c| c.is_ascii_graphic()),
    (b"lower", |c| c.is_ascii_lowercase()),
    (b"print", |c| c.is_ascii() && !c.is_ascii_control()),
    (b"punct", |c| c.is_ascii_punctuation()),
    (b"space", |c| c.is_ascii_whitespace()),
    (b"upper", |c| c.is_ascii_uppercase()),
    (b"xdigit", |c| c.is_ascii_hexdigit()),
];

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_litmatch_1() {
        assert!(litmatch(b"", b""));
        assert!(litmatch(b"p", b"p"));
        assert!(!litmatch(b"p", b"P"));
        assert!(litmatch(b"/usr", b"/usr"));
        assert!(!litmatch(b"/usr", b"/usr/"));
    }

    #[test]
    fn test_contains_1() {
        assert!(contains(b"hello world", b"world"));
        assert!(contains(b"hello world", b"hello"));
        assert!(!contains(b"hello world", b"xyz"));
        assert!(contains(b"hello", b""));
        assert!(!contains(b"", b"x"));
    }

    #[test]
    fn test_is_literal_1() {
        assert!(is_literal(b"hello"));
        assert!(is_literal(b"/usr/bin/bash"));
        assert!(is_literal(b""));
        assert!(!is_literal(b"*.txt"));
        assert!(!is_literal(b"file?.log"));
        assert!(!is_literal(b"[abc]"));
    }

    #[test]
    fn test_globmatch_1() {
        assert!(globmatch(b"/usr", b"/usr", MatchMethod::Literal));
        assert!(!globmatch(b"/usr", b"/usr/bin", MatchMethod::Literal));
    }

    #[test]
    fn test_globmatch_2() {
        assert!(globmatch(b"/usr", b"/usr/bin", MatchMethod::Prefix));
        assert!(!globmatch(b"/usr", b"/usrlocal", MatchMethod::Prefix));
    }

    #[test]
    fn test_globmatch_3() {
        assert!(globmatch(
            b"/usr/*/bash",
            b"/usr/bin/bash",
            MatchMethod::Glob
        ));
        assert!(!globmatch(
            b"/usr/*/bash",
            b"/usr/local/bin/bash",
            MatchMethod::Glob
        ));
    }

    #[test]
    fn test_inamematch_1() {
        assert!(inamematch("hello", "HELLO"));
        assert!(inamematch("hello", "say hello world"));
        assert!(!inamematch("xyz", "hello"));
    }

    #[test]
    fn test_inamematch_2() {
        assert!(inamematch("*.TXT", "file.txt"));
        assert!(!inamematch("*.TXT", "file.log"));
    }

    #[test]
    fn test_get_prefix_1() {
        let pat = XPath::from_bytes(b"/usr/***");
        let result = get_prefix(pat);
        assert_eq!(result.unwrap().as_bytes(), b"/usr");
    }

    #[test]
    fn test_get_prefix_2() {
        let pat = XPath::from_bytes(b"/usr/**");
        let result = get_prefix(pat);
        assert_eq!(result.unwrap().as_bytes(), b"/usr/");
    }

    #[test]
    fn test_get_prefix_3() {
        let pat = XPath::from_bytes(b"/usr/*");
        assert!(get_prefix(pat).is_none());
    }

    #[test]
    fn test_get_prefix_4() {
        let pat = XPath::from_bytes(b"/usr/[ab]/***");
        assert!(get_prefix(pat).is_none());
    }

    #[test]
    fn test_get_prefix_5() {
        let pat = XPath::from_bytes(b"/usr/[ab]/**");
        assert!(get_prefix(pat).is_none());
    }

    #[test]
    fn test_get_match_method_1() {
        let mut pat = XPathBuf::from("/usr/**");
        let (method, split) = get_match_method(&mut pat);
        assert_eq!(method, MatchMethod::Prefix);
        assert!(split.is_none());
        assert_eq!(pat.as_bytes(), b"/usr/");
    }

    #[test]
    fn test_get_match_method_2() {
        let mut pat = XPathBuf::from("/usr/bin");
        let (method, split) = get_match_method(&mut pat);
        assert_eq!(method, MatchMethod::Literal);
        assert!(split.is_none());
    }

    #[test]
    fn test_get_match_method_3() {
        let mut pat = XPathBuf::from("/usr/*.so");
        let (method, split) = get_match_method(&mut pat);
        assert_eq!(method, MatchMethod::Glob);
        assert!(split.is_none());
    }

    #[test]
    fn test_get_match_method_4() {
        let mut pat = XPathBuf::from("/usr/[ab]/***");
        let (method, split) = get_match_method(&mut pat);
        assert_eq!(method, MatchMethod::Glob);
        assert!(split.is_some());
        assert_eq!(split.unwrap().as_bytes(), b"/usr/[ab]/**");
        assert_eq!(pat.as_bytes(), b"/usr/[ab]");
    }

    #[test]
    fn test_prematch_1() {
        assert!(prematch(b"", b""));
        assert!(prematch(b"p", b"p"));
        assert!(!prematch(b"p", b"P"));
        assert!(prematch(b"/usr", b"/usr"));
        assert!(prematch(b"/usr", b"/usr/"));
        assert!(prematch(b"/usr", b"/usr/bin"));
        assert!(!prematch(b"/usr", b"/usra"));
        assert!(!prematch(b"/usr", b"/usra/bin"));
    }

    #[test]
    fn test_prematch_2() {
        assert!(!prematch(b"/usr/bin", b"/usr"));
    }

    #[test]
    fn test_prematch_3() {
        assert!(prematch(b"/usr/", b"/usr/bin"));
    }

    #[test]
    fn test_wildmatch_1() {
        assert!(wildmatch(b"\\a", b"a"));
        assert!(!wildmatch(b"\\a", b"b"));
    }

    #[test]
    fn test_wildmatch_2() {
        assert!(!wildmatch(b"\\", b"a"));
    }

    #[test]
    fn test_wildmatch_3() {
        assert!(wildmatch(b"[[:alpha:]]", b"a"));
        assert!(!wildmatch(b"[[:alpha:]]", b"1"));
    }

    #[test]
    fn test_wildmatch_4() {
        assert!(wildmatch(b"[[:digit:]]", b"5"));
        assert!(!wildmatch(b"[[:digit:]]", b"x"));
    }

    #[test]
    fn test_wildmatch_5() {
        assert!(wildmatch(b"[[:upper:]]", b"Z"));
        assert!(!wildmatch(b"[[:upper:]]", b"z"));
    }

    #[test]
    fn test_wildmatch_6() {
        assert!(wildmatch(b"[[:lower:]]", b"z"));
        assert!(!wildmatch(b"[[:lower:]]", b"Z"));
    }

    #[test]
    fn test_wildmatch_7() {
        assert!(wildmatch(b"[[:alnum:]]", b"a"));
        assert!(wildmatch(b"[[:alnum:]]", b"5"));
        assert!(!wildmatch(b"[[:alnum:]]", b"!"));
    }

    #[test]
    fn test_wildmatch_8() {
        assert!(wildmatch(b"[[:space:]]", b" "));
        assert!(wildmatch(b"[[:space:]]", b"\t"));
        assert!(!wildmatch(b"[[:space:]]", b"a"));
    }

    #[test]
    fn test_wildmatch_9() {
        assert!(wildmatch(b"[[:xdigit:]]", b"f"));
        assert!(wildmatch(b"[[:xdigit:]]", b"A"));
        assert!(!wildmatch(b"[[:xdigit:]]", b"g"));
    }

    #[test]
    fn test_wildmatch_10() {
        assert!(wildmatch(b"[[:print:]]", b"a"));
        assert!(!wildmatch(b"[[:print:]]", b"\x01"));
    }

    #[test]
    fn test_wildmatch_11() {
        assert!(wildmatch(b"[[:punct:]]", b"!"));
        assert!(!wildmatch(b"[[:punct:]]", b"a"));
    }

    #[test]
    fn test_wildmatch_12() {
        assert!(wildmatch(b"[[:graph:]]", b"a"));
        assert!(!wildmatch(b"[[:graph:]]", b" "));
    }

    #[test]
    fn test_wildmatch_13() {
        assert!(wildmatch(b"[[:cntrl:]]", b"\x01"));
        assert!(!wildmatch(b"[[:cntrl:]]", b"a"));
    }

    #[test]
    fn test_wildmatch_14() {
        assert!(wildmatch(b"[[:blank:]]", b" "));
        assert!(wildmatch(b"[[:blank:]]", b"\t"));
        assert!(!wildmatch(b"[[:blank:]]", b"a"));
    }

    #[test]
    fn test_wildmatch_15() {
        assert!(!wildmatch(b"[[:bogus:]]", b"a"));
    }

    #[test]
    fn test_wildmatch_16() {
        assert!(wildmatch(b"[!a]", b"b"));
        assert!(!wildmatch(b"[!a]", b"a"));
    }

    #[test]
    fn test_wildmatch_17() {
        assert!(wildmatch(b"[^a]", b"b"));
        assert!(!wildmatch(b"[^a]", b"a"));
    }

    #[test]
    fn test_wildmatch_18() {
        assert!(wildmatch(b"[a-z]", b"m"));
        assert!(!wildmatch(b"[a-z]", b"M"));
    }

    #[test]
    fn test_wildmatch_19() {
        assert!(wildmatch(b"[\\a-\\z]", b"m"));
    }

    #[test]
    fn test_wildmatch_20() {
        assert!(wildmatch(b"[\\]]", b"]"));
        assert!(!wildmatch(b"[\\]]", b"a"));
    }

    #[test]
    fn test_wildmatch_21() {
        assert!(!wildmatch(b"[abc", b"a"));
    }

    #[test]
    fn test_wildmatch_22() {
        assert!(wildmatch(b"[]]", b"]"));
    }

    #[test]
    fn test_wildmatch_23() {
        assert!(!wildmatch(b"?", b"/"));
    }

    #[test]
    fn test_wildmatch_24() {
        assert!(wildmatch(b"/usr/*", b"/usr/bin"));
        assert!(!wildmatch(b"/usr/*", b"/usr/bin/bash"));
    }

    #[test]
    fn test_wildmatch_25() {
        assert!(wildmatch(b"/usr/**", b"/usr/bin/bash"));
        assert!(wildmatch(b"**", b"anything/at/all"));
    }

    #[test]
    fn test_wildmatch_26() {
        assert!(wildmatch(b"/usr/**/bash", b"/usr/bin/bash"));
        assert!(wildmatch(b"/usr/**/bash", b"/usr/bash"));
        assert!(wildmatch(b"/usr/**/bash", b"/usr/local/bin/bash"));
    }

    #[test]
    fn test_wildmatch_27() {
        assert!(wildmatch(b"/**/lib/*.so", b"/usr/lib/libc.so"));
        assert!(!wildmatch(b"/**/lib/*.so", b"/usr/lib/sub/libc.so"));
    }

    #[test]
    fn test_wildmatch_28() {
        assert!(wildmatch(b"abc*", b"abc"));
        assert!(wildmatch(b"abc**", b"abc"));
    }

    #[test]
    fn test_wildmatch_29() {
        assert!(wildmatch(b"", b""));
        assert!(!wildmatch(b"", b"a"));
        assert!(!wildmatch(b"a", b""));
    }

    #[test]
    fn test_wildmatch_30() {
        assert!(wildmatch(b"[[.a.]", b"["));
    }

    #[test]
    fn test_wildmatch_31() {
        assert!(!wildmatch(b"*", b"a/b"));
    }

    #[test]
    fn test_wildmatch_32() {
        assert!(!wildmatch(b"[abc]", b"/"));
    }

    #[test]
    fn test_wildmatch_33() {
        assert!(!wildmatch(b"a?", b"a"));
    }

    #[test]
    fn test_wildmatch_34() {
        assert!(!wildmatch(b"a\\", b"ab"));
    }

    #[test]
    fn test_wildmatch_35() {
        assert!(!wildmatch(b"*z", b"abc"));
    }

    #[test]
    fn test_wildmatch_36() {
        assert!(wildmatch(b"a/**/*", b"a/b"));
        assert!(wildmatch(b"a/**/*", b"a/b/c"));
    }

    #[test]
    fn test_wildmatch_blob() {
        use std::io::BufRead;

        let data = include_bytes!("wildtest.txt.xz");
        let decoder = xz2::read::XzDecoder::new(&data[..]);
        let reader = std::io::BufReader::new(decoder);

        let mut failures = 0;
        let mut test_cnt = 0;

        for (index, line) in reader.lines().enumerate() {
            let line = line.expect("Failed to read line from wildtest.txt.xz");
            let line_bytes = line.as_bytes();
            let line_num = index + 1;

            if line_bytes.starts_with(&[b'#'])
                || line_bytes.iter().all(|&b| b == b' ' || b == b'\t')
                || line.is_empty()
            {
                continue;
            }

            if let Some((expected, fnmatch_same, text, pattern)) = parse_test_line(line_bytes) {
                test_cnt += 1;
                if let Err(err) = run_wildtest(line_num, expected, fnmatch_same, text, pattern) {
                    eprintln!("FAIL[{test_cnt}]\t{err}");
                    if !err.contains("fnmatch") {
                        failures += 1;
                    }
                }
            } else {
                unreachable!("BUG: Fix test at line {test_cnt}: {line}!");
            }
        }

        if failures > 0 {
            panic!("{failures} out of {test_cnt} tests failed.");
        }
    }

    // Parse a test line without allocating intermediate structures.
    //
    // Returns (expected, fnmatch_same, text, pattern) if valid, None otherwise.
    fn parse_test_line(line: &[u8]) -> Option<(bool, bool, &[u8], &[u8])> {
        let mut parts = [&b""[..]; 4];
        let mut part_idx = 0;
        let mut i = 0;

        while i < line.len() && part_idx < 4 {
            // Skip whitespace
            while i < line.len() && matches!(line[i], b' ' | b'\t') {
                i += 1;
            }
            if i >= line.len() {
                break;
            }

            // Check for quoted section
            if matches!(line[i], b'\'' | b'"' | b'`') {
                let quote = line[i];
                i += 1;
                let start = i;
                while i < line.len() && line[i] != quote {
                    i += 1;
                }
                parts[part_idx] = &line[start..i];
                if i < line.len() {
                    i += 1; // Skip closing quote
                }
            } else {
                // Unquoted section
                let start = i;
                while i < line.len() && !matches!(line[i], b' ' | b'\t') {
                    i += 1;
                }
                parts[part_idx] = &line[start..i];
            }
            part_idx += 1;
        }

        if part_idx >= 4 {
            let expected = parts[0].first() == Some(&b'1');
            let fnmatch_same = parts[1].first() == Some(&b'1');
            Some((expected, fnmatch_same, parts[2], parts[3]))
        } else {
            None
        }
    }

    fn run_wildtest(
        line: usize,
        expected: bool,
        fnmatch_same: bool,
        text: &[u8],
        pattern: &[u8],
    ) -> Result<(), String> {
        let result = wildmatch(pattern, text);
        if result != expected {
            let text = String::from_utf8_lossy(text);
            let pattern = String::from_utf8_lossy(pattern);
            let msg = format!(
                "[!] Test failed on line {line}: text='{text}', pattern='{pattern}', expected={expected}, got={result}",
            );
            return Err(msg);
        }

        let fn_result = fnmatch(pattern, text);
        let same = fn_result == result;
        if same != fnmatch_same {
            let text = String::from_utf8_lossy(text);
            let pattern = String::from_utf8_lossy(pattern);
            let msg = format!(
                "[!] fnmatch divergence on line {line}: text='{text}', pattern='{pattern}', wildmatch={result}, fnmatch={fn_result}, expected_same={fnmatch_same}",
            );
            return Err(msg);
        }

        Ok(())
    }

    fn fnmatch(pat: &[u8], input: &[u8]) -> bool {
        pat.with_nix_path(|pat_cstr| {
            input.with_nix_path(|input_cstr| {
                let flags = libc::FNM_PATHNAME | libc::FNM_NOESCAPE | libc::FNM_PERIOD;
                // SAFETY: FFI call to fnmatch(3)
                unsafe { libc::fnmatch(pat_cstr.as_ptr(), input_cstr.as_ptr(), flags) == 0 }
            })
        })
        .map(|res| res.unwrap())
        .unwrap()
    }
}