Struct hyperscan::expression::LiteralSet
source · pub struct LiteralSet<'a> { /* private fields */ }compiler only.Expand description
Collection of literals.
This is the analogue to ExpressionSet for Literal expressions, which
cannot be combined with Expression patterns in the same database.
This struct provides an immutable (returning Self) builder interface
to attach additional configuration to the initial set of patterns
constructed with Self::from_lits().
Implementations§
source§impl<'a> LiteralSet<'a>
impl<'a> LiteralSet<'a>
sourcepub fn from_lits(lits: impl IntoIterator<Item = &'a Literal>) -> Self
pub fn from_lits(lits: impl IntoIterator<Item = &'a Literal>) -> Self
Construct a pattern set from references to parsed literals.
The length of this initial exprs argument is returned by
Self::len(), and all subsequent configuration methods are checked to
provide iterators of the same length:
use hyperscan::expression::*;
let a: Literal = "a\0b".parse().unwrap();
// Fails due to argument length mismatch:
LiteralSet::from_lits([&a])
.with_flags([]);sourcepub fn with_flags(self, flags: impl IntoIterator<Item = Flags>) -> Self
pub fn with_flags(self, flags: impl IntoIterator<Item = Flags>) -> Self
Provide flags which modify the behavior of each expression.
The length of flags is checked to be the same as Self::len().
If this builder method is not used, Flags::default() will be assigned
to all patterns.
use hyperscan::{expression::*, flags::*, matchers::*};
// Create two expressions to demonstrate separate flags for each pattern:
let a: Literal = "a".parse()?;
let b: Literal = "b".parse()?;
// Get the start of match for one pattern, but not the other:
let db = LiteralSet::from_lits([&a, &b])
.with_flags([Flags::default(), Flags::SOM_LEFTMOST])
.compile(Mode::BLOCK)?;
let mut scratch = db.allocate_scratch()?;
let mut matches: Vec<&str> = Vec::new();
scratch.scan_sync(&db, "aardvark imbibbe".into(), |m| {
matches.push(unsafe { m.source.as_str() });
MatchResult::Continue
})?;
// Start of match is preserved for only one pattern:
assert_eq!(&matches, &["a", "aa", "aardva", "b", "b", "b"]);sourcepub fn with_ids(self, ids: impl IntoIterator<Item = ExprId>) -> Self
pub fn with_ids(self, ids: impl IntoIterator<Item = ExprId>) -> Self
Assign an ID number to each pattern.
The length of ids is checked to be the same as Self::len(). Multiple
patterns can be assigned the same ID.
If this builder method is not used, hyperscan will assign them all the ID number 0:
use hyperscan::{expression::*, flags::*, state::*, matchers::*};
// Create two expressions to demonstrate multiple pattern IDs.
let a: Literal = "a".parse()?;
let b: Literal = "b".parse()?;
// Create one db with ID numbers, and one without.
let set1 = LiteralSet::from_lits([&a, &b]).compile(Mode::BLOCK)?;
let set2 = LiteralSet::from_lits([&a, &b])
.with_ids([ExprId(300), ExprId(12)])
.compile(Mode::BLOCK)?;
let mut scratch = Scratch::new();
scratch.setup_for_db(&set1)?;
scratch.setup_for_db(&set2)?;
let msg: ByteSlice = "aardvark imbibbe".into();
// The first db doesn't differentiate matches by ID number:
let mut matches1: Vec<ExpressionIndex> = Vec::new();
scratch.scan_sync(&set1, msg, |m| {
matches1.push(m.id);
MatchResult::Continue
})?;
assert_eq!(
&matches1,
&[
ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0), ExpressionIndex(0),
ExpressionIndex(0), ExpressionIndex(0),
],
);
// The second db returns corresponding ExpressionIndex instances:
let mut matches2: Vec<ExpressionIndex> = Vec::new();
scratch.scan_sync(&set2, msg, |m| {
matches2.push(m.id);
MatchResult::Continue
})?;
assert_eq!(
&matches2,
&[
ExpressionIndex(300), ExpressionIndex(300), ExpressionIndex(300),
ExpressionIndex(12), ExpressionIndex(12), ExpressionIndex(12),
],
);sourcepub fn compile(self, mode: Mode) -> Result<Database, HyperscanCompileError>
pub fn compile(self, mode: Mode) -> Result<Database, HyperscanCompileError>
Call Database::compile_multi_literal() with None for the platform.
Trait Implementations§
source§impl<'a> Clone for LiteralSet<'a>
impl<'a> Clone for LiteralSet<'a>
source§fn clone(&self) -> LiteralSet<'a>
fn clone(&self) -> LiteralSet<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more