ferralk-glob 0.9.2

Portable, byte-first glob matching for ferralk.
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
//! Bit-parallel Shift-And engine for the general match path.
//!
//! The general matcher explores the token/path state graph one state at a
//! time, bounded by the [`FailedStates`](crate::FailedStates) memo to
//! `tokens x path` visits. This module walks the same graph column by column
//! instead: every token expands to byte-consuming *positions*, the set of
//! reachable positions lives in a compact bitset, and each candidate byte
//! advances it one machine word at a time. The state is proportional to the
//! compiled pattern rather than the pattern-by-candidate product.
//!
//! This is not a regex engine and stays inside ADR-0013: there is no program
//! to dispatch, no captures, no search — only the token list this crate
//! already compiles, laid out as a Glushkov automaton whose transitions are
//! word-parallel. The semantics are the general matcher's, derived rule by
//! rule from [`Pattern::matches_from`](crate::Pattern::matches_from); the
//! differential tests and the fuzz harness hold the two engines equal.
//!
//! One engine serves every entry point. The component policy is the only rule
//! that changes between [`is_match`](crate::Pattern::is_match),
//! [`is_match_path`](crate::Pattern::is_match_path) and
//! [`is_match_glob_path`](crate::Pattern::is_match_glob_path), and it only
//! ever decides which wildcard positions may consume a separator byte — so
//! each policy is one precomputed block mask, chosen per call, while the byte
//! table and the star structure are shared.

use crate::{IrBudget, PatternError, PatternOptions, TOO_MUCH_COMPILED_IR, Token, is_separator};

/// Most byte-consuming positions the single-register sweep may hold.
///
/// The state word also carries one boundary past the last position (the
/// accept boundary), so 63 positions is what a `u64` holds. Longer patterns
/// use the multiword representation below.
const MAX_NARROW_POSITIONS: usize = 63;

/// What one compiled engine charges against the shared IR budget.
///
/// The engine is a fixed-size block — dominated by the 2 KiB byte table — so
/// it is charged as its size in [`Token`]-sized units, the currency the
/// budget already counts.
const NARROW_IR_UNITS: usize = size_of::<NarrowSweepEngine>().div_ceil(size_of::<Token>());

/// Persistent and temporary word rows allocated while compiling a wide sweep.
///
/// The byte table owns 256 rows, the engine keeps six policy/state rows, and
/// compilation uses two more rows for the wildcard sets. Charging the peak
/// before allocation keeps the existing compiled-IR budget meaningful for a
/// literal whose bytes expand to many sweep positions.
const WIDE_WORD_ROWS_AT_COMPILE: usize = 256 + 6 + 2;

/// A compiled Shift-And automaton for one alternative's token list.
///
/// Bit `p` of the state bitset is the *boundary* before position `p`: it is set
/// when the tokens up to that position have matched the candidate bytes
/// consumed so far. Bit `position_count` is the boundary after the last
/// position, which accepts once the candidate is exhausted.
///
/// The byte table answers "which positions consume this byte" with the
/// component and leading-dot policies left out; those depend on where in the
/// candidate the byte sits, so they are applied per byte as block masks. Case
/// folding and class membership are resolved into the table at compile time.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) enum SweepEngine {
    Narrow(Box<NarrowSweepEngine>),
    Wide(Box<WideSweepEngine>),
}

/// Mutable state of one sweep. Extglob repetition keeps one per alternative
/// and injects a new start boundary whenever the previous repetition reaches
/// the current candidate offset.
pub(crate) enum SweepState {
    Narrow(u64),
    Wide { state: Vec<u64>, next: Vec<u64> },
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct NarrowSweepEngine {
    /// Positions consuming each byte, before any policy mask.
    table: [u64; 256],
    /// Positions that repeat and may be skipped: every star-like token.
    stars: u64,
    /// Positions a separator byte never reaches under `component_wildcards`
    /// without `root_component_wildcards`: wildcards directly after an
    /// explicit separator, and `PathStar` wherever it stands.
    sep_block_component: u64,
    /// Positions a separator byte never reaches under both component options:
    /// every wildcard except the recursive stars.
    sep_block_glob: u64,
    /// Positions a `.` at a component start never reaches. Empty when hidden
    /// entries match; otherwise every wildcard position, literals exempt.
    dot_block: u64,
    /// Boundaries that accept once the candidate is exhausted: the final
    /// boundary, plus the boundary before a trailing `Separator` +
    /// `RecursiveStar` pair — `src/**` accepts `src` itself.
    accept: u64,
    /// The epsilon closure of the start boundary.
    initial: u64,
    /// The option the leading-dot rule was folded in under, pinned so a
    /// mismatched match-time option is caught in debug builds.
    match_hidden: bool,
    /// The option the byte table was folded under, pinned likewise.
    case_insensitive: bool,
}

impl SweepEngine {
    /// Compiles the automaton for `tokens`.
    ///
    /// Every token kind has a position encoding. Extglob programs never reach
    /// this — the caller keeps them on their own matcher.
    pub(crate) fn compile(
        tokens: &[Token],
        options: PatternOptions,
        budget: &mut IrBudget,
    ) -> Result<Option<Box<Self>>, PatternError> {
        let position_count = position_count(tokens)?;
        if position_count > MAX_NARROW_POSITIONS {
            return WideSweepEngine::compile(tokens, options, position_count, budget)
                .map(|engine| Some(Box::new(Self::Wide(Box::new(engine)))));
        }
        budget.charge(NARROW_IR_UNITS, 0)?;

        let mut engine = NarrowSweepEngine {
            table: [0_u64; 256],
            stars: 0,
            sep_block_component: 0,
            sep_block_glob: 0,
            dot_block: 0,
            accept: 1_u64 << position_count,
            initial: 0,
            match_hidden: options.match_hidden,
            case_insensitive: options.case_insensitive,
        };

        // Wildcard positions answer to the component and leading-dot
        // policies; the subset that consumes any byte at all is widened into
        // the table wholesale after the loop, while a class contributes
        // exactly its members.
        let mut wildcards = 0_u64;
        let mut consume_any = 0_u64;
        let mut position = 0_usize;
        for (token_index, token) in tokens.iter().enumerate() {
            let bit = 1_u64 << position;
            // The component policy asks whether the *token before this one*
            // is an explicit separator; a wildcard elsewhere in the pattern
            // stays free to cross separators under `component_wildcards`
            // alone. Mirrors `Pattern::component_wildcard`.
            let after_separator =
                token_index > 0 && matches!(tokens[token_index - 1], Token::Separator);
            match token {
                Token::Literal(literal) => {
                    for &expected in literal {
                        let bit = 1_u64 << position;
                        engine.table[usize::from(expected)] |= bit;
                        if options.case_insensitive {
                            engine.table[usize::from(expected.to_ascii_lowercase())] |= bit;
                            engine.table[usize::from(expected.to_ascii_uppercase())] |= bit;
                        }
                        position += 1;
                    }
                    // Literal positions are exempt from every policy mask:
                    // an escaped separator or a literal dot matches wherever
                    // it stands, exactly as `advance_literal` has it.
                    continue;
                }
                Token::Separator => {
                    engine.table[usize::from(b'/')] |= bit;
                    if cfg!(windows) {
                        engine.table[usize::from(b'\\')] |= bit;
                    }
                }
                Token::Any => {
                    wildcards |= bit;
                    consume_any |= bit;
                    if after_separator {
                        engine.sep_block_component |= bit;
                    }
                    engine.sep_block_glob |= bit;
                }
                Token::Class(class) => {
                    for byte in 0..=u8::MAX {
                        if class.matches(byte, options.case_insensitive) {
                            engine.table[usize::from(byte)] |= bit;
                        }
                    }
                    wildcards |= bit;
                    if after_separator {
                        engine.sep_block_component |= bit;
                    }
                    engine.sep_block_glob |= bit;
                }
                Token::Star => {
                    engine.stars |= bit;
                    wildcards |= bit;
                    consume_any |= bit;
                    if after_separator {
                        engine.sep_block_component |= bit;
                    }
                    engine.sep_block_glob |= bit;
                }
                // A non-recursive `**` in a path filter is component-local
                // wherever it stands, not only after a separator; see
                // `Token::PathStar` in `matches_from`.
                Token::PathStar => {
                    engine.stars |= bit;
                    wildcards |= bit;
                    consume_any |= bit;
                    engine.sep_block_component |= bit;
                    engine.sep_block_glob |= bit;
                }
                // Recursive stars cross separators under every policy; only
                // the leading-dot rule still binds them.
                Token::RecursiveStar | Token::RecursivePrefix => {
                    engine.stars |= bit;
                    wildcards |= bit;
                    consume_any |= bit;
                }
            }
            position += 1;
        }
        debug_assert_eq!(position, position_count);

        for entry in &mut engine.table {
            *entry |= consume_any;
        }
        engine.dot_block = if options.match_hidden { 0 } else { wildcards };

        // `src/**` accepts `src`: a candidate that ends where the separator
        // would be, with only the terminal recursive star behind it, matches.
        // The boundary before that separator position is therefore accepting.
        // Mirrors the end-of-path case in `matches_from`.
        if let [.., Token::Separator, Token::RecursiveStar] = tokens {
            engine.accept |= 1_u64 << (position_count - 2);
        }
        engine.initial = eclose(1, engine.stars);
        Ok(Some(Box::new(Self::Narrow(Box::new(engine)))))
    }

    /// Matches the entire candidate, byte by byte.
    ///
    /// The two component options are the only ones read at match time; the
    /// rest were folded into the tables when the pattern was compiled, and
    /// they never change between entry points of one [`Pattern`](crate::Pattern).
    pub(crate) fn is_match(&self, path: &[u8], options: PatternOptions) -> bool {
        let mut state = self.empty_state();
        self.inject_start(&mut state);
        let mut at_component_start = options.candidate_starts_component;
        for &byte in path {
            if !self.advance(&mut state, byte, at_component_start, options) {
                return false;
            }
            at_component_start = is_separator(byte);
        }
        self.accepts(&state)
    }

    pub(crate) fn matching_prefix_ends(
        &self,
        path: &[u8],
        options: PatternOptions,
        base: usize,
        output: &mut Vec<usize>,
    ) {
        let mut state = self.empty_state();
        self.inject_start(&mut state);
        if self.accepts(&state) {
            output.push(base);
        }
        let mut at_component_start = options.candidate_starts_component;
        for (offset, &byte) in path.iter().enumerate() {
            if !self.advance(&mut state, byte, at_component_start, options) {
                break;
            }
            if self.accepts(&state) {
                output.push(base + offset + 1);
            }
            at_component_start = is_separator(byte);
        }
    }

    pub(crate) fn empty_state(&self) -> SweepState {
        match self {
            Self::Narrow(_) => SweepState::Narrow(0),
            Self::Wide(engine) => SweepState::Wide {
                state: vec![0; engine.stars.len()],
                next: vec![0; engine.stars.len()],
            },
        }
    }

    pub(crate) fn inject_start(&self, state: &mut SweepState) {
        match (self, state) {
            (Self::Narrow(engine), SweepState::Narrow(state)) => *state |= engine.initial,
            (Self::Wide(engine), SweepState::Wide { state, .. }) => {
                for (state, initial) in state.iter_mut().zip(&engine.initial) {
                    *state |= *initial;
                }
            }
            _ => unreachable!("a sweep state belongs to its engine"),
        }
    }

    pub(crate) fn advance(
        &self,
        state: &mut SweepState,
        byte: u8,
        at_component_start: bool,
        options: PatternOptions,
    ) -> bool {
        match (self, state) {
            (Self::Narrow(engine), SweepState::Narrow(state)) => {
                *state = engine.advance(*state, byte, at_component_start, options);
                *state != 0
            }
            (Self::Wide(engine), SweepState::Wide { state, next }) => {
                engine.advance(state, next, byte, at_component_start, options)
            }
            _ => unreachable!("a sweep state belongs to its engine"),
        }
    }

    pub(crate) fn accepts(&self, state: &SweepState) -> bool {
        match (self, state) {
            (Self::Narrow(engine), SweepState::Narrow(state)) => state & engine.accept != 0,
            (Self::Wide(engine), SweepState::Wide { state, .. }) => state
                .iter()
                .zip(&engine.accept)
                .any(|(state, accept)| state & accept != 0),
            _ => unreachable!("a sweep state belongs to its engine"),
        }
    }
}

impl NarrowSweepEngine {
    fn advance(
        &self,
        state: u64,
        byte: u8,
        at_component_start: bool,
        options: PatternOptions,
    ) -> u64 {
        debug_assert_eq!(
            (options.match_hidden, options.case_insensitive),
            (self.match_hidden, self.case_insensitive),
            "sweep tables were folded under different options"
        );
        let sep_block = if !options.component_wildcards {
            0
        } else if options.root_component_wildcards {
            self.sep_block_glob
        } else {
            self.sep_block_component
        };

        let separator = is_separator(byte);
        let mut mask = self.table[usize::from(byte)];
        if separator {
            mask &= !sep_block;
        } else if byte == b'.' && at_component_start {
            mask &= !self.dot_block;
        }
        let consuming = state & mask;
        eclose(
            ((consuming & !self.stars) << 1) | (consuming & self.stars),
            self.stars,
        )
    }
}

/// Byte-consuming positions `tokens` expand to.
fn position_count(tokens: &[Token]) -> Result<usize, PatternError> {
    let mut count = 0_usize;
    for token in tokens {
        count = count
            .checked_add(match token {
                Token::Literal(literal) => literal.len(),
                _ => 1,
            })
            .ok_or_else(|| PatternError::new(0, TOO_MUCH_COMPILED_IR))?;
    }
    Ok(count)
}

/// A multiword Shift-And engine for alternatives too wide for one register.
///
/// Matching keeps one word per 64 pattern positions, independent of candidate
/// length. The byte table is contiguous by byte then word, so each candidate
/// byte touches only its own row and the compact policy/state rows.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct WideSweepEngine {
    table: Vec<u64>,
    stars: Vec<u64>,
    sep_block_component: Vec<u64>,
    sep_block_glob: Vec<u64>,
    dot_block: Vec<u64>,
    accept: Vec<u64>,
    initial: Vec<u64>,
    match_hidden: bool,
    case_insensitive: bool,
}

impl WideSweepEngine {
    fn compile(
        tokens: &[Token],
        options: PatternOptions,
        position_count: usize,
        budget: &mut IrBudget,
    ) -> Result<Self, PatternError> {
        let word_count = (position_count + 1).div_ceil(u64::BITS as usize);
        let peak_words = word_count
            .checked_mul(WIDE_WORD_ROWS_AT_COMPILE)
            .ok_or_else(|| PatternError::new(0, TOO_MUCH_COMPILED_IR))?;
        let peak_bytes = peak_words
            .checked_mul(size_of::<u64>())
            .ok_or_else(|| PatternError::new(0, TOO_MUCH_COMPILED_IR))?;
        budget.charge(peak_bytes.div_ceil(size_of::<Token>()), 0)?;

        let table_len = 256_usize
            .checked_mul(word_count)
            .ok_or_else(|| PatternError::new(0, TOO_MUCH_COMPILED_IR))?;
        let mut engine = Self {
            table: vec![0; table_len],
            stars: vec![0; word_count],
            sep_block_component: vec![0; word_count],
            sep_block_glob: vec![0; word_count],
            dot_block: vec![0; word_count],
            accept: vec![0; word_count],
            initial: vec![0; word_count],
            match_hidden: options.match_hidden,
            case_insensitive: options.case_insensitive,
        };
        set_bit(&mut engine.accept, position_count);

        let mut wildcards = vec![0_u64; word_count];
        let mut consume_any = vec![0_u64; word_count];
        let mut position = 0_usize;
        for (token_index, token) in tokens.iter().enumerate() {
            let after_separator =
                token_index > 0 && matches!(tokens[token_index - 1], Token::Separator);
            match token {
                Token::Literal(literal) => {
                    for &expected in literal {
                        engine.set_table(expected, position);
                        if options.case_insensitive {
                            engine.set_table(expected.to_ascii_lowercase(), position);
                            engine.set_table(expected.to_ascii_uppercase(), position);
                        }
                        position += 1;
                    }
                    continue;
                }
                Token::Separator => {
                    engine.set_table(b'/', position);
                    if cfg!(windows) {
                        engine.set_table(b'\\', position);
                    }
                }
                Token::Any => {
                    set_bit(&mut wildcards, position);
                    set_bit(&mut consume_any, position);
                    if after_separator {
                        set_bit(&mut engine.sep_block_component, position);
                    }
                    set_bit(&mut engine.sep_block_glob, position);
                }
                Token::Class(class) => {
                    for byte in 0..=u8::MAX {
                        if class.matches(byte, options.case_insensitive) {
                            engine.set_table(byte, position);
                        }
                    }
                    set_bit(&mut wildcards, position);
                    if after_separator {
                        set_bit(&mut engine.sep_block_component, position);
                    }
                    set_bit(&mut engine.sep_block_glob, position);
                }
                Token::Star => {
                    set_bit(&mut engine.stars, position);
                    set_bit(&mut wildcards, position);
                    set_bit(&mut consume_any, position);
                    if after_separator {
                        set_bit(&mut engine.sep_block_component, position);
                    }
                    set_bit(&mut engine.sep_block_glob, position);
                }
                Token::PathStar => {
                    set_bit(&mut engine.stars, position);
                    set_bit(&mut wildcards, position);
                    set_bit(&mut consume_any, position);
                    set_bit(&mut engine.sep_block_component, position);
                    set_bit(&mut engine.sep_block_glob, position);
                }
                Token::RecursiveStar | Token::RecursivePrefix => {
                    set_bit(&mut engine.stars, position);
                    set_bit(&mut wildcards, position);
                    set_bit(&mut consume_any, position);
                }
            }
            position += 1;
        }
        debug_assert_eq!(position, position_count);

        for row in engine.table.chunks_exact_mut(word_count) {
            for (word, any) in row.iter_mut().zip(&consume_any) {
                *word |= *any;
            }
        }
        if !options.match_hidden {
            engine.dot_block.copy_from_slice(&wildcards);
        }
        if let [.., Token::Separator, Token::RecursiveStar] = tokens {
            set_bit(&mut engine.accept, position_count - 2);
        }
        set_bit(&mut engine.initial, 0);
        eclose_wide(&mut engine.initial, &engine.stars);
        Ok(engine)
    }

    fn set_table(&mut self, byte: u8, position: usize) {
        let word_count = self.stars.len();
        let word = position / u64::BITS as usize;
        let bit = position % u64::BITS as usize;
        self.table[usize::from(byte) * word_count + word] |= 1_u64 << bit;
    }

    fn advance(
        &self,
        state: &mut Vec<u64>,
        next: &mut Vec<u64>,
        byte: u8,
        at_component_start: bool,
        options: PatternOptions,
    ) -> bool {
        debug_assert_eq!(
            (options.match_hidden, options.case_insensitive),
            (self.match_hidden, self.case_insensitive),
            "sweep tables were folded under different options"
        );
        let sep_block = if !options.component_wildcards {
            None
        } else if options.root_component_wildcards {
            Some(self.sep_block_glob.as_slice())
        } else {
            Some(self.sep_block_component.as_slice())
        };

        let word_count = self.stars.len();
        let separator = is_separator(byte);
        let row_start = usize::from(byte) * word_count;
        let row = &self.table[row_start..row_start + word_count];
        let blocked = if separator {
            sep_block
        } else if byte == b'.' && at_component_start {
            Some(self.dot_block.as_slice())
        } else {
            None
        };

        let mut carry = 0_u64;
        let mut any = false;
        for index in 0..word_count {
            let mask = row[index] & !blocked.map_or(0, |bits| bits[index]);
            let consuming = state[index] & mask;
            let advancing = consuming & !self.stars[index];
            let shifted = (advancing << 1) | carry;
            carry = advancing >> (u64::BITS - 1);
            next[index] = shifted | (consuming & self.stars[index]);
            any |= next[index] != 0;
        }
        if !any {
            state.fill(0);
            return false;
        }
        eclose_wide(next, &self.stars);
        std::mem::swap(state, next);
        next.fill(0);
        true
    }
}

fn set_bit(bits: &mut [u64], position: usize) {
    bits[position / u64::BITS as usize] |= 1_u64 << (position % u64::BITS as usize);
}

/// Multiword form of [`eclose`], using the same addition identity over the
/// complete little-endian bitset and carrying between machine words.
fn eclose_wide(state: &mut [u64], stars: &[u64]) {
    let mut carry = false;
    for (state, &stars) in state.iter_mut().zip(stars) {
        let (sum, first_carry) = stars.overflowing_add(*state & stars);
        let (sum, second_carry) = sum.overflowing_add(u64::from(carry));
        *state |= sum ^ stars;
        carry = first_carry || second_carry;
    }
}

/// Epsilon closure: propagates each boundary upward through runs of stars.
///
/// A star position may match zero bytes, so a boundary before it is also a
/// boundary after it. Within one maximal run of star bits the closure of a
/// set bit is everything from that bit to just past the run, and the carry of
/// a single addition walks exactly that span: adding the run's own mask to
/// the set bits ripples from the lowest set bit of each run to the first zero
/// above it, and the XOR recovers every bit the ripple flipped. Bits the
/// ripple stepped over without flipping are set in `state` already, which the
/// union restores. Runs without a set bit add their own mask back unchanged
/// and cancel in the XOR.
///
/// The addition cannot overflow: star bits sit below [`MAX_POSITIONS`], so
/// the highest carry lands on bit 63.
const fn eclose(state: u64, stars: u64) -> u64 {
    state | ((stars + (state & stars)) ^ stars)
}

#[cfg(test)]
mod tests {
    use super::{MAX_NARROW_POSITIONS, SweepEngine, eclose, position_count};
    use crate::{IrBudget, PatternOptions, Token};

    /// The closure spelled as the loop the bit trick replaces.
    fn eclose_reference(state: u64, stars: u64) -> u64 {
        let mut closed = state;
        loop {
            let grown = closed | (closed & stars) << 1;
            if grown == closed {
                return closed;
            }
            closed = grown;
        }
    }

    #[test]
    fn eclose_matches_the_reference_loop_over_generated_masks() {
        // A multiplicative generator covers runs, gaps, and multiple set bits
        // per run without depending on a random-number crate.
        let mut seed = 0x9E37_79B9_7F4A_7C15_u64;
        let mut next = || {
            seed = seed.wrapping_mul(0x2545_F491_4F6C_DD1D).wrapping_add(1);
            seed
        };
        for _ in 0..10_000 {
            // Narrow star bits stay below the single-word cap.
            let stars = next() & ((1 << MAX_NARROW_POSITIONS) - 1);
            let state = next();
            assert_eq!(
                eclose(state, stars),
                eclose_reference(state, stars),
                "closure diverges for state {state:#x} over stars {stars:#x}"
            );
        }
    }

    #[test]
    fn eclose_handles_runs_touching_the_position_cap() {
        let stars = ((1_u64 << MAX_NARROW_POSITIONS) - 1) & !1;
        assert_eq!(
            eclose(1 << 1, stars) & (1 << MAX_NARROW_POSITIONS),
            1 << MAX_NARROW_POSITIONS,
            "a run ending at the cap must close into the accept boundary"
        );
    }

    #[test]
    fn position_counting_respects_the_cap() {
        let short = vec![Token::Literal(vec![b'a'; 60]), Token::Star, Token::Any];
        assert_eq!(position_count(&short), Ok(62));
        let exact = vec![Token::Literal(vec![b'a'; 63])];
        assert_eq!(position_count(&exact), Ok(63));
        let long = vec![Token::Literal(vec![b'a'; 63]), Token::Any];
        assert_eq!(position_count(&long), Ok(64));

        let mut budget = IrBudget::new();
        assert!(
            SweepEngine::compile(&long, PatternOptions::default(), &mut budget)
                .expect("the wide sweep is valid")
                .is_some(),
            "an oversized pattern must compile to the wide sweep"
        );
    }

    #[test]
    fn wide_eclose_carries_across_word_boundaries() {
        let mut state = vec![0, 0];
        let mut stars = vec![0, 0];
        super::set_bit(&mut state, 62);
        for position in 62..=66 {
            super::set_bit(&mut stars, position);
        }
        super::eclose_wide(&mut state, &stars);
        assert_ne!(state[1] & (1 << 3), 0, "closure must reach boundary 67");
    }
}