#[repr(transparent)]pub struct Database<T>(_, _);
Expand description
A compiled pattern database that can then be used to scan data.
Implementations
sourceimpl<T: Mode> Database<T>
impl<T: Mode> Database<T>
sourcepub fn compile<S: Into<String>>(
expression: S,
flags: Flags,
platform: Option<&PlatformRef>
) -> Result<Database<T>, Error>
pub fn compile<S: Into<String>>(
expression: S,
flags: Flags,
platform: Option<&PlatformRef>
) -> Result<Database<T>, Error>
The basic regular expression compiler.
This is the function call with which an expression is compiled into a Hyperscan database which can be passed to the runtime functions.
sourcepub fn compile_literal<S: Into<String>>(
expression: S,
flags: LiteralFlags,
platform: Option<&PlatformRef>
) -> Result<Database<T>, Error>
pub fn compile_literal<S: Into<String>>(
expression: S,
flags: LiteralFlags,
platform: Option<&PlatformRef>
) -> Result<Database<T>, Error>
The pure literal expression compiler.
This is the function call with which an pure literal expression is compiled into a Hyperscan database which can be passed to the runtime functions.
Methods from Deref<Target = DatabaseRef<T>>
sourcepub fn serialize(&self) -> Result<Malloc<[u8]>>
pub fn serialize(&self) -> Result<Malloc<[u8]>>
Serialize a pattern database to a stream of bytes.
Examples
let pattern: Pattern = r"/foo(bar)+/i".parse().unwrap();
let db: BlockDatabase = pattern.left_most().build().unwrap();
let buf = db.serialize().unwrap();
assert!(buf.len() > 0);
assert!(buf.size().unwrap() > 0);
println!("database ({}) need {} bytes", buf.info().unwrap(), buf.size().unwrap());
let deserialized_db: BlockDatabase = buf.deserialize().unwrap();
let mut s = deserialized_db.alloc_scratch().unwrap();
let mut matches = Vec::new();
deserialized_db.scan("hello foobar!", &mut s, |id, from, to, _flags| {
matches.push(from..to);
Matching::Continue
}).unwrap();
assert_eq!(matches, vec![6..12]);
sourcepub fn scan<T, F>(
&self,
data: T,
scratch: &ScratchRef,
on_match_event: F
) -> Result<()>where
T: AsRef<[u8]>,
F: MatchEventHandler,
pub fn scan<T, F>(
&self,
data: T,
scratch: &ScratchRef,
on_match_event: F
) -> Result<()>where
T: AsRef<[u8]>,
F: MatchEventHandler,
The block (non-streaming) regular expression scanner.
This is the function call in which the actual pattern matching takes place for block-mode pattern databases.
Examples
let db: BlockDatabase = pattern! {"test"; CASELESS | SOM_LEFTMOST}.build().unwrap();
let s = db.alloc_scratch().unwrap();
let mut matches = vec![];
db.scan("foo test bar", &s, |_, from, to, _| {
matches.push(from..to);
Matching::Continue
}).unwrap();
assert_eq!(matches, vec![4..8]);
sourcepub fn scan<I, T, F>(
&self,
data: I,
scratch: &ScratchRef,
on_match_event: F
) -> Result<()>where
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
F: MatchEventHandler,
pub fn scan<I, T, F>(
&self,
data: I,
scratch: &ScratchRef,
on_match_event: F
) -> Result<()>where
I: IntoIterator<Item = T>,
T: AsRef<[u8]>,
F: MatchEventHandler,
The vectored regular expression scanner.
This is the function call in which the actual pattern matching takes place for vectoring-mode pattern databases.
Examples
let db: VectoredDatabase = pattern!{"test"; CASELESS|SOM_LEFTMOST}.build().unwrap();
let s = db.alloc_scratch().unwrap();
let mut matches = vec![];
db.scan(vec!["foo", "test", "bar"], &s, |id, from, to, _| {
matches.push(from..to);
Matching::Continue
}).unwrap();
assert_eq!(matches, vec![3..7]);
sourcepub fn scan<R, F>(
&self,
reader: &mut R,
scratch: &ScratchRef,
on_match_event: F
) -> Result<()>where
R: Read,
F: MatchEventHandler,
pub fn scan<R, F>(
&self,
reader: &mut R,
scratch: &ScratchRef,
on_match_event: F
) -> Result<()>where
R: Read,
F: MatchEventHandler,
Pattern matching takes place for stream-mode pattern databases.
Examples
const SCAN_BUF_SIZE: usize = 4096;
let mut buf = String::from_utf8(vec![b'x'; SCAN_BUF_SIZE - 2]).unwrap();
buf.push_str("baaab");
let db: StreamingDatabase = pattern! { "a+"; SOM_LEFTMOST }.build().unwrap();
let s = db.alloc_scratch().unwrap();
let mut cur = Cursor::new(buf.as_bytes());
let mut matches = vec![];
db.scan(&mut cur, &s, |_, from, to, _| {
matches.push((from, to));
Matching::Continue
})
.unwrap();
assert_eq!(matches, vec![(4095, 4096), (4095, 4097), (4095, 4098)]);
sourcepub fn alloc_scratch(&self) -> Result<Scratch>
pub fn alloc_scratch(&self) -> Result<Scratch>
Allocate a “scratch” space for use by Hyperscan.
sourcepub fn realloc_scratch<'a>(
&'a self,
s: &'a mut Scratch
) -> Result<&'a mut Scratch>
pub fn realloc_scratch<'a>(
&'a self,
s: &'a mut Scratch
) -> Result<&'a mut Scratch>
Reallocate a “scratch” space for use by Hyperscan.
sourcepub fn stream_size(&self) -> Result<usize>
pub fn stream_size(&self) -> Result<usize>
Provides the size of the stream state allocated by a single stream opened against the given database.
sourcepub fn open_stream(&self) -> Result<Stream>
pub fn open_stream(&self) -> Result<Stream>
Open and initialise a stream.
sourcepub fn expand_stream(&self, buf: &[u8]) -> Result<Stream>
pub fn expand_stream(&self, buf: &[u8]) -> Result<Stream>
Decompresses a compressed representation created by StreamRef::compress()
into a new stream.
Note: buf
must correspond to a complete compressed representation created by StreamRef::compress()
of a stream
that was opened against db
. It is not always possible to detect misuse of this API and behaviour is undefined
if these properties are not satisfied.
Examples
let db: StreamingDatabase = pattern! {"test"; SOM_LEFTMOST}.build().unwrap();
let s = db.alloc_scratch().unwrap();
let st = db.open_stream().unwrap();
let mut matches = vec![];
let mut callback = |_, from, to, _| {
matches.push((from, to));
Matching::Continue
};
st.scan("foo t", &s, &mut callback).unwrap();
st.scan("es", &s, &mut callback).unwrap();
let mut buf = [0; 8192];
let len = st.compress(&mut buf).unwrap();
st.close(&s, Matching::Terminate).unwrap();
let st2 = db.expand_stream(&buf[..len]).unwrap();
st2.scan("t bar", &s, &mut callback).unwrap();
st2.close(&s, &mut callback).unwrap();
assert_eq!(matches, vec![(4, 8)]);