regex_cursor/engines/meta/
regex.rs

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