Skip to main content

regex_automata/meta/
regex.rs

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