regex_automata/meta/
regex.rs

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