Struct PatternSet

pub struct PatternSet { /* private fields */ }

A set of PatternIDs.

A set of pattern identifiers is useful for recording which patterns have matched a particular haystack. A pattern set only includes pattern identifiers. It does not include offset information.

Example

This shows basic usage of a set.

use regex_automata::{PatternID, PatternSet};

let pid1 = PatternID::must(5);
let pid2 = PatternID::must(8);
// Create a new empty set.
let mut set = PatternSet::new(10);
// Insert pattern IDs.
set.insert(pid1);
set.insert(pid2);
// Test membership.
assert!(set.contains(pid1));
assert!(set.contains(pid2));
// Get all members.
assert_eq!(
    vec![5, 8],
    set.iter().map(|p| p.as_usize()).collect::<Vec<usize>>(),
);
// Clear the set.
set.clear();
// Test that it is indeed empty.
assert!(set.is_empty());

Implementations

impl PatternSet

fn new(capacity: usize) -> PatternSet

Create a new set of pattern identifiers with the given capacity.

The given capacity typically corresponds to (at least) the number of patterns in a compiled regex object.

Panics

This panics if the given capacity exceeds PatternID::LIMIT. This is impossible if you use the pattern_len() method as defined on any of the regex engines in this crate. Namely, a regex will fail to build by returning an error if the number of patterns given to it exceeds the limit. Therefore, the number of patterns in a valid regex is always a correct capacity to provide here.

fn clear(&mut self)

Clear this set such that it contains no pattern IDs.

fn contains(&self, pid: PatternID) -> bool

Return true if and only if the given pattern identifier is in this set.

fn insert(&mut self, pid: PatternID) -> bool

Insert the given pattern identifier into this set and return true if the given pattern ID was not previously in this set.

If the pattern identifier is already in this set, then this is a no-op.

Use PatternSet::try_insert for a fallible version of this routine.

Panics

This panics if this pattern set has insufficient capacity to store the given pattern ID.

fn try_insert(&mut self, pid: PatternID) -> Result<bool, PatternSetInsertError>

Insert the given pattern identifier into this set and return true if the given pattern ID was not previously in this set.

If the pattern identifier is already in this set, then this is a no-op.

Errors

This returns an error if this pattern set has insufficient capacity to store the given pattern ID.

fn is_empty(&self) -> bool

Return true if and only if this set has no pattern identifiers in it.

fn is_full(&self) -> bool

Return true if and only if this set has the maximum number of pattern identifiers in the set. This occurs precisely when PatternSet::len() == PatternSet::capacity().

This particular property is useful to test because it may allow one to stop a search earlier than you might otherwise. Namely, if a search is only reporting which patterns match a haystack and if you know all of the patterns match at a given point, then there's no new information that can be learned by continuing the search. (Because a pattern set does not keep track of offset information.)

fn len(&self) -> usize

Returns the total number of pattern identifiers in this set.

fn capacity(&self) -> usize

Returns the total number of pattern identifiers that may be stored in this set.

This is guaranteed to be less than or equal to PatternID::LIMIT.

Typically, the capacity of a pattern set matches the number of patterns in a regex object with which you are searching.

fn iter(&self) -> PatternSetIter<'_>

Returns an iterator over all pattern identifiers in this set.

The iterator yields pattern identifiers in ascending order, starting at zero.

Trait Implementations

impl Clone for PatternSet

fn clone(&self) -> Self

impl Debug for PatternSet

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

impl Eq for PatternSet

impl PartialEq for PatternSet

fn eq(&self, other: &Self) -> bool

impl StructuralPartialEq for PatternSet

Auto Trait Implementations

impl Freeze for PatternSet

impl RefUnwindSafe for PatternSet

impl Send for PatternSet

impl Sync for PatternSet

impl Unpin for PatternSet

impl UnsafeUnpin for PatternSet

impl UnwindSafe for PatternSet

Blanket Implementations

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

fn type_id(&self) -> TypeId

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

fn borrow(&self) -> &T

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

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

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

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for PatternSet

fn from(t: T) -> T

Returns the argument unchanged.

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

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

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, U> TryFrom<U> for PatternSet where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

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

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