wildcard-glob 11.2.0

Zero-dependency, zero-allocation compiled wildcard and path glob pattern matcher supporting *, **, ?, character classes, brace alternation, and multi-pattern GlobSet.
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
use super::*;

/// Expands top-level and nested `{a,b}` brace alternations into concrete branch strings.
///
/// # Arguments
///
/// - `&str` - Raw glob pattern string.
///
/// # Returns
///
/// - `Result<Vec<String>, GlobError>` - Expanded pattern strings or a syntax/limit error.
pub(crate) fn expand_braces(pattern: &str) -> Result<Vec<String>, GlobError> {
    let bytes: &[u8] = pattern.as_bytes();
    let len: usize = bytes.len();
    let mut open_idx: Option<usize> = None;
    let mut close_idx: Option<usize> = None;
    let mut depth: usize = 0;
    let mut in_class: bool = false;
    let mut idx: usize = 0;
    while idx < len {
        let b: u8 = bytes[idx];
        if b == b'\\' {
            if idx + 1 >= len {
                return Err(GlobError::TrailingEscape);
            }
            let next_len: usize = pattern[idx + 1..].chars().next().map_or(1, char::len_utf8);
            idx += 1 + next_len;
            continue;
        }
        if in_class {
            if b == b']' {
                in_class = false;
            }
            idx += 1;
            continue;
        }
        if b == b'[' {
            in_class = true;
            idx += 1;
            continue;
        }
        if b == b'{' {
            if depth == 0 {
                open_idx = Some(idx);
            }
            depth += 1;
        } else if b == b'}' {
            if depth == 0 {
                return Err(GlobError::UnmatchedCloseBrace);
            }
            depth -= 1;
            if depth == 0 {
                close_idx = Some(idx);
                break;
            }
        }
        idx += 1;
    }
    if in_class {
        return Err(GlobError::UnclosedClass);
    }
    if depth != 0 {
        return Err(GlobError::UnclosedBrace);
    }
    let Some(start) = open_idx else {
        return Ok(vec![pattern.to_owned()]);
    };
    let Some(end) = close_idx else {
        return Err(GlobError::UnclosedBrace);
    };
    let prefix: &str = &pattern[..start];
    let suffix: &str = &pattern[end + 1..];
    let mut alternatives: Vec<&str> = Vec::new();
    let mut alt_start: usize = start + 1;
    let mut nested_depth: usize = 0;
    let mut inner_class: bool = false;
    let mut pos: usize = start + 1;
    while pos < end {
        let b: u8 = bytes[pos];
        if b == b'\\' {
            if pos + 1 >= end {
                return Err(GlobError::TrailingEscape);
            }
            let next_len: usize = pattern[pos + 1..end]
                .chars()
                .next()
                .map_or(1, char::len_utf8);
            pos += 1 + next_len;
            continue;
        }
        if inner_class {
            if b == b']' {
                inner_class = false;
            }
            pos += 1;
            continue;
        }
        if b == b'[' {
            inner_class = true;
        } else if b == b'{' {
            nested_depth += 1;
        } else if b == b'}' {
            nested_depth = nested_depth.saturating_sub(1);
        } else if b == b',' && nested_depth == 0 {
            alternatives.push(&pattern[alt_start..pos]);
            alt_start = pos + 1;
        }
        pos += 1;
    }
    alternatives.push(&pattern[alt_start..end]);
    let mut expanded: Vec<String> = Vec::with_capacity(alternatives.len());
    let mut combined: String = String::new();
    for alt in alternatives {
        combined.clear();
        combined.reserve(prefix.len() + alt.len() + suffix.len());
        combined.push_str(prefix);
        combined.push_str(alt);
        combined.push_str(suffix);
        let sub: Vec<String> = expand_braces(&combined)?;
        if expanded.len().saturating_add(sub.len()) > MAX_BRACE_BRANCHES {
            return Err(GlobError::ExpansionLimitExceeded);
        }
        expanded.extend(sub);
    }
    Ok(expanded)
}

/// Builds a [`CharClass`] with a pre-negated 128-bit ASCII bitset and fallback non-ASCII ranges.
///
/// # Arguments
///
/// - `bool` - Whether the character class is negated.
/// - `&[(char, char)]` - Parsed inclusive character ranges.
///
/// # Returns
///
/// - `CharClass` - Compiled O(1) ASCII + Unicode character class.
fn build_char_class(negated: bool, ranges: &[(char, char)]) -> CharClass {
    let mut ascii_lo: u64 = 0;
    let mut ascii_hi: u64 = 0;
    let mut non_ascii: Vec<(char, char)> = Vec::new();
    for &(lo, hi) in ranges {
        let lo_u32: u32 = lo as u32;
        let hi_u32: u32 = hi as u32;
        if lo_u32 < 128 {
            let asc_end: u32 = hi_u32.min(127);
            let mut c: u32 = lo_u32;
            while c <= asc_end {
                if c < 64 {
                    ascii_lo |= 1u64 << c;
                } else {
                    ascii_hi |= 1u64 << (c - 64);
                }
                c += 1;
            }
        }
        if hi_u32 >= 128 {
            let non_lo: char = if lo_u32 < 128 { char::from(128u8) } else { lo };
            non_ascii.push((non_lo, hi));
        }
    }
    if negated {
        ascii_lo = !ascii_lo;
        ascii_hi = !ascii_hi;
    }
    CharClass {
        ascii_lo,
        ascii_hi,
        negated,
        non_ascii,
    }
}

/// Collects a slice of pure `GlobToken::Literal` tokens into a `String`, or returns `None`.
///
/// # Arguments
///
/// - `&[GlobToken]` - Candidate literal token slice.
///
/// # Returns
///
/// - `Option<String>` - Literal string if every token is `GlobToken::Literal`.
#[inline]
fn collect_literals(tokens: &[GlobToken]) -> Option<String> {
    let mut byte_len: usize = 0;
    for tok in tokens {
        match tok {
            GlobToken::Literal(c) => byte_len += c.len_utf8(),
            _ => return None,
        }
    }
    let mut out: String = String::with_capacity(byte_len);
    for tok in tokens {
        if let GlobToken::Literal(c) = tok {
            out.push(*c);
        }
    }
    Some(out)
}

/// Compiles one brace-free glob pattern string into an optimized [`CompiledBranch`].
///
/// # Arguments
///
/// - `&str` - Brace-free pattern string.
/// - `bool` - Whether `*` and `?` stop at path separators (`/`).
///
/// # Returns
///
/// - `Result<CompiledBranch, GlobError>` - Compiled branch representation or syntax error.
pub(crate) fn compile_branch(pattern: &str, path_aware: bool) -> Result<CompiledBranch, GlobError> {
    let bytes: &[u8] = pattern.as_bytes();
    let len: usize = bytes.len();
    let mut tokens: Vec<GlobToken> = Vec::with_capacity(len);
    let mut classes: Vec<CharClass> = Vec::new();
    let mut idx: usize = 0;
    while idx < len {
        let ch: char = if bytes[idx] < 128 {
            char::from(bytes[idx])
        } else {
            pattern[idx..].chars().next().unwrap_or_default()
        };
        let ch_len: usize = ch.len_utf8();
        match ch {
            '\\' => {
                idx += 1;
                if idx >= len {
                    return Err(GlobError::TrailingEscape);
                }
                let esc: char = if bytes[idx] < 128 {
                    char::from(bytes[idx])
                } else {
                    pattern[idx..].chars().next().unwrap_or_default()
                };
                tokens.push(GlobToken::Literal(esc));
                idx += esc.len_utf8();
            }
            '?' => {
                tokens.push(GlobToken::AnyChar);
                idx += 1;
            }
            '*' => {
                let mut is_double: bool = false;
                while idx + 1 < len && bytes[idx + 1] == b'*' {
                    is_double = true;
                    idx += 1;
                }
                let tok: GlobToken = if is_double || !path_aware {
                    GlobToken::DoubleStar
                } else {
                    GlobToken::Star
                };
                if tokens.last() != Some(&tok) {
                    tokens.push(tok);
                }
                idx += 1;
            }
            '[' => {
                idx += 1;
                if idx >= len {
                    return Err(GlobError::UnclosedClass);
                }
                let negated: bool = bytes[idx] == b'!' || bytes[idx] == b'^';
                if negated {
                    idx += 1;
                }
                let mut ranges: Vec<(char, char)> = Vec::new();
                let mut closed: bool = false;
                let mut first_in_class: bool = true;
                while idx < len {
                    if bytes[idx] == b']' && !first_in_class {
                        closed = true;
                        idx += 1;
                        break;
                    }
                    first_in_class = false;
                    let start_ch: char = if bytes[idx] == b'\\' {
                        idx += 1;
                        if idx >= len {
                            return Err(GlobError::TrailingEscape);
                        }
                        if bytes[idx] < 128 {
                            char::from(bytes[idx])
                        } else {
                            pattern[idx..].chars().next().unwrap_or_default()
                        }
                    } else if bytes[idx] < 128 {
                        char::from(bytes[idx])
                    } else {
                        pattern[idx..].chars().next().unwrap_or_default()
                    };
                    idx += start_ch.len_utf8();
                    if idx + 1 < len && bytes[idx] == b'-' && bytes[idx + 1] != b']' {
                        idx += 1;
                        let end_ch: char = if bytes[idx] == b'\\' {
                            idx += 1;
                            if idx >= len {
                                return Err(GlobError::TrailingEscape);
                            }
                            if bytes[idx] < 128 {
                                char::from(bytes[idx])
                            } else {
                                pattern[idx..].chars().next().unwrap_or_default()
                            }
                        } else if bytes[idx] < 128 {
                            char::from(bytes[idx])
                        } else {
                            pattern[idx..].chars().next().unwrap_or_default()
                        };
                        if start_ch > end_ch {
                            return Err(GlobError::InvalidRange);
                        }
                        ranges.push((start_ch, end_ch));
                        idx += end_ch.len_utf8();
                    } else {
                        ranges.push((start_ch, start_ch));
                    }
                }
                if !closed {
                    return Err(GlobError::UnclosedClass);
                }
                let cls_idx: u16 = classes.len() as u16;
                classes.push(build_char_class(negated, &ranges));
                tokens.push(GlobToken::Class(cls_idx));
            }
            other => {
                tokens.push(GlobToken::Literal(other));
                idx += ch_len;
            }
        }
    }
    if let Some(exact) = collect_literals(&tokens) {
        return Ok(CompiledBranch::Exact(exact));
    }
    if let Some((last, init)) = tokens.split_last()
        && let Some(prefix) = collect_literals(init)
    {
        match last {
            GlobToken::DoubleStar => return Ok(CompiledBranch::Prefix(prefix)),
            GlobToken::Star => return Ok(CompiledBranch::StarPrefix(prefix)),
            _ => {}
        }
    }
    if let Some((first, tail)) = tokens.split_first()
        && let Some(suffix) = collect_literals(tail)
    {
        match first {
            GlobToken::DoubleStar => return Ok(CompiledBranch::Suffix(suffix)),
            GlobToken::Star => return Ok(CompiledBranch::StarSuffix(suffix)),
            _ => {}
        }
    }
    if tokens.len() >= 3
        && tokens[0] == GlobToken::DoubleStar
        && tokens[tokens.len() - 1] == GlobToken::DoubleStar
        && let Some(mid) = collect_literals(&tokens[1..tokens.len() - 1])
    {
        return Ok(CompiledBranch::Contains(mid));
    }
    if tokens.len() >= 3
        && tokens[0] == GlobToken::DoubleStar
        && tokens[1] == GlobToken::Literal(PATH_SEP)
        && tokens[2] == GlobToken::Star
        && let Some(suffix) = collect_literals(&tokens[3..])
        && !suffix.as_bytes().contains(&PATH_SEP_BYTE)
    {
        return Ok(CompiledBranch::DoubleStarSlashStarSuffix(suffix));
    }
    let mut wild_pos: Option<usize> = None;
    let mut single_wild: bool = true;
    for (i, tok) in tokens.iter().enumerate() {
        match tok {
            GlobToken::Literal(_) => {}
            GlobToken::Star | GlobToken::DoubleStar => {
                if wild_pos.is_some() {
                    single_wild = false;
                    break;
                }
                wild_pos = Some(i);
            }
            _ => {
                single_wild = false;
                break;
            }
        }
    }
    if single_wild
        && let Some(pos) = wild_pos
        && pos > 0
        && pos + 1 < tokens.len()
        && let Some(prefix) = collect_literals(&tokens[..pos])
        && let Some(suffix) = collect_literals(&tokens[pos + 1..])
    {
        return match tokens[pos] {
            GlobToken::DoubleStar => Ok(CompiledBranch::PrefixDoubleStarSuffix { prefix, suffix }),
            GlobToken::Star => Ok(CompiledBranch::PrefixStarSuffix { prefix, suffix }),
            _ => Ok(CompiledBranch::Tokens { tokens, classes }),
        };
    }
    Ok(CompiledBranch::Tokens { tokens, classes })
}

/// Evaluates whether a compiled [`CompiledBranch`] matches `candidate`.
///
/// # Arguments
///
/// - `&CompiledBranch` - Compiled branch representation.
/// - `&str` - Candidate string to test.
/// - `bool` - Whether single-segment wildcards stop at `/`.
///
/// # Returns
///
/// - `bool` - `true` if `candidate` matches `branch`.
#[inline]
pub(crate) fn match_branch(branch: &CompiledBranch, candidate: &str, path_aware: bool) -> bool {
    match branch {
        CompiledBranch::Exact(lit) => candidate == lit.as_str(),
        CompiledBranch::Prefix(pre) => candidate.starts_with(pre.as_str()),
        CompiledBranch::Suffix(suf) => candidate.ends_with(suf.as_str()),
        CompiledBranch::StarPrefix(pre) => {
            candidate.starts_with(pre.as_str())
                && !candidate.as_bytes()[pre.len()..].contains(&PATH_SEP_BYTE)
        }
        CompiledBranch::StarSuffix(suf) => {
            candidate.ends_with(suf.as_str())
                && !candidate.as_bytes()[..candidate.len() - suf.len()].contains(&PATH_SEP_BYTE)
        }
        CompiledBranch::PrefixStarSuffix { prefix, suffix } => {
            let min_len: usize = prefix.len() + suffix.len();
            candidate.len() >= min_len
                && candidate.starts_with(prefix.as_str())
                && candidate.ends_with(suffix.as_str())
                && !candidate.as_bytes()[prefix.len()..candidate.len() - suffix.len()]
                    .contains(&PATH_SEP_BYTE)
        }
        CompiledBranch::PrefixDoubleStarSuffix { prefix, suffix } => {
            candidate.len() >= prefix.len() + suffix.len()
                && candidate.starts_with(prefix.as_str())
                && candidate.ends_with(suffix.as_str())
        }
        CompiledBranch::Contains(sub) => candidate.contains(sub.as_str()),
        CompiledBranch::DoubleStarSlashStarSuffix(suf) => {
            candidate.ends_with(suf.as_str())
                && candidate.as_bytes()[..candidate.len() - suf.len()].contains(&PATH_SEP_BYTE)
        }
        CompiledBranch::Tokens { tokens, classes } => {
            match_tokens(tokens, classes, candidate, path_aware)
        }
    }
}

/// Evaluates whether a token slice matches `text` without heap allocations or exponential blowup.
///
/// # Arguments
///
/// - `&[GlobToken]` - Compiled glob tokens.
/// - `&[CharClass]` - Side table of character classes.
/// - `&str` - Candidate string to test.
/// - `bool` - Whether single-segment wildcards exclude `/`.
///
/// # Returns
///
/// - `bool` - `true` if `text` matches `tokens`.
#[inline]
pub(crate) fn match_tokens(
    tokens: &[GlobToken],
    classes: &[CharClass],
    text: &str,
    path_aware: bool,
) -> bool {
    if text.is_ascii() {
        return match_tokens_ascii(tokens, classes, text.as_bytes(), path_aware);
    }
    match_tokens_utf8(tokens, classes, text, path_aware)
}

/// Tests whether an ASCII byte matches a compiled [`CharClass`] in O(1).
///
/// # Arguments
///
/// - `&CharClass` - Compiled character class.
/// - `u8` - ASCII byte (`0..=127`).
///
/// # Returns
///
/// - `bool` - `true` if the byte matches the class.
#[inline(always)]
fn match_class_ascii(cls: &CharClass, b: u8) -> bool {
    let word: u64 = if b < 64 { cls.ascii_lo } else { cls.ascii_hi };
    ((word >> (b & 63)) & 1) != 0
}

/// Tests whether a Unicode character matches a compiled [`CharClass`].
///
/// # Arguments
///
/// - `&CharClass` - Compiled character class.
/// - `char` - Candidate character.
///
/// # Returns
///
/// - `bool` - `true` if the character matches the class.
#[inline(always)]
fn match_class_char(cls: &CharClass, ch: char) -> bool {
    let code: u32 = ch as u32;
    if code < 128 {
        return match_class_ascii(cls, code as u8);
    }
    let in_set: bool = cls
        .non_ascii
        .iter()
        .any(|(lo, hi): &(char, char)| ch >= *lo && ch <= *hi);
    in_set != cls.negated
}

/// Evaluates a token sequence against an ASCII byte slice with zero heap allocation.
///
/// # Arguments
///
/// - `&[GlobToken]` - Compiled glob tokens.
/// - `&[CharClass]` - Side table of character classes.
/// - `&[u8]` - ASCII input bytes.
/// - `bool` - Whether single-segment wildcards exclude `/`.
///
/// # Returns
///
/// - `bool` - `true` when matched.
fn match_tokens_ascii(
    tokens: &[GlobToken],
    classes: &[CharClass],
    bytes: &[u8],
    path_aware: bool,
) -> bool {
    let tlen: usize = tokens.len();
    let blen: usize = bytes.len();
    let mut ti: usize = 0;
    let mut bi: usize = 0;
    let mut star_ti: Option<usize> = None;
    let mut star_bi: usize = 0;
    let mut star_is_double: bool = false;
    let mut dstar_ti: Option<usize> = None;
    let mut dstar_bi: usize = 0;
    while bi < blen {
        let byte: u8 = bytes[bi];
        if ti < tlen {
            match tokens[ti] {
                GlobToken::DoubleStar => {
                    dstar_ti = Some(ti);
                    dstar_bi = bi;
                    star_ti = None;
                    ti += 1;
                    continue;
                }
                GlobToken::Star => {
                    star_ti = Some(ti);
                    star_bi = bi;
                    star_is_double = !path_aware;
                    ti += 1;
                    continue;
                }
                GlobToken::AnyChar if !path_aware || byte != PATH_SEP_BYTE => {
                    ti += 1;
                    bi += 1;
                    continue;
                }
                GlobToken::Literal(expected) if expected as u32 == u32::from(byte) => {
                    ti += 1;
                    bi += 1;
                    continue;
                }
                GlobToken::Class(cls_idx)
                    if (!path_aware || byte != PATH_SEP_BYTE)
                        && match_class_ascii(&classes[cls_idx as usize], byte) =>
                {
                    ti += 1;
                    bi += 1;
                    continue;
                }
                _ => {}
            }
        }
        if let Some(s_ti) = star_ti
            && (star_is_double || bytes[star_bi] != PATH_SEP_BYTE)
        {
            star_bi += 1;
            bi = star_bi;
            ti = s_ti + 1;
            continue;
        }
        if let Some(ds_ti) = dstar_ti {
            dstar_bi += 1;
            bi = dstar_bi;
            ti = ds_ti + 1;
            star_ti = None;
            continue;
        }
        return false;
    }
    while ti < tlen && matches!(tokens[ti], GlobToken::Star | GlobToken::DoubleStar) {
        ti += 1;
    }
    ti == tlen
}

/// Evaluates a token sequence directly over a UTF-8 string slice with zero heap allocation.
///
/// # Arguments
///
/// - `&[GlobToken]` - Compiled glob tokens.
/// - `&[CharClass]` - Side table of character classes.
/// - `&str` - UTF-8 input string.
/// - `bool` - Whether single-segment wildcards exclude `/`.
///
/// # Returns
///
/// - `bool` - `true` when matched.
fn match_tokens_utf8(
    tokens: &[GlobToken],
    classes: &[CharClass],
    text: &str,
    path_aware: bool,
) -> bool {
    let tlen: usize = tokens.len();
    let blen: usize = text.len();
    let bytes: &[u8] = text.as_bytes();
    let mut ti: usize = 0;
    let mut bi: usize = 0;
    let mut star_ti: Option<usize> = None;
    let mut star_bi: usize = 0;
    let mut star_is_double: bool = false;
    let mut dstar_ti: Option<usize> = None;
    let mut dstar_bi: usize = 0;
    while bi < blen {
        let ch: char = if bytes[bi] < 128 {
            char::from(bytes[bi])
        } else {
            text[bi..].chars().next().unwrap_or_default()
        };
        let ch_len: usize = ch.len_utf8();
        if ti < tlen {
            match tokens[ti] {
                GlobToken::DoubleStar => {
                    dstar_ti = Some(ti);
                    dstar_bi = bi;
                    star_ti = None;
                    ti += 1;
                    continue;
                }
                GlobToken::Star => {
                    star_ti = Some(ti);
                    star_bi = bi;
                    star_is_double = !path_aware;
                    ti += 1;
                    continue;
                }
                GlobToken::AnyChar if !path_aware || ch != PATH_SEP => {
                    ti += 1;
                    bi += ch_len;
                    continue;
                }
                GlobToken::Literal(expected) if expected == ch => {
                    ti += 1;
                    bi += ch_len;
                    continue;
                }
                GlobToken::Class(cls_idx)
                    if (!path_aware || ch != PATH_SEP)
                        && match_class_char(&classes[cls_idx as usize], ch) =>
                {
                    ti += 1;
                    bi += ch_len;
                    continue;
                }
                _ => {}
            }
        }
        if let Some(s_ti) = star_ti
            && (star_is_double || bytes[star_bi] != PATH_SEP_BYTE)
        {
            let step: usize = if bytes[star_bi] < 128 {
                1
            } else {
                text[star_bi..].chars().next().map_or(1, char::len_utf8)
            };
            star_bi += step;
            bi = star_bi;
            ti = s_ti + 1;
            continue;
        }
        if let Some(ds_ti) = dstar_ti {
            let step: usize = if bytes[dstar_bi] < 128 {
                1
            } else {
                text[dstar_bi..].chars().next().map_or(1, char::len_utf8)
            };
            dstar_bi += step;
            bi = dstar_bi;
            ti = ds_ti + 1;
            star_ti = None;
            continue;
        }
        return false;
    }
    while ti < tlen && matches!(tokens[ti], GlobToken::Star | GlobToken::DoubleStar) {
        ti += 1;
    }
    ti == tlen
}