ferroni 2.8.0

Oniguruma regex in pure Rust, with a TextMate scanner, portable prepared Eager archives, and no C toolchain
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
//! Indexed, versioned scanner programs. See ADR-018 for the compatibility contract.
mod bytes;
pub use bytes::PrecompiledBytes;
mod schema;
mod validate;
mod wire;

use super::*;
use std::ops::Range;
use wire::{Reader, Wire};

/// Version of the compiled program format, independent of the crate version.
/// A bytecode, Unicode-data, seek, or optimizer semantic change must bump it.
pub const PRECOMPILED_FORMAT_VERSION: u32 = 2;
const MAGIC: &[u8; 8] = b"FERRONI\0";
const MAX_BLOB: usize = 256 * 1024 * 1024;
const MAX_PROGRAM: usize = 16 * 1024 * 1024;
const MAX_ENTRIES: usize = 100_000;
type Result<T> = std::result::Result<T, PrecompiledError>;

/// An incompatible, malformed, oversized, or nonportable precompiled archive.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum PrecompiledError {
    /// The magic or bytecode format version is not supported.
    UnsupportedFormat,
    /// The index, checksum, program, or seek failed validation.
    InvalidData,
    /// A format size, allocation, or program complexity bound was exceeded.
    ResourceLimit,
    /// The program uses process-local callbacks and cannot be exported.
    NonportablePattern,
}
impl fmt::Display for PrecompiledError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::UnsupportedFormat => "unsupported Ferroni precompiled format",
            Self::InvalidData => "invalid Ferroni precompiled data",
            Self::ResourceLimit => "Ferroni precompiled resource limit exceeded",
            Self::NonportablePattern => "pattern contains process-local callouts",
        })
    }
}
impl std::error::Error for PrecompiledError {}

/// An owned archive indexed by pattern text and compile settings. Opening it
/// checks only its header and index; programs are decoded and validated one at
/// a time, when requested. Clones share the bytes and index.
///
/// Enable the `precompiled` Cargo feature to use this API. Export with
/// [`ScannerPatternCache::export_precompiled`] and attach with
/// [`ScannerPatternCache::add_precompiled`]. Use archives from a trusted
/// producer: validation checks structure, bounds, and corruption, not whether a
/// producer's program implements the pattern text it claims to implement.
/// Search limits continue to apply as they do to compiled patterns.
#[derive(Clone)]
pub struct PrecompiledPatterns {
    bytes: PrecompiledBytes,
    index: Arc<HashMap<Vec<u8>, Entry>>,
}
#[derive(Clone)]
struct Entry {
    range: Range<usize>,
    checksum: u32,
}
impl fmt::Debug for PrecompiledPatterns {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("PrecompiledPatterns")
            .field("len", &self.len())
            .field("bytes", &self.bytes.len())
            .finish()
    }
}

// Retained on each compiled entry, so exporting after a process-global setting
// change cannot label old bytecode with the new environment. Callout registries
// need no fingerprint: those programs are deliberately not portable.
#[derive(Clone, PartialEq, Eq)]
pub(super) struct Environment {
    fold: u32,
    captures: i32,
    nesting: u32,
    nodes: u32,
    prefilter: bool,
    properties: Vec<(Vec<u8>, Vec<u32>)>,
}
impl Environment {
    pub(super) fn current() -> Self {
        Self {
            fold: crate::regcomp::onig_get_default_case_fold_flag(),
            captures: crate::regparse::capture_num_limit(),
            nesting: crate::regparse::onig_get_parse_depth_limit(),
            nodes: crate::regparse::onig_get_ast_node_limit(),
            prefilter: cfg!(feature = "dfa-prefilter"),
            properties: crate::unicode::precompiled_user_properties(),
        }
    }
    fn write(&self, out: &mut Vec<u8>) -> Result<()> {
        self.fold.write(out)?;
        self.captures.write(out)?;
        self.nesting.write(out)?;
        self.nodes.write(out)?;
        self.prefilter.write(out)?;
        self.properties.write(out)
    }
    fn read(input: &mut Reader<'_>) -> Result<Self> {
        Ok(Self {
            fold: Wire::read(input)?,
            captures: Wire::read(input)?,
            nesting: Wire::read(input)?,
            nodes: Wire::read(input)?,
            prefilter: Wire::read(input)?,
            properties: Wire::read(input)?,
        })
    }
}
pub(super) fn key(pattern: &str, settings: PatternSettings, env: &Environment) -> Result<Vec<u8>> {
    let mut key = Vec::new();
    settings.options.bits().write(&mut key)?;
    syntax_tag(settings.syntax).write(&mut key)?;
    settings.optimize_backtracking.write(&mut key)?;
    settings.prefilter.write(&mut key)?;
    env.write(&mut key)?;
    pattern.to_owned().write(&mut key)?;
    Ok(key)
}
fn syntax_tag(syntax: Syntax) -> u8 {
    match syntax {
        Syntax::Oniguruma => 0,
        Syntax::Asis => 1,
        Syntax::PosixBasic => 2,
        Syntax::PosixExtended => 3,
        Syntax::Emacs => 4,
        Syntax::Grep => 5,
        Syntax::GnuRegex => 6,
        Syntax::Java => 7,
        Syntax::Perl => 8,
        Syntax::PerlNg => 9,
        Syntax::Ruby => 10,
        Syntax::Python => 11,
    }
}
pub(super) fn check_key(bytes: &[u8]) -> Result<()> {
    key_settings(bytes).map(|_| ())
}

pub(super) fn key_settings(bytes: &[u8]) -> Result<PatternSettings> {
    let mut input = Reader::new(bytes);
    let options =
        OnigOptionType::from_bits(u32::read(&mut input)?).ok_or(PrecompiledError::InvalidData)?;
    let syntax = match u8::read(&mut input)? {
        0 => Syntax::Oniguruma,
        1 => Syntax::Asis,
        2 => Syntax::PosixBasic,
        3 => Syntax::PosixExtended,
        4 => Syntax::Emacs,
        5 => Syntax::Grep,
        6 => Syntax::GnuRegex,
        7 => Syntax::Java,
        8 => Syntax::Perl,
        9 => Syntax::PerlNg,
        10 => Syntax::Ruby,
        11 => Syntax::Python,
        _ => return Err(PrecompiledError::InvalidData),
    };
    let optimize_backtracking = bool::read(&mut input)?;
    let prefilter = bool::read(&mut input)?;
    Environment::read(&mut input)?;
    String::read(&mut input)?;
    input.done()?;
    Ok(PatternSettings {
        options,
        syntax,
        optimize_backtracking,
        prefilter,
        prefilter_warmup: 0,
    })
}
// A corruption check, not authentication. The structural validator still runs
// after it, including for a maliciously recomputed checksum. FNV-1a over
// little-endian 64-bit words with an extra shift per word, then the tail bytes,
// folded to 32 bits: every load checks its program, and a byte-wise hash made
// that a third of the load time (format version 1).
pub(super) fn checksum(bytes: &[u8]) -> u32 {
    const PRIME: u64 = 0x0000_0100_0000_01b3;
    let mut hash = 0xcbf2_9ce4_8422_2325_u64;
    let (words, tail) = bytes.as_chunks::<8>();
    for word in words {
        hash = (hash ^ u64::from_le_bytes(*word)).wrapping_mul(PRIME);
        hash ^= hash >> 29;
    }
    for &byte in tail {
        hash = (hash ^ u64::from(byte)).wrapping_mul(PRIME);
    }
    (hash ^ (hash >> 32)) as u32
}
impl PrecompiledPatterns {
    #[cfg(feature = "precompiled-eager")]
    pub(super) fn records(&self) -> Vec<(Vec<u8>, Range<usize>, u32)> {
        let mut entries: Vec<_> = self
            .index
            .iter()
            .map(|(key, entry)| (key.clone(), entry.range.clone(), entry.checksum))
            .collect();
        entries.sort_by(|a, b| a.0.cmp(&b.0));
        entries
    }
    #[cfg(feature = "precompiled-eager")]
    pub(super) fn archive_bytes(&self) -> &[u8] {
        &self.bytes
    }
    #[cfg(feature = "scanner-group-nfa-prototype")]
    pub(super) fn prototype_checksum(&self, parallel: bool) -> u32 {
        #[cfg(feature = "scanner-load-profile")]
        let _phase = crate::scanner::load_profile::scope(
            crate::scanner::load_profile::Phase::GroupBindingChecksum,
        );
        super::group_prototype::record_checksum(&self.bytes, parallel)
    }
    #[cfg(feature = "scanner-group-nfa-prototype")]
    pub(super) fn prototype_keys(&self) -> Vec<Vec<u8>> {
        let mut keys: Vec<_> = self.index.keys().cloned().collect();
        keys.sort();
        keys
    }
    /// Open an archive, retaining its bytes and indexing its entries. No regex
    /// or pre-filter seek is decoded here. Unsupported versions and malformed
    /// indexes return an error; a bad individual program is reported by
    /// [`validate_pattern`](Self::validate_pattern), or ignored by the cache.
    pub fn from_bytes(bytes: impl Into<Arc<[u8]>>) -> Result<Self> {
        Self::from_shared_bytes(PrecompiledBytes::from(bytes.into()))
    }

    /// Open a shared byte image without copying it into an `Arc<[u8]>`.
    ///
    /// Validation and lazy decoding are identical to [`Self::from_bytes`].
    /// The retained view keeps its owner alive until its final clone is dropped.
    pub fn from_shared_bytes(bytes: PrecompiledBytes) -> Result<Self> {
        #[cfg(feature = "scanner-load-profile")]
        let _phase =
            crate::scanner::load_profile::scope(crate::scanner::load_profile::Phase::ProgramIndex);
        if bytes.len() > MAX_BLOB {
            return Err(PrecompiledError::ResourceLimit);
        }
        let mut input = Reader::new(&bytes);
        if input.take(8)? != MAGIC || u32::read(&mut input)? != PRECOMPILED_FORMAT_VERSION {
            return Err(PrecompiledError::UnsupportedFormat);
        }
        let index_len = usize::read(&mut input)?;
        let mut index_input = Reader::new(input.take(index_len)?);
        let count = usize::read(&mut index_input)?;
        if count > MAX_ENTRIES {
            return Err(PrecompiledError::ResourceLimit);
        }
        let data_start = bytes.len() - input.bytes.len();
        let mut index = HashMap::new();
        let mut expected = data_start;
        for _ in 0..count {
            let key = Vec::<u8>::read(&mut index_input)?;
            check_key(&key)?;
            let offset = usize::read(&mut index_input)?;
            let len = usize::read(&mut index_input)?;
            let checksum = u32::read(&mut index_input)?;
            if len > MAX_PROGRAM {
                return Err(PrecompiledError::ResourceLimit);
            }
            let start = data_start
                .checked_add(offset)
                .ok_or(PrecompiledError::InvalidData)?;
            let end = start
                .checked_add(len)
                .ok_or(PrecompiledError::InvalidData)?;
            if start != expected || end > bytes.len() || len == 0 {
                return Err(PrecompiledError::InvalidData);
            }
            expected = end;
            if index
                .insert(
                    key,
                    Entry {
                        range: start..end,
                        checksum,
                    },
                )
                .is_some()
            {
                return Err(PrecompiledError::InvalidData);
            }
        }
        index_input.done()?;
        if expected != bytes.len() {
            return Err(PrecompiledError::InvalidData);
        }
        Ok(Self {
            bytes,
            index: Arc::new(index),
        })
    }
    /// Number of indexed programs, including ones not decoded yet.
    pub fn len(&self) -> usize {
        self.index.len()
    }
    /// Whether the archive indexes no programs.
    pub fn is_empty(&self) -> bool {
        self.index.is_empty()
    }
    /// Decode and validate the matching entry without retaining it. Returns
    /// `false` if the text, configuration, or process-wide compile settings do
    /// not match an entry. Use this for diagnostics; cache misses automatically
    /// fall back to compilation on an entry error.
    ///
    /// `optimize_backtracking` selects the experimental rewrite mode used by
    /// [`Scanner::with_backtracking_optimization_and_pattern_cache`].
    pub fn validate_pattern(
        &self,
        pattern: &str,
        config: &ScannerConfig,
        optimize_backtracking: bool,
    ) -> Result<bool> {
        self.load(
            pattern,
            PatternSettings::of(config, optimize_backtracking),
            &Environment::current(),
        )
        .map(|reg| reg.is_some())
    }
    pub(super) fn load(
        &self,
        pattern: &str,
        settings: PatternSettings,
        env: &Environment,
    ) -> Result<Option<(Arc<RegexType>, SharedSeek)>> {
        #[cfg(feature = "scanner-load-profile")]
        let _phase =
            crate::scanner::load_profile::scope(crate::scanner::load_profile::Phase::ProgramLookup);
        let Some(entry) = self.index.get(&key(pattern, settings, env)?) else {
            return Ok(None);
        };
        let bytes = &self.bytes[entry.range.clone()];
        decode_record(bytes, entry.checksum, settings).map(Some)
    }
}

pub(super) fn decode_record(
    bytes: &[u8],
    expected_checksum: u32,
    settings: PatternSettings,
) -> Result<(Arc<RegexType>, SharedSeek)> {
    let intact = {
        #[cfg(feature = "scanner-load-profile")]
        let _phase = crate::scanner::load_profile::scope(
            crate::scanner::load_profile::Phase::ProgramChecksum,
        );
        checksum(bytes) == expected_checksum
    };
    if !intact {
        return Err(PrecompiledError::InvalidData);
    }
    let mut input = Reader::new(bytes);
    let reg = schema::read_reg(&mut input, settings)?;
    // Without the DFA feature, SharedSeek is the unit placeholder.
    #[cfg_attr(not(feature = "dfa-prefilter"), allow(clippy::let_unit_value))]
    let seek = schema::read_seek(&mut input)?;
    input.done()?;
    Ok((Arc::new(reg), seek))
}

#[derive(Clone)]
pub(super) enum ProgramSource {
    Archive(PrecompiledPatterns),
    #[cfg(feature = "precompiled-eager")]
    Records(super::PrecompiledEagerRecords),
}

impl ProgramSource {
    pub(super) fn load(
        &self,
        pattern: &str,
        settings: PatternSettings,
        env: &Environment,
    ) -> Result<Option<(Arc<RegexType>, SharedSeek)>> {
        match self {
            Self::Archive(source) => source.load(pattern, settings, env),
            #[cfg(feature = "precompiled-eager")]
            Self::Records(source) => source.load_program(pattern, settings, env),
        }
    }

    #[cfg(feature = "scanner-group-nfa-prototype")]
    pub(super) fn archive(&self) -> Option<&PrecompiledPatterns> {
        match self {
            Self::Archive(source) => Some(source),
            #[cfg(feature = "precompiled-eager")]
            Self::Records(_) => None,
        }
    }
}
impl ScannerPatternCache {
    /// Attach a source to consult on cache misses. Existing decoded entries
    /// stay in use. Sources are tried in insertion order; absent, incompatible,
    /// or corrupt entries fall back to the next source and then the compiler.
    /// Attaching a source never compiles or decodes a pattern.
    ///
    /// ```
    /// # #[cfg(feature = "precompiled")] {
    /// use ferroni::{Scanner, ScannerConfig, ScannerPatternCache};
    /// use ferroni::scanner::PrecompiledPatterns;
    /// let config = ScannerConfig::default();
    /// let mut build = ScannerPatternCache::new();
    /// Scanner::with_pattern_cache(&[r"\w+", r"\d+"], &config, &mut build)?;
    /// let blob = build.export_precompiled()?;
    /// let mut runtime = ScannerPatternCache::new();
    /// runtime.add_precompiled(PrecompiledPatterns::from_bytes(blob)?);
    /// assert!(runtime.is_empty()); // Only the index has been loaded.
    /// let scanner = Scanner::with_pattern_cache(&[r"\d+"], &config, &mut runtime)?;
    /// assert_eq!(runtime.len(), 1);
    /// # }
    /// # Ok::<(), Box<dyn std::error::Error>>(())
    /// ```
    pub fn add_precompiled(&mut self, source: PrecompiledPatterns) {
        self.precompiled.push(ProgramSource::Archive(source));
    }
    /// Remove attached archives, keeping decoded programs and existing scanners.
    /// [`clear`](Self::clear) clears decoded programs and automata but retains
    /// these sources, so subsequent misses can load them again.
    pub fn clear_precompiled(&mut self) {
        self.precompiled.clear();
        #[cfg(feature = "scanner-group-nfa-prototype")]
        {
            self.group_source = None;
        }
    }
    /// Export the cache's decoded programs in deterministic pattern/settings
    /// order. Callout programs are omitted: their callbacks belong to the
    /// producing process. Missing and runtime-resolved patterns compile normally.
    /// The archive carries bytecode, compact maps and seeks; lazy automata are
    /// built at runtime. Export can fail if a format resource bound is exceeded.
    pub fn export_precompiled(&self) -> Result<Vec<u8>> {
        let mut programs = Vec::new();
        for (settings, patterns) in &self.compiled {
            for (pattern, cached) in patterns {
                if cached.reg.extp.as_ref().is_some_and(|e| e.callout_num != 0) {
                    continue;
                }
                let key = key(pattern, *settings, &cached.environment)?;
                let mut bytes = Vec::new();
                schema::write_reg(&cached.reg, &mut bytes)?;
                schema::write_seek(&cached.seek, &mut bytes)?;
                if bytes.len() > MAX_PROGRAM {
                    return Err(PrecompiledError::ResourceLimit);
                }
                programs.push((key, bytes));
            }
        }
        programs.sort_by(|a, b| a.0.cmp(&b.0));
        // Warmup changes automata scheduling, not compiled programs. Two cache
        // settings that differ only in warmup export one identical program.
        programs.dedup_by(|a, b| a.0 == b.0);
        if programs.len() > MAX_ENTRIES {
            return Err(PrecompiledError::ResourceLimit);
        }
        let mut index = Vec::new();
        programs.len().write(&mut index)?;
        let mut offset = 0usize;
        for (key, bytes) in &programs {
            key.write(&mut index)?;
            offset.write(&mut index)?;
            bytes.len().write(&mut index)?;
            checksum(bytes).write(&mut index)?;
            offset = offset
                .checked_add(bytes.len())
                .ok_or(PrecompiledError::ResourceLimit)?;
        }
        if index.len().saturating_add(offset).saturating_add(16) > MAX_BLOB {
            return Err(PrecompiledError::ResourceLimit);
        }
        let mut out = MAGIC.to_vec();
        PRECOMPILED_FORMAT_VERSION.write(&mut out)?;
        index.len().write(&mut out)?;
        out.extend(index);
        for (_, bytes) in programs {
            out.extend(bytes);
        }
        Ok(out)
    }
}

#[cfg(test)]
thread_local! { pub(super) static COMPILES: std::cell::Cell<usize> = const { std::cell::Cell::new(0) }; }
#[cfg(test)]
mod tests {
    use super::*;
    #[test]
    fn multibyte_optional_regression() {
        let config = ScannerConfig::default();
        let source = PrecompiledPatterns::from_bytes(archive(&["(?:雪+)??霜"], &config)).unwrap();
        let entry = source.index.values().next().unwrap();
        let mut input = Reader::new(&source.bytes[entry.range.clone()]);
        schema::read_reg(&mut input, PatternSettings::of(&config, false)).expect("regex");
        schema::read_seek(&mut input).expect("seek");
    }
    #[test]
    fn format_v2_literal_schema_is_pinned() {
        let settings = PatternSettings::of(&ScannerConfig::default(), false);
        let (reg, seek) = settings.compile("a").unwrap();
        let mut bytes = Vec::new();
        schema::write_reg(&reg, &mut bytes).unwrap();
        assert_eq!(PRECOMPILED_FORMAT_VERSION, 2);
        assert_eq!((checksum(&bytes), bytes.len()), (3_233_327_443, 240));
        let mut bytes = Vec::new();
        schema::write_seek(&seek, &mut bytes).unwrap();
        #[cfg(feature = "dfa-prefilter")]
        assert_eq!(bytes, [2, 4, 0, 0, 0, 1, 1, 1, 97]);
        #[cfg(not(feature = "dfa-prefilter"))]
        assert_eq!(bytes, [0]);
    }
    fn archive(patterns: &[&str], config: &ScannerConfig) -> Vec<u8> {
        let mut cache = ScannerPatternCache::new();
        Scanner::with_pattern_cache(patterns, config, &mut cache).unwrap();
        cache.export_precompiled().unwrap()
    }
    #[test]
    fn indexes_without_decoding_and_loads_only_requested_programs() {
        let config = ScannerConfig::default();
        let source = PrecompiledPatterns::from_bytes(archive(
            &[r"\w+", r"\d+", "alpha|beta|gamma|delta"],
            &config,
        ))
        .unwrap();
        assert_eq!(source.len(), 3);
        let mut cache = ScannerPatternCache::new();
        cache.add_precompiled(source);
        assert!(cache.is_empty());
        COMPILES.with(|n| n.set(0));
        let mut scanner = Scanner::with_pattern_cache(&[r"\d+"], &config, &mut cache).unwrap();
        assert_eq!(COMPILES.with(|n| n.get()), 0);
        assert_eq!(cache.len(), 1);
        let m = scanner
            .find_next_match("a42", 0, ScannerFindOptions::NONE)
            .unwrap();
        assert_eq!((m.captures()[0].start, m.captures()[0].end), (1, 3));
        cache.clear();
        Scanner::with_pattern_cache(&[r"\d+"], &config, &mut cache).unwrap();
        assert_eq!(COMPILES.with(|n| n.get()), 0);
        cache.clear();
        cache.clear_precompiled();
        Scanner::with_pattern_cache(&[r"\d+"], &config, &mut cache).unwrap();
        assert_eq!(COMPILES.with(|n| n.get()), 1);
        assert!(
            scanner
                .find_next_match("42", 0, ScannerFindOptions::NONE)
                .is_some()
        );
    }
    #[test]
    fn corrupt_unused_program_stays_lazy_and_requested_program_falls_back() {
        let config = ScannerConfig::default();
        let mut bytes = archive(&["abc", "def"], &config);
        let source = PrecompiledPatterns::from_bytes(bytes.clone()).unwrap();
        let damaged = source
            .index
            .get(
                &key(
                    "def",
                    PatternSettings::of(&config, false),
                    &Environment::current(),
                )
                .unwrap(),
            )
            .unwrap()
            .range
            .start;
        bytes[damaged] ^= 0x40;
        let source = PrecompiledPatterns::from_bytes(bytes).unwrap();
        assert_eq!(
            source.validate_pattern("def", &config, false),
            Err(PrecompiledError::InvalidData)
        );
        assert_eq!(source.validate_pattern("abc", &config, false), Ok(true));
        let mut cache = ScannerPatternCache::new();
        cache.add_precompiled(source);
        COMPILES.with(|n| n.set(0));
        Scanner::with_pattern_cache(&["abc"], &config, &mut cache).unwrap();
        assert_eq!(COMPILES.with(|n| n.get()), 0);
        Scanner::with_pattern_cache(&["def"], &config, &mut cache).unwrap();
        assert_eq!(COMPILES.with(|n| n.get()), 1);
    }
    #[test]
    fn compile_modes_and_environment_do_not_alias() {
        let config = ScannerConfig::default();
        let source = PrecompiledPatterns::from_bytes(archive(&["a+"], &config)).unwrap();
        assert_eq!(
            source.validate_pattern("a+", &config.clone().options(ONIG_OPTION_IGNORECASE), false),
            Ok(false)
        );
        assert_eq!(
            source.validate_pattern("a+", &config.clone().syntax(ScannerSyntax::Ruby), false),
            Ok(false)
        );
        assert_eq!(source.validate_pattern("a+", &config, true), Ok(false));
        assert_eq!(
            source.validate_pattern("a+", &config.clone().prefilter_warmup(17), false),
            Ok(true)
        );
        let mut env = Environment::current();
        env.fold ^= 1;
        assert!(
            source
                .load("a+", PatternSettings::of(&config, false), &env)
                .unwrap()
                .is_none()
        );
    }

    #[test]
    fn eager_acceleration_uses_existing_archives_without_compiling() {
        let _limits = crate::regexec::shared_limits();
        let config = ScannerConfig::default();
        let patterns = &[r"\d+", r"[a-z]+"];
        let bytes = archive(patterns, &config);
        let source = PrecompiledPatterns::from_bytes(bytes.clone()).unwrap();
        let eager = config.acceleration(crate::AccelerationMode::Eager);
        let mut cache = ScannerPatternCache::new();
        cache.add_precompiled(source);
        COMPILES.with(|n| n.set(0));
        let mut scanner = Scanner::with_pattern_cache(patterns, &eager, &mut cache).unwrap();
        assert_eq!(COMPILES.with(|n| n.get()), 0);
        assert_eq!(
            scanner.prefilter_stats().built,
            cfg!(feature = "dfa-prefilter")
        );
        // Eager changes runtime preparation, not the archive schema or bytes.
        assert_eq!(cache.export_precompiled().unwrap(), bytes);
        assert_eq!(
            scanner
                .find_next_match("abc 42", 0, ScannerFindOptions::NONE)
                .unwrap()
                .index,
            1
        );
    }
    #[test]
    fn deterministic_export_and_rewrite_reports() {
        let config = ScannerConfig::default();
        assert_eq!(
            archive(&["abc", "def"], &config),
            archive(&["def", "abc"], &config)
        );
        let patterns = [r"((\d+)_?)+\.\d+\b", "(a+)+$"];
        let mut cache = ScannerPatternCache::new();
        let before = Scanner::with_backtracking_optimization_and_pattern_cache(
            &patterns, &config, &mut cache,
        )
        .unwrap();
        let source = PrecompiledPatterns::from_bytes(cache.export_precompiled().unwrap()).unwrap();
        for p in patterns {
            assert_eq!(source.validate_pattern(p, &config, true), Ok(true), "{p}");
        }
        let mut runtime = ScannerPatternCache::new();
        runtime.add_precompiled(source);
        COMPILES.with(|n| n.set(0));
        let after = Scanner::with_backtracking_optimization_and_pattern_cache(
            &patterns,
            &config,
            &mut runtime,
        )
        .unwrap();
        assert_eq!(COMPILES.with(|n| n.get()), 0);
        assert_eq!(before.warnings(), after.warnings());
        assert_eq!(
            before.backtracking_rewrites(),
            after.backtracking_rewrites()
        );
    }
    #[test]
    fn rejects_version_truncation_and_oversized_index() {
        let config = ScannerConfig::default();
        let bytes = archive(&["abc"], &config);
        for len in 0..bytes.len() {
            assert!(
                PrecompiledPatterns::from_bytes(bytes[..len].to_vec()).is_err(),
                "{len}"
            );
        }
        let mut version = bytes.clone();
        version[8..12].copy_from_slice(&u32::MAX.to_le_bytes());
        assert_eq!(
            PrecompiledPatterns::from_bytes(version).unwrap_err(),
            PrecompiledError::UnsupportedFormat
        );
        let mut count = bytes;
        count[16..20].copy_from_slice(&u32::MAX.to_le_bytes());
        assert_eq!(
            PrecompiledPatterns::from_bytes(count).unwrap_err(),
            PrecompiledError::ResourceLimit
        );
    }
    #[test]
    fn binary_and_bmh_maps_round_trip_compactly() {
        for map in [
            [0; 256],
            [1; 256],
            std::array::from_fn(|i| (i % 2) as u8),
            [7; 256],
            std::array::from_fn(|i| i as u8),
        ] {
            let mut out = Vec::new();
            wire::write_map(&map, &mut out).unwrap();
            assert_eq!(wire::read_map(&mut Reader::new(&out)).unwrap(), map);
            if map.iter().all(|b| *b <= 1) {
                assert_eq!(out.len(), 33);
            }
        }
    }
}