Skip to main content

is_matching/
is_matching.rs

1use elyze::matcher::Match;
2use elyze::scanner::Scanner;
3
4// define a structure to implement the `Match` trait
5struct Hello;
6
7// implement the `Match` trait
8impl Match<u8> for Hello {
9    fn is_matching(&self, data: &[u8]) -> (bool, usize) {
10        // define the pattern to match
11        let pattern = b"hello";
12        // check if the subslice of data matches the pattern
13        (&data[..pattern.len()] == pattern, pattern.len())
14    }
15
16    fn size(&self) -> usize {
17        5
18    }
19}
20
21fn main() {
22    let mut scanner = Scanner::new(b"hello world");
23    let (found, size) = Hello.is_matching(scanner.remaining());
24    if !found {
25        println!("not found");
26        return;
27    }
28    let data = &scanner.remaining()[..size];
29    scanner.bump_by(size);
30    println!("found: {:?}", String::from_utf8_lossy(data));
31}