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

A set of literal byte strings extracted from a regular expression.

Every member of the set is a Literal, which is represented by a Vec<u8>. (Notably, it may contain invalid UTF-8.) Every member is said to be either complete or cut. A complete literal means that it extends until the beginning (or end) of the regular expression. In some circumstances, this can be used to indicate a match in the regular expression.

A key aspect of literal extraction is knowing when to stop. It is not feasible to blindly extract all literals from a regular expression, even if there are finitely many. For example, the regular expression [0-9]{10} has 10^10 distinct literals. For this reason, literal extraction is bounded to some low number by default using heuristics, but the limits can be tweaked.

WARNING: Literal extraction uses stack space proportional to the size of the Hir expression. At some point, this drawback will be eliminated. To protect yourself, set a reasonable nest_limit on your Parser. This is done for you by default.

Implementations§

source§

impl Literals

source

pub fn empty() -> Literals

Returns a new empty set of literals using default limits.

source

pub fn prefixes(expr: &Hir) -> Literals

Returns a set of literal prefixes extracted from the given Hir.

source

pub fn suffixes(expr: &Hir) -> Literals

Returns a set of literal suffixes extracted from the given Hir.

source

pub fn limit_size(&self) -> usize

Get the approximate size limit (in bytes) of this set.

source

pub fn set_limit_size(&mut self, size: usize) -> &mut Literals

Set the approximate size limit (in bytes) of this set.

If extracting a literal would put the set over this limit, then extraction stops.

The new limits will only apply to additions to this set. Existing members remain unchanged, even if the set exceeds the new limit.

source

pub fn limit_class(&self) -> usize

Get the character class size limit for this set.

source

pub fn set_limit_class(&mut self, size: usize) -> &mut Literals

Limits the size of character(or byte) classes considered.

A value of 0 prevents all character classes from being considered.

This limit also applies to case insensitive literals, since each character in the case insensitive literal is converted to a class, and then case folded.

The new limits will only apply to additions to this set. Existing members remain unchanged, even if the set exceeds the new limit.

source

pub fn literals(&self) -> &[Literal]

Returns the set of literals as a slice. Its order is unspecified.

source

pub fn min_len(&self) -> Option<usize>

Returns the length of the smallest literal.

Returns None is there are no literals in the set.

source

pub fn all_complete(&self) -> bool

Returns true if all members in this set are complete.

source

pub fn any_complete(&self) -> bool

Returns true if any member in this set is complete.

source

pub fn contains_empty(&self) -> bool

Returns true if this set contains an empty literal.

source

pub fn is_empty(&self) -> bool

Returns true if this set is empty or if all of its members is empty.

source

pub fn to_empty(&self) -> Literals

Returns a new empty set of literals using this set’s limits.

source

pub fn longest_common_prefix(&self) -> &[u8]

Returns the longest common prefix of all members in this set.

source

pub fn longest_common_suffix(&self) -> &[u8]

Returns the longest common suffix of all members in this set.

source

pub fn trim_suffix(&self, num_bytes: usize) -> Option<Literals>

Returns a new set of literals with the given number of bytes trimmed from the suffix of each literal.

If any literal would be cut out completely by trimming, then None is returned.

Any duplicates that are created as a result of this transformation are removed.

source

pub fn unambiguous_prefixes(&self) -> Literals

Returns a new set of prefixes of this set of literals that are guaranteed to be unambiguous.

Any substring match with a member of the set is returned is guaranteed to never overlap with a substring match of another member of the set at the same starting position.

Given any two members of the returned set, neither is a substring of the other.

source

pub fn unambiguous_suffixes(&self) -> Literals

Returns a new set of suffixes of this set of literals that are guaranteed to be unambiguous.

Any substring match with a member of the set is returned is guaranteed to never overlap with a substring match of another member of the set at the same ending position.

Given any two members of the returned set, neither is a substring of the other.

source

pub fn union_prefixes(&mut self, expr: &Hir) -> bool

Unions the prefixes from the given expression to this set.

If prefixes could not be added (for example, this set would exceed its size limits or the set of prefixes from expr includes the empty string), then false is returned.

Note that prefix literals extracted from expr are said to be complete if and only if the literal extends from the beginning of expr to the end of expr.

source

pub fn union_suffixes(&mut self, expr: &Hir) -> bool

Unions the suffixes from the given expression to this set.

If suffixes could not be added (for example, this set would exceed its size limits or the set of suffixes from expr includes the empty string), then false is returned.

Note that prefix literals extracted from expr are said to be complete if and only if the literal extends from the end of expr to the beginning of expr.

source

pub fn union(&mut self, lits: Literals) -> bool

Unions this set with another set.

If the union would cause the set to exceed its limits, then the union is skipped and it returns false. Otherwise, if the union succeeds, it returns true.

source

pub fn cross_product(&mut self, lits: &Literals) -> bool

Extends this set with another set.

The set of literals is extended via a cross product.

If a cross product would cause this set to exceed its limits, then the cross product is skipped and it returns false. Otherwise, if the cross product succeeds, it returns true.

source

pub fn cross_add(&mut self, bytes: &[u8]) -> bool

Extends each literal in this set with the bytes given.

If the set is empty, then the given literal is added to the set.

If adding any number of bytes to all members of this set causes a limit to be exceeded, then no bytes are added and false is returned. If a prefix of bytes can be fit into this set, then it is used and all resulting literals are cut.

source

pub fn add(&mut self, lit: Literal) -> bool

Adds the given literal to this set.

Returns false if adding this literal would cause the class to be too big.

source

pub fn add_char_class(&mut self, cls: &ClassUnicode) -> bool

Extends each literal in this set with the character class given.

Returns false if the character class was too big to add.

source

pub fn add_byte_class(&mut self, cls: &ClassBytes) -> bool

Extends each literal in this set with the byte class given.

Returns false if the byte class was too big to add.

source

pub fn cut(&mut self)

Cuts every member of this set. When a member is cut, it can never be extended.

source

pub fn reverse(&mut self)

Reverses all members in place.

source

pub fn clear(&mut self)

Clears this set of all members.

Trait Implementations§

source§

impl Clone for Literals

source§

fn clone(&self) -> Literals

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
source§

impl Debug for Literals

source§

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

Formats the value using the given formatter. Read more
source§

impl PartialEq<Literals> for Literals

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Eq for Literals

source§

impl StructuralEq for Literals

source§

impl StructuralPartialEq for Literals

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere 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.

source§

impl<T> ToOwned for Twhere 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
source§

impl<T, U> TryFrom<U> for Twhere 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 Twhere 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.