matcher_rs 0.12.1

A high-performance matcher designed to solve LOGICAL and TEXT VARIATIONS problems in word matching, implemented in Rust.
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
//! Hot-path scan and rule evaluation for [`super::SimpleMatcher`].
//!
//! This module implements the runtime half of the two-pass matching pipeline. Given a
//! compiled [`SimpleMatcher`], it:
//!
//! 1. Obtains a `&mut` reference to the thread-local [`SIMPLE_MATCH_STATE`].
//! 2. Transforms the input text through the process-type tree (or skips transformation
//!    for [`SearchMode::AllSimple`](super::SearchMode::AllSimple) matchers).
//! 3. Scans each text variant through the automata ([`ScanPlan`](super::engine::ScanPlan)).
//! 4. Dispatches each raw match value into the rule state machine
//!    ([`RuleSet::process_entry`](super::rule::RuleSet::process_entry)).
//! 5. Collects or checks results depending on the caller (`is_match` vs `process`).
//!
//! # Fast paths
//!
//! - **`is_match_simple`** — all rules are single-literal, no transforms. Delegates
//!   directly to the automaton's `is_match`.
//! - **`is_match_inner<true>`** — single process type. The `SINGLE_PT` const generic
//!   compiles out the process-type mask check in `process_entry`.
//! - **`process_simple`** — all-simple matchers collecting results. Each hit is a
//!   completed rule; no need for the full state machine.
//!
//! # Safety
//!
//! All functions in this module obtain `&mut SimpleMatchState` from
//! [`SIMPLE_MATCH_STATE`] via `UnsafeCell::get()`. This is safe because the static is
//! `#[thread_local]` (no cross-thread sharing) and the functions are not re-entrant.
//! See [`SIMPLE_MATCH_STATE`] for the full safety argument.

use crate::process::variant::return_string_to_pool;
use crate::process::{ProcessedTextMasks, return_processed_string_to_pool, walk_process_tree};

use super::rule::PatternDispatch;
use super::state::{SIMPLE_MATCH_STATE, ScanContext, SimpleMatchState};
use super::{SimpleMatcher, SimpleResult};

/// Hot-path search helpers layered on top of the compiled scan engines.
impl SimpleMatcher {
    /// Fast path for matchers that contain only direct simple literal rules.
    ///
    /// No state machine, no thread-local state, no transform tree — just a single
    /// automaton `is_match` call. Used when [`SearchMode::AllSimple`](super::SearchMode::AllSimple)
    /// is active.
    pub(super) fn is_match_simple(&self, text: &str) -> bool {
        self.scan.is_match(text)
    }

    /// Specialized `is_match` for matchers with a single-bit ProcessType (tree = [root, child]).
    ///
    /// Bypasses the full `walk_process_tree` machinery: no `TRANSFORM_STATE` TLS access,
    /// no masks pool, no `scanned_masks` tracking, no `dedup_insert`. Instead, applies the
    /// single transform step directly and scans at most two texts.
    ///
    /// # Safety
    ///
    /// Obtains `&mut SimpleMatchState` from [`SIMPLE_MATCH_STATE`] via `UnsafeCell::get()`.
    /// See module-level safety documentation.
    #[inline(always)]
    pub(super) fn is_match_single_step(&self, text: &str) -> bool {
        let tree = self.process.tree();
        debug_assert!(
            tree.len() == 2,
            "is_match_single_step requires exactly 2 tree nodes"
        );

        // SAFETY: `#[thread_local]` guarantees single-thread ownership; not re-entrant.
        let state = unsafe { &mut *SIMPLE_MATCH_STATE.get() };
        state.prepare(self.rules.len());

        let root_is_ascii = text.is_ascii();
        let root_mask = tree[0].pt_index_mask;
        let child_node = &tree[1];
        let child_mask = child_node.pt_index_mask;

        // Apply the single transform step.
        let step = child_node
            .step
            .expect("non-root process tree nodes always cache a transform step");
        let output = step.apply(text, root_is_ascii);

        match output.changed {
            None => {
                // Transform was a no-op — single unique text with merged mask.
                let merged_mask = root_mask | child_mask;
                if merged_mask != 0 {
                    let ctx = ScanContext {
                        text_index: 0,
                        process_type_mask: merged_mask,
                        num_variants: 1,
                        exit_early: true,
                        is_ascii: root_is_ascii,
                    };
                    if self.scan_variant::<true>(text, ctx, state) {
                        return true;
                    }
                }
            }
            Some(transformed) => {
                // Two distinct variants: root (original) and child (transformed).
                if root_mask != 0 {
                    let ctx = ScanContext {
                        text_index: 0,
                        process_type_mask: root_mask,
                        num_variants: 2,
                        exit_early: true,
                        is_ascii: root_is_ascii,
                    };
                    if self.scan_variant::<true>(text, ctx, state) {
                        return_string_to_pool(transformed);
                        return true;
                    }
                }
                let ctx = ScanContext {
                    text_index: 1,
                    process_type_mask: child_mask,
                    num_variants: 2,
                    exit_early: true,
                    is_ascii: output.is_ascii,
                };
                let matched = self.scan_variant::<true>(&transformed, ctx, state);
                return_string_to_pool(transformed);
                if matched {
                    return true;
                }
            }
        }

        self.rules.has_match(state)
    }

    /// Specialized `process` for matchers with a single-bit ProcessType (tree = [root, child]).
    ///
    /// Bypasses the full `walk_process_tree` machinery: no `TRANSFORM_STATE` TLS access,
    /// no masks pool, no `scanned_masks` tracking, no `dedup_insert`. Instead, applies the
    /// single transform step directly and scans at most two texts, then collects all matches.
    ///
    /// # Safety
    ///
    /// Obtains `&mut SimpleMatchState` from [`SIMPLE_MATCH_STATE`] via `UnsafeCell::get()`.
    /// See module-level safety documentation.
    #[inline(always)]
    pub(super) fn process_single_step<'a>(
        &'a self,
        text: &'a str,
        results: &mut Vec<SimpleResult<'a>>,
    ) {
        let tree = self.process.tree();
        debug_assert!(
            tree.len() == 2,
            "process_single_step requires exactly 2 tree nodes"
        );

        // SAFETY: `#[thread_local]` guarantees single-thread ownership; not re-entrant.
        let state = unsafe { &mut *SIMPLE_MATCH_STATE.get() };
        state.prepare(self.rules.len());

        let root_is_ascii = text.is_ascii();
        let root_mask = tree[0].pt_index_mask;
        let child_node = &tree[1];
        let child_mask = child_node.pt_index_mask;

        let step = child_node
            .step
            .expect("non-root process tree nodes always cache a transform step");
        let output = step.apply(text, root_is_ascii);

        match output.changed {
            None => {
                let merged_mask = root_mask | child_mask;
                if merged_mask != 0 {
                    let ctx = ScanContext {
                        text_index: 0,
                        process_type_mask: merged_mask,
                        num_variants: 1,
                        exit_early: false,
                        is_ascii: root_is_ascii,
                    };
                    self.scan_variant::<true>(text, ctx, state);
                }
            }
            Some(transformed) => {
                if root_mask != 0 {
                    let ctx = ScanContext {
                        text_index: 0,
                        process_type_mask: root_mask,
                        num_variants: 2,
                        exit_early: false,
                        is_ascii: root_is_ascii,
                    };
                    self.scan_variant::<true>(text, ctx, state);
                }
                if child_mask != 0 {
                    let ctx = ScanContext {
                        text_index: 1,
                        process_type_mask: child_mask,
                        num_variants: 2,
                        exit_early: false,
                        is_ascii: output.is_ascii,
                    };
                    self.scan_variant::<true>(&transformed, ctx, state);
                }
                return_string_to_pool(transformed);
            }
        }

        self.rules.collect_matches(state, results);
    }

    /// General `is_match` path for matchers that need transform traversal or rule state.
    ///
    /// Walks the process-type tree with [`walk_process_tree`], scanning each transformed
    /// variant as it is produced. If any variant scan requests early exit (a rule is
    /// already satisfied), the tree walk stops and returns `true` immediately. Otherwise,
    /// after all variants are scanned, the touched rules are checked via
    /// [`RuleSet::has_match`](super::rule::RuleSet::has_match).
    ///
    /// # Const generic `SINGLE_PT`
    ///
    /// When `true`, the process-type mask check inside
    /// [`RuleSet::process_entry`](super::rule::RuleSet::process_entry) is compiled out.
    ///
    /// # Safety
    ///
    /// Obtains `&mut SimpleMatchState` from [`SIMPLE_MATCH_STATE`] via `UnsafeCell::get()`.
    /// See module-level safety documentation.
    #[inline(always)]
    pub(super) fn is_match_inner<const SINGLE_PT: bool>(&self, text: &str) -> bool {
        let tree = self.process.tree();
        let max_pt = tree.len();
        // SAFETY: `#[thread_local]` guarantees single-thread ownership; not re-entrant.
        let state = unsafe { &mut *SIMPLE_MATCH_STATE.get() };
        state.prepare(self.rules.len());
        let (text_masks, stopped) =
            walk_process_tree::<true, _>(tree, text, &mut |txt, idx, mask, is_ascii| {
                let ctx = ScanContext {
                    text_index: idx,
                    process_type_mask: mask,
                    num_variants: max_pt,
                    exit_early: true,
                    is_ascii,
                };
                self.scan_variant::<SINGLE_PT>(txt, ctx, state)
            });
        if stopped {
            return_processed_string_to_pool(text_masks);
            return true;
        }
        let result = self.rules.has_match(state);
        return_processed_string_to_pool(text_masks);
        result
    }

    /// Collects matches for an all-simple matcher without building transformed variants.
    ///
    /// Every automaton hit is a completed rule, so results are emitted immediately
    /// via [`RuleSet::push_result_if_new`](super::rule::RuleSet::push_result_if_new).
    /// Deduplication is handled by the generation stamp in [`SimpleMatchState::mark_positive`].
    ///
    /// All patterns have [`DIRECT_RULE_BIT`](super::rule::DIRECT_RULE_BIT) encoding
    /// in all-simple mode, so every hit resolves to [`PatternDispatch::DirectRule`].
    ///
    /// # Safety
    ///
    /// Obtains `&mut SimpleMatchState` from [`SIMPLE_MATCH_STATE`] via `UnsafeCell::get()`.
    /// See module-level safety documentation.
    pub(super) fn process_simple<'a>(&'a self, text: &'a str, results: &mut Vec<SimpleResult<'a>>) {
        // SAFETY: `#[thread_local]` guarantees single-thread ownership; not re-entrant.
        let state = unsafe { &mut *SIMPLE_MATCH_STATE.get() };
        state.prepare(self.rules.len());

        let _ = self
            .scan
            .for_each_match_value(text, text.is_ascii(), |raw_value| {
                match self.scan.patterns().dispatch(raw_value) {
                    PatternDispatch::DirectRule { rule_idx, .. } => {
                        self.rules.push_result_if_new(rule_idx, state, results);
                    }
                    PatternDispatch::SingleEntry(entry) => {
                        self.rules
                            .push_result_if_new(entry.rule_idx as usize, state, results);
                    }
                    PatternDispatch::Entries(entries) => {
                        for entry in entries {
                            self.rules
                                .push_result_if_new(entry.rule_idx as usize, state, results);
                        }
                    }
                }
                false
            });
    }

    /// Collects matches from a precomputed list of transformed text variants.
    ///
    /// Used by [`SimpleMatcher::process_into`](super::SimpleMatcher::process_into) after
    /// the process-type tree has been walked and all variants are available. Scans every
    /// variant, then collects all satisfied rules into `results`.
    ///
    /// # Safety
    ///
    /// Obtains `&mut SimpleMatchState` from [`SIMPLE_MATCH_STATE`] via `UnsafeCell::get()`.
    /// See module-level safety documentation.
    pub(super) fn process_preprocessed_into<'a>(
        &'a self,
        processed_text_process_type_masks: &ProcessedTextMasks<'a>,
        results: &mut Vec<SimpleResult<'a>>,
    ) {
        // SAFETY: `#[thread_local]` guarantees single-thread ownership; not re-entrant.
        let state = unsafe { &mut *SIMPLE_MATCH_STATE.get() };
        state.prepare(self.rules.len());

        self.scan_all_variants(processed_text_process_type_masks, state, false);
        self.rules.collect_matches(state, results);
    }

    /// Scans every transformed variant, selecting the single-process-type fast path when possible.
    ///
    /// Dispatches to [`scan_all_variants_inner`](Self::scan_all_variants_inner) with the
    /// appropriate `SINGLE_PT` const generic based on the matcher's [`SearchMode`](super::SearchMode).
    ///
    /// Returns `true` if early exit was triggered (only possible when `exit_early` is `true`).
    pub(super) fn scan_all_variants<'a>(
        &'a self,
        processed_text_process_type_masks: &ProcessedTextMasks<'a>,
        state: &mut SimpleMatchState,
        exit_early: bool,
    ) -> bool {
        if self.process.mode().single_pt_index().is_some() {
            self.scan_all_variants_inner::<true>(
                processed_text_process_type_masks,
                state,
                exit_early,
            )
        } else {
            self.scan_all_variants_inner::<false>(
                processed_text_process_type_masks,
                state,
                exit_early,
            )
        }
    }

    /// Shared variant-scan loop for both general and single-process-type modes.
    ///
    /// Iterates over each text variant in `processed_text_process_type_masks`, skipping
    /// variants with a zero mask (unused process-type slots). For each variant, constructs
    /// a [`ScanContext`] and calls [`scan_variant`](Self::scan_variant).
    ///
    /// Returns `true` if any variant scan triggered early exit.
    #[inline(always)]
    fn scan_all_variants_inner<'a, const SINGLE_PT: bool>(
        &'a self,
        processed_text_process_type_masks: &ProcessedTextMasks<'a>,
        state: &mut SimpleMatchState,
        exit_early: bool,
    ) -> bool {
        if self.scan.patterns().is_empty() {
            return false;
        }

        let num_variants = processed_text_process_type_masks.len();

        for (index, text_variant) in processed_text_process_type_masks.iter().enumerate() {
            if text_variant.mask == 0 {
                continue;
            }
            let ctx = ScanContext {
                text_index: index,
                process_type_mask: text_variant.mask,
                num_variants,
                exit_early,
                is_ascii: text_variant.is_ascii,
            };
            if self.scan_variant::<SINGLE_PT>(text_variant.text.as_ref(), ctx, state) {
                return true;
            }
        }

        false
    }

    /// Scans one processed text variant and forwards each raw hit into rule evaluation.
    ///
    /// Delegates to [`ScanPlan::for_each_match_value`](super::engine::ScanPlan::for_each_match_value)
    /// with [`process_match`](Self::process_match) as the callback. Returns `true` if
    /// the callback triggered early exit.
    #[inline(always)]
    pub(super) fn scan_variant<const SINGLE_PT: bool>(
        &self,
        processed_text: &str,
        ctx: ScanContext,
        state: &mut SimpleMatchState,
    ) -> bool {
        self.scan
            .for_each_match_value(processed_text, ctx.is_ascii, |raw_value| {
                self.process_match::<SINGLE_PT>(raw_value, ctx, state)
            })
    }

    /// Processes one raw match value reported by the scan engine.
    ///
    /// Dispatches the raw value through [`PatternIndex::dispatch`](super::rule::PatternIndex::dispatch)
    /// and handles each [`PatternDispatch`] variant:
    ///
    /// - [`DirectRule`](PatternDispatch::DirectRule) — marks the rule positive immediately
    ///   and returns `exit_early` (no state machine needed).
    /// - [`SingleEntry`](PatternDispatch::SingleEntry) — feeds the single entry into
    ///   [`RuleSet::process_entry`](super::rule::RuleSet::process_entry).
    /// - [`Entries`](PatternDispatch::Entries) — iterates all entries, short-circuiting
    ///   if any one triggers early exit.
    ///
    /// Returns `true` when the caller should stop scanning (early exit satisfied).
    #[inline(always)]
    pub(super) fn process_match<const SINGLE_PT: bool>(
        &self,
        raw_value: u32,
        ctx: ScanContext,
        state: &mut SimpleMatchState,
    ) -> bool {
        match self.scan.patterns().dispatch(raw_value) {
            PatternDispatch::DirectRule { rule_idx, pt_index } => {
                if !SINGLE_PT && ctx.process_type_mask & (1u64 << pt_index) == 0 {
                    return false;
                }
                state.mark_positive(rule_idx);
                ctx.exit_early
            }
            PatternDispatch::SingleEntry(entry) => {
                self.rules.process_entry::<SINGLE_PT>(entry, ctx, state)
            }
            PatternDispatch::Entries(entries) => {
                for entry in entries {
                    if self.rules.process_entry::<SINGLE_PT>(entry, ctx, state) {
                        return true;
                    }
                }
                false
            }
        }
    }
}