Coverage Report

Created: 2026-10-28 07:07

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/rust/registry/src/index.crates.io-1949cf8c6b5b557f/regex-automata-0.4.18/src/meta/regex.rs
Line
Count
Source
1
use core::{
2
    borrow::Borrow,
3
    panic::{RefUnwindSafe, UnwindSafe},
4
};
5
6
use alloc::{boxed::Box, sync::Arc, vec, vec::Vec};
7
8
use regex_syntax::{
9
    ast,
10
    hir::{self, Hir},
11
};
12
13
use crate::{
14
    meta::{
15
        error::BuildError,
16
        strategy::{self, Strategy},
17
        wrappers,
18
    },
19
    nfa::thompson::{self, WhichCaptures},
20
    util::{
21
        captures::{Captures, GroupInfo},
22
        iter,
23
        look::LookMatcher,
24
        pool::{Pool, PoolGuard},
25
        prefilter::Prefilter,
26
        primitives::{NonMaxUsize, PatternID},
27
        search::{HalfMatch, Input, Match, MatchKind, PatternSet, Span},
28
    },
29
};
30
31
/// A type alias for our pool of meta::Cache that fixes the type parameters to
32
/// what we use for the meta regex below.
33
type CachePool = Pool<Cache, CachePoolFn>;
34
35
/// Same as above, but for the guard returned by a pool.
36
type CachePoolGuard<'a> = PoolGuard<'a, Cache, CachePoolFn>;
37
38
/// The type of the closure we use to create new caches. We need to spell out
39
/// all of the marker traits or else we risk leaking !MARKER impls.
40
type CachePoolFn =
41
    Box<dyn Fn() -> Cache + Send + Sync + UnwindSafe + RefUnwindSafe>;
42
43
/// A regex matcher that works by composing several other regex matchers
44
/// automatically.
45
///
46
/// In effect, a meta regex papers over a lot of the quirks or performance
47
/// problems in each of the regex engines in this crate. Its goal is to provide
48
/// an infallible and simple API that "just does the right thing" in the common
49
/// case.
50
///
51
/// A meta regex is the implementation of a `Regex` in the `regex` crate.
52
/// Indeed, the `regex` crate API is essentially just a light wrapper over
53
/// this type. This includes the `regex` crate's `RegexSet` API!
54
///
55
/// # Composition
56
///
57
/// This is called a "meta" matcher precisely because it uses other regex
58
/// matchers to provide a convenient high level regex API. Here are some
59
/// examples of how other regex matchers are composed:
60
///
61
/// * When calling [`Regex::captures`], instead of immediately
62
/// running a slower but more capable regex engine like the
63
/// [`PikeVM`](crate::nfa::thompson::pikevm::PikeVM), the meta regex engine
64
/// will usually first look for the bounds of a match with a higher throughput
65
/// regex engine like a [lazy DFA](crate::hybrid). Only when a match is found
66
/// is a slower engine like `PikeVM` used to find the matching span for each
67
/// capture group.
68
/// * While higher throughout engines like the lazy DFA cannot handle
69
/// Unicode word boundaries in general, they can still be used on pure ASCII
70
/// haystacks by pretending that Unicode word boundaries are just plain ASCII
71
/// word boundaries. However, if a haystack is not ASCII, the meta regex engine
72
/// will automatically switch to a (possibly slower) regex engine that supports
73
/// Unicode word boundaries in general.
74
/// * In some cases where a regex pattern is just a simple literal or a small
75
/// set of literals, an actual regex engine won't be used at all. Instead,
76
/// substring or multi-substring search algorithms will be employed.
77
///
78
/// There are many other forms of composition happening too, but the above
79
/// should give a general idea. In particular, it may perhaps be surprising
80
/// that *multiple* regex engines might get executed for a single search. That
81
/// is, the decision of what regex engine to use is not _just_ based on the
82
/// pattern, but also based on the dynamic execution of the search itself.
83
///
84
/// The primary reason for this composition is performance. The fundamental
85
/// tension is that the faster engines tend to be less capable, and the more
86
/// capable engines tend to be slower.
87
///
88
/// Note that the forms of composition that are allowed are determined by
89
/// compile time crate features and configuration. For example, if the `hybrid`
90
/// feature isn't enabled, or if [`Config::hybrid`] has been disabled, then the
91
/// meta regex engine will never use a lazy DFA.
92
///
93
/// # Synchronization and cloning
94
///
95
/// Most of the regex engines in this crate require some kind of mutable
96
/// "scratch" space to read and write from while performing a search. Since
97
/// a meta regex composes these regex engines, a meta regex also requires
98
/// mutable scratch space. This scratch space is called a [`Cache`].
99
///
100
/// Most regex engines _also_ usually have a read-only component, typically
101
/// a [Thompson `NFA`](crate::nfa::thompson::NFA).
102
///
103
/// In order to make the `Regex` API convenient, most of the routines hide
104
/// the fact that a `Cache` is needed at all. To achieve this, a [memory
105
/// pool](crate::util::pool::Pool) is used internally to retrieve `Cache`
106
/// values in a thread safe way that also permits reuse. This in turn implies
107
/// that every such search call requires some form of synchronization. Usually
108
/// this synchronization is fast enough to not notice, but in some cases, it
109
/// can be a bottleneck. This typically occurs when all of the following are
110
/// true:
111
///
112
/// * The same `Regex` is shared across multiple threads simultaneously,
113
/// usually via a [`util::lazy::Lazy`](crate::util::lazy::Lazy) or something
114
/// similar from the `once_cell` or `lazy_static` crates.
115
/// * The primary unit of work in each thread is a regex search.
116
/// * Searches are run on very short haystacks.
117
///
118
/// This particular case can lead to high contention on the pool used by a
119
/// `Regex` internally, which can in turn increase latency to a noticeable
120
/// effect. This cost can be mitigated in one of the following ways:
121
///
122
/// * Use a distinct copy of a `Regex` in each thread, usually by cloning it.
123
/// Cloning a `Regex` _does not_ do a deep copy of its read-only component.
124
/// But it does lead to each `Regex` having its own memory pool, which in
125
/// turn eliminates the problem of contention. In general, this technique should
126
/// not result in any additional memory usage when compared to sharing the same
127
/// `Regex` across multiple threads simultaneously.
128
/// * Use lower level APIs, like [`Regex::search_with`], which permit passing
129
/// a `Cache` explicitly. In this case, it is up to you to determine how best
130
/// to provide a `Cache`. For example, you might put a `Cache` in thread-local
131
/// storage if your use case allows for it.
132
///
133
/// Overall, this is an issue that happens rarely in practice, but it can
134
/// happen.
135
///
136
/// # Warning: spin-locks may be used in alloc-only mode
137
///
138
/// When this crate is built without the `std` feature and the high level APIs
139
/// on a `Regex` are used, then a spin-lock will be used to synchronize access
140
/// to an internal pool of `Cache` values. This may be undesirable because
141
/// a spin-lock is [effectively impossible to implement correctly in user
142
/// space][spinlocks-are-bad]. That is, more concretely, the spin-lock could
143
/// result in a deadlock.
144
///
145
/// [spinlocks-are-bad]: https://matklad.github.io/2020/01/02/spinlocks-considered-harmful.html
146
///
147
/// If one wants to avoid the use of spin-locks when the `std` feature is
148
/// disabled, then you must use APIs that accept a `Cache` value explicitly.
149
/// For example, [`Regex::search_with`].
150
///
151
/// # Example
152
///
153
/// ```
154
/// use regex_automata::meta::Regex;
155
///
156
/// let re = Regex::new(r"^[0-9]{4}-[0-9]{2}-[0-9]{2}$")?;
157
/// assert!(re.is_match("2010-03-14"));
158
///
159
/// # Ok::<(), Box<dyn std::error::Error>>(())
160
/// ```
161
///
162
/// # Example: anchored search
163
///
164
/// This example shows how to use [`Input::anchored`] to run an anchored
165
/// search, even when the regex pattern itself isn't anchored. An anchored
166
/// search guarantees that if a match is found, then the start offset of the
167
/// match corresponds to the offset at which the search was started.
168
///
169
/// ```
170
/// use regex_automata::{meta::Regex, Anchored, Input, Match};
171
///
172
/// let re = Regex::new(r"\bfoo\b")?;
173
/// let input = Input::new("xx foo xx").range(3..).anchored(Anchored::Yes);
174
/// // The offsets are in terms of the original haystack.
175
/// assert_eq!(Some(Match::must(0, 3..6)), re.find(input));
176
///
177
/// // Notice that no match occurs here, because \b still takes the
178
/// // surrounding context into account, even if it means looking back
179
/// // before the start of your search.
180
/// let hay = "xxfoo xx";
181
/// let input = Input::new(hay).range(2..).anchored(Anchored::Yes);
182
/// assert_eq!(None, re.find(input));
183
/// // Indeed, you cannot achieve the above by simply slicing the
184
/// // haystack itself, since the regex engine can't see the
185
/// // surrounding context. This is why 'Input' permits setting
186
/// // the bounds of a search!
187
/// let input = Input::new(&hay[2..]).anchored(Anchored::Yes);
188
/// // WRONG!
189
/// assert_eq!(Some(Match::must(0, 0..3)), re.find(input));
190
///
191
/// # Ok::<(), Box<dyn std::error::Error>>(())
192
/// ```
193
///
194
/// # Example: earliest search
195
///
196
/// This example shows how to use [`Input::earliest`] to run a search that
197
/// might stop before finding the typical leftmost match.
198
///
199
/// ```
200
/// use regex_automata::{meta::Regex, Anchored, Input, Match};
201
///
202
/// let re = Regex::new(r"[a-z]{3}|b")?;
203
/// let input = Input::new("abc").earliest(true);
204
/// assert_eq!(Some(Match::must(0, 1..2)), re.find(input));
205
///
206
/// // Note that "earliest" isn't really a match semantic unto itself.
207
/// // Instead, it is merely an instruction to whatever regex engine
208
/// // gets used internally to quit as soon as it can. For example,
209
/// // this regex uses a different search technique, and winds up
210
/// // producing a different (but valid) match!
211
/// let re = Regex::new(r"abc|b")?;
212
/// let input = Input::new("abc").earliest(true);
213
/// assert_eq!(Some(Match::must(0, 0..3)), re.find(input));
214
///
215
/// # Ok::<(), Box<dyn std::error::Error>>(())
216
/// ```
217
///
218
/// # Example: change the line terminator
219
///
220
/// This example shows how to enable multi-line mode by default and change
221
/// the line terminator to the NUL byte:
222
///
223
/// ```
224
/// use regex_automata::{meta::Regex, util::syntax, Match};
225
///
226
/// let re = Regex::builder()
227
///     .syntax(syntax::Config::new().multi_line(true))
228
///     .configure(Regex::config().line_terminator(b'\x00'))
229
///     .build(r"^foo$")?;
230
/// let hay = "\x00foo\x00";
231
/// assert_eq!(Some(Match::must(0, 1..4)), re.find(hay));
232
///
233
/// # Ok::<(), Box<dyn std::error::Error>>(())
234
/// ```
235
#[derive(Debug)]
236
pub struct Regex {
237
    /// The actual regex implementation.
238
    imp: Arc<RegexI>,
239
    /// A thread safe pool of caches.
240
    ///
241
    /// For the higher level search APIs, a `Cache` is automatically plucked
242
    /// from this pool before running a search. The lower level `with` methods
243
    /// permit the caller to provide their own cache, thereby bypassing
244
    /// accesses to this pool.
245
    ///
246
    /// Note that we put this outside the `Arc` so that cloning a `Regex`
247
    /// results in creating a fresh `CachePool`. This in turn permits callers
248
    /// to clone regexes into separate threads where each such regex gets
249
    /// the pool's "thread owner" optimization. Otherwise, if one shares the
250
    /// `Regex` directly, then the pool will go through a slower mutex path for
251
    /// all threads except for the "owner."
252
    pool: CachePool,
253
}
254
255
/// The internal implementation of `Regex`, split out so that it can be wrapped
256
/// in an `Arc`.
257
#[derive(Debug)]
258
struct RegexI {
259
    /// The core matching engine.
260
    ///
261
    /// Why is this reference counted when RegexI is already wrapped in an Arc?
262
    /// Well, we need to capture this in a closure to our `Pool` below in order
263
    /// to create new `Cache` values when needed. So since it needs to be in
264
    /// two places, we make it reference counted.
265
    ///
266
    /// We make `RegexI` itself reference counted too so that `Regex` itself
267
    /// stays extremely small and very cheap to clone.
268
    strat: Arc<dyn Strategy>,
269
    /// Metadata about the regexes driving the strategy. The metadata is also
270
    /// usually stored inside the strategy too, but we put it here as well
271
    /// so that we can get quick access to it (without virtual calls) before
272
    /// executing the regex engine. For example, we use this metadata to
273
    /// detect a subset of cases where we know a match is impossible, and can
274
    /// thus avoid calling into the strategy at all.
275
    ///
276
    /// Since `RegexInfo` is stored in multiple places, it is also reference
277
    /// counted.
278
    info: RegexInfo,
279
}
280
281
/// Convenience constructors for a `Regex` using the default configuration.
282
impl Regex {
283
    /// Builds a `Regex` from a single pattern string using the default
284
    /// configuration.
285
    ///
286
    /// If there was a problem parsing the pattern or a problem turning it into
287
    /// a regex matcher, then an error is returned.
288
    ///
289
    /// If you want to change the configuration of a `Regex`, use a [`Builder`]
290
    /// with a [`Config`].
291
    ///
292
    /// # Example
293
    ///
294
    /// ```
295
    /// use regex_automata::{meta::Regex, Match};
296
    ///
297
    /// let re = Regex::new(r"(?Rm)^foo$")?;
298
    /// let hay = "\r\nfoo\r\n";
299
    /// assert_eq!(Some(Match::must(0, 2..5)), re.find(hay));
300
    ///
301
    /// # Ok::<(), Box<dyn std::error::Error>>(())
302
    /// ```
303
0
    pub fn new(pattern: &str) -> Result<Regex, BuildError> {
304
0
        Regex::builder().build(pattern)
305
0
    }
306
307
    /// Builds a `Regex` from many pattern strings using the default
308
    /// configuration.
309
    ///
310
    /// If there was a problem parsing any of the patterns or a problem turning
311
    /// them into a regex matcher, then an error is returned.
312
    ///
313
    /// If you want to change the configuration of a `Regex`, use a [`Builder`]
314
    /// with a [`Config`].
315
    ///
316
    /// # Example: simple lexer
317
    ///
318
    /// This simplistic example leverages the multi-pattern support to build a
319
    /// simple little lexer. The pattern ID in the match tells you which regex
320
    /// matched, which in turn might be used to map back to the "type" of the
321
    /// token returned by the lexer.
322
    ///
323
    /// ```
324
    /// use regex_automata::{meta::Regex, Match};
325
    ///
326
    /// let re = Regex::new_many(&[
327
    ///     r"[[:space:]]",
328
    ///     r"[A-Za-z0-9][A-Za-z0-9_]+",
329
    ///     r"->",
330
    ///     r".",
331
    /// ])?;
332
    /// let haystack = "fn is_boss(bruce: i32, springsteen: String) -> bool;";
333
    /// let matches: Vec<Match> = re.find_iter(haystack).collect();
334
    /// assert_eq!(matches, vec![
335
    ///     Match::must(1, 0..2),   // 'fn'
336
    ///     Match::must(0, 2..3),   // ' '
337
    ///     Match::must(1, 3..10),  // 'is_boss'
338
    ///     Match::must(3, 10..11), // '('
339
    ///     Match::must(1, 11..16), // 'bruce'
340
    ///     Match::must(3, 16..17), // ':'
341
    ///     Match::must(0, 17..18), // ' '
342
    ///     Match::must(1, 18..21), // 'i32'
343
    ///     Match::must(3, 21..22), // ','
344
    ///     Match::must(0, 22..23), // ' '
345
    ///     Match::must(1, 23..34), // 'springsteen'
346
    ///     Match::must(3, 34..35), // ':'
347
    ///     Match::must(0, 35..36), // ' '
348
    ///     Match::must(1, 36..42), // 'String'
349
    ///     Match::must(3, 42..43), // ')'
350
    ///     Match::must(0, 43..44), // ' '
351
    ///     Match::must(2, 44..46), // '->'
352
    ///     Match::must(0, 46..47), // ' '
353
    ///     Match::must(1, 47..51), // 'bool'
354
    ///     Match::must(3, 51..52), // ';'
355
    /// ]);
356
    ///
357
    /// # Ok::<(), Box<dyn std::error::Error>>(())
358
    /// ```
359
    ///
360
    /// One can write a lexer like the above using a regex like
361
    /// `(?P<space>[[:space:]])|(?P<ident>[A-Za-z0-9][A-Za-z0-9_]+)|...`,
362
    /// but then you need to ask whether capture group matched to determine
363
    /// which branch in the regex matched, and thus, which token the match
364
    /// corresponds to. In contrast, the above example includes the pattern ID
365
    /// in the match. There's no need to use capture groups at all.
366
    ///
367
    /// # Example: finding the pattern that caused an error
368
    ///
369
    /// When a syntax error occurs, it is possible to ask which pattern
370
    /// caused the syntax error.
371
    ///
372
    /// ```
373
    /// use regex_automata::{meta::Regex, PatternID};
374
    ///
375
    /// let err = Regex::new_many(&["a", "b", r"\p{Foo}", "c"]).unwrap_err();
376
    /// assert_eq!(Some(PatternID::must(2)), err.pattern());
377
    /// ```
378
    ///
379
    /// # Example: zero patterns is valid
380
    ///
381
    /// Building a regex with zero patterns results in a regex that never
382
    /// matches anything. Because this routine is generic, passing an empty
383
    /// slice usually requires a turbo-fish (or something else to help type
384
    /// inference).
385
    ///
386
    /// ```
387
    /// use regex_automata::{meta::Regex, util::syntax, Match};
388
    ///
389
    /// let re = Regex::new_many::<&str>(&[])?;
390
    /// assert_eq!(None, re.find(""));
391
    ///
392
    /// # Ok::<(), Box<dyn std::error::Error>>(())
393
    /// ```
394
0
    pub fn new_many<P: AsRef<str>>(
395
0
        patterns: &[P],
396
0
    ) -> Result<Regex, BuildError> {
397
0
        Regex::builder().build_many(patterns)
398
0
    }
399
400
    /// Return a default configuration for a `Regex`.
401
    ///
402
    /// This is a convenience routine to avoid needing to import the [`Config`]
403
    /// type when customizing the construction of a `Regex`.
404
    ///
405
    /// # Example: lower the NFA size limit
406
    ///
407
    /// In some cases, the default size limit might be too big. The size limit
408
    /// can be lowered, which will prevent large regex patterns from compiling.
409
    ///
410
    /// ```
411
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
412
    /// use regex_automata::meta::Regex;
413
    ///
414
    /// let result = Regex::builder()
415
    ///     .configure(Regex::config().nfa_size_limit(Some(20 * (1<<10))))
416
    ///     // Not even 20KB is enough to build a single large Unicode class!
417
    ///     .build(r"\pL");
418
    /// assert!(result.is_err());
419
    ///
420
    /// # Ok::<(), Box<dyn std::error::Error>>(())
421
    /// ```
422
0
    pub fn config() -> Config {
423
0
        Config::new()
424
0
    }
425
426
    /// Return a builder for configuring the construction of a `Regex`.
427
    ///
428
    /// This is a convenience routine to avoid needing to import the
429
    /// [`Builder`] type in common cases.
430
    ///
431
    /// # Example: change the line terminator
432
    ///
433
    /// This example shows how to enable multi-line mode by default and change
434
    /// the line terminator to the NUL byte:
435
    ///
436
    /// ```
437
    /// use regex_automata::{meta::Regex, util::syntax, Match};
438
    ///
439
    /// let re = Regex::builder()
440
    ///     .syntax(syntax::Config::new().multi_line(true))
441
    ///     .configure(Regex::config().line_terminator(b'\x00'))
442
    ///     .build(r"^foo$")?;
443
    /// let hay = "\x00foo\x00";
444
    /// assert_eq!(Some(Match::must(0, 1..4)), re.find(hay));
445
    ///
446
    /// # Ok::<(), Box<dyn std::error::Error>>(())
447
    /// ```
448
0
    pub fn builder() -> Builder {
449
0
        Builder::new()
450
0
    }
451
}
452
453
/// High level convenience routines for using a regex to search a haystack.
454
impl Regex {
455
    /// Returns true if and only if this regex matches the given haystack.
456
    ///
457
    /// This routine may short circuit if it knows that scanning future input
458
    /// will never lead to a different result. (Consider how this might make
459
    /// a difference given the regex `a+` on the haystack `aaaaaaaaaaaaaaa`.
460
    /// This routine _may_ stop after it sees the first `a`, but routines like
461
    /// `find` need to continue searching because `+` is greedy by default.)
462
    ///
463
    /// # Example
464
    ///
465
    /// ```
466
    /// use regex_automata::meta::Regex;
467
    ///
468
    /// let re = Regex::new("foo[0-9]+bar")?;
469
    ///
470
    /// assert!(re.is_match("foo12345bar"));
471
    /// assert!(!re.is_match("foobar"));
472
    ///
473
    /// # Ok::<(), Box<dyn std::error::Error>>(())
474
    /// ```
475
    ///
476
    /// # Example: consistency with search APIs
477
    ///
478
    /// `is_match` is guaranteed to return `true` whenever `find` returns a
479
    /// match. This includes searches that are executed entirely within a
480
    /// codepoint:
481
    ///
482
    /// ```
483
    /// use regex_automata::{meta::Regex, Input};
484
    ///
485
    /// let re = Regex::new("a*")?;
486
    ///
487
    /// // This doesn't match because the default configuration bans empty
488
    /// // matches from splitting a codepoint.
489
    /// assert!(!re.is_match(Input::new("☃").span(1..2)));
490
    /// assert_eq!(None, re.find(Input::new("☃").span(1..2)));
491
    ///
492
    /// # Ok::<(), Box<dyn std::error::Error>>(())
493
    /// ```
494
    ///
495
    /// Notice that when UTF-8 mode is disabled, then the above reports a
496
    /// match because the restriction against zero-width matches that split a
497
    /// codepoint has been lifted:
498
    ///
499
    /// ```
500
    /// use regex_automata::{meta::Regex, Input, Match};
501
    ///
502
    /// let re = Regex::builder()
503
    ///     .configure(Regex::config().utf8_empty(false))
504
    ///     .build("a*")?;
505
    ///
506
    /// assert!(re.is_match(Input::new("☃").span(1..2)));
507
    /// assert_eq!(
508
    ///     Some(Match::must(0, 1..1)),
509
    ///     re.find(Input::new("☃").span(1..2)),
510
    /// );
511
    ///
512
    /// # Ok::<(), Box<dyn std::error::Error>>(())
513
    /// ```
514
    ///
515
    /// A similar idea applies when using line anchors with CRLF mode enabled,
516
    /// which prevents them from matching between a `\r` and a `\n`.
517
    ///
518
    /// ```
519
    /// use regex_automata::{meta::Regex, Input, Match};
520
    ///
521
    /// let re = Regex::new(r"(?Rm:$)")?;
522
    /// assert!(!re.is_match(Input::new("\r\n").span(1..1)));
523
    /// // A regular line anchor, which only considers \n as a
524
    /// // line terminator, will match.
525
    /// let re = Regex::new(r"(?m:$)")?;
526
    /// assert!(re.is_match(Input::new("\r\n").span(1..1)));
527
    ///
528
    /// # Ok::<(), Box<dyn std::error::Error>>(())
529
    /// ```
530
    #[inline]
531
0
    pub fn is_match<'h, I: Into<Input<'h>>>(&self, input: I) -> bool {
532
0
        let input = input.into().earliest(true);
533
0
        if self.imp.info.is_impossible(&input) {
534
0
            return false;
535
0
        }
536
0
        let mut guard = self.pool.get();
537
0
        let result = self.imp.strat.is_match(&mut guard, &input);
538
        // See 'Regex::search' for why we put the guard back explicitly.
539
0
        PoolGuard::put(guard);
540
0
        result
541
0
    }
542
543
    /// Executes a leftmost search and returns the first match that is found,
544
    /// if one exists.
545
    ///
546
    /// # Example
547
    ///
548
    /// ```
549
    /// use regex_automata::{meta::Regex, Match};
550
    ///
551
    /// let re = Regex::new("foo[0-9]+")?;
552
    /// assert_eq!(Some(Match::must(0, 0..8)), re.find("foo12345"));
553
    ///
554
    /// # Ok::<(), Box<dyn std::error::Error>>(())
555
    /// ```
556
    #[inline]
557
0
    pub fn find<'h, I: Into<Input<'h>>>(&self, input: I) -> Option<Match> {
558
0
        self.search(&input.into())
559
0
    }
560
561
    /// Executes a leftmost forward search and writes the spans of capturing
562
    /// groups that participated in a match into the provided [`Captures`]
563
    /// value. If no match was found, then [`Captures::is_match`] is guaranteed
564
    /// to return `false`.
565
    ///
566
    /// # Example
567
    ///
568
    /// ```
569
    /// use regex_automata::{meta::Regex, Span};
570
    ///
571
    /// let re = Regex::new(r"^([0-9]{4})-([0-9]{2})-([0-9]{2})$")?;
572
    /// let mut caps = re.create_captures();
573
    ///
574
    /// re.captures("2010-03-14", &mut caps);
575
    /// assert!(caps.is_match());
576
    /// assert_eq!(Some(Span::from(0..4)), caps.get_group(1));
577
    /// assert_eq!(Some(Span::from(5..7)), caps.get_group(2));
578
    /// assert_eq!(Some(Span::from(8..10)), caps.get_group(3));
579
    ///
580
    /// # Ok::<(), Box<dyn std::error::Error>>(())
581
    /// ```
582
    #[inline]
583
0
    pub fn captures<'h, I: Into<Input<'h>>>(
584
0
        &self,
585
0
        input: I,
586
0
        caps: &mut Captures,
587
0
    ) {
588
0
        self.search_captures(&input.into(), caps)
589
0
    }
590
591
    /// Returns an iterator over all non-overlapping leftmost matches in
592
    /// the given haystack. If no match exists, then the iterator yields no
593
    /// elements.
594
    ///
595
    /// # Example
596
    ///
597
    /// ```
598
    /// use regex_automata::{meta::Regex, Match};
599
    ///
600
    /// let re = Regex::new("foo[0-9]+")?;
601
    /// let haystack = "foo1 foo12 foo123";
602
    /// let matches: Vec<Match> = re.find_iter(haystack).collect();
603
    /// assert_eq!(matches, vec![
604
    ///     Match::must(0, 0..4),
605
    ///     Match::must(0, 5..10),
606
    ///     Match::must(0, 11..17),
607
    /// ]);
608
    /// # Ok::<(), Box<dyn std::error::Error>>(())
609
    /// ```
610
    #[inline]
611
0
    pub fn find_iter<'r, 'h, I: Into<Input<'h>>>(
612
0
        &'r self,
613
0
        input: I,
614
0
    ) -> FindMatches<'r, 'h> {
615
0
        let cache = self.pool.get();
616
0
        let it = iter::Searcher::new(input.into());
617
0
        FindMatches { re: self, cache, it }
618
0
    }
619
620
    /// Returns an iterator over all non-overlapping `Captures` values. If no
621
    /// match exists, then the iterator yields no elements.
622
    ///
623
    /// This yields the same matches as [`Regex::find_iter`], but it includes
624
    /// the spans of all capturing groups that participate in each match.
625
    ///
626
    /// **Tip:** See [`util::iter::Searcher`](crate::util::iter::Searcher) for
627
    /// how to correctly iterate over all matches in a haystack while avoiding
628
    /// the creation of a new `Captures` value for every match. (Which you are
629
    /// forced to do with an `Iterator`.)
630
    ///
631
    /// # Example
632
    ///
633
    /// ```
634
    /// use regex_automata::{meta::Regex, Span};
635
    ///
636
    /// let re = Regex::new("foo(?P<numbers>[0-9]+)")?;
637
    ///
638
    /// let haystack = "foo1 foo12 foo123";
639
    /// let matches: Vec<Span> = re
640
    ///     .captures_iter(haystack)
641
    ///     // The unwrap is OK since 'numbers' matches if the pattern matches.
642
    ///     .map(|caps| caps.get_group_by_name("numbers").unwrap())
643
    ///     .collect();
644
    /// assert_eq!(matches, vec![
645
    ///     Span::from(3..4),
646
    ///     Span::from(8..10),
647
    ///     Span::from(14..17),
648
    /// ]);
649
    /// # Ok::<(), Box<dyn std::error::Error>>(())
650
    /// ```
651
    #[inline]
652
0
    pub fn captures_iter<'r, 'h, I: Into<Input<'h>>>(
653
0
        &'r self,
654
0
        input: I,
655
0
    ) -> CapturesMatches<'r, 'h> {
656
0
        let cache = self.pool.get();
657
0
        let caps = self.create_captures();
658
0
        let it = iter::Searcher::new(input.into());
659
0
        CapturesMatches { re: self, cache, caps, it }
660
0
    }
661
662
    /// Returns an iterator of spans of the haystack given, delimited by a
663
    /// match of the regex. Namely, each element of the iterator corresponds to
664
    /// a part of the haystack that *isn't* matched by the regular expression.
665
    ///
666
    /// # Example
667
    ///
668
    /// To split a string delimited by arbitrary amounts of spaces or tabs:
669
    ///
670
    /// ```
671
    /// use regex_automata::meta::Regex;
672
    ///
673
    /// let re = Regex::new(r"[ \t]+")?;
674
    /// let hay = "a b \t  c\td    e";
675
    /// let fields: Vec<&str> = re.split(hay).map(|span| &hay[span]).collect();
676
    /// assert_eq!(fields, vec!["a", "b", "c", "d", "e"]);
677
    ///
678
    /// # Ok::<(), Box<dyn std::error::Error>>(())
679
    /// ```
680
    ///
681
    /// # Example: more cases
682
    ///
683
    /// Basic usage:
684
    ///
685
    /// ```
686
    /// use regex_automata::meta::Regex;
687
    ///
688
    /// let re = Regex::new(r" ")?;
689
    /// let hay = "Mary had a little lamb";
690
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
691
    /// assert_eq!(got, vec!["Mary", "had", "a", "little", "lamb"]);
692
    ///
693
    /// let re = Regex::new(r"X")?;
694
    /// let hay = "";
695
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
696
    /// assert_eq!(got, vec![""]);
697
    ///
698
    /// let re = Regex::new(r"X")?;
699
    /// let hay = "lionXXtigerXleopard";
700
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
701
    /// assert_eq!(got, vec!["lion", "", "tiger", "leopard"]);
702
    ///
703
    /// let re = Regex::new(r"::")?;
704
    /// let hay = "lion::tiger::leopard";
705
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
706
    /// assert_eq!(got, vec!["lion", "tiger", "leopard"]);
707
    ///
708
    /// # Ok::<(), Box<dyn std::error::Error>>(())
709
    /// ```
710
    ///
711
    /// If a haystack contains multiple contiguous matches, you will end up
712
    /// with empty spans yielded by the iterator:
713
    ///
714
    /// ```
715
    /// use regex_automata::meta::Regex;
716
    ///
717
    /// let re = Regex::new(r"X")?;
718
    /// let hay = "XXXXaXXbXc";
719
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
720
    /// assert_eq!(got, vec!["", "", "", "", "a", "", "b", "c"]);
721
    ///
722
    /// let re = Regex::new(r"/")?;
723
    /// let hay = "(///)";
724
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
725
    /// assert_eq!(got, vec!["(", "", "", ")"]);
726
    ///
727
    /// # Ok::<(), Box<dyn std::error::Error>>(())
728
    /// ```
729
    ///
730
    /// Separators at the start or end of a haystack are neighbored by empty
731
    /// spans.
732
    ///
733
    /// ```
734
    /// use regex_automata::meta::Regex;
735
    ///
736
    /// let re = Regex::new(r"0")?;
737
    /// let hay = "010";
738
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
739
    /// assert_eq!(got, vec!["", "1", ""]);
740
    ///
741
    /// # Ok::<(), Box<dyn std::error::Error>>(())
742
    /// ```
743
    ///
744
    /// When the empty string is used as a regex, it splits at every valid
745
    /// UTF-8 boundary by default (which includes the beginning and end of the
746
    /// haystack):
747
    ///
748
    /// ```
749
    /// use regex_automata::meta::Regex;
750
    ///
751
    /// let re = Regex::new(r"")?;
752
    /// let hay = "rust";
753
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
754
    /// assert_eq!(got, vec!["", "r", "u", "s", "t", ""]);
755
    ///
756
    /// // Splitting by an empty string is UTF-8 aware by default!
757
    /// let re = Regex::new(r"")?;
758
    /// let hay = "☃";
759
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
760
    /// assert_eq!(got, vec!["", "☃", ""]);
761
    ///
762
    /// # Ok::<(), Box<dyn std::error::Error>>(())
763
    /// ```
764
    ///
765
    /// But note that UTF-8 mode for empty strings can be disabled, which will
766
    /// then result in a match at every byte offset in the haystack,
767
    /// including between every UTF-8 code unit.
768
    ///
769
    /// ```
770
    /// use regex_automata::meta::Regex;
771
    ///
772
    /// let re = Regex::builder()
773
    ///     .configure(Regex::config().utf8_empty(false))
774
    ///     .build(r"")?;
775
    /// let hay = "☃".as_bytes();
776
    /// let got: Vec<&[u8]> = re.split(hay).map(|sp| &hay[sp]).collect();
777
    /// assert_eq!(got, vec![
778
    ///     // Writing byte string slices is just brutal. The problem is that
779
    ///     // b"foo" has type &[u8; 3] instead of &[u8].
780
    ///     &[][..], &[b'\xE2'][..], &[b'\x98'][..], &[b'\x83'][..], &[][..],
781
    /// ]);
782
    ///
783
    /// # Ok::<(), Box<dyn std::error::Error>>(())
784
    /// ```
785
    ///
786
    /// Contiguous separators (commonly shows up with whitespace), can lead to
787
    /// possibly surprising behavior. For example, this code is correct:
788
    ///
789
    /// ```
790
    /// use regex_automata::meta::Regex;
791
    ///
792
    /// let re = Regex::new(r" ")?;
793
    /// let hay = "    a  b c";
794
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
795
    /// assert_eq!(got, vec!["", "", "", "", "a", "", "b", "c"]);
796
    ///
797
    /// # Ok::<(), Box<dyn std::error::Error>>(())
798
    /// ```
799
    ///
800
    /// It does *not* give you `["a", "b", "c"]`. For that behavior, you'd want
801
    /// to match contiguous space characters:
802
    ///
803
    /// ```
804
    /// use regex_automata::meta::Regex;
805
    ///
806
    /// let re = Regex::new(r" +")?;
807
    /// let hay = "    a  b c";
808
    /// let got: Vec<&str> = re.split(hay).map(|sp| &hay[sp]).collect();
809
    /// // N.B. This does still include a leading empty span because ' +'
810
    /// // matches at the beginning of the haystack.
811
    /// assert_eq!(got, vec!["", "a", "b", "c"]);
812
    ///
813
    /// # Ok::<(), Box<dyn std::error::Error>>(())
814
    /// ```
815
    #[inline]
816
0
    pub fn split<'r, 'h, I: Into<Input<'h>>>(
817
0
        &'r self,
818
0
        input: I,
819
0
    ) -> Split<'r, 'h> {
820
0
        Split { finder: self.find_iter(input), last: 0 }
821
0
    }
822
823
    /// Returns an iterator of at most `limit` spans of the haystack given,
824
    /// delimited by a match of the regex. (A `limit` of `0` will return no
825
    /// spans.) Namely, each element of the iterator corresponds to a part
826
    /// of the haystack that *isn't* matched by the regular expression. The
827
    /// remainder of the haystack that is not split will be the last element in
828
    /// the iterator.
829
    ///
830
    /// # Example
831
    ///
832
    /// Get the first two words in some haystack:
833
    ///
834
    /// ```
835
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
836
    /// use regex_automata::meta::Regex;
837
    ///
838
    /// let re = Regex::new(r"\W+").unwrap();
839
    /// let hay = "Hey! How are you?";
840
    /// let fields: Vec<&str> =
841
    ///     re.splitn(hay, 3).map(|span| &hay[span]).collect();
842
    /// assert_eq!(fields, vec!["Hey", "How", "are you?"]);
843
    ///
844
    /// # Ok::<(), Box<dyn std::error::Error>>(())
845
    /// ```
846
    ///
847
    /// # Examples: more cases
848
    ///
849
    /// ```
850
    /// use regex_automata::meta::Regex;
851
    ///
852
    /// let re = Regex::new(r" ")?;
853
    /// let hay = "Mary had a little lamb";
854
    /// let got: Vec<&str> = re.splitn(hay, 3).map(|sp| &hay[sp]).collect();
855
    /// assert_eq!(got, vec!["Mary", "had", "a little lamb"]);
856
    ///
857
    /// let re = Regex::new(r"X")?;
858
    /// let hay = "";
859
    /// let got: Vec<&str> = re.splitn(hay, 3).map(|sp| &hay[sp]).collect();
860
    /// assert_eq!(got, vec![""]);
861
    ///
862
    /// let re = Regex::new(r"X")?;
863
    /// let hay = "lionXXtigerXleopard";
864
    /// let got: Vec<&str> = re.splitn(hay, 3).map(|sp| &hay[sp]).collect();
865
    /// assert_eq!(got, vec!["lion", "", "tigerXleopard"]);
866
    ///
867
    /// let re = Regex::new(r"::")?;
868
    /// let hay = "lion::tiger::leopard";
869
    /// let got: Vec<&str> = re.splitn(hay, 2).map(|sp| &hay[sp]).collect();
870
    /// assert_eq!(got, vec!["lion", "tiger::leopard"]);
871
    ///
872
    /// let re = Regex::new(r"X")?;
873
    /// let hay = "abcXdef";
874
    /// let got: Vec<&str> = re.splitn(hay, 1).map(|sp| &hay[sp]).collect();
875
    /// assert_eq!(got, vec!["abcXdef"]);
876
    ///
877
    /// let re = Regex::new(r"X")?;
878
    /// let hay = "abcdef";
879
    /// let got: Vec<&str> = re.splitn(hay, 2).map(|sp| &hay[sp]).collect();
880
    /// assert_eq!(got, vec!["abcdef"]);
881
    ///
882
    /// let re = Regex::new(r"X")?;
883
    /// let hay = "abcXdef";
884
    /// let got: Vec<&str> = re.splitn(hay, 0).map(|sp| &hay[sp]).collect();
885
    /// assert!(got.is_empty());
886
    ///
887
    /// # Ok::<(), Box<dyn std::error::Error>>(())
888
    /// ```
889
0
    pub fn splitn<'r, 'h, I: Into<Input<'h>>>(
890
0
        &'r self,
891
0
        input: I,
892
0
        limit: usize,
893
0
    ) -> SplitN<'r, 'h> {
894
0
        SplitN { splits: self.split(input), limit }
895
0
    }
896
}
897
898
/// Lower level search routines that give more control.
899
impl Regex {
900
    /// Returns the start and end offset of the leftmost match. If no match
901
    /// exists, then `None` is returned.
902
    ///
903
    /// This is like [`Regex::find`] but, but it accepts a concrete `&Input`
904
    /// instead of an `Into<Input>`.
905
    ///
906
    /// # Example
907
    ///
908
    /// ```
909
    /// use regex_automata::{meta::Regex, Input, Match};
910
    ///
911
    /// let re = Regex::new(r"Samwise|Sam")?;
912
    /// let input = Input::new(
913
    ///     "one of the chief characters, Samwise the Brave",
914
    /// );
915
    /// assert_eq!(Some(Match::must(0, 29..36)), re.search(&input));
916
    ///
917
    /// # Ok::<(), Box<dyn std::error::Error>>(())
918
    /// ```
919
    #[inline]
920
0
    pub fn search(&self, input: &Input<'_>) -> Option<Match> {
921
0
        if self.imp.info.captures_disabled()
922
0
            || self.imp.info.is_impossible(input)
923
        {
924
0
            return None;
925
0
        }
926
0
        let mut guard = self.pool.get();
927
0
        let result = self.imp.strat.search(&mut guard, input);
928
        // We do this dance with the guard and explicitly put it back in the
929
        // pool because it seems to result in better codegen. If we let the
930
        // guard's Drop impl put it back in the pool, then functions like
931
        // ptr::drop_in_place get called and they *don't* get inlined. This
932
        // isn't usually a big deal, but in latency sensitive benchmarks the
933
        // extra function call can matter.
934
        //
935
        // I used `rebar measure -f '^grep/every-line$' -e meta` to measure
936
        // the effects here.
937
        //
938
        // Note that this doesn't eliminate the latency effects of using the
939
        // pool. There is still some (minor) cost for the "thread owner" of the
940
        // pool. (i.e., The thread that first calls a regex search routine.)
941
        // However, for other threads using the regex, the pool access can be
942
        // quite expensive as it goes through a mutex. Callers can avoid this
943
        // by either cloning the Regex (which creates a distinct copy of the
944
        // pool), or callers can use the lower level APIs that accept a 'Cache'
945
        // directly and do their own handling.
946
0
        PoolGuard::put(guard);
947
0
        result
948
0
    }
949
950
    /// Returns the end offset of the leftmost match. If no match exists, then
951
    /// `None` is returned.
952
    ///
953
    /// This is distinct from [`Regex::search`] in that it only returns the end
954
    /// of a match and not the start of the match. Depending on a variety of
955
    /// implementation details, this _may_ permit the regex engine to do less
956
    /// overall work. For example, if a DFA is being used to execute a search,
957
    /// then the start of a match usually requires running a separate DFA in
958
    /// reverse to the find the start of a match. If one only needs the end of
959
    /// a match, then the separate reverse scan to find the start of a match
960
    /// can be skipped. (Note that the reverse scan is avoided even when using
961
    /// `Regex::search` when possible, for example, in the case of an anchored
962
    /// search.)
963
    ///
964
    /// # Example
965
    ///
966
    /// ```
967
    /// use regex_automata::{meta::Regex, Input, HalfMatch};
968
    ///
969
    /// let re = Regex::new(r"Samwise|Sam")?;
970
    /// let input = Input::new(
971
    ///     "one of the chief characters, Samwise the Brave",
972
    /// );
973
    /// assert_eq!(Some(HalfMatch::must(0, 36)), re.search_half(&input));
974
    ///
975
    /// # Ok::<(), Box<dyn std::error::Error>>(())
976
    /// ```
977
    #[inline]
978
0
    pub fn search_half(&self, input: &Input<'_>) -> Option<HalfMatch> {
979
0
        if self.imp.info.captures_disabled()
980
0
            || self.imp.info.is_impossible(input)
981
        {
982
0
            return None;
983
0
        }
984
0
        let mut guard = self.pool.get();
985
0
        let result = self.imp.strat.search_half(&mut guard, input);
986
        // See 'Regex::search' for why we put the guard back explicitly.
987
0
        PoolGuard::put(guard);
988
0
        result
989
0
    }
Unexecuted instantiation: <regex_automata::meta::regex::Regex>::search_half
Unexecuted instantiation: <regex_automata::meta::regex::Regex>::search_half
990
991
    /// Executes a leftmost forward search and writes the spans of capturing
992
    /// groups that participated in a match into the provided [`Captures`]
993
    /// value. If no match was found, then [`Captures::is_match`] is guaranteed
994
    /// to return `false`.
995
    ///
996
    /// This is like [`Regex::captures`], but it accepts a concrete `&Input`
997
    /// instead of an `Into<Input>`.
998
    ///
999
    /// # Example: specific pattern search
1000
    ///
1001
    /// This example shows how to build a multi-pattern `Regex` that permits
1002
    /// searching for specific patterns.
1003
    ///
1004
    /// ```
1005
    /// use regex_automata::{
1006
    ///     meta::Regex,
1007
    ///     Anchored, Match, PatternID, Input,
1008
    /// };
1009
    ///
1010
    /// let re = Regex::new_many(&["[a-z0-9]{6}", "[a-z][a-z0-9]{5}"])?;
1011
    /// let mut caps = re.create_captures();
1012
    /// let haystack = "foo123";
1013
    ///
1014
    /// // Since we are using the default leftmost-first match and both
1015
    /// // patterns match at the same starting position, only the first pattern
1016
    /// // will be returned in this case when doing a search for any of the
1017
    /// // patterns.
1018
    /// let expected = Some(Match::must(0, 0..6));
1019
    /// re.search_captures(&Input::new(haystack), &mut caps);
1020
    /// assert_eq!(expected, caps.get_match());
1021
    ///
1022
    /// // But if we want to check whether some other pattern matches, then we
1023
    /// // can provide its pattern ID.
1024
    /// let expected = Some(Match::must(1, 0..6));
1025
    /// let input = Input::new(haystack)
1026
    ///     .anchored(Anchored::Pattern(PatternID::must(1)));
1027
    /// re.search_captures(&input, &mut caps);
1028
    /// assert_eq!(expected, caps.get_match());
1029
    ///
1030
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1031
    /// ```
1032
    ///
1033
    /// # Example: specifying the bounds of a search
1034
    ///
1035
    /// This example shows how providing the bounds of a search can produce
1036
    /// different results than simply sub-slicing the haystack.
1037
    ///
1038
    /// ```
1039
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1040
    /// use regex_automata::{meta::Regex, Match, Input};
1041
    ///
1042
    /// let re = Regex::new(r"\b[0-9]{3}\b")?;
1043
    /// let mut caps = re.create_captures();
1044
    /// let haystack = "foo123bar";
1045
    ///
1046
    /// // Since we sub-slice the haystack, the search doesn't know about
1047
    /// // the larger context and assumes that `123` is surrounded by word
1048
    /// // boundaries. And of course, the match position is reported relative
1049
    /// // to the sub-slice as well, which means we get `0..3` instead of
1050
    /// // `3..6`.
1051
    /// let expected = Some(Match::must(0, 0..3));
1052
    /// let input = Input::new(&haystack[3..6]);
1053
    /// re.search_captures(&input, &mut caps);
1054
    /// assert_eq!(expected, caps.get_match());
1055
    ///
1056
    /// // But if we provide the bounds of the search within the context of the
1057
    /// // entire haystack, then the search can take the surrounding context
1058
    /// // into account. (And if we did find a match, it would be reported
1059
    /// // as a valid offset into `haystack` instead of its sub-slice.)
1060
    /// let expected = None;
1061
    /// let input = Input::new(haystack).range(3..6);
1062
    /// re.search_captures(&input, &mut caps);
1063
    /// assert_eq!(expected, caps.get_match());
1064
    ///
1065
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1066
    /// ```
1067
    #[inline]
1068
0
    pub fn search_captures(&self, input: &Input<'_>, caps: &mut Captures) {
1069
0
        caps.set_pattern(None);
1070
0
        let pid = self.search_slots(input, caps.slots_mut());
1071
0
        caps.set_pattern(pid);
1072
0
    }
1073
1074
    /// Executes a leftmost forward search and writes the spans of capturing
1075
    /// groups that participated in a match into the provided `slots`, and
1076
    /// returns the matching pattern ID. The contents of the slots for patterns
1077
    /// other than the matching pattern are unspecified. If no match was found,
1078
    /// then `None` is returned and the contents of `slots` is unspecified.
1079
    ///
1080
    /// This is like [`Regex::search`], but it accepts a raw slots slice
1081
    /// instead of a `Captures` value. This is useful in contexts where you
1082
    /// don't want or need to allocate a `Captures`.
1083
    ///
1084
    /// It is legal to pass _any_ number of slots to this routine. If the regex
1085
    /// engine would otherwise write a slot offset that doesn't fit in the
1086
    /// provided slice, then it is simply skipped. In general though, there are
1087
    /// usually three slice lengths you might want to use:
1088
    ///
1089
    /// * An empty slice, if you only care about which pattern matched.
1090
    /// * A slice with [`pattern_len() * 2`](Regex::pattern_len) slots, if you
1091
    /// only care about the overall match spans for each matching pattern.
1092
    /// * A slice with
1093
    /// [`slot_len()`](crate::util::captures::GroupInfo::slot_len) slots, which
1094
    /// permits recording match offsets for every capturing group in every
1095
    /// pattern.
1096
    ///
1097
    /// # Example
1098
    ///
1099
    /// This example shows how to find the overall match offsets in a
1100
    /// multi-pattern search without allocating a `Captures` value. Indeed, we
1101
    /// can put our slots right on the stack.
1102
    ///
1103
    /// ```
1104
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1105
    /// use regex_automata::{meta::Regex, PatternID, Input};
1106
    ///
1107
    /// let re = Regex::new_many(&[
1108
    ///     r"\pL+",
1109
    ///     r"\d+",
1110
    /// ])?;
1111
    /// let input = Input::new("!@#123");
1112
    ///
1113
    /// // We only care about the overall match offsets here, so we just
1114
    /// // allocate two slots for each pattern. Each slot records the start
1115
    /// // and end of the match.
1116
    /// let mut slots = [None; 4];
1117
    /// let pid = re.search_slots(&input, &mut slots);
1118
    /// assert_eq!(Some(PatternID::must(1)), pid);
1119
    ///
1120
    /// // The overall match offsets are always at 'pid * 2' and 'pid * 2 + 1'.
1121
    /// // See 'GroupInfo' for more details on the mapping between groups and
1122
    /// // slot indices.
1123
    /// let slot_start = pid.unwrap().as_usize() * 2;
1124
    /// let slot_end = slot_start + 1;
1125
    /// assert_eq!(Some(3), slots[slot_start].map(|s| s.get()));
1126
    /// assert_eq!(Some(6), slots[slot_end].map(|s| s.get()));
1127
    ///
1128
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1129
    /// ```
1130
    #[inline]
1131
0
    pub fn search_slots(
1132
0
        &self,
1133
0
        input: &Input<'_>,
1134
0
        slots: &mut [Option<NonMaxUsize>],
1135
0
    ) -> Option<PatternID> {
1136
0
        if self.imp.info.captures_disabled()
1137
0
            || self.imp.info.is_impossible(input)
1138
        {
1139
0
            return None;
1140
0
        }
1141
0
        let mut guard = self.pool.get();
1142
0
        let result = self.imp.strat.search_slots(&mut guard, input, slots);
1143
        // See 'Regex::search' for why we put the guard back explicitly.
1144
0
        PoolGuard::put(guard);
1145
0
        result
1146
0
    }
1147
1148
    /// Writes the set of patterns that match anywhere in the given search
1149
    /// configuration to `patset`. If multiple patterns match at the same
1150
    /// position and this `Regex` was configured with [`MatchKind::All`]
1151
    /// semantics, then all matching patterns are written to the given set.
1152
    ///
1153
    /// Unless all of the patterns in this `Regex` are anchored, then generally
1154
    /// speaking, this will scan the entire haystack.
1155
    ///
1156
    /// This search routine *does not* clear the pattern set. This gives some
1157
    /// flexibility to the caller (e.g., running multiple searches with the
1158
    /// same pattern set), but does make the API bug-prone if you're reusing
1159
    /// the same pattern set for multiple searches but intended them to be
1160
    /// independent.
1161
    ///
1162
    /// If a pattern ID matched but the given `PatternSet` does not have
1163
    /// sufficient capacity to store it, then it is not inserted and silently
1164
    /// dropped.
1165
    ///
1166
    /// # Example
1167
    ///
1168
    /// This example shows how to find all matching patterns in a haystack,
1169
    /// even when some patterns match at the same position as other patterns.
1170
    /// It is important that we configure the `Regex` with [`MatchKind::All`]
1171
    /// semantics here, or else overlapping matches will not be reported.
1172
    ///
1173
    /// ```
1174
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1175
    /// use regex_automata::{meta::Regex, Input, MatchKind, PatternSet};
1176
    ///
1177
    /// let patterns = &[
1178
    ///     r"\w+", r"\d+", r"\pL+", r"foo", r"bar", r"barfoo", r"foobar",
1179
    /// ];
1180
    /// let re = Regex::builder()
1181
    ///     .configure(Regex::config().match_kind(MatchKind::All))
1182
    ///     .build_many(patterns)?;
1183
    ///
1184
    /// let input = Input::new("foobar");
1185
    /// let mut patset = PatternSet::new(re.pattern_len());
1186
    /// re.which_overlapping_matches(&input, &mut patset);
1187
    /// let expected = vec![0, 2, 3, 4, 6];
1188
    /// let got: Vec<usize> = patset.iter().map(|p| p.as_usize()).collect();
1189
    /// assert_eq!(expected, got);
1190
    ///
1191
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1192
    /// ```
1193
    #[inline]
1194
0
    pub fn which_overlapping_matches(
1195
0
        &self,
1196
0
        input: &Input<'_>,
1197
0
        patset: &mut PatternSet,
1198
0
    ) {
1199
0
        if self.imp.info.is_impossible(input) {
1200
0
            return;
1201
0
        }
1202
0
        let mut guard = self.pool.get();
1203
0
        let result = self
1204
0
            .imp
1205
0
            .strat
1206
0
            .which_overlapping_matches(&mut guard, input, patset);
1207
        // See 'Regex::search' for why we put the guard back explicitly.
1208
0
        PoolGuard::put(guard);
1209
0
        result
1210
0
    }
1211
}
1212
1213
/// Lower level search routines that give more control, and require the caller
1214
/// to provide an explicit [`Cache`] parameter.
1215
impl Regex {
1216
    /// This is like [`Regex::search`], but requires the caller to
1217
    /// explicitly pass a [`Cache`].
1218
    ///
1219
    /// # Why pass a `Cache` explicitly?
1220
    ///
1221
    /// Passing a `Cache` explicitly will bypass the use of an internal memory
1222
    /// pool used by `Regex` to get a `Cache` for a search. The use of this
1223
    /// pool can be slower in some cases when a `Regex` is used from multiple
1224
    /// threads simultaneously. Typically, performance only becomes an issue
1225
    /// when there is heavy contention, which in turn usually only occurs
1226
    /// when each thread's primary unit of work is a regex search on a small
1227
    /// haystack.
1228
    ///
1229
    /// # Example
1230
    ///
1231
    /// ```
1232
    /// use regex_automata::{meta::Regex, Input, Match};
1233
    ///
1234
    /// let re = Regex::new(r"Samwise|Sam")?;
1235
    /// let mut cache = re.create_cache();
1236
    /// let input = Input::new(
1237
    ///     "one of the chief characters, Samwise the Brave",
1238
    /// );
1239
    /// assert_eq!(
1240
    ///     Some(Match::must(0, 29..36)),
1241
    ///     re.search_with(&mut cache, &input),
1242
    /// );
1243
    ///
1244
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1245
    /// ```
1246
    #[inline]
1247
0
    pub fn search_with(
1248
0
        &self,
1249
0
        cache: &mut Cache,
1250
0
        input: &Input<'_>,
1251
0
    ) -> Option<Match> {
1252
0
        if self.imp.info.captures_disabled()
1253
0
            || self.imp.info.is_impossible(input)
1254
        {
1255
0
            return None;
1256
0
        }
1257
0
        self.imp.strat.search(cache, input)
1258
0
    }
1259
1260
    /// This is like [`Regex::search_half`], but requires the caller to
1261
    /// explicitly pass a [`Cache`].
1262
    ///
1263
    /// # Why pass a `Cache` explicitly?
1264
    ///
1265
    /// Passing a `Cache` explicitly will bypass the use of an internal memory
1266
    /// pool used by `Regex` to get a `Cache` for a search. The use of this
1267
    /// pool can be slower in some cases when a `Regex` is used from multiple
1268
    /// threads simultaneously. Typically, performance only becomes an issue
1269
    /// when there is heavy contention, which in turn usually only occurs
1270
    /// when each thread's primary unit of work is a regex search on a small
1271
    /// haystack.
1272
    ///
1273
    /// # Example
1274
    ///
1275
    /// ```
1276
    /// use regex_automata::{meta::Regex, Input, HalfMatch};
1277
    ///
1278
    /// let re = Regex::new(r"Samwise|Sam")?;
1279
    /// let mut cache = re.create_cache();
1280
    /// let input = Input::new(
1281
    ///     "one of the chief characters, Samwise the Brave",
1282
    /// );
1283
    /// assert_eq!(
1284
    ///     Some(HalfMatch::must(0, 36)),
1285
    ///     re.search_half_with(&mut cache, &input),
1286
    /// );
1287
    ///
1288
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1289
    /// ```
1290
    #[inline]
1291
0
    pub fn search_half_with(
1292
0
        &self,
1293
0
        cache: &mut Cache,
1294
0
        input: &Input<'_>,
1295
0
    ) -> Option<HalfMatch> {
1296
0
        if self.imp.info.captures_disabled()
1297
0
            || self.imp.info.is_impossible(input)
1298
        {
1299
0
            return None;
1300
0
        }
1301
0
        self.imp.strat.search_half(cache, input)
1302
0
    }
1303
1304
    /// This is like [`Regex::search_captures`], but requires the caller to
1305
    /// explicitly pass a [`Cache`].
1306
    ///
1307
    /// # Why pass a `Cache` explicitly?
1308
    ///
1309
    /// Passing a `Cache` explicitly will bypass the use of an internal memory
1310
    /// pool used by `Regex` to get a `Cache` for a search. The use of this
1311
    /// pool can be slower in some cases when a `Regex` is used from multiple
1312
    /// threads simultaneously. Typically, performance only becomes an issue
1313
    /// when there is heavy contention, which in turn usually only occurs
1314
    /// when each thread's primary unit of work is a regex search on a small
1315
    /// haystack.
1316
    ///
1317
    /// # Example: specific pattern search
1318
    ///
1319
    /// This example shows how to build a multi-pattern `Regex` that permits
1320
    /// searching for specific patterns.
1321
    ///
1322
    /// ```
1323
    /// use regex_automata::{
1324
    ///     meta::Regex,
1325
    ///     Anchored, Match, PatternID, Input,
1326
    /// };
1327
    ///
1328
    /// let re = Regex::new_many(&["[a-z0-9]{6}", "[a-z][a-z0-9]{5}"])?;
1329
    /// let (mut cache, mut caps) = (re.create_cache(), re.create_captures());
1330
    /// let haystack = "foo123";
1331
    ///
1332
    /// // Since we are using the default leftmost-first match and both
1333
    /// // patterns match at the same starting position, only the first pattern
1334
    /// // will be returned in this case when doing a search for any of the
1335
    /// // patterns.
1336
    /// let expected = Some(Match::must(0, 0..6));
1337
    /// re.search_captures_with(&mut cache, &Input::new(haystack), &mut caps);
1338
    /// assert_eq!(expected, caps.get_match());
1339
    ///
1340
    /// // But if we want to check whether some other pattern matches, then we
1341
    /// // can provide its pattern ID.
1342
    /// let expected = Some(Match::must(1, 0..6));
1343
    /// let input = Input::new(haystack)
1344
    ///     .anchored(Anchored::Pattern(PatternID::must(1)));
1345
    /// re.search_captures_with(&mut cache, &input, &mut caps);
1346
    /// assert_eq!(expected, caps.get_match());
1347
    ///
1348
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1349
    /// ```
1350
    ///
1351
    /// # Example: specifying the bounds of a search
1352
    ///
1353
    /// This example shows how providing the bounds of a search can produce
1354
    /// different results than simply sub-slicing the haystack.
1355
    ///
1356
    /// ```
1357
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1358
    /// use regex_automata::{meta::Regex, Match, Input};
1359
    ///
1360
    /// let re = Regex::new(r"\b[0-9]{3}\b")?;
1361
    /// let (mut cache, mut caps) = (re.create_cache(), re.create_captures());
1362
    /// let haystack = "foo123bar";
1363
    ///
1364
    /// // Since we sub-slice the haystack, the search doesn't know about
1365
    /// // the larger context and assumes that `123` is surrounded by word
1366
    /// // boundaries. And of course, the match position is reported relative
1367
    /// // to the sub-slice as well, which means we get `0..3` instead of
1368
    /// // `3..6`.
1369
    /// let expected = Some(Match::must(0, 0..3));
1370
    /// let input = Input::new(&haystack[3..6]);
1371
    /// re.search_captures_with(&mut cache, &input, &mut caps);
1372
    /// assert_eq!(expected, caps.get_match());
1373
    ///
1374
    /// // But if we provide the bounds of the search within the context of the
1375
    /// // entire haystack, then the search can take the surrounding context
1376
    /// // into account. (And if we did find a match, it would be reported
1377
    /// // as a valid offset into `haystack` instead of its sub-slice.)
1378
    /// let expected = None;
1379
    /// let input = Input::new(haystack).range(3..6);
1380
    /// re.search_captures_with(&mut cache, &input, &mut caps);
1381
    /// assert_eq!(expected, caps.get_match());
1382
    ///
1383
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1384
    /// ```
1385
    #[inline]
1386
0
    pub fn search_captures_with(
1387
0
        &self,
1388
0
        cache: &mut Cache,
1389
0
        input: &Input<'_>,
1390
0
        caps: &mut Captures,
1391
0
    ) {
1392
0
        caps.set_pattern(None);
1393
0
        let pid = self.search_slots_with(cache, input, caps.slots_mut());
1394
0
        caps.set_pattern(pid);
1395
0
    }
1396
1397
    /// This is like [`Regex::search_slots`], but requires the caller to
1398
    /// explicitly pass a [`Cache`].
1399
    ///
1400
    /// # Why pass a `Cache` explicitly?
1401
    ///
1402
    /// Passing a `Cache` explicitly will bypass the use of an internal memory
1403
    /// pool used by `Regex` to get a `Cache` for a search. The use of this
1404
    /// pool can be slower in some cases when a `Regex` is used from multiple
1405
    /// threads simultaneously. Typically, performance only becomes an issue
1406
    /// when there is heavy contention, which in turn usually only occurs
1407
    /// when each thread's primary unit of work is a regex search on a small
1408
    /// haystack.
1409
    ///
1410
    /// # Example
1411
    ///
1412
    /// This example shows how to find the overall match offsets in a
1413
    /// multi-pattern search without allocating a `Captures` value. Indeed, we
1414
    /// can put our slots right on the stack.
1415
    ///
1416
    /// ```
1417
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1418
    /// use regex_automata::{meta::Regex, PatternID, Input};
1419
    ///
1420
    /// let re = Regex::new_many(&[
1421
    ///     r"\pL+",
1422
    ///     r"\d+",
1423
    /// ])?;
1424
    /// let mut cache = re.create_cache();
1425
    /// let input = Input::new("!@#123");
1426
    ///
1427
    /// // We only care about the overall match offsets here, so we just
1428
    /// // allocate two slots for each pattern. Each slot records the start
1429
    /// // and end of the match.
1430
    /// let mut slots = [None; 4];
1431
    /// let pid = re.search_slots_with(&mut cache, &input, &mut slots);
1432
    /// assert_eq!(Some(PatternID::must(1)), pid);
1433
    ///
1434
    /// // The overall match offsets are always at 'pid * 2' and 'pid * 2 + 1'.
1435
    /// // See 'GroupInfo' for more details on the mapping between groups and
1436
    /// // slot indices.
1437
    /// let slot_start = pid.unwrap().as_usize() * 2;
1438
    /// let slot_end = slot_start + 1;
1439
    /// assert_eq!(Some(3), slots[slot_start].map(|s| s.get()));
1440
    /// assert_eq!(Some(6), slots[slot_end].map(|s| s.get()));
1441
    ///
1442
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1443
    /// ```
1444
    #[inline]
1445
0
    pub fn search_slots_with(
1446
0
        &self,
1447
0
        cache: &mut Cache,
1448
0
        input: &Input<'_>,
1449
0
        slots: &mut [Option<NonMaxUsize>],
1450
0
    ) -> Option<PatternID> {
1451
0
        if self.imp.info.captures_disabled()
1452
0
            || self.imp.info.is_impossible(input)
1453
        {
1454
0
            return None;
1455
0
        }
1456
0
        self.imp.strat.search_slots(cache, input, slots)
1457
0
    }
1458
1459
    /// This is like [`Regex::which_overlapping_matches`], but requires the
1460
    /// caller to explicitly pass a [`Cache`].
1461
    ///
1462
    /// Passing a `Cache` explicitly will bypass the use of an internal memory
1463
    /// pool used by `Regex` to get a `Cache` for a search. The use of this
1464
    /// pool can be slower in some cases when a `Regex` is used from multiple
1465
    /// threads simultaneously. Typically, performance only becomes an issue
1466
    /// when there is heavy contention, which in turn usually only occurs
1467
    /// when each thread's primary unit of work is a regex search on a small
1468
    /// haystack.
1469
    ///
1470
    /// # Why pass a `Cache` explicitly?
1471
    ///
1472
    /// # Example
1473
    ///
1474
    /// ```
1475
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1476
    /// use regex_automata::{meta::Regex, Input, MatchKind, PatternSet};
1477
    ///
1478
    /// let patterns = &[
1479
    ///     r"\w+", r"\d+", r"\pL+", r"foo", r"bar", r"barfoo", r"foobar",
1480
    /// ];
1481
    /// let re = Regex::builder()
1482
    ///     .configure(Regex::config().match_kind(MatchKind::All))
1483
    ///     .build_many(patterns)?;
1484
    /// let mut cache = re.create_cache();
1485
    ///
1486
    /// let input = Input::new("foobar");
1487
    /// let mut patset = PatternSet::new(re.pattern_len());
1488
    /// re.which_overlapping_matches_with(&mut cache, &input, &mut patset);
1489
    /// let expected = vec![0, 2, 3, 4, 6];
1490
    /// let got: Vec<usize> = patset.iter().map(|p| p.as_usize()).collect();
1491
    /// assert_eq!(expected, got);
1492
    ///
1493
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1494
    /// ```
1495
    #[inline]
1496
0
    pub fn which_overlapping_matches_with(
1497
0
        &self,
1498
0
        cache: &mut Cache,
1499
0
        input: &Input<'_>,
1500
0
        patset: &mut PatternSet,
1501
0
    ) {
1502
0
        if self.imp.info.is_impossible(input) {
1503
0
            return;
1504
0
        }
1505
0
        self.imp.strat.which_overlapping_matches(cache, input, patset)
1506
0
    }
1507
}
1508
1509
/// Various non-search routines for querying properties of a `Regex` and
1510
/// convenience routines for creating [`Captures`] and [`Cache`] values.
1511
impl Regex {
1512
    /// Creates a new object for recording capture group offsets. This is used
1513
    /// in search APIs like [`Regex::captures`] and [`Regex::search_captures`].
1514
    ///
1515
    /// This is a convenience routine for
1516
    /// `Captures::all(re.group_info().clone())`. Callers may build other types
1517
    /// of `Captures` values that record less information (and thus require
1518
    /// less work from the regex engine) using [`Captures::matches`] and
1519
    /// [`Captures::empty`].
1520
    ///
1521
    /// # Example
1522
    ///
1523
    /// This shows some alternatives to [`Regex::create_captures`]:
1524
    ///
1525
    /// ```
1526
    /// use regex_automata::{
1527
    ///     meta::Regex,
1528
    ///     util::captures::Captures,
1529
    ///     Match, PatternID, Span,
1530
    /// };
1531
    ///
1532
    /// let re = Regex::new(r"(?<first>[A-Z][a-z]+) (?<last>[A-Z][a-z]+)")?;
1533
    ///
1534
    /// // This is equivalent to Regex::create_captures. It stores matching
1535
    /// // offsets for all groups in the regex.
1536
    /// let mut all = Captures::all(re.group_info().clone());
1537
    /// re.captures("Bruce Springsteen", &mut all);
1538
    /// assert_eq!(Some(Match::must(0, 0..17)), all.get_match());
1539
    /// assert_eq!(Some(Span::from(0..5)), all.get_group_by_name("first"));
1540
    /// assert_eq!(Some(Span::from(6..17)), all.get_group_by_name("last"));
1541
    ///
1542
    /// // In this version, we only care about the implicit groups, which
1543
    /// // means offsets for the explicit groups will be unavailable. It can
1544
    /// // sometimes be faster to ask for fewer groups, since the underlying
1545
    /// // regex engine needs to do less work to keep track of them.
1546
    /// let mut matches = Captures::matches(re.group_info().clone());
1547
    /// re.captures("Bruce Springsteen", &mut matches);
1548
    /// // We still get the overall match info.
1549
    /// assert_eq!(Some(Match::must(0, 0..17)), matches.get_match());
1550
    /// // But now the explicit groups are unavailable.
1551
    /// assert_eq!(None, matches.get_group_by_name("first"));
1552
    /// assert_eq!(None, matches.get_group_by_name("last"));
1553
    ///
1554
    /// // Finally, in this version, we don't ask to keep track of offsets for
1555
    /// // *any* groups. All we get back is whether a match occurred, and if
1556
    /// // so, the ID of the pattern that matched.
1557
    /// let mut empty = Captures::empty(re.group_info().clone());
1558
    /// re.captures("Bruce Springsteen", &mut empty);
1559
    /// // it's a match!
1560
    /// assert!(empty.is_match());
1561
    /// // for pattern ID 0
1562
    /// assert_eq!(Some(PatternID::ZERO), empty.pattern());
1563
    /// // Match offsets are unavailable.
1564
    /// assert_eq!(None, empty.get_match());
1565
    /// // And of course, explicit groups are unavailable too.
1566
    /// assert_eq!(None, empty.get_group_by_name("first"));
1567
    /// assert_eq!(None, empty.get_group_by_name("last"));
1568
    ///
1569
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1570
    /// ```
1571
0
    pub fn create_captures(&self) -> Captures {
1572
0
        Captures::all(self.group_info().clone())
1573
0
    }
1574
1575
    /// Creates a new cache for use with lower level search APIs like
1576
    /// [`Regex::search_with`].
1577
    ///
1578
    /// The cache returned should only be used for searches for this `Regex`.
1579
    /// If you want to reuse the cache for another `Regex`, then you must call
1580
    /// [`Cache::reset`] with that `Regex`.
1581
    ///
1582
    /// This is a convenience routine for [`Cache::new`].
1583
    ///
1584
    /// # Example
1585
    ///
1586
    /// ```
1587
    /// use regex_automata::{meta::Regex, Input, Match};
1588
    ///
1589
    /// let re = Regex::new(r"(?-u)m\w+\s+m\w+")?;
1590
    /// let mut cache = re.create_cache();
1591
    /// let input = Input::new("crazy janey and her mission man");
1592
    /// assert_eq!(
1593
    ///     Some(Match::must(0, 20..31)),
1594
    ///     re.search_with(&mut cache, &input),
1595
    /// );
1596
    ///
1597
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1598
    /// ```
1599
0
    pub fn create_cache(&self) -> Cache {
1600
0
        self.imp.strat.create_cache()
1601
0
    }
1602
1603
    /// Returns the total number of patterns in this regex.
1604
    ///
1605
    /// The standard [`Regex::new`] constructor always results in a `Regex`
1606
    /// with a single pattern, but [`Regex::new_many`] permits building a
1607
    /// multi-pattern regex.
1608
    ///
1609
    /// A `Regex` guarantees that the maximum possible `PatternID` returned in
1610
    /// any match is `Regex::pattern_len() - 1`. In the case where the number
1611
    /// of patterns is `0`, a match is impossible.
1612
    ///
1613
    /// # Example
1614
    ///
1615
    /// ```
1616
    /// use regex_automata::meta::Regex;
1617
    ///
1618
    /// let re = Regex::new(r"(?m)^[a-z]$")?;
1619
    /// assert_eq!(1, re.pattern_len());
1620
    ///
1621
    /// let re = Regex::new_many::<&str>(&[])?;
1622
    /// assert_eq!(0, re.pattern_len());
1623
    ///
1624
    /// let re = Regex::new_many(&["a", "b", "c"])?;
1625
    /// assert_eq!(3, re.pattern_len());
1626
    ///
1627
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1628
    /// ```
1629
0
    pub fn pattern_len(&self) -> usize {
1630
0
        self.imp.info.pattern_len()
1631
0
    }
1632
1633
    /// Returns the total number of capturing groups.
1634
    ///
1635
    /// This includes the implicit capturing group corresponding to the
1636
    /// entire match. Therefore, the minimum value returned is `1`.
1637
    ///
1638
    /// # Example
1639
    ///
1640
    /// This shows a few patterns and how many capture groups they have.
1641
    ///
1642
    /// ```
1643
    /// use regex_automata::meta::Regex;
1644
    ///
1645
    /// let len = |pattern| {
1646
    ///     Regex::new(pattern).map(|re| re.captures_len())
1647
    /// };
1648
    ///
1649
    /// assert_eq!(1, len("a")?);
1650
    /// assert_eq!(2, len("(a)")?);
1651
    /// assert_eq!(3, len("(a)|(b)")?);
1652
    /// assert_eq!(5, len("(a)(b)|(c)(d)")?);
1653
    /// assert_eq!(2, len("(a)|b")?);
1654
    /// assert_eq!(2, len("a|(b)")?);
1655
    /// assert_eq!(2, len("(b)*")?);
1656
    /// assert_eq!(2, len("(b)+")?);
1657
    ///
1658
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1659
    /// ```
1660
    ///
1661
    /// # Example: multiple patterns
1662
    ///
1663
    /// This routine also works for multiple patterns. The total number is
1664
    /// the sum of the capture groups of each pattern.
1665
    ///
1666
    /// ```
1667
    /// use regex_automata::meta::Regex;
1668
    ///
1669
    /// let len = |patterns| {
1670
    ///     Regex::new_many(patterns).map(|re| re.captures_len())
1671
    /// };
1672
    ///
1673
    /// assert_eq!(2, len(&["a", "b"])?);
1674
    /// assert_eq!(4, len(&["(a)", "(b)"])?);
1675
    /// assert_eq!(6, len(&["(a)|(b)", "(c)|(d)"])?);
1676
    /// assert_eq!(8, len(&["(a)(b)|(c)(d)", "(x)(y)"])?);
1677
    /// assert_eq!(3, len(&["(a)", "b"])?);
1678
    /// assert_eq!(3, len(&["a", "(b)"])?);
1679
    /// assert_eq!(4, len(&["(a)", "(b)*"])?);
1680
    /// assert_eq!(4, len(&["(a)+", "(b)+"])?);
1681
    ///
1682
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1683
    /// ```
1684
0
    pub fn captures_len(&self) -> usize {
1685
0
        self.imp
1686
0
            .info
1687
0
            .props_union()
1688
0
            .explicit_captures_len()
1689
0
            .saturating_add(self.pattern_len())
1690
0
    }
1691
1692
    /// Returns the total number of capturing groups that appear in every
1693
    /// possible match.
1694
    ///
1695
    /// If the number of capture groups can vary depending on the match, then
1696
    /// this returns `None`. That is, a value is only returned when the number
1697
    /// of matching groups is invariant or "static."
1698
    ///
1699
    /// Note that like [`Regex::captures_len`], this **does** include the
1700
    /// implicit capturing group corresponding to the entire match. Therefore,
1701
    /// when a non-None value is returned, it is guaranteed to be at least `1`.
1702
    /// Stated differently, a return value of `Some(0)` is impossible.
1703
    ///
1704
    /// # Example
1705
    ///
1706
    /// This shows a few cases where a static number of capture groups is
1707
    /// available and a few cases where it is not.
1708
    ///
1709
    /// ```
1710
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1711
    ///
1712
    /// use regex_automata::meta::Regex;
1713
    ///
1714
    /// let len = |pattern| {
1715
    ///     Regex::new(pattern).map(|re| re.static_captures_len())
1716
    /// };
1717
    ///
1718
    /// assert_eq!(Some(1), len("a")?);
1719
    /// assert_eq!(Some(2), len("(a)")?);
1720
    /// assert_eq!(Some(2), len("(a)|(b)")?);
1721
    /// assert_eq!(Some(3), len("(a)(b)|(c)(d)")?);
1722
    /// assert_eq!(None, len("(a)|b")?);
1723
    /// assert_eq!(None, len("a|(b)")?);
1724
    /// assert_eq!(None, len("(b)*")?);
1725
    /// assert_eq!(Some(2), len("(b)+")?);
1726
    ///
1727
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1728
    /// ```
1729
    ///
1730
    /// # Example: multiple patterns
1731
    ///
1732
    /// This property extends to regexes with multiple patterns as well. In
1733
    /// order for their to be a static number of capture groups in this case,
1734
    /// every pattern must have the same static number.
1735
    ///
1736
    /// ```
1737
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
1738
    ///
1739
    /// use regex_automata::meta::Regex;
1740
    ///
1741
    /// let len = |patterns| {
1742
    ///     Regex::new_many(patterns).map(|re| re.static_captures_len())
1743
    /// };
1744
    ///
1745
    /// assert_eq!(Some(1), len(&["a", "b"])?);
1746
    /// assert_eq!(Some(2), len(&["(a)", "(b)"])?);
1747
    /// assert_eq!(Some(2), len(&["(a)|(b)", "(c)|(d)"])?);
1748
    /// assert_eq!(Some(3), len(&["(a)(b)|(c)(d)", "(x)(y)"])?);
1749
    /// assert_eq!(None, len(&["(a)", "b"])?);
1750
    /// assert_eq!(None, len(&["a", "(b)"])?);
1751
    /// assert_eq!(None, len(&["(a)", "(b)*"])?);
1752
    /// assert_eq!(Some(2), len(&["(a)+", "(b)+"])?);
1753
    ///
1754
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1755
    /// ```
1756
    #[inline]
1757
0
    pub fn static_captures_len(&self) -> Option<usize> {
1758
0
        self.imp
1759
0
            .info
1760
0
            .props_union()
1761
0
            .static_explicit_captures_len()
1762
0
            .map(|len| len.saturating_add(1))
1763
0
    }
1764
1765
    /// Return information about the capture groups in this `Regex`.
1766
    ///
1767
    /// A `GroupInfo` is an immutable object that can be cheaply cloned. It
1768
    /// is responsible for maintaining a mapping between the capture groups
1769
    /// in the concrete syntax of zero or more regex patterns and their
1770
    /// internal representation used by some of the regex matchers. It is also
1771
    /// responsible for maintaining a mapping between the name of each group
1772
    /// (if one exists) and its corresponding group index.
1773
    ///
1774
    /// A `GroupInfo` is ultimately what is used to build a [`Captures`] value,
1775
    /// which is some mutable space where group offsets are stored as a result
1776
    /// of a search.
1777
    ///
1778
    /// # Example
1779
    ///
1780
    /// This shows some alternatives to [`Regex::create_captures`]:
1781
    ///
1782
    /// ```
1783
    /// use regex_automata::{
1784
    ///     meta::Regex,
1785
    ///     util::captures::Captures,
1786
    ///     Match, PatternID, Span,
1787
    /// };
1788
    ///
1789
    /// let re = Regex::new(r"(?<first>[A-Z][a-z]+) (?<last>[A-Z][a-z]+)")?;
1790
    ///
1791
    /// // This is equivalent to Regex::create_captures. It stores matching
1792
    /// // offsets for all groups in the regex.
1793
    /// let mut all = Captures::all(re.group_info().clone());
1794
    /// re.captures("Bruce Springsteen", &mut all);
1795
    /// assert_eq!(Some(Match::must(0, 0..17)), all.get_match());
1796
    /// assert_eq!(Some(Span::from(0..5)), all.get_group_by_name("first"));
1797
    /// assert_eq!(Some(Span::from(6..17)), all.get_group_by_name("last"));
1798
    ///
1799
    /// // In this version, we only care about the implicit groups, which
1800
    /// // means offsets for the explicit groups will be unavailable. It can
1801
    /// // sometimes be faster to ask for fewer groups, since the underlying
1802
    /// // regex engine needs to do less work to keep track of them.
1803
    /// let mut matches = Captures::matches(re.group_info().clone());
1804
    /// re.captures("Bruce Springsteen", &mut matches);
1805
    /// // We still get the overall match info.
1806
    /// assert_eq!(Some(Match::must(0, 0..17)), matches.get_match());
1807
    /// // But now the explicit groups are unavailable.
1808
    /// assert_eq!(None, matches.get_group_by_name("first"));
1809
    /// assert_eq!(None, matches.get_group_by_name("last"));
1810
    ///
1811
    /// // Finally, in this version, we don't ask to keep track of offsets for
1812
    /// // *any* groups. All we get back is whether a match occurred, and if
1813
    /// // so, the ID of the pattern that matched.
1814
    /// let mut empty = Captures::empty(re.group_info().clone());
1815
    /// re.captures("Bruce Springsteen", &mut empty);
1816
    /// // it's a match!
1817
    /// assert!(empty.is_match());
1818
    /// // for pattern ID 0
1819
    /// assert_eq!(Some(PatternID::ZERO), empty.pattern());
1820
    /// // Match offsets are unavailable.
1821
    /// assert_eq!(None, empty.get_match());
1822
    /// // And of course, explicit groups are unavailable too.
1823
    /// assert_eq!(None, empty.get_group_by_name("first"));
1824
    /// assert_eq!(None, empty.get_group_by_name("last"));
1825
    ///
1826
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1827
    /// ```
1828
    #[inline]
1829
0
    pub fn group_info(&self) -> &GroupInfo {
1830
0
        self.imp.strat.group_info()
1831
0
    }
1832
1833
    /// Returns the configuration object used to build this `Regex`.
1834
    ///
1835
    /// If no configuration object was explicitly passed, then the
1836
    /// configuration returned represents the default.
1837
    #[inline]
1838
0
    pub fn get_config(&self) -> &Config {
1839
0
        self.imp.info.config()
1840
0
    }
1841
1842
    /// Returns true if this regex has a high chance of being "accelerated."
1843
    ///
1844
    /// The precise meaning of "accelerated" is specifically left unspecified,
1845
    /// but the general meaning is that the search is a high likelihood of
1846
    /// running faster than a character-at-a-time loop inside a standard
1847
    /// regex engine.
1848
    ///
1849
    /// When a regex is accelerated, it is only a *probabilistic* claim. That
1850
    /// is, just because the regex is believed to be accelerated, that doesn't
1851
    /// mean it will definitely execute searches very fast. Similarly, if a
1852
    /// regex is *not* accelerated, that is also a probabilistic claim. That
1853
    /// is, a regex for which `is_accelerated` returns `false` could still run
1854
    /// searches more quickly than a regex for which `is_accelerated` returns
1855
    /// `true`.
1856
    ///
1857
    /// Whether a regex is marked as accelerated or not is dependent on
1858
    /// implementations details that may change in a semver compatible release.
1859
    /// That is, a regex that is accelerated in a `x.y.1` release might not be
1860
    /// accelerated in a `x.y.2` release.
1861
    ///
1862
    /// Basically, the value of acceleration boils down to a hedge: a hodge
1863
    /// podge of internal heuristics combine to make a probabilistic guess
1864
    /// that this regex search may run "fast." The value in knowing this from
1865
    /// a caller's perspective is that it may act as a signal that no further
1866
    /// work should be done to accelerate a search. For example, a grep-like
1867
    /// tool might try to do some extra work extracting literals from a regex
1868
    /// to create its own heuristic acceleration strategies. But it might
1869
    /// choose to defer to this crate's acceleration strategy if one exists.
1870
    /// This routine permits querying whether such a strategy is active for a
1871
    /// particular regex.
1872
    ///
1873
    /// # Example
1874
    ///
1875
    /// ```
1876
    /// use regex_automata::meta::Regex;
1877
    ///
1878
    /// // A simple literal is very likely to be accelerated.
1879
    /// let re = Regex::new(r"foo")?;
1880
    /// assert!(re.is_accelerated());
1881
    ///
1882
    /// // A regex with no literals is likely to not be accelerated.
1883
    /// let re = Regex::new(r"\w")?;
1884
    /// assert!(!re.is_accelerated());
1885
    ///
1886
    /// # Ok::<(), Box<dyn std::error::Error>>(())
1887
    /// ```
1888
    #[inline]
1889
0
    pub fn is_accelerated(&self) -> bool {
1890
0
        self.imp.strat.is_accelerated()
1891
0
    }
1892
1893
    /// Return the total approximate heap memory, in bytes, used by this `Regex`.
1894
    ///
1895
    /// Note that currently, there is no high level configuration for setting
1896
    /// a limit on the specific value returned by this routine. Instead, the
1897
    /// following routines can be used to control heap memory at a bit of a
1898
    /// lower level:
1899
    ///
1900
    /// * [`Config::nfa_size_limit`] controls how big _any_ of the NFAs are
1901
    /// allowed to be.
1902
    /// * [`Config::onepass_size_limit`] controls how big the one-pass DFA is
1903
    /// allowed to be.
1904
    /// * [`Config::hybrid_cache_capacity`] controls how much memory the lazy
1905
    /// DFA is permitted to allocate to store its transition table.
1906
    /// * [`Config::dfa_size_limit`] controls how big a fully compiled DFA is
1907
    /// allowed to be.
1908
    /// * [`Config::dfa_state_limit`] controls the conditions under which the
1909
    /// meta regex engine will even attempt to build a fully compiled DFA.
1910
    #[inline]
1911
0
    pub fn memory_usage(&self) -> usize {
1912
0
        self.imp.strat.memory_usage()
1913
0
    }
1914
}
1915
1916
impl Clone for Regex {
1917
0
    fn clone(&self) -> Regex {
1918
0
        let imp = Arc::clone(&self.imp);
1919
0
        let pool = {
1920
0
            let strat = Arc::clone(&imp.strat);
1921
0
            let create: CachePoolFn = Box::new(move || strat.create_cache());
1922
0
            Pool::with_capacity(
1923
0
                self.imp.info.config().get_pool_capacity(),
1924
0
                create,
1925
            )
1926
        };
1927
0
        Regex { imp, pool }
1928
0
    }
1929
}
1930
1931
#[derive(Clone, Debug)]
1932
pub(crate) struct RegexInfo(Arc<RegexInfoI>);
1933
1934
#[derive(Clone, Debug)]
1935
struct RegexInfoI {
1936
    config: Config,
1937
    props: Vec<hir::Properties>,
1938
    props_union: hir::Properties,
1939
}
1940
1941
impl RegexInfo {
1942
    /// Creates a new `RegexInfo` from the configuration and HIRs that make up
1943
    /// a meta regex.
1944
    ///
1945
    /// This is exported for use in some tests.
1946
0
    pub(super) fn new(config: Config, hirs: &[&Hir]) -> RegexInfo {
1947
        // Collect all of the properties from each of the HIRs, and also
1948
        // union them into one big set of properties representing all HIRs
1949
        // as if they were in one big alternation.
1950
0
        let mut props = vec![];
1951
0
        for hir in hirs.iter() {
1952
0
            props.push(hir.properties().clone());
1953
0
        }
1954
0
        let props_union = hir::Properties::union(&props);
1955
1956
0
        RegexInfo(Arc::new(RegexInfoI { config, props, props_union }))
1957
0
    }
1958
1959
0
    pub(crate) fn config(&self) -> &Config {
1960
0
        &self.0.config
1961
0
    }
1962
1963
0
    pub(crate) fn props(&self) -> &[hir::Properties] {
1964
0
        &self.0.props
1965
0
    }
1966
1967
0
    pub(crate) fn props_union(&self) -> &hir::Properties {
1968
0
        &self.0.props_union
1969
0
    }
1970
1971
0
    pub(crate) fn pattern_len(&self) -> usize {
1972
0
        self.props().len()
1973
0
    }
1974
1975
0
    pub(crate) fn memory_usage(&self) -> usize {
1976
0
        self.props().iter().map(|p| p.memory_usage()).sum::<usize>()
1977
0
            + self.props_union().memory_usage()
1978
0
    }
1979
1980
    /// Returns true when the search is guaranteed to be anchored. That is,
1981
    /// when a match is reported, its offset is guaranteed to correspond to
1982
    /// the start of the search.
1983
    ///
1984
    /// This includes returning true when `input` _isn't_ anchored but the
1985
    /// underlying regex is.
1986
    #[cfg_attr(feature = "perf-inline", inline(always))]
1987
0
    pub(crate) fn is_anchored_start(&self, input: &Input<'_>) -> bool {
1988
0
        input.get_anchored().is_anchored() || self.is_always_anchored_start()
1989
0
    }
1990
1991
    /// Returns true when this regex is always anchored to the start of a
1992
    /// search. And in particular, that regardless of an `Input` configuration,
1993
    /// if any match is reported it must start at `0`.
1994
    #[cfg_attr(feature = "perf-inline", inline(always))]
1995
0
    pub(crate) fn is_always_anchored_start(&self) -> bool {
1996
        use regex_syntax::hir::Look;
1997
0
        self.props_union().look_set_prefix().contains(Look::Start)
1998
0
    }
1999
2000
    /// Returns true when this regex is always anchored to the end of a
2001
    /// search. And in particular, that regardless of an `Input` configuration,
2002
    /// if any match is reported it must end at the end of the haystack.
2003
    #[cfg_attr(feature = "perf-inline", inline(always))]
2004
0
    pub(crate) fn is_always_anchored_end(&self) -> bool {
2005
        use regex_syntax::hir::Look;
2006
0
        self.props_union().look_set_suffix().contains(Look::End)
2007
0
    }
2008
2009
    /// Returns true when the regex's NFA lacks capture states.
2010
    ///
2011
    /// In this case, some regex engines (like the PikeVM) are unable to report
2012
    /// match offsets, while some (like the lazy DFA can). To avoid whether a
2013
    /// match or not is reported based on engine selection, routines that
2014
    /// return match offsets will _always_ report `None` when this is true.
2015
    ///
2016
    /// Yes, this is a weird case and it's a little fucked up. But
2017
    /// `WhichCaptures::None` comes with an appropriate warning.
2018
0
    fn captures_disabled(&self) -> bool {
2019
0
        matches!(self.config().get_which_captures(), WhichCaptures::None)
2020
0
    }
2021
2022
    /// Returns true if and only if it is known that a match is impossible
2023
    /// for the given input. This is useful for short-circuiting and avoiding
2024
    /// running the regex engine if it's known no match can be reported.
2025
    ///
2026
    /// Note that this doesn't necessarily detect every possible case. For
2027
    /// example, when `pattern_len() == 0`, a match is impossible, but that
2028
    /// case is so rare that it's fine to be handled by the regex engine
2029
    /// itself. That is, it's not worth the cost of adding it here in order to
2030
    /// make it a little faster. The reason is that this is called for every
2031
    /// search. so there is some cost to adding checks here. Arguably, some of
2032
    /// the checks that are here already probably shouldn't be here...
2033
    #[cfg_attr(feature = "perf-inline", inline(always))]
2034
0
    fn is_impossible(&self, input: &Input<'_>) -> bool {
2035
        // The underlying regex is anchored, so if we don't start the search
2036
        // at position 0, a match is impossible, because the anchor can only
2037
        // match at position 0.
2038
0
        if input.start() > 0 && self.is_always_anchored_start() {
2039
0
            return true;
2040
0
        }
2041
        // Same idea, but for the end anchor.
2042
0
        if input.end() < input.haystack().len()
2043
0
            && self.is_always_anchored_end()
2044
        {
2045
0
            return true;
2046
0
        }
2047
        // If the haystack is smaller than the minimum length required, then
2048
        // we know there can be no match.
2049
0
        let minlen = match self.props_union().minimum_len() {
2050
0
            None => return false,
2051
0
            Some(minlen) => minlen,
2052
        };
2053
0
        if input.get_span().len() < minlen {
2054
0
            return true;
2055
0
        }
2056
        // Same idea as minimum, but for maximum. This is trickier. We can
2057
        // only apply the maximum when we know the entire span that we're
2058
        // searching *has* to match according to the regex (and possibly the
2059
        // input configuration). If we know there is too much for the regex
2060
        // to match, we can bail early.
2061
        //
2062
        // I don't think we can apply the maximum otherwise unfortunately.
2063
0
        if self.is_anchored_start(input) && self.is_always_anchored_end() {
2064
0
            let maxlen = match self.props_union().maximum_len() {
2065
0
                None => return false,
2066
0
                Some(maxlen) => maxlen,
2067
            };
2068
0
            if input.get_span().len() > maxlen {
2069
0
                return true;
2070
0
            }
2071
0
        }
2072
0
        false
2073
0
    }
2074
}
2075
2076
/// An iterator over all non-overlapping matches.
2077
///
2078
/// The iterator yields a [`Match`] value until no more matches could be found.
2079
///
2080
/// The lifetime parameters are as follows:
2081
///
2082
/// * `'r` represents the lifetime of the `Regex` that produced this iterator.
2083
/// * `'h` represents the lifetime of the haystack being searched.
2084
///
2085
/// This iterator can be created with the [`Regex::find_iter`] method.
2086
#[derive(Debug)]
2087
pub struct FindMatches<'r, 'h> {
2088
    re: &'r Regex,
2089
    cache: CachePoolGuard<'r>,
2090
    it: iter::Searcher<'h>,
2091
}
2092
2093
impl<'r, 'h> FindMatches<'r, 'h> {
2094
    /// Returns the `Regex` value that created this iterator.
2095
    #[inline]
2096
0
    pub fn regex(&self) -> &'r Regex {
2097
0
        self.re
2098
0
    }
2099
2100
    /// Returns the current `Input` associated with this iterator.
2101
    ///
2102
    /// The `start` position on the given `Input` may change during iteration,
2103
    /// but all other values are guaranteed to remain invariant.
2104
    #[inline]
2105
0
    pub fn input<'s>(&'s self) -> &'s Input<'h> {
2106
0
        self.it.input()
2107
0
    }
2108
}
2109
2110
impl<'r, 'h> Iterator for FindMatches<'r, 'h> {
2111
    type Item = Match;
2112
2113
    #[inline]
2114
0
    fn next(&mut self) -> Option<Match> {
2115
0
        let FindMatches { re, ref mut cache, ref mut it } = *self;
2116
0
        it.advance(|input| Ok(re.search_with(cache, input)))
2117
0
    }
2118
2119
    #[inline]
2120
0
    fn count(self) -> usize {
2121
        // If all we care about is a count of matches, then we only need to
2122
        // find the end position of each match. This can give us a 2x perf
2123
        // boost in some cases, because it avoids needing to do a reverse scan
2124
        // to find the start of a match.
2125
0
        let FindMatches { re, mut cache, it } = self;
2126
        // This does the deref for PoolGuard once instead of every iter.
2127
0
        let cache = &mut *cache;
2128
0
        it.into_half_matches_iter(
2129
0
            |input| Ok(re.search_half_with(cache, input)),
2130
        )
2131
0
        .count()
2132
0
    }
2133
}
2134
2135
impl<'r, 'h> core::iter::FusedIterator for FindMatches<'r, 'h> {}
2136
2137
/// An iterator over all non-overlapping leftmost matches with their capturing
2138
/// groups.
2139
///
2140
/// The iterator yields a [`Captures`] value until no more matches could be
2141
/// found.
2142
///
2143
/// The lifetime parameters are as follows:
2144
///
2145
/// * `'r` represents the lifetime of the `Regex` that produced this iterator.
2146
/// * `'h` represents the lifetime of the haystack being searched.
2147
///
2148
/// This iterator can be created with the [`Regex::captures_iter`] method.
2149
#[derive(Debug)]
2150
pub struct CapturesMatches<'r, 'h> {
2151
    re: &'r Regex,
2152
    cache: CachePoolGuard<'r>,
2153
    caps: Captures,
2154
    it: iter::Searcher<'h>,
2155
}
2156
2157
impl<'r, 'h> CapturesMatches<'r, 'h> {
2158
    /// Returns the `Regex` value that created this iterator.
2159
    #[inline]
2160
0
    pub fn regex(&self) -> &'r Regex {
2161
0
        self.re
2162
0
    }
2163
2164
    /// Returns the current `Input` associated with this iterator.
2165
    ///
2166
    /// The `start` position on the given `Input` may change during iteration,
2167
    /// but all other values are guaranteed to remain invariant.
2168
    #[inline]
2169
0
    pub fn input<'s>(&'s self) -> &'s Input<'h> {
2170
0
        self.it.input()
2171
0
    }
2172
}
2173
2174
impl<'r, 'h> Iterator for CapturesMatches<'r, 'h> {
2175
    type Item = Captures;
2176
2177
    #[inline]
2178
0
    fn next(&mut self) -> Option<Captures> {
2179
        // Splitting 'self' apart seems necessary to appease borrowck.
2180
0
        let CapturesMatches { re, ref mut cache, ref mut caps, ref mut it } =
2181
0
            *self;
2182
0
        let _ = it.advance(|input| {
2183
0
            re.search_captures_with(cache, input, caps);
2184
0
            Ok(caps.get_match())
2185
0
        });
2186
0
        if caps.is_match() {
2187
0
            Some(caps.clone())
2188
        } else {
2189
0
            None
2190
        }
2191
0
    }
2192
2193
    #[inline]
2194
0
    fn count(self) -> usize {
2195
0
        let CapturesMatches { re, mut cache, it, .. } = self;
2196
        // This does the deref for PoolGuard once instead of every iter.
2197
0
        let cache = &mut *cache;
2198
0
        it.into_half_matches_iter(
2199
0
            |input| Ok(re.search_half_with(cache, input)),
2200
        )
2201
0
        .count()
2202
0
    }
2203
}
2204
2205
impl<'r, 'h> core::iter::FusedIterator for CapturesMatches<'r, 'h> {}
2206
2207
/// Yields all substrings delimited by a regular expression match.
2208
///
2209
/// The spans correspond to the offsets between matches.
2210
///
2211
/// The lifetime parameters are as follows:
2212
///
2213
/// * `'r` represents the lifetime of the `Regex` that produced this iterator.
2214
/// * `'h` represents the lifetime of the haystack being searched.
2215
///
2216
/// This iterator can be created with the [`Regex::split`] method.
2217
#[derive(Debug)]
2218
pub struct Split<'r, 'h> {
2219
    finder: FindMatches<'r, 'h>,
2220
    last: usize,
2221
}
2222
2223
impl<'r, 'h> Split<'r, 'h> {
2224
    /// Returns the current `Input` associated with this iterator.
2225
    ///
2226
    /// The `start` position on the given `Input` may change during iteration,
2227
    /// but all other values are guaranteed to remain invariant.
2228
    #[inline]
2229
0
    pub fn input<'s>(&'s self) -> &'s Input<'h> {
2230
0
        self.finder.input()
2231
0
    }
2232
}
2233
2234
impl<'r, 'h> Iterator for Split<'r, 'h> {
2235
    type Item = Span;
2236
2237
0
    fn next(&mut self) -> Option<Span> {
2238
0
        match self.finder.next() {
2239
            None => {
2240
0
                let len = self.finder.it.input().haystack().len();
2241
0
                if self.last > len {
2242
0
                    None
2243
                } else {
2244
0
                    let span = Span::from(self.last..len);
2245
0
                    self.last = len + 1; // Next call will return None
2246
0
                    Some(span)
2247
                }
2248
            }
2249
0
            Some(m) => {
2250
0
                let span = Span::from(self.last..m.start());
2251
0
                self.last = m.end();
2252
0
                Some(span)
2253
            }
2254
        }
2255
0
    }
2256
}
2257
2258
impl<'r, 'h> core::iter::FusedIterator for Split<'r, 'h> {}
2259
2260
/// Yields at most `N` spans delimited by a regular expression match.
2261
///
2262
/// The spans correspond to the offsets between matches. The last span will be
2263
/// whatever remains after splitting.
2264
///
2265
/// The lifetime parameters are as follows:
2266
///
2267
/// * `'r` represents the lifetime of the `Regex` that produced this iterator.
2268
/// * `'h` represents the lifetime of the haystack being searched.
2269
///
2270
/// This iterator can be created with the [`Regex::splitn`] method.
2271
#[derive(Debug)]
2272
pub struct SplitN<'r, 'h> {
2273
    splits: Split<'r, 'h>,
2274
    limit: usize,
2275
}
2276
2277
impl<'r, 'h> SplitN<'r, 'h> {
2278
    /// Returns the current `Input` associated with this iterator.
2279
    ///
2280
    /// The `start` position on the given `Input` may change during iteration,
2281
    /// but all other values are guaranteed to remain invariant.
2282
    #[inline]
2283
0
    pub fn input<'s>(&'s self) -> &'s Input<'h> {
2284
0
        self.splits.input()
2285
0
    }
2286
}
2287
2288
impl<'r, 'h> Iterator for SplitN<'r, 'h> {
2289
    type Item = Span;
2290
2291
0
    fn next(&mut self) -> Option<Span> {
2292
0
        if self.limit == 0 {
2293
0
            return None;
2294
0
        }
2295
2296
0
        self.limit -= 1;
2297
0
        if self.limit > 0 {
2298
0
            return self.splits.next();
2299
0
        }
2300
2301
0
        let len = self.splits.finder.it.input().haystack().len();
2302
0
        if self.splits.last > len {
2303
            // We've already returned all substrings.
2304
0
            None
2305
        } else {
2306
            // self.n == 0, so future calls will return None immediately
2307
0
            Some(Span::from(self.splits.last..len))
2308
        }
2309
0
    }
2310
2311
0
    fn size_hint(&self) -> (usize, Option<usize>) {
2312
0
        (0, Some(self.limit))
2313
0
    }
2314
}
2315
2316
impl<'r, 'h> core::iter::FusedIterator for SplitN<'r, 'h> {}
2317
2318
/// Represents mutable scratch space used by regex engines during a search.
2319
///
2320
/// Most of the regex engines in this crate require some kind of
2321
/// mutable state in order to execute a search. This mutable state is
2322
/// explicitly separated from the core regex object (such as a
2323
/// [`thompson::NFA`](crate::nfa::thompson::NFA)) so that the read-only regex
2324
/// object can be shared across multiple threads simultaneously without any
2325
/// synchronization. Conversely, a `Cache` must either be duplicated if using
2326
/// the same `Regex` from multiple threads, or else there must be some kind of
2327
/// synchronization that guarantees exclusive access while it's in use by one
2328
/// thread.
2329
///
2330
/// A `Regex` attempts to do this synchronization for you by using a thread
2331
/// pool internally. Its size scales roughly with the number of simultaneous
2332
/// regex searches.
2333
///
2334
/// For cases where one does not want to rely on a `Regex`'s internal thread
2335
/// pool, lower level routines such as [`Regex::search_with`] are provided
2336
/// that permit callers to pass a `Cache` into the search routine explicitly.
2337
///
2338
/// General advice is that the thread pool is often more than good enough.
2339
/// However, it may be possible to observe the effects of its latency,
2340
/// especially when searching many small haystacks from many threads
2341
/// simultaneously.
2342
///
2343
/// Caches can be created from their corresponding `Regex` via
2344
/// [`Regex::create_cache`]. A cache can only be used with either the `Regex`
2345
/// that created it, or the `Regex` that was most recently used to reset it
2346
/// with [`Cache::reset`]. Using a cache with any other `Regex` may result in
2347
/// panics or incorrect results.
2348
///
2349
/// # Example
2350
///
2351
/// ```
2352
/// use regex_automata::{meta::Regex, Input, Match};
2353
///
2354
/// let re = Regex::new(r"(?-u)m\w+\s+m\w+")?;
2355
/// let mut cache = re.create_cache();
2356
/// let input = Input::new("crazy janey and her mission man");
2357
/// assert_eq!(
2358
///     Some(Match::must(0, 20..31)),
2359
///     re.search_with(&mut cache, &input),
2360
/// );
2361
///
2362
/// # Ok::<(), Box<dyn std::error::Error>>(())
2363
/// ```
2364
#[derive(Debug, Clone)]
2365
pub struct Cache {
2366
    pub(crate) capmatches: Captures,
2367
    pub(crate) pikevm: wrappers::PikeVMCache,
2368
    pub(crate) backtrack: wrappers::BoundedBacktrackerCache,
2369
    pub(crate) onepass: wrappers::OnePassCache,
2370
    pub(crate) hybrid: wrappers::HybridCache,
2371
    pub(crate) revhybrid: wrappers::ReverseHybridCache,
2372
}
2373
2374
impl Cache {
2375
    /// Creates a new `Cache` for use with this regex.
2376
    ///
2377
    /// The cache returned should only be used for searches for the given
2378
    /// `Regex`. If you want to reuse the cache for another `Regex`, then you
2379
    /// must call [`Cache::reset`] with that `Regex`.
2380
0
    pub fn new(re: &Regex) -> Cache {
2381
0
        re.create_cache()
2382
0
    }
2383
2384
    /// Reset this cache such that it can be used for searching with the given
2385
    /// `Regex` (and only that `Regex`).
2386
    ///
2387
    /// A cache reset permits potentially reusing memory already allocated in
2388
    /// this cache with a different `Regex`.
2389
    ///
2390
    /// # Example
2391
    ///
2392
    /// This shows how to re-purpose a cache for use with a different `Regex`.
2393
    ///
2394
    /// ```
2395
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2396
    /// use regex_automata::{meta::Regex, Match, Input};
2397
    ///
2398
    /// let re1 = Regex::new(r"\w")?;
2399
    /// let re2 = Regex::new(r"\W")?;
2400
    ///
2401
    /// let mut cache = re1.create_cache();
2402
    /// assert_eq!(
2403
    ///     Some(Match::must(0, 0..2)),
2404
    ///     re1.search_with(&mut cache, &Input::new("Δ")),
2405
    /// );
2406
    ///
2407
    /// // Using 'cache' with re2 is not allowed. It may result in panics or
2408
    /// // incorrect results. In order to re-purpose the cache, we must reset
2409
    /// // it with the Regex we'd like to use it with.
2410
    /// //
2411
    /// // Similarly, after this reset, using the cache with 're1' is also not
2412
    /// // allowed.
2413
    /// cache.reset(&re2);
2414
    /// assert_eq!(
2415
    ///     Some(Match::must(0, 0..3)),
2416
    ///     re2.search_with(&mut cache, &Input::new("☃")),
2417
    /// );
2418
    ///
2419
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2420
    /// ```
2421
0
    pub fn reset(&mut self, re: &Regex) {
2422
0
        re.imp.strat.reset_cache(self)
2423
0
    }
2424
2425
    /// Returns the heap memory usage, in bytes, of this cache.
2426
    ///
2427
    /// This does **not** include the stack size used up by this cache. To
2428
    /// compute that, use `std::mem::size_of::<Cache>()`.
2429
0
    pub fn memory_usage(&self) -> usize {
2430
0
        let mut bytes = 0;
2431
0
        bytes += self.pikevm.memory_usage();
2432
0
        bytes += self.backtrack.memory_usage();
2433
0
        bytes += self.onepass.memory_usage();
2434
0
        bytes += self.hybrid.memory_usage();
2435
0
        bytes += self.revhybrid.memory_usage();
2436
0
        bytes
2437
0
    }
2438
}
2439
2440
/// An object describing the configuration of a `Regex`.
2441
///
2442
/// This configuration only includes options for the
2443
/// non-syntax behavior of a `Regex`, and can be applied via the
2444
/// [`Builder::configure`] method. For configuring the syntax options, see
2445
/// [`util::syntax::Config`](crate::util::syntax::Config).
2446
///
2447
/// # Example: lower the NFA size limit
2448
///
2449
/// In some cases, the default size limit might be too big. The size limit can
2450
/// be lowered, which will prevent large regex patterns from compiling.
2451
///
2452
/// ```
2453
/// # if cfg!(miri) { return Ok(()); } // miri takes too long
2454
/// use regex_automata::meta::Regex;
2455
///
2456
/// let result = Regex::builder()
2457
///     .configure(Regex::config().nfa_size_limit(Some(20 * (1<<10))))
2458
///     // Not even 20KB is enough to build a single large Unicode class!
2459
///     .build(r"\pL");
2460
/// assert!(result.is_err());
2461
///
2462
/// # Ok::<(), Box<dyn std::error::Error>>(())
2463
/// ```
2464
#[derive(Clone, Debug, Default)]
2465
pub struct Config {
2466
    // As with other configuration types in this crate, we put all our knobs
2467
    // in options so that we can distinguish between "default" and "not set."
2468
    // This makes it possible to easily combine multiple configurations
2469
    // without default values overwriting explicitly specified values. See the
2470
    // 'overwrite' method.
2471
    //
2472
    // For docs on the fields below, see the corresponding method setters.
2473
    match_kind: Option<MatchKind>,
2474
    utf8_empty: Option<bool>,
2475
    autopre: Option<bool>,
2476
    pre: Option<Option<Prefilter>>,
2477
    which_captures: Option<WhichCaptures>,
2478
    nfa_size_limit: Option<Option<usize>>,
2479
    onepass_size_limit: Option<Option<usize>>,
2480
    hybrid_cache_capacity: Option<usize>,
2481
    hybrid: Option<bool>,
2482
    dfa: Option<bool>,
2483
    dfa_size_limit: Option<Option<usize>>,
2484
    dfa_state_limit: Option<Option<usize>>,
2485
    onepass: Option<bool>,
2486
    backtrack: Option<bool>,
2487
    byte_classes: Option<bool>,
2488
    line_terminator: Option<u8>,
2489
    pool_capacity: Option<usize>,
2490
}
2491
2492
impl Config {
2493
    /// Create a new configuration object for a `Regex`.
2494
0
    pub fn new() -> Config {
2495
0
        Config::default()
2496
0
    }
2497
2498
    /// Set the match semantics for a `Regex`.
2499
    ///
2500
    /// The default value is [`MatchKind::LeftmostFirst`].
2501
    ///
2502
    /// # Example
2503
    ///
2504
    /// ```
2505
    /// use regex_automata::{meta::Regex, Match, MatchKind};
2506
    ///
2507
    /// // By default, leftmost-first semantics are used, which
2508
    /// // disambiguates matches at the same position by selecting
2509
    /// // the one that corresponds earlier in the pattern.
2510
    /// let re = Regex::new("sam|samwise")?;
2511
    /// assert_eq!(Some(Match::must(0, 0..3)), re.find("samwise"));
2512
    ///
2513
    /// // But with 'all' semantics, match priority is ignored
2514
    /// // and all match states are included. When coupled with
2515
    /// // a leftmost search, the search will report the last
2516
    /// // possible match.
2517
    /// let re = Regex::builder()
2518
    ///     .configure(Regex::config().match_kind(MatchKind::All))
2519
    ///     .build("sam|samwise")?;
2520
    /// assert_eq!(Some(Match::must(0, 0..7)), re.find("samwise"));
2521
    /// // Beware that this can lead to skipping matches!
2522
    /// // Usually 'all' is used for anchored reverse searches
2523
    /// // only, or for overlapping searches.
2524
    /// assert_eq!(Some(Match::must(0, 4..11)), re.find("sam samwise"));
2525
    ///
2526
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2527
    /// ```
2528
0
    pub fn match_kind(self, kind: MatchKind) -> Config {
2529
0
        Config { match_kind: Some(kind), ..self }
2530
0
    }
2531
2532
    /// Toggles whether empty matches are permitted to occur between the code
2533
    /// units of a UTF-8 encoded codepoint.
2534
    ///
2535
    /// This should generally be enabled when search a `&str` or anything that
2536
    /// you otherwise know is valid UTF-8. It should be disabled in all other
2537
    /// cases. Namely, if the haystack is not valid UTF-8 and this is enabled,
2538
    /// then behavior is unspecified.
2539
    ///
2540
    /// By default, this is enabled.
2541
    ///
2542
    /// # Example
2543
    ///
2544
    /// ```
2545
    /// use regex_automata::{meta::Regex, Match};
2546
    ///
2547
    /// let re = Regex::new("")?;
2548
    /// let got: Vec<Match> = re.find_iter("☃").collect();
2549
    /// // Matches only occur at the beginning and end of the snowman.
2550
    /// assert_eq!(got, vec![
2551
    ///     Match::must(0, 0..0),
2552
    ///     Match::must(0, 3..3),
2553
    /// ]);
2554
    ///
2555
    /// let re = Regex::builder()
2556
    ///     .configure(Regex::config().utf8_empty(false))
2557
    ///     .build("")?;
2558
    /// let got: Vec<Match> = re.find_iter("☃").collect();
2559
    /// // Matches now occur at every position!
2560
    /// assert_eq!(got, vec![
2561
    ///     Match::must(0, 0..0),
2562
    ///     Match::must(0, 1..1),
2563
    ///     Match::must(0, 2..2),
2564
    ///     Match::must(0, 3..3),
2565
    /// ]);
2566
    ///
2567
    /// Ok::<(), Box<dyn std::error::Error>>(())
2568
    /// ```
2569
0
    pub fn utf8_empty(self, yes: bool) -> Config {
2570
0
        Config { utf8_empty: Some(yes), ..self }
2571
0
    }
2572
2573
    /// Toggles whether automatic prefilter support is enabled.
2574
    ///
2575
    /// If this is disabled and [`Config::prefilter`] is not set, then the
2576
    /// meta regex engine will not use any prefilters. This can sometimes
2577
    /// be beneficial in cases where you know (or have measured) that the
2578
    /// prefilter leads to overall worse search performance.
2579
    ///
2580
    /// By default, this is enabled.
2581
    ///
2582
    /// # Example
2583
    ///
2584
    /// ```
2585
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2586
    /// use regex_automata::{meta::Regex, Match};
2587
    ///
2588
    /// let re = Regex::builder()
2589
    ///     .configure(Regex::config().auto_prefilter(false))
2590
    ///     .build(r"Bruce \w+")?;
2591
    /// let hay = "Hello Bruce Springsteen!";
2592
    /// assert_eq!(Some(Match::must(0, 6..23)), re.find(hay));
2593
    ///
2594
    /// Ok::<(), Box<dyn std::error::Error>>(())
2595
    /// ```
2596
0
    pub fn auto_prefilter(self, yes: bool) -> Config {
2597
0
        Config { autopre: Some(yes), ..self }
2598
0
    }
2599
2600
    /// Overrides and sets the prefilter to use inside a `Regex`.
2601
    ///
2602
    /// This permits one to forcefully set a prefilter in cases where the
2603
    /// caller knows better than whatever the automatic prefilter logic is
2604
    /// capable of.
2605
    ///
2606
    /// By default, this is set to `None` and an automatic prefilter will be
2607
    /// used if one could be built. (Assuming [`Config::auto_prefilter`] is
2608
    /// enabled, which it is by default.)
2609
    ///
2610
    /// # Example
2611
    ///
2612
    /// This example shows how to set your own prefilter. In the case of a
2613
    /// pattern like `Bruce \w+`, the automatic prefilter is likely to be
2614
    /// constructed in a way that it will look for occurrences of `Bruce `.
2615
    /// In most cases, this is the best choice. But in some cases, it may be
2616
    /// the case that running `memchr` on `B` is the best choice. One can
2617
    /// achieve that behavior by overriding the automatic prefilter logic
2618
    /// and providing a prefilter that just matches `B`.
2619
    ///
2620
    /// ```
2621
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2622
    /// use regex_automata::{
2623
    ///     meta::Regex,
2624
    ///     util::prefilter::Prefilter,
2625
    ///     Match, MatchKind,
2626
    /// };
2627
    ///
2628
    /// let pre = Prefilter::new(MatchKind::LeftmostFirst, &["B"])
2629
    ///     .expect("a prefilter");
2630
    /// let re = Regex::builder()
2631
    ///     .configure(Regex::config().prefilter(Some(pre)))
2632
    ///     .build(r"Bruce \w+")?;
2633
    /// let hay = "Hello Bruce Springsteen!";
2634
    /// assert_eq!(Some(Match::must(0, 6..23)), re.find(hay));
2635
    ///
2636
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2637
    /// ```
2638
    ///
2639
    /// # Example: incorrect prefilters can lead to incorrect results!
2640
    ///
2641
    /// Be warned that setting an incorrect prefilter can lead to missed
2642
    /// matches. So if you use this option, ensure your prefilter can _never_
2643
    /// report false negatives. (A false positive is, on the other hand, quite
2644
    /// okay and generally unavoidable.)
2645
    ///
2646
    /// ```
2647
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2648
    /// use regex_automata::{
2649
    ///     meta::Regex,
2650
    ///     util::prefilter::Prefilter,
2651
    ///     Match, MatchKind,
2652
    /// };
2653
    ///
2654
    /// let pre = Prefilter::new(MatchKind::LeftmostFirst, &["Z"])
2655
    ///     .expect("a prefilter");
2656
    /// let re = Regex::builder()
2657
    ///     .configure(Regex::config().prefilter(Some(pre)))
2658
    ///     .build(r"Bruce \w+")?;
2659
    /// let hay = "Hello Bruce Springsteen!";
2660
    /// // Oops! No match found, but there should be one!
2661
    /// assert_eq!(None, re.find(hay));
2662
    ///
2663
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2664
    /// ```
2665
0
    pub fn prefilter(self, pre: Option<Prefilter>) -> Config {
2666
0
        Config { pre: Some(pre), ..self }
2667
0
    }
2668
2669
    /// Configures what kinds of groups are compiled as "capturing" in the
2670
    /// underlying regex engine.
2671
    ///
2672
    /// This is set to [`WhichCaptures::All`] by default. Callers may wish to
2673
    /// use [`WhichCaptures::Implicit`] in cases where one wants avoid the
2674
    /// overhead of capture states for explicit groups.
2675
    ///
2676
    /// Note that another approach to avoiding the overhead of capture groups
2677
    /// is by using non-capturing groups in the regex pattern. That is,
2678
    /// `(?:a)` instead of `(a)`. This option is useful when you can't control
2679
    /// the concrete syntax but know that you don't need the underlying capture
2680
    /// states. For example, using `WhichCaptures::Implicit` will behave as if
2681
    /// all explicit capturing groups in the pattern were non-capturing.
2682
    ///
2683
    /// Setting this to `WhichCaptures::None` is usually not the right thing to
2684
    /// do. When no capture states are compiled, some regex engines (such as
2685
    /// the `PikeVM`) won't be able to report match offsets. This will manifest
2686
    /// as no match being found. Indeed, in order to enforce consistent
2687
    /// behavior, the meta regex engine will always report `None` for routines
2688
    /// that return match offsets even if one of its regex engines could
2689
    /// service the request. This avoids "match or not" behavior from being
2690
    /// influenced by user input (since user input can influence the selection
2691
    /// of the regex engine).
2692
    ///
2693
    /// # Example
2694
    ///
2695
    /// This example demonstrates how the results of capture groups can change
2696
    /// based on this option. First we show the default (all capture groups in
2697
    /// the pattern are capturing):
2698
    ///
2699
    /// ```
2700
    /// use regex_automata::{meta::Regex, Match, Span};
2701
    ///
2702
    /// let re = Regex::new(r"foo([0-9]+)bar")?;
2703
    /// let hay = "foo123bar";
2704
    ///
2705
    /// let mut caps = re.create_captures();
2706
    /// re.captures(hay, &mut caps);
2707
    /// assert_eq!(Some(Span::from(0..9)), caps.get_group(0));
2708
    /// assert_eq!(Some(Span::from(3..6)), caps.get_group(1));
2709
    ///
2710
    /// Ok::<(), Box<dyn std::error::Error>>(())
2711
    /// ```
2712
    ///
2713
    /// And now we show the behavior when we only include implicit capture
2714
    /// groups. In this case, we can only find the overall match span, but the
2715
    /// spans of any other explicit group don't exist because they are treated
2716
    /// as non-capturing. (In effect, when `WhichCaptures::Implicit` is used,
2717
    /// there is no real point in using [`Regex::captures`] since it will never
2718
    /// be able to report more information than [`Regex::find`].)
2719
    ///
2720
    /// ```
2721
    /// use regex_automata::{
2722
    ///     meta::Regex,
2723
    ///     nfa::thompson::WhichCaptures,
2724
    ///     Match,
2725
    ///     Span,
2726
    /// };
2727
    ///
2728
    /// let re = Regex::builder()
2729
    ///     .configure(Regex::config().which_captures(WhichCaptures::Implicit))
2730
    ///     .build(r"foo([0-9]+)bar")?;
2731
    /// let hay = "foo123bar";
2732
    ///
2733
    /// let mut caps = re.create_captures();
2734
    /// re.captures(hay, &mut caps);
2735
    /// assert_eq!(Some(Span::from(0..9)), caps.get_group(0));
2736
    /// assert_eq!(None, caps.get_group(1));
2737
    ///
2738
    /// Ok::<(), Box<dyn std::error::Error>>(())
2739
    /// ```
2740
    ///
2741
    /// # Example: strange `Regex::find` behavior
2742
    ///
2743
    /// As noted above, when using [`WhichCaptures::None`], this means that
2744
    /// `Regex::is_match` could return `true` while `Regex::find` returns
2745
    /// `None`:
2746
    ///
2747
    /// ```
2748
    /// use regex_automata::{
2749
    ///     meta::Regex,
2750
    ///     nfa::thompson::WhichCaptures,
2751
    ///     Input,
2752
    ///     Match,
2753
    ///     Span,
2754
    /// };
2755
    ///
2756
    /// let re = Regex::builder()
2757
    ///     .configure(Regex::config().which_captures(WhichCaptures::None))
2758
    ///     .build(r"foo([0-9]+)bar")?;
2759
    /// let hay = "foo123bar";
2760
    ///
2761
    /// assert!(re.is_match(hay));
2762
    /// assert_eq!(re.find(hay), None);
2763
    /// assert_eq!(re.search_half(&Input::new(hay)), None);
2764
    ///
2765
    /// Ok::<(), Box<dyn std::error::Error>>(())
2766
    /// ```
2767
0
    pub fn which_captures(mut self, which_captures: WhichCaptures) -> Config {
2768
0
        self.which_captures = Some(which_captures);
2769
0
        self
2770
0
    }
2771
2772
    /// Sets the size limit, in bytes, to enforce on the construction of every
2773
    /// NFA build by the meta regex engine.
2774
    ///
2775
    /// Setting it to `None` disables the limit. This is not recommended if
2776
    /// you're compiling untrusted patterns.
2777
    ///
2778
    /// Note that this limit is applied to _each_ NFA built, and if any of
2779
    /// them exceed the limit, then construction will fail. This limit does
2780
    /// _not_ correspond to the total memory used by all NFAs in the meta regex
2781
    /// engine.
2782
    ///
2783
    /// This defaults to some reasonable number that permits most reasonable
2784
    /// patterns.
2785
    ///
2786
    /// # Example
2787
    ///
2788
    /// ```
2789
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2790
    /// use regex_automata::meta::Regex;
2791
    ///
2792
    /// let result = Regex::builder()
2793
    ///     .configure(Regex::config().nfa_size_limit(Some(20 * (1<<10))))
2794
    ///     // Not even 20KB is enough to build a single large Unicode class!
2795
    ///     .build(r"\pL");
2796
    /// assert!(result.is_err());
2797
    ///
2798
    /// // But notice that building such a regex with the exact same limit
2799
    /// // can succeed depending on other aspects of the configuration. For
2800
    /// // example, a single *forward* NFA will (at time of writing) fit into
2801
    /// // the 20KB limit, but a *reverse* NFA of the same pattern will not.
2802
    /// // So if one configures a meta regex such that a reverse NFA is never
2803
    /// // needed and thus never built, then the 20KB limit will be enough for
2804
    /// // a pattern like \pL!
2805
    /// let result = Regex::builder()
2806
    ///     .configure(Regex::config()
2807
    ///         .nfa_size_limit(Some(20 * (1<<10)))
2808
    ///         // The DFAs are the only thing that (currently) need a reverse
2809
    ///         // NFA. So if both are disabled, the meta regex engine will
2810
    ///         // skip building the reverse NFA. Note that this isn't an API
2811
    ///         // guarantee. A future semver compatible version may introduce
2812
    ///         // new use cases for a reverse NFA.
2813
    ///         .hybrid(false)
2814
    ///         .dfa(false)
2815
    ///     )
2816
    ///     // Not even 20KB is enough to build a single large Unicode class!
2817
    ///     .build(r"\pL");
2818
    /// assert!(result.is_ok());
2819
    ///
2820
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2821
    /// ```
2822
0
    pub fn nfa_size_limit(self, limit: Option<usize>) -> Config {
2823
0
        Config { nfa_size_limit: Some(limit), ..self }
2824
0
    }
2825
2826
    /// Sets the size limit, in bytes, for the one-pass DFA.
2827
    ///
2828
    /// Setting it to `None` disables the limit. Disabling the limit is
2829
    /// strongly discouraged when compiling untrusted patterns. Even if the
2830
    /// patterns are trusted, it still may not be a good idea, since a one-pass
2831
    /// DFA can use a lot of memory. With that said, as the size of a regex
2832
    /// increases, the likelihood of it being one-pass likely decreases.
2833
    ///
2834
    /// This defaults to some reasonable number that permits most reasonable
2835
    /// one-pass patterns.
2836
    ///
2837
    /// # Example
2838
    ///
2839
    /// This shows how to set the one-pass DFA size limit. Note that since
2840
    /// a one-pass DFA is an optional component of the meta regex engine,
2841
    /// this size limit only impacts what is built internally and will never
2842
    /// determine whether a `Regex` itself fails to build.
2843
    ///
2844
    /// ```
2845
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2846
    /// use regex_automata::meta::Regex;
2847
    ///
2848
    /// let result = Regex::builder()
2849
    ///     .configure(Regex::config().onepass_size_limit(Some(2 * (1<<20))))
2850
    ///     .build(r"\pL{5}");
2851
    /// assert!(result.is_ok());
2852
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2853
    /// ```
2854
0
    pub fn onepass_size_limit(self, limit: Option<usize>) -> Config {
2855
0
        Config { onepass_size_limit: Some(limit), ..self }
2856
0
    }
2857
2858
    /// Set the cache capacity, in bytes, for the lazy DFA.
2859
    ///
2860
    /// The cache capacity of the lazy DFA determines approximately how much
2861
    /// heap memory it is allowed to use to store its state transitions. The
2862
    /// state transitions are computed at search time, and if the cache fills
2863
    /// up it, it is cleared. At this point, any previously generated state
2864
    /// transitions are lost and are re-generated if they're needed again.
2865
    ///
2866
    /// This sort of cache filling and clearing works quite well _so long as
2867
    /// cache clearing happens infrequently_. If it happens too often, then the
2868
    /// meta regex engine will stop using the lazy DFA and switch over to a
2869
    /// different regex engine.
2870
    ///
2871
    /// In cases where the cache is cleared too often, it may be possible to
2872
    /// give the cache more space and reduce (or eliminate) how often it is
2873
    /// cleared. Similarly, sometimes a regex is so big that the lazy DFA isn't
2874
    /// used at all if its cache capacity isn't big enough.
2875
    ///
2876
    /// The capacity set here is a _limit_ on how much memory is used. The
2877
    /// actual memory used is only allocated as it's needed.
2878
    ///
2879
    /// Determining the right value for this is a little tricky and will likely
2880
    /// required some profiling. Enabling the `logging` feature and setting the
2881
    /// log level to `trace` will also tell you how often the cache is being
2882
    /// cleared.
2883
    ///
2884
    /// # Example
2885
    ///
2886
    /// ```
2887
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2888
    /// use regex_automata::meta::Regex;
2889
    ///
2890
    /// let result = Regex::builder()
2891
    ///     .configure(Regex::config().hybrid_cache_capacity(20 * (1<<20)))
2892
    ///     .build(r"\pL{5}");
2893
    /// assert!(result.is_ok());
2894
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2895
    /// ```
2896
0
    pub fn hybrid_cache_capacity(self, limit: usize) -> Config {
2897
0
        Config { hybrid_cache_capacity: Some(limit), ..self }
2898
0
    }
2899
2900
    /// Sets the size limit, in bytes, for heap memory used for a fully
2901
    /// compiled DFA.
2902
    ///
2903
    /// **NOTE:** If you increase this, you'll likely also need to increase
2904
    /// [`Config::dfa_state_limit`].
2905
    ///
2906
    /// In contrast to the lazy DFA, building a full DFA requires computing
2907
    /// all of its state transitions up front. This can be a very expensive
2908
    /// process, and runs in worst case `2^n` time and space (where `n` is
2909
    /// proportional to the size of the regex). However, a full DFA unlocks
2910
    /// some additional optimization opportunities.
2911
    ///
2912
    /// Because full DFAs can be so expensive, the default limits for them are
2913
    /// incredibly small. Generally speaking, if your regex is moderately big
2914
    /// or if you're using Unicode features (`\w` is Unicode-aware by default
2915
    /// for example), then you can expect that the meta regex engine won't even
2916
    /// attempt to build a DFA for it.
2917
    ///
2918
    /// If this and [`Config::dfa_state_limit`] are set to `None`, then the
2919
    /// meta regex will not use any sort of limits when deciding whether to
2920
    /// build a DFA. This in turn makes construction of a `Regex` take
2921
    /// worst case exponential time and space. Even short patterns can result
2922
    /// in huge space blow ups. So it is strongly recommended to keep some kind
2923
    /// of limit set!
2924
    ///
2925
    /// The default is set to a small number that permits some simple regexes
2926
    /// to get compiled into DFAs in reasonable time.
2927
    ///
2928
    /// # Example
2929
    ///
2930
    /// ```
2931
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2932
    /// use regex_automata::meta::Regex;
2933
    ///
2934
    /// let result = Regex::builder()
2935
    ///     // 100MB is much bigger than the default.
2936
    ///     .configure(Regex::config()
2937
    ///         .dfa_size_limit(Some(100 * (1<<20)))
2938
    ///         // We don't care about size too much here, so just
2939
    ///         // remove the NFA state limit altogether.
2940
    ///         .dfa_state_limit(None))
2941
    ///     .build(r"\pL{5}");
2942
    /// assert!(result.is_ok());
2943
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2944
    /// ```
2945
0
    pub fn dfa_size_limit(self, limit: Option<usize>) -> Config {
2946
0
        Config { dfa_size_limit: Some(limit), ..self }
2947
0
    }
2948
2949
    /// Sets a limit on the total number of NFA states, beyond which, a full
2950
    /// DFA is not attempted to be compiled.
2951
    ///
2952
    /// This limit works in concert with [`Config::dfa_size_limit`]. Namely,
2953
    /// where as `Config::dfa_size_limit` is applied by attempting to construct
2954
    /// a DFA, this limit is used to avoid the attempt in the first place. This
2955
    /// is useful to avoid hefty initialization costs associated with building
2956
    /// a DFA for cases where it is obvious the DFA will ultimately be too big.
2957
    ///
2958
    /// By default, this is set to a very small number.
2959
    ///
2960
    /// # Example
2961
    ///
2962
    /// ```
2963
    /// # if cfg!(miri) { return Ok(()); } // miri takes too long
2964
    /// use regex_automata::meta::Regex;
2965
    ///
2966
    /// let result = Regex::builder()
2967
    ///     .configure(Regex::config()
2968
    ///         // Sometimes the default state limit rejects DFAs even
2969
    ///         // if they would fit in the size limit. Here, we disable
2970
    ///         // the check on the number of NFA states and just rely on
2971
    ///         // the size limit.
2972
    ///         .dfa_state_limit(None))
2973
    ///     .build(r"(?-u)\w{30}");
2974
    /// assert!(result.is_ok());
2975
    /// # Ok::<(), Box<dyn std::error::Error>>(())
2976
    /// ```
2977
0
    pub fn dfa_state_limit(self, limit: Option<usize>) -> Config {
2978
0
        Config { dfa_state_limit: Some(limit), ..self }
2979
0
    }
2980
2981
    /// Whether to attempt to shrink the size of the alphabet for the regex
2982
    /// pattern or not. When enabled, the alphabet is shrunk into a set of
2983
    /// equivalence classes, where every byte in the same equivalence class
2984
    /// cannot discriminate between a match or non-match.
2985
    ///
2986
    /// **WARNING:** This is only useful for debugging DFAs. Disabling this
2987
    /// does not yield any speed advantages. Indeed, disabling it can result
2988
    /// in much higher memory usage. Disabling byte classes is useful for
2989
    /// debugging the actual generated transitions because it lets one see the
2990
    /// transitions defined on actual bytes instead of the equivalence classes.
2991
    ///
2992
    /// This option is enabled by default and should never be disabled unless
2993
    /// one is debugging the meta regex engine's internals.
2994
    ///
2995
    /// # Example
2996
    ///
2997
    /// ```
2998
    /// use regex_automata::{meta::Regex, Match};
2999
    ///
3000
    /// let re = Regex::builder()
3001
    ///     .configure(Regex::config().byte_classes(false))
3002
    ///     .build(r"[a-z]+")?;
3003
    /// let hay = "!!quux!!";
3004
    /// assert_eq!(Some(Match::must(0, 2..6)), re.find(hay));
3005
    ///
3006
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3007
    /// ```
3008
0
    pub fn byte_classes(self, yes: bool) -> Config {
3009
0
        Config { byte_classes: Some(yes), ..self }
3010
0
    }
3011
3012
    /// Set the line terminator to be used by the `^` and `$` anchors in
3013
    /// multi-line mode.
3014
    ///
3015
    /// This option has no effect when CRLF mode is enabled. That is,
3016
    /// regardless of this setting, `(?Rm:^)` and `(?Rm:$)` will always treat
3017
    /// `\r` and `\n` as line terminators (and will never match between a `\r`
3018
    /// and a `\n`).
3019
    ///
3020
    /// By default, `\n` is the line terminator.
3021
    ///
3022
    /// **Warning**: This does not change the behavior of `.`. To do that,
3023
    /// you'll need to configure the syntax option
3024
    /// [`syntax::Config::line_terminator`](crate::util::syntax::Config::line_terminator)
3025
    /// in addition to this. Otherwise, `.` will continue to match any
3026
    /// character other than `\n`.
3027
    ///
3028
    /// # Example
3029
    ///
3030
    /// ```
3031
    /// use regex_automata::{meta::Regex, util::syntax, Match};
3032
    ///
3033
    /// let re = Regex::builder()
3034
    ///     .syntax(syntax::Config::new().multi_line(true))
3035
    ///     .configure(Regex::config().line_terminator(b'\x00'))
3036
    ///     .build(r"^foo$")?;
3037
    /// let hay = "\x00foo\x00";
3038
    /// assert_eq!(Some(Match::must(0, 1..4)), re.find(hay));
3039
    ///
3040
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3041
    /// ```
3042
0
    pub fn line_terminator(self, byte: u8) -> Config {
3043
0
        Config { line_terminator: Some(byte), ..self }
3044
0
    }
3045
3046
    /// Sets the capacity used to manage a pool of [`Cache`] values in the
3047
    /// higher level convenience APIs.
3048
    ///
3049
    /// When not configured explicitly, a reasonable default is selected. It
3050
    /// is rarely expected that a number large than the number of logical CPUs
3051
    /// makes sense as a value. A smaller number could result in slowdowns if
3052
    /// many regex queries are run under contention.
3053
0
    pub fn pool_capacity(self, capacity: usize) -> Config {
3054
0
        Config { pool_capacity: Some(capacity), ..self }
3055
0
    }
3056
3057
    /// Toggle whether the hybrid NFA/DFA (also known as the "lazy DFA") should
3058
    /// be available for use by the meta regex engine.
3059
    ///
3060
    /// Enabling this does not necessarily mean that the lazy DFA will
3061
    /// definitely be used. It just means that it will be _available_ for use
3062
    /// if the meta regex engine thinks it will be useful.
3063
    ///
3064
    /// When the `hybrid` crate feature is enabled, then this is enabled by
3065
    /// default. Otherwise, if the crate feature is disabled, then this is
3066
    /// always disabled, regardless of its setting by the caller.
3067
0
    pub fn hybrid(self, yes: bool) -> Config {
3068
0
        Config { hybrid: Some(yes), ..self }
3069
0
    }
3070
3071
    /// Toggle whether a fully compiled DFA should be available for use by the
3072
    /// meta regex engine.
3073
    ///
3074
    /// Enabling this does not necessarily mean that a DFA will definitely be
3075
    /// used. It just means that it will be _available_ for use if the meta
3076
    /// regex engine thinks it will be useful.
3077
    ///
3078
    /// When the `dfa-build` crate feature is enabled, then this is enabled by
3079
    /// default. Otherwise, if the crate feature is disabled, then this is
3080
    /// always disabled, regardless of its setting by the caller.
3081
0
    pub fn dfa(self, yes: bool) -> Config {
3082
0
        Config { dfa: Some(yes), ..self }
3083
0
    }
3084
3085
    /// Toggle whether a one-pass DFA should be available for use by the meta
3086
    /// regex engine.
3087
    ///
3088
    /// Enabling this does not necessarily mean that a one-pass DFA will
3089
    /// definitely be used. It just means that it will be _available_ for
3090
    /// use if the meta regex engine thinks it will be useful. (Indeed, a
3091
    /// one-pass DFA can only be used when the regex is one-pass. See the
3092
    /// [`dfa::onepass`](crate::dfa::onepass) module for more details.)
3093
    ///
3094
    /// When the `dfa-onepass` crate feature is enabled, then this is enabled
3095
    /// by default. Otherwise, if the crate feature is disabled, then this is
3096
    /// always disabled, regardless of its setting by the caller.
3097
0
    pub fn onepass(self, yes: bool) -> Config {
3098
0
        Config { onepass: Some(yes), ..self }
3099
0
    }
3100
3101
    /// Toggle whether a bounded backtracking regex engine should be available
3102
    /// for use by the meta regex engine.
3103
    ///
3104
    /// Enabling this does not necessarily mean that a bounded backtracker will
3105
    /// definitely be used. It just means that it will be _available_ for use
3106
    /// if the meta regex engine thinks it will be useful.
3107
    ///
3108
    /// When the `nfa-backtrack` crate feature is enabled, then this is enabled
3109
    /// by default. Otherwise, if the crate feature is disabled, then this is
3110
    /// always disabled, regardless of its setting by the caller.
3111
0
    pub fn backtrack(self, yes: bool) -> Config {
3112
0
        Config { backtrack: Some(yes), ..self }
3113
0
    }
3114
3115
    /// Returns the match kind on this configuration, as set by
3116
    /// [`Config::match_kind`].
3117
    ///
3118
    /// If it was not explicitly set, then a default value is returned.
3119
0
    pub fn get_match_kind(&self) -> MatchKind {
3120
0
        self.match_kind.unwrap_or(MatchKind::LeftmostFirst)
3121
0
    }
3122
3123
    /// Returns whether empty matches must fall on valid UTF-8 boundaries, as
3124
    /// set by [`Config::utf8_empty`].
3125
    ///
3126
    /// If it was not explicitly set, then a default value is returned.
3127
0
    pub fn get_utf8_empty(&self) -> bool {
3128
0
        self.utf8_empty.unwrap_or(true)
3129
0
    }
3130
3131
    /// Returns whether automatic prefilters are enabled, as set by
3132
    /// [`Config::auto_prefilter`].
3133
    ///
3134
    /// If it was not explicitly set, then a default value is returned.
3135
0
    pub fn get_auto_prefilter(&self) -> bool {
3136
0
        self.autopre.unwrap_or(true)
3137
0
    }
3138
3139
    /// Returns a manually set prefilter, if one was set by
3140
    /// [`Config::prefilter`].
3141
    ///
3142
    /// If it was not explicitly set, then a default value is returned.
3143
0
    pub fn get_prefilter(&self) -> Option<&Prefilter> {
3144
0
        self.pre.as_ref().unwrap_or(&None).as_ref()
3145
0
    }
3146
3147
    /// Returns the capture configuration, as set by
3148
    /// [`Config::which_captures`].
3149
    ///
3150
    /// If it was not explicitly set, then a default value is returned.
3151
0
    pub fn get_which_captures(&self) -> WhichCaptures {
3152
0
        self.which_captures.unwrap_or(WhichCaptures::All)
3153
0
    }
3154
3155
    /// Returns NFA size limit, as set by [`Config::nfa_size_limit`].
3156
    ///
3157
    /// If it was not explicitly set, then a default value is returned.
3158
0
    pub fn get_nfa_size_limit(&self) -> Option<usize> {
3159
0
        self.nfa_size_limit.unwrap_or(Some(10 * (1 << 20)))
3160
0
    }
3161
3162
    /// Returns one-pass DFA size limit, as set by
3163
    /// [`Config::onepass_size_limit`].
3164
    ///
3165
    /// If it was not explicitly set, then a default value is returned.
3166
0
    pub fn get_onepass_size_limit(&self) -> Option<usize> {
3167
0
        self.onepass_size_limit.unwrap_or(Some(1 * (1 << 20)))
3168
0
    }
3169
3170
    /// Returns hybrid NFA/DFA cache capacity, as set by
3171
    /// [`Config::hybrid_cache_capacity`].
3172
    ///
3173
    /// If it was not explicitly set, then a default value is returned.
3174
0
    pub fn get_hybrid_cache_capacity(&self) -> usize {
3175
0
        self.hybrid_cache_capacity.unwrap_or(2 * (1 << 20))
3176
0
    }
3177
3178
    /// Returns DFA size limit, as set by [`Config::dfa_size_limit`].
3179
    ///
3180
    /// If it was not explicitly set, then a default value is returned.
3181
0
    pub fn get_dfa_size_limit(&self) -> Option<usize> {
3182
        // The default for this is VERY small because building a full DFA is
3183
        // ridiculously costly. But for regexes that are very small, it can be
3184
        // beneficial to use a full DFA. In particular, a full DFA can enable
3185
        // additional optimizations via something called "accelerated" states.
3186
        // Namely, when there's a state with only a few outgoing transitions,
3187
        // we can temporary suspend walking the transition table and use memchr
3188
        // for just those outgoing transitions to skip ahead very quickly.
3189
        //
3190
        // Generally speaking, if Unicode is enabled in your regex and you're
3191
        // using some kind of Unicode feature, then it's going to blow this
3192
        // size limit. Moreover, Unicode tends to defeat the "accelerated"
3193
        // state optimization too, so it's a double whammy.
3194
        //
3195
        // We also use a limit on the number of NFA states to avoid even
3196
        // starting the DFA construction process. Namely, DFA construction
3197
        // itself could make lots of initial allocs proportional to the size
3198
        // of the NFA, and if the NFA is large, it doesn't make sense to pay
3199
        // that cost if we know it's likely to be blown by a large margin.
3200
0
        self.dfa_size_limit.unwrap_or(Some(40 * (1 << 10)))
3201
0
    }
3202
3203
    /// Returns DFA size limit in terms of the number of states in the NFA, as
3204
    /// set by [`Config::dfa_state_limit`].
3205
    ///
3206
    /// If it was not explicitly set, then a default value is returned.
3207
0
    pub fn get_dfa_state_limit(&self) -> Option<usize> {
3208
        // Again, as with the size limit, we keep this very small.
3209
0
        self.dfa_state_limit.unwrap_or(Some(30))
3210
0
    }
3211
3212
    /// Returns whether byte classes are enabled, as set by
3213
    /// [`Config::byte_classes`].
3214
    ///
3215
    /// If it was not explicitly set, then a default value is returned.
3216
0
    pub fn get_byte_classes(&self) -> bool {
3217
0
        self.byte_classes.unwrap_or(true)
3218
0
    }
3219
3220
    /// Returns the line terminator for this configuration, as set by
3221
    /// [`Config::line_terminator`].
3222
    ///
3223
    /// If it was not explicitly set, then a default value is returned.
3224
0
    pub fn get_line_terminator(&self) -> u8 {
3225
0
        self.line_terminator.unwrap_or(b'\n')
3226
0
    }
3227
3228
    /// Returns the configured pool capacity, as set by
3229
    /// [`Config::pool_capacity`].
3230
    ///
3231
    /// If it was not explicitly set, then a default value is returned.
3232
0
    pub fn get_pool_capacity(&self) -> usize {
3233
        // The default is an empirically chosen value that balances memory
3234
        // usage with runtime performance. In practice, with `std` enabled,
3235
        // we choose a value that matches the total number of CPUs.
3236
        const DEFAULT_POOL_CAPACITY: usize = 8;
3237
3238
0
        self.pool_capacity.unwrap_or_else(|| {
3239
            #[cfg(feature = "std")]
3240
            {
3241
                use crate::util::lazy::Lazy;
3242
3243
0
                static AVAILABLE_PARALLELISM: Lazy<usize> = Lazy::new(|| {
3244
0
                    std::thread::available_parallelism()
3245
0
                        .map(|n| n.get())
3246
0
                        .unwrap_or(DEFAULT_POOL_CAPACITY)
3247
0
                });
3248
0
                *Lazy::get(&AVAILABLE_PARALLELISM)
3249
            }
3250
            #[cfg(not(feature = "std"))]
3251
            {
3252
                DEFAULT_POOL_CAPACITY
3253
            }
3254
0
        })
3255
0
    }
3256
3257
    /// Returns whether the hybrid NFA/DFA regex engine may be used, as set by
3258
    /// [`Config::hybrid`].
3259
    ///
3260
    /// If it was not explicitly set, then a default value is returned.
3261
0
    pub fn get_hybrid(&self) -> bool {
3262
        #[cfg(feature = "hybrid")]
3263
        {
3264
0
            self.hybrid.unwrap_or(true)
3265
        }
3266
        #[cfg(not(feature = "hybrid"))]
3267
        {
3268
            false
3269
        }
3270
0
    }
3271
3272
    /// Returns whether the DFA regex engine may be used, as set by
3273
    /// [`Config::dfa`].
3274
    ///
3275
    /// If it was not explicitly set, then a default value is returned.
3276
0
    pub fn get_dfa(&self) -> bool {
3277
        #[cfg(feature = "dfa-build")]
3278
        {
3279
            self.dfa.unwrap_or(true)
3280
        }
3281
        #[cfg(not(feature = "dfa-build"))]
3282
        {
3283
0
            false
3284
        }
3285
0
    }
3286
3287
    /// Returns whether the one-pass DFA regex engine may be used, as set by
3288
    /// [`Config::onepass`].
3289
    ///
3290
    /// If it was not explicitly set, then a default value is returned.
3291
0
    pub fn get_onepass(&self) -> bool {
3292
        #[cfg(feature = "dfa-onepass")]
3293
        {
3294
0
            self.onepass.unwrap_or(true)
3295
        }
3296
        #[cfg(not(feature = "dfa-onepass"))]
3297
        {
3298
            false
3299
        }
3300
0
    }
3301
3302
    /// Returns whether the bounded backtracking regex engine may be used, as
3303
    /// set by [`Config::backtrack`].
3304
    ///
3305
    /// If it was not explicitly set, then a default value is returned.
3306
0
    pub fn get_backtrack(&self) -> bool {
3307
        #[cfg(feature = "nfa-backtrack")]
3308
        {
3309
0
            self.backtrack.unwrap_or(true)
3310
        }
3311
        #[cfg(not(feature = "nfa-backtrack"))]
3312
        {
3313
            false
3314
        }
3315
0
    }
3316
3317
    /// Returns a "baseline" Thompson configuration for constructing NFAs based
3318
    /// on this configuration.
3319
    ///
3320
    /// This is just a convenience routine to avoid repeating configuration
3321
    /// construction.
3322
    ///
3323
    /// Callers may still need to set other things, like whether the NFA should
3324
    /// be compiled in reverse. Callers may also override settings, like
3325
    /// forcing no capture states to be included.
3326
0
    pub(crate) fn to_thompson_config(&self) -> thompson::Config {
3327
0
        let mut lookm = LookMatcher::new();
3328
0
        lookm.set_line_terminator(self.get_line_terminator());
3329
0
        thompson::Config::new()
3330
0
            .utf8(self.get_utf8_empty())
3331
0
            .reverse(false)
3332
0
            .nfa_size_limit(self.get_nfa_size_limit())
3333
0
            .shrink(false)
3334
0
            .which_captures(self.get_which_captures())
3335
0
            .look_matcher(lookm)
3336
0
    }
3337
3338
    /// Overwrite the default configuration such that the options in `o` are
3339
    /// always used. If an option in `o` is not set, then the corresponding
3340
    /// option in `self` is used. If it's not set in `self` either, then it
3341
    /// remains not set.
3342
0
    pub(crate) fn overwrite(&self, o: Config) -> Config {
3343
        Config {
3344
0
            match_kind: o.match_kind.or(self.match_kind),
3345
0
            utf8_empty: o.utf8_empty.or(self.utf8_empty),
3346
0
            autopre: o.autopre.or(self.autopre),
3347
0
            pre: o.pre.or_else(|| self.pre.clone()),
3348
0
            which_captures: o.which_captures.or(self.which_captures),
3349
0
            nfa_size_limit: o.nfa_size_limit.or(self.nfa_size_limit),
3350
0
            onepass_size_limit: o
3351
0
                .onepass_size_limit
3352
0
                .or(self.onepass_size_limit),
3353
0
            hybrid_cache_capacity: o
3354
0
                .hybrid_cache_capacity
3355
0
                .or(self.hybrid_cache_capacity),
3356
0
            hybrid: o.hybrid.or(self.hybrid),
3357
0
            dfa: o.dfa.or(self.dfa),
3358
0
            dfa_size_limit: o.dfa_size_limit.or(self.dfa_size_limit),
3359
0
            dfa_state_limit: o.dfa_state_limit.or(self.dfa_state_limit),
3360
0
            onepass: o.onepass.or(self.onepass),
3361
0
            backtrack: o.backtrack.or(self.backtrack),
3362
0
            byte_classes: o.byte_classes.or(self.byte_classes),
3363
0
            line_terminator: o.line_terminator.or(self.line_terminator),
3364
0
            pool_capacity: o.pool_capacity.or(self.pool_capacity),
3365
        }
3366
0
    }
3367
}
3368
3369
/// A builder for configuring and constructing a `Regex`.
3370
///
3371
/// The builder permits configuring two different aspects of a `Regex`:
3372
///
3373
/// * [`Builder::configure`] will set high-level configuration options as
3374
/// described by a [`Config`].
3375
/// * [`Builder::syntax`] will set the syntax level configuration options
3376
/// as described by a [`util::syntax::Config`](crate::util::syntax::Config).
3377
/// This only applies when building a `Regex` from pattern strings.
3378
///
3379
/// Once configured, the builder can then be used to construct a `Regex` from
3380
/// one of 4 different inputs:
3381
///
3382
/// * [`Builder::build`] creates a regex from a single pattern string.
3383
/// * [`Builder::build_many`] creates a regex from many pattern strings.
3384
/// * [`Builder::build_from_hir`] creates a regex from a
3385
/// [`regex-syntax::Hir`](Hir) expression.
3386
/// * [`Builder::build_many_from_hir`] creates a regex from many
3387
/// [`regex-syntax::Hir`](Hir) expressions.
3388
///
3389
/// The latter two methods in particular provide a way to construct a fully
3390
/// feature regular expression matcher directly from an `Hir` expression
3391
/// without having to first convert it to a string. (This is in contrast to the
3392
/// top-level `regex` crate which intentionally provides no such API in order
3393
/// to avoid making `regex-syntax` a public dependency.)
3394
///
3395
/// As a convenience, this builder may be created via [`Regex::builder`], which
3396
/// may help avoid an extra import.
3397
///
3398
/// # Example: change the line terminator
3399
///
3400
/// This example shows how to enable multi-line mode by default and change the
3401
/// line terminator to the NUL byte:
3402
///
3403
/// ```
3404
/// use regex_automata::{meta::Regex, util::syntax, Match};
3405
///
3406
/// let re = Regex::builder()
3407
///     .syntax(syntax::Config::new().multi_line(true))
3408
///     .configure(Regex::config().line_terminator(b'\x00'))
3409
///     .build(r"^foo$")?;
3410
/// let hay = "\x00foo\x00";
3411
/// assert_eq!(Some(Match::must(0, 1..4)), re.find(hay));
3412
///
3413
/// # Ok::<(), Box<dyn std::error::Error>>(())
3414
/// ```
3415
///
3416
/// # Example: disable UTF-8 requirement
3417
///
3418
/// By default, regex patterns are required to match UTF-8. This includes
3419
/// regex patterns that can produce matches of length zero. In the case of an
3420
/// empty match, by default, matches will not appear between the code units of
3421
/// a UTF-8 encoded codepoint.
3422
///
3423
/// However, it can be useful to disable this requirement, particularly if
3424
/// you're searching things like `&[u8]` that are not known to be valid UTF-8.
3425
///
3426
/// ```
3427
/// use regex_automata::{meta::Regex, util::syntax, Match};
3428
///
3429
/// let mut builder = Regex::builder();
3430
/// // Disables the requirement that non-empty matches match UTF-8.
3431
/// builder.syntax(syntax::Config::new().utf8(false));
3432
/// // Disables the requirement that empty matches match UTF-8 boundaries.
3433
/// builder.configure(Regex::config().utf8_empty(false));
3434
///
3435
/// // We can match raw bytes via \xZZ syntax, but we need to disable
3436
/// // Unicode mode to do that. We could disable it everywhere, or just
3437
/// // selectively, as shown here.
3438
/// let re = builder.build(r"(?-u:\xFF)foo(?-u:\xFF)")?;
3439
/// let hay = b"\xFFfoo\xFF";
3440
/// assert_eq!(Some(Match::must(0, 0..5)), re.find(hay));
3441
///
3442
/// // We can also match between code units.
3443
/// let re = builder.build(r"")?;
3444
/// let hay = "☃";
3445
/// assert_eq!(re.find_iter(hay).collect::<Vec<Match>>(), vec![
3446
///     Match::must(0, 0..0),
3447
///     Match::must(0, 1..1),
3448
///     Match::must(0, 2..2),
3449
///     Match::must(0, 3..3),
3450
/// ]);
3451
///
3452
/// # Ok::<(), Box<dyn std::error::Error>>(())
3453
/// ```
3454
#[derive(Clone, Debug)]
3455
pub struct Builder {
3456
    config: Config,
3457
    ast: ast::parse::ParserBuilder,
3458
    hir: hir::translate::TranslatorBuilder,
3459
}
3460
3461
impl Builder {
3462
    /// Creates a new builder for configuring and constructing a [`Regex`].
3463
0
    pub fn new() -> Builder {
3464
0
        Builder {
3465
0
            config: Config::default(),
3466
0
            ast: ast::parse::ParserBuilder::new(),
3467
0
            hir: hir::translate::TranslatorBuilder::new(),
3468
0
        }
3469
0
    }
3470
3471
    /// Builds a `Regex` from a single pattern string.
3472
    ///
3473
    /// If there was a problem parsing the pattern or a problem turning it into
3474
    /// a regex matcher, then an error is returned.
3475
    ///
3476
    /// # Example
3477
    ///
3478
    /// This example shows how to configure syntax options.
3479
    ///
3480
    /// ```
3481
    /// use regex_automata::{meta::Regex, util::syntax, Match};
3482
    ///
3483
    /// let re = Regex::builder()
3484
    ///     .syntax(syntax::Config::new().crlf(true).multi_line(true))
3485
    ///     .build(r"^foo$")?;
3486
    /// let hay = "\r\nfoo\r\n";
3487
    /// assert_eq!(Some(Match::must(0, 2..5)), re.find(hay));
3488
    ///
3489
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3490
    /// ```
3491
0
    pub fn build(&self, pattern: &str) -> Result<Regex, BuildError> {
3492
0
        self.build_many(&[pattern])
3493
0
    }
3494
3495
    /// Builds a `Regex` from many pattern strings.
3496
    ///
3497
    /// If there was a problem parsing any of the patterns or a problem turning
3498
    /// them into a regex matcher, then an error is returned.
3499
    ///
3500
    /// # Example: finding the pattern that caused an error
3501
    ///
3502
    /// When a syntax error occurs, it is possible to ask which pattern
3503
    /// caused the syntax error.
3504
    ///
3505
    /// ```
3506
    /// use regex_automata::{meta::Regex, PatternID};
3507
    ///
3508
    /// let err = Regex::builder()
3509
    ///     .build_many(&["a", "b", r"\p{Foo}", "c"])
3510
    ///     .unwrap_err();
3511
    /// assert_eq!(Some(PatternID::must(2)), err.pattern());
3512
    /// ```
3513
    ///
3514
    /// # Example: zero patterns is valid
3515
    ///
3516
    /// Building a regex with zero patterns results in a regex that never
3517
    /// matches anything. Because this routine is generic, passing an empty
3518
    /// slice usually requires a turbo-fish (or something else to help type
3519
    /// inference).
3520
    ///
3521
    /// ```
3522
    /// use regex_automata::{meta::Regex, util::syntax, Match};
3523
    ///
3524
    /// let re = Regex::builder()
3525
    ///     .build_many::<&str>(&[])?;
3526
    /// assert_eq!(None, re.find(""));
3527
    ///
3528
    /// # Ok::<(), Box<dyn std::error::Error>>(())
3529
    /// ```
3530
0
    pub fn build_many<P: AsRef<str>>(
3531
0
        &self,
3532
0
        patterns: &[P],
3533
0
    ) -> Result<Regex, BuildError> {
3534
        use crate::util::primitives::IteratorIndexExt;
3535
        log! {
3536
            debug!("building meta regex with {} patterns:", patterns.len());
3537
            for (pid, p) in patterns.iter().with_pattern_ids() {
3538
                let p = p.as_ref();
3539
                // We might split a grapheme with this truncation logic, but
3540
                // that's fine. We at least avoid splitting a codepoint.
3541
                let maxoff = p
3542
                    .char_indices()
3543
                    .map(|(i, ch)| i + ch.len_utf8())
3544
                    .take(1000)
3545
                    .last()
3546
                    .unwrap_or(0);
3547
                if maxoff < p.len() {
3548
                    debug!("{pid:?}: {}[... snip ...]", &p[..maxoff]);
3549
                } else {
3550
                    debug!("{pid:?}: {p}");
3551
                }
3552
            }
3553
        }
3554
0
        let (mut asts, mut hirs) = (vec![], vec![]);
3555
0
        for (pid, p) in patterns.iter().with_pattern_ids() {
3556
0
            let ast = self
3557
0
                .ast
3558
0
                .build()
3559
0
                .parse(p.as_ref())
3560
0
                .map_err(|err| BuildError::ast(pid, err))?;
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many::<alloc::string::String>::{closure#0}
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many::<&str>::{closure#0}
3561
0
            asts.push(ast);
3562
        }
3563
0
        for ((pid, p), ast) in
3564
0
            patterns.iter().with_pattern_ids().zip(asts.iter())
3565
        {
3566
0
            let hir = self
3567
0
                .hir
3568
0
                .build()
3569
0
                .translate(p.as_ref(), ast)
3570
0
                .map_err(|err| BuildError::hir(pid, err))?;
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many::<alloc::string::String>::{closure#1}
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many::<&str>::{closure#1}
3571
0
            hirs.push(hir);
3572
        }
3573
0
        self.build_many_from_hir(&hirs)
3574
0
    }
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many::<alloc::string::String>
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many::<&str>
3575
3576
    /// Builds a `Regex` directly from an `Hir` expression.
3577
    ///
3578
    /// This is useful if you needed to parse a pattern string into an `Hir`
3579
    /// for other reasons (such as analysis or transformations). This routine
3580
    /// permits building a `Regex` directly from the `Hir` expression instead
3581
    /// of first converting the `Hir` back to a pattern string.
3582
    ///
3583
    /// When using this method, any options set via [`Builder::syntax`] are
3584
    /// ignored. Namely, the syntax options only apply when parsing a pattern
3585
    /// string, which isn't relevant here.
3586
    ///
3587
    /// If there was a problem building the underlying regex matcher for the
3588
    /// given `Hir`, then an error is returned.
3589
    ///
3590
    /// # Example
3591
    ///
3592
    /// This example shows how one can hand-construct an `Hir` expression and
3593
    /// build a regex from it without doing any parsing at all.
3594
    ///
3595
    /// ```
3596
    /// use {
3597
    ///     regex_automata::{meta::Regex, Match},
3598
    ///     regex_syntax::hir::{Hir, Look},
3599
    /// };
3600
    ///
3601
    /// // (?Rm)^foo$
3602
    /// let hir = Hir::concat(vec![
3603
    ///     Hir::look(Look::StartCRLF),
3604
    ///     Hir::literal("foo".as_bytes()),
3605
    ///     Hir::look(Look::EndCRLF),
3606
    /// ]);
3607
    /// let re = Regex::builder()
3608
    ///     .build_from_hir(&hir)?;
3609
    /// let hay = "\r\nfoo\r\n";
3610
    /// assert_eq!(Some(Match::must(0, 2..5)), re.find(hay));
3611
    ///
3612
    /// Ok::<(), Box<dyn std::error::Error>>(())
3613
    /// ```
3614
0
    pub fn build_from_hir(&self, hir: &Hir) -> Result<Regex, BuildError> {
3615
0
        self.build_many_from_hir(&[hir])
3616
0
    }
3617
3618
    /// Builds a `Regex` directly from many `Hir` expressions.
3619
    ///
3620
    /// This is useful if you needed to parse pattern strings into `Hir`
3621
    /// expressions for other reasons (such as analysis or transformations).
3622
    /// This routine permits building a `Regex` directly from the `Hir`
3623
    /// expressions instead of first converting the `Hir` expressions back to
3624
    /// pattern strings.
3625
    ///
3626
    /// When using this method, any options set via [`Builder::syntax`] are
3627
    /// ignored. Namely, the syntax options only apply when parsing a pattern
3628
    /// string, which isn't relevant here.
3629
    ///
3630
    /// If there was a problem building the underlying regex matcher for the
3631
    /// given `Hir` expressions, then an error is returned.
3632
    ///
3633
    /// Note that unlike [`Builder::build_many`], this can only fail as a
3634
    /// result of building the underlying matcher. In that case, there is
3635
    /// no single `Hir` expression that can be isolated as a reason for the
3636
    /// failure. So if this routine fails, it's not possible to determine which
3637
    /// `Hir` expression caused the failure.
3638
    ///
3639
    /// # Example
3640
    ///
3641
    /// This example shows how one can hand-construct multiple `Hir`
3642
    /// expressions and build a single regex from them without doing any
3643
    /// parsing at all.
3644
    ///
3645
    /// ```
3646
    /// use {
3647
    ///     regex_automata::{meta::Regex, Match},
3648
    ///     regex_syntax::hir::{Hir, Look},
3649
    /// };
3650
    ///
3651
    /// // (?Rm)^foo$
3652
    /// let hir1 = Hir::concat(vec![
3653
    ///     Hir::look(Look::StartCRLF),
3654
    ///     Hir::literal("foo".as_bytes()),
3655
    ///     Hir::look(Look::EndCRLF),
3656
    /// ]);
3657
    /// // (?Rm)^bar$
3658
    /// let hir2 = Hir::concat(vec![
3659
    ///     Hir::look(Look::StartCRLF),
3660
    ///     Hir::literal("bar".as_bytes()),
3661
    ///     Hir::look(Look::EndCRLF),
3662
    /// ]);
3663
    /// let re = Regex::builder()
3664
    ///     .build_many_from_hir(&[&hir1, &hir2])?;
3665
    /// let hay = "\r\nfoo\r\nbar";
3666
    /// let got: Vec<Match> = re.find_iter(hay).collect();
3667
    /// let expected = vec![
3668
    ///     Match::must(0, 2..5),
3669
    ///     Match::must(1, 7..10),
3670
    /// ];
3671
    /// assert_eq!(expected, got);
3672
    ///
3673
    /// Ok::<(), Box<dyn std::error::Error>>(())
3674
    /// ```
3675
0
    pub fn build_many_from_hir<H: Borrow<Hir>>(
3676
0
        &self,
3677
0
        hirs: &[H],
3678
0
    ) -> Result<Regex, BuildError> {
3679
0
        let config = self.config.clone();
3680
        // We collect the HIRs into a vec so we can write internal routines
3681
        // with '&[&Hir]'. i.e., Don't use generics everywhere to keep code
3682
        // bloat down..
3683
0
        let hirs: Vec<&Hir> = hirs.iter().map(|hir| hir.borrow()).collect();
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many_from_hir::<regex_syntax::hir::Hir>::{closure#0}
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many_from_hir::<&regex_syntax::hir::Hir>::{closure#0}
3684
0
        let info = RegexInfo::new(config, &hirs);
3685
0
        let strat = strategy::new(&info, &hirs)?;
3686
0
        let pool = {
3687
0
            let strat = Arc::clone(&strat);
3688
0
            let create: CachePoolFn = Box::new(move || strat.create_cache());
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many_from_hir::<regex_syntax::hir::Hir>::{closure#1}
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many_from_hir::<&regex_syntax::hir::Hir>::{closure#1}
3689
0
            Pool::with_capacity(self.config.get_pool_capacity(), create)
3690
        };
3691
0
        Ok(Regex { imp: Arc::new(RegexI { strat, info }), pool })
3692
0
    }
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many_from_hir::<regex_syntax::hir::Hir>
Unexecuted instantiation: <regex_automata::meta::regex::Builder>::build_many_from_hir::<&regex_syntax::hir::Hir>
3693
3694
    /// Configure the behavior of a `Regex`.
3695
    ///
3696
    /// This configuration controls non-syntax options related to the behavior
3697
    /// of a `Regex`. This includes things like whether empty matches can split
3698
    /// a codepoint, prefilters, line terminators and a long list of options
3699
    /// for configuring which regex engines the meta regex engine will be able
3700
    /// to use internally.
3701
    ///
3702
    /// # Example
3703
    ///
3704
    /// This example shows how to disable UTF-8 empty mode. This will permit
3705
    /// empty matches to occur between the UTF-8 encoding of a codepoint.
3706
    ///
3707
    /// ```
3708
    /// use regex_automata::{meta::Regex, Match};
3709
    ///
3710
    /// let re = Regex::new("")?;
3711
    /// let got: Vec<Match> = re.find_iter("☃").collect();
3712
    /// // Matches only occur at the beginning and end of the snowman.
3713
    /// assert_eq!(got, vec![
3714
    ///     Match::must(0, 0..0),
3715
    ///     Match::must(0, 3..3),
3716
    /// ]);
3717
    ///
3718
    /// let re = Regex::builder()
3719
    ///     .configure(Regex::config().utf8_empty(false))
3720
    ///     .build("")?;
3721
    /// let got: Vec<Match> = re.find_iter("☃").collect();
3722
    /// // Matches now occur at every position!
3723
    /// assert_eq!(got, vec![
3724
    ///     Match::must(0, 0..0),
3725
    ///     Match::must(0, 1..1),
3726
    ///     Match::must(0, 2..2),
3727
    ///     Match::must(0, 3..3),
3728
    /// ]);
3729
    ///
3730
    /// Ok::<(), Box<dyn std::error::Error>>(())
3731
    /// ```
3732
0
    pub fn configure(&mut self, config: Config) -> &mut Builder {
3733
0
        self.config = self.config.overwrite(config);
3734
0
        self
3735
0
    }
3736
3737
    /// Configure the syntax options when parsing a pattern string while
3738
    /// building a `Regex`.
3739
    ///
3740
    /// These options _only_ apply when [`Builder::build`] or [`Builder::build_many`]
3741
    /// are used. The other build methods accept `Hir` values, which have
3742
    /// already been parsed.
3743
    ///
3744
    /// # Example
3745
    ///
3746
    /// This example shows how to enable case insensitive mode.
3747
    ///
3748
    /// ```
3749
    /// use regex_automata::{meta::Regex, util::syntax, Match};
3750
    ///
3751
    /// let re = Regex::builder()
3752
    ///     .syntax(syntax::Config::new().case_insensitive(true))
3753
    ///     .build(r"δ")?;
3754
    /// assert_eq!(Some(Match::must(0, 0..2)), re.find(r"Δ"));
3755
    ///
3756
    /// Ok::<(), Box<dyn std::error::Error>>(())
3757
    /// ```
3758
0
    pub fn syntax(
3759
0
        &mut self,
3760
0
        config: crate::util::syntax::Config,
3761
0
    ) -> &mut Builder {
3762
0
        config.apply_ast(&mut self.ast);
3763
0
        config.apply_hir(&mut self.hir);
3764
0
        self
3765
0
    }
3766
}
3767
3768
#[cfg(test)]
3769
mod tests {
3770
    use super::*;
3771
3772
    // I found this in the course of building out the benchmark suite for
3773
    // rebar.
3774
    #[test]
3775
    fn regression_suffix_literal_count() {
3776
        let _ = env_logger::try_init();
3777
3778
        let re = Regex::new(r"[a-zA-Z]+ing").unwrap();
3779
        assert_eq!(1, re.find_iter("tingling").count());
3780
    }
3781
}