Struct rama::http::matcher::uri::dep::regex::bytes::RegexSet

pub struct RegexSet { /* private fields */ }
Expand description

Match multiple, possibly overlapping, regexes in a single search.

A regex set corresponds to the union of zero or more regular expressions. That is, a regex set will match a haystack when at least one of its constituent regexes matches. A regex set as its formulated here provides a touch more power: it will also report which regular expressions in the set match. Indeed, this is the key difference between regex sets and a single Regex with many alternates, since only one alternate can match at a time.

For example, consider regular expressions to match email addresses and domains: [a-z]+@[a-z]+\.(com|org|net) and [a-z]+\.(com|org|net). If a regex set is constructed from those regexes, then searching the haystack foo@example.com will report both regexes as matching. Of course, one could accomplish this by compiling each regex on its own and doing two searches over the haystack. The key advantage of using a regex set is that it will report the matching regexes using a single pass through the haystack. If one has hundreds or thousands of regexes to match repeatedly (like a URL router for a complex web application or a user agent matcher), then a regex set can realize huge performance gains.

Unlike the top-level RegexSet, this RegexSet searches haystacks with type &[u8] instead of &str. Consequently, this RegexSet is permitted to match invalid UTF-8.

§Limitations

Regex sets are limited to answering the following two questions:

  1. Does any regex in the set match?
  2. If so, which regexes in the set match?

As with the main Regex type, it is cheaper to ask (1) instead of (2) since the matching engines can stop after the first match is found.

You cannot directly extract Match or Captures objects from a regex set. If you need these operations, the recommended approach is to compile each pattern in the set independently and scan the exact same haystack a second time with those independently compiled patterns:

use regex::bytes::{Regex, RegexSet};

let patterns = ["foo", "bar"];
// Both patterns will match different ranges of this string.
let hay = b"barfoo";

// Compile a set matching any of our patterns.
let set = RegexSet::new(patterns).unwrap();
// Compile each pattern independently.
let regexes: Vec<_> = set
    .patterns()
    .iter()
    .map(|pat| Regex::new(pat).unwrap())
    .collect();

// Match against the whole set first and identify the individual
// matching patterns.
let matches: Vec<&[u8]> = set
    .matches(hay)
    .into_iter()
    // Dereference the match index to get the corresponding
    // compiled pattern.
    .map(|index| &regexes[index])
    // To get match locations or any other info, we then have to search the
    // exact same haystack again, using our separately-compiled pattern.
    .map(|re| re.find(hay).unwrap().as_bytes())
    .collect();

// Matches arrive in the order the constituent patterns were declared,
// not the order they appear in the haystack.
assert_eq!(vec![&b"foo"[..], &b"bar"[..]], matches);

§Performance

A RegexSet has the same performance characteristics as Regex. Namely, search takes O(m * n) time, where m is proportional to the size of the regex set and n is proportional to the length of the haystack.

§Trait implementations

The Default trait is implemented for RegexSet. The default value is an empty set. An empty set can also be explicitly constructed via RegexSet::empty.

§Example

This shows how the above two regexes (for matching email addresses and domains) might work:

use regex::bytes::RegexSet;

let set = RegexSet::new(&[
    r"[a-z]+@[a-z]+\.(com|org|net)",
    r"[a-z]+\.(com|org|net)",
]).unwrap();

// Ask whether any regexes in the set match.
assert!(set.is_match(b"foo@example.com"));

// Identify which regexes in the set match.
let matches: Vec<_> = set.matches(b"foo@example.com").into_iter().collect();
assert_eq!(vec![0, 1], matches);

// Try again, but with a haystack that only matches one of the regexes.
let matches: Vec<_> = set.matches(b"example.com").into_iter().collect();
assert_eq!(vec![1], matches);

// Try again, but with a haystack that doesn't match any regex in the set.
let matches: Vec<_> = set.matches(b"example").into_iter().collect();
assert!(matches.is_empty());

Note that it would be possible to adapt the above example to using Regex with an expression like:

(?P<email>[a-z]+@(?P<email_domain>[a-z]+[.](com|org|net)))|(?P<domain>[a-z]+[.](com|org|net))

After a match, one could then inspect the capture groups to figure out which alternates matched. The problem is that it is hard to make this approach scale when there are many regexes since the overlap between each alternate isn’t always obvious to reason about.

Implementations§

§

impl RegexSet

pub fn new<I, S>(exprs: I) -> Result<RegexSet, Error>
where S: AsRef<str>, I: IntoIterator<Item = S>,

Create a new regex set with the given regular expressions.

This takes an iterator of S, where S is something that can produce a &str. If any of the strings in the iterator are not valid regular expressions, then an error is returned.

§Example

Create a new regex set from an iterator of strings:

use regex::bytes::RegexSet;

let set = RegexSet::new([r"\w+", r"\d+"]).unwrap();
assert!(set.is_match(b"foo"));

pub fn empty() -> RegexSet

Create a new empty regex set.

An empty regex never matches anything.

This is a convenience function for RegexSet::new([]), but doesn’t require one to specify the type of the input.

§Example
use regex::bytes::RegexSet;

let set = RegexSet::empty();
assert!(set.is_empty());
// an empty set matches nothing
assert!(!set.is_match(b""));

pub fn is_match(&self, haystack: &[u8]) -> bool

Returns true if and only if one of the regexes in this set matches the haystack given.

This method should be preferred if you only need to test whether any of the regexes in the set should match, but don’t care about which regexes matched. This is because the underlying matching engine will quit immediately after seeing the first match instead of continuing to find all matches.

Note that as with searches using Regex, the expression is unanchored by default. That is, if the regex does not start with ^ or \A, or end with $ or \z, then it is permitted to match anywhere in the haystack.

§Example

Tests whether a set matches somewhere in a haystack:

use regex::bytes::RegexSet;

let set = RegexSet::new([r"\w+", r"\d+"]).unwrap();
assert!(set.is_match(b"foo"));
assert!(!set.is_match("☃".as_bytes()));

pub fn is_match_at(&self, haystack: &[u8], start: usize) -> bool

Returns true if and only if one of the regexes in this set matches the haystack given, with the search starting at the offset given.

The significance of the starting point is that it takes the surrounding context into consideration. For example, the \A anchor can only match when start == 0.

§Panics

This panics when start >= haystack.len() + 1.

§Example

This example shows the significance of start. Namely, consider a haystack foobar and a desire to execute a search starting at offset 3. You could search a substring explicitly, but then the look-around assertions won’t work correctly. Instead, you can use this method to specify the start position of a search.

use regex::bytes::RegexSet;

let set = RegexSet::new([r"\bbar\b", r"(?m)^bar$"]).unwrap();
let hay = b"foobar";
// We get a match here, but it's probably not intended.
assert!(set.is_match(&hay[3..]));
// No match because the  assertions take the context into account.
assert!(!set.is_match_at(hay, 3));

pub fn matches(&self, haystack: &[u8]) -> SetMatches

Returns the set of regexes that match in the given haystack.

The set returned contains the index of each regex that matches in the given haystack. The index is in correspondence with the order of regular expressions given to RegexSet’s constructor.

The set can also be used to iterate over the matched indices. The order of iteration is always ascending with respect to the matching indices.

Note that as with searches using Regex, the expression is unanchored by default. That is, if the regex does not start with ^ or \A, or end with $ or \z, then it is permitted to match anywhere in the haystack.

§Example

Tests which regular expressions match the given haystack:

use regex::bytes::RegexSet;

let set = RegexSet::new([
    r"\w+",
    r"\d+",
    r"\pL+",
    r"foo",
    r"bar",
    r"barfoo",
    r"foobar",
]).unwrap();
let matches: Vec<_> = set.matches(b"foobar").into_iter().collect();
assert_eq!(matches, vec![0, 2, 3, 4, 6]);

// You can also test whether a particular regex matched:
let matches = set.matches(b"foobar");
assert!(!matches.matched(5));
assert!(matches.matched(6));

pub fn matches_at(&self, haystack: &[u8], start: usize) -> SetMatches

Returns the set of regexes that match in the given haystack.

The set returned contains the index of each regex that matches in the given haystack. The index is in correspondence with the order of regular expressions given to RegexSet’s constructor.

The set can also be used to iterate over the matched indices. The order of iteration is always ascending with respect to the matching indices.

The significance of the starting point is that it takes the surrounding context into consideration. For example, the \A anchor can only match when start == 0.

§Panics

This panics when start >= haystack.len() + 1.

§Example

Tests which regular expressions match the given haystack:

use regex::bytes::RegexSet;

let set = RegexSet::new([r"\bbar\b", r"(?m)^bar$"]).unwrap();
let hay = b"foobar";
// We get matches here, but it's probably not intended.
let matches: Vec<_> = set.matches(&hay[3..]).into_iter().collect();
assert_eq!(matches, vec![0, 1]);
// No matches because the  assertions take the context into account.
let matches: Vec<_> = set.matches_at(hay, 3).into_iter().collect();
assert_eq!(matches, vec![]);

pub fn len(&self) -> usize

Returns the total number of regexes in this set.

§Example
use regex::bytes::RegexSet;

assert_eq!(0, RegexSet::empty().len());
assert_eq!(1, RegexSet::new([r"[0-9]"]).unwrap().len());
assert_eq!(2, RegexSet::new([r"[0-9]", r"[a-z]"]).unwrap().len());

pub fn is_empty(&self) -> bool

Returns true if this set contains no regexes.

§Example
use regex::bytes::RegexSet;

assert!(RegexSet::empty().is_empty());
assert!(!RegexSet::new([r"[0-9]"]).unwrap().is_empty());

pub fn patterns(&self) -> &[String]

Returns the regex patterns that this regex set was constructed from.

This function can be used to determine the pattern for a match. The slice returned has exactly as many patterns givens to this regex set, and the order of the slice is the same as the order of the patterns provided to the set.

§Example
use regex::bytes::RegexSet;

let set = RegexSet::new(&[
    r"\w+",
    r"\d+",
    r"\pL+",
    r"foo",
    r"bar",
    r"barfoo",
    r"foobar",
]).unwrap();
let matches: Vec<_> = set
    .matches(b"foobar")
    .into_iter()
    .map(|index| &set.patterns()[index])
    .collect();
assert_eq!(matches, vec![r"\w+", r"\pL+", r"foo", r"bar", r"foobar"]);

Trait Implementations§

§

impl Clone for RegexSet

§

fn clone(&self) -> RegexSet

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for RegexSet

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Default for RegexSet

§

fn default() -> RegexSet

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> CloneToUninit for T
where T: Clone,

source§

default unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
§

impl<T> Conv for T

§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FutureExt for T

§

fn with_context(self, otel_cx: Context) -> WithContext<Self>

Attaches the provided Context to this type, returning a WithContext wrapper. Read more
§

fn with_current_context(self) -> WithContext<Self>

Attaches the current Context to this type, returning a WithContext wrapper. Read more
§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> Pipe for T
where T: ?Sized,

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<T> PolicyExt for T
where T: ?Sized,

§

fn and<S, P, B, E>(self, other: P) -> And<T, P>
where T: Policy<S, B, E>, P: Policy<S, B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
§

fn or<S, P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<S, B, E>, P: Policy<S, B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more