Struct hyperscan::DatabaseRef
source · [−]pub struct DatabaseRef<T>(_, _);
Expand description
A borrowed reference to a Database
.
Implementations
sourceimpl<T> DatabaseRef<T>where
T: Mode + 'static,
impl<T> DatabaseRef<T>where
T: Mode + 'static,
sourceimpl<T> DatabaseRef<T>
impl<T> DatabaseRef<T>
sourceimpl<T> DatabaseRef<T>
impl<T> 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]);
sourceimpl DatabaseRef<Block>
impl DatabaseRef<Block>
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]);
sourceimpl DatabaseRef<Vectored>
impl DatabaseRef<Vectored>
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]);
sourceimpl DatabaseRef<Streaming>
impl DatabaseRef<Streaming>
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)]);
sourceimpl<T> DatabaseRef<T>
impl<T> DatabaseRef<T>
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.
sourceimpl DatabaseRef<Streaming>
impl DatabaseRef<Streaming>
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.
sourceimpl DatabaseRef<Streaming>
impl DatabaseRef<Streaming>
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)]);