regex_automata::util::look

Struct LookMatcher

Source
pub struct LookMatcher { /* private fields */ }
Expand description

A matcher for look-around assertions.

This matcher permits configuring aspects of how look-around assertions are matched.

§Example

A LookMatcher can change the line terminator used for matching multi-line anchors such as (?m:^) and (?m:$).

use regex_automata::{
    nfa::thompson::{self, pikevm::PikeVM},
    util::look::LookMatcher,
    Match, Input,
};

let mut lookm = LookMatcher::new();
lookm.set_line_terminator(b'\x00');

let re = PikeVM::builder()
    .thompson(thompson::Config::new().look_matcher(lookm))
    .build(r"(?m)^[a-z]+$")?;
let mut cache = re.create_cache();

// Multi-line assertions now use NUL as a terminator.
assert_eq!(
    Some(Match::must(0, 1..4)),
    re.find(&mut cache, b"\x00abc\x00"),
);
// ... and \n is no longer recognized as a terminator.
assert_eq!(
    None,
    re.find(&mut cache, b"\nabc\n"),
);

Implementations§

Source§

impl LookMatcher

Source

pub fn new() -> LookMatcher

Creates a new default matcher for look-around assertions.

Source

pub fn set_line_terminator(&mut self, byte: u8) -> &mut LookMatcher

Sets the line terminator for use with (?m:^) and (?m:$).

Namely, instead of ^ matching after \n and $ matching immediately before a \n, this will cause it to match after and before the byte given.

It can occasionally be useful to use this to configure the line terminator to the NUL byte when searching binary data.

Note that this does not apply to CRLF-aware line anchors such as (?Rm:^) and (?Rm:$). CRLF-aware line anchors are hard-coded to use \r and \n.

Source

pub fn get_line_terminator(&self) -> u8

Returns the line terminator that was configured for this matcher.

If no line terminator was configured, then this returns \n.

Note that the line terminator should only be used for matching (?m:^) and (?m:$) assertions. It specifically should not be used for matching the CRLF aware assertions (?Rm:^) and (?Rm:$).

Source

pub fn matches(&self, look: Look, haystack: &[u8], at: usize) -> bool

Returns true when the position at in haystack satisfies the given look-around assertion.

§Panics

This panics when testing any Unicode word boundary assertion in this set and when the Unicode word data is not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Since it’s generally expected that this routine is called inside of a matching engine, callers should check the error condition when building the matching engine. If there is a Unicode word boundary in the matcher and the data isn’t available, then the matcher should fail to build.

Callers can check the error condition with LookSet::available.

This also may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

pub fn matches_set(&self, set: LookSet, haystack: &[u8], at: usize) -> bool

Returns true when all of the assertions in the given set match at the given position in the haystack.

§Panics

This panics when testing any Unicode word boundary assertion in this set and when the Unicode word data is not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Since it’s generally expected that this routine is called inside of a matching engine, callers should check the error condition when building the matching engine. If there is a Unicode word boundary in the matcher and the data isn’t available, then the matcher should fail to build.

Callers can check the error condition with LookSet::available.

This also may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

pub fn is_start(&self, _haystack: &[u8], at: usize) -> bool

Returns true when Look::Start is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::End is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::StartLF is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::EndLF is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::StartCRLF is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::EndCRLF is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::WordAscii is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::WordAsciiNegate is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

pub fn is_word_unicode( &self, haystack: &[u8], at: usize, ) -> Result<bool, UnicodeWordBoundaryError>

Returns true when Look::WordUnicode is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

§Errors

This returns an error when Unicode word boundary tables are not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Source

pub fn is_word_unicode_negate( &self, haystack: &[u8], at: usize, ) -> Result<bool, UnicodeWordBoundaryError>

Returns true when Look::WordUnicodeNegate is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

§Errors

This returns an error when Unicode word boundary tables are not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Source

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

Returns true when Look::WordStartAscii is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::WordEndAscii is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

pub fn is_word_start_unicode( &self, haystack: &[u8], at: usize, ) -> Result<bool, UnicodeWordBoundaryError>

Returns true when Look::WordStartUnicode is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

§Errors

This returns an error when Unicode word boundary tables are not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Source

pub fn is_word_end_unicode( &self, haystack: &[u8], at: usize, ) -> Result<bool, UnicodeWordBoundaryError>

Returns true when Look::WordEndUnicode is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

§Errors

This returns an error when Unicode word boundary tables are not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Source

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

Returns true when Look::WordStartHalfAscii is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

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

Returns true when Look::WordEndHalfAscii is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

Source

pub fn is_word_start_half_unicode( &self, haystack: &[u8], at: usize, ) -> Result<bool, UnicodeWordBoundaryError>

Returns true when Look::WordStartHalfUnicode is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

§Errors

This returns an error when Unicode word boundary tables are not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Source

pub fn is_word_end_half_unicode( &self, haystack: &[u8], at: usize, ) -> Result<bool, UnicodeWordBoundaryError>

Returns true when Look::WordEndHalfUnicode is satisfied at the given position in haystack.

§Panics

This may panic when at > haystack.len(). Note that at == haystack.len() is legal and guaranteed not to panic.

§Errors

This returns an error when Unicode word boundary tables are not available. Specifically, this only occurs when the unicode-word-boundary feature is not enabled.

Trait Implementations§

Source§

impl Clone for LookMatcher

Source§

fn clone(&self) -> LookMatcher

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 LookMatcher

Source§

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

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

impl Default for LookMatcher

Source§

fn default() -> LookMatcher

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§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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 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.

Source§

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

Source§

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

Source§

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>,

Source§

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.

Layout§

Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.

Size: 1 byte