Trait bparse::Pattern

source ·
pub trait Pattern {
    // Required method
    fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])>;

    // Provided methods
    fn or<P>(self, other: P) -> Or<Self, P>
       where Self: Sized { ... }
    fn and<P>(self, next: P) -> And<Self, P>
       where Self: Sized { ... }
    fn repeats<I: Into<Interval<usize>>>(self, count: I) -> Repeat<Self>
       where Self: Sized { ... }
}
Expand description

Expresses that the implementing type can be used to match a byte slice

Required Methods§

source

fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])>

Tests the pattern against the input slice. If the pattern matches, the matching part is returned along with what is left of the input. Returns None if the pattern does not match

Provided Methods§

source

fn or<P>(self, other: P) -> Or<Self, P>
where Self: Sized,

Expresses an alternate pattern.

Returns a new pattern that will match an input slice if either self or other match it.

§Example
use bparse::Pattern;

let input = b"b";
let pattern = "a".or("b");

assert_eq!(b"b", pattern.test(input).unwrap().0);
source

fn and<P>(self, next: P) -> And<Self, P>
where Self: Sized,

Expreses a sequence of patterns

Returns a new pattern that will test an input slice against self and other in sequence, feeding the remainder from the first match as the input to the second.

§Example
use bparse::Pattern;

let input = b"abc";
let pattern = "a".and("b").and("c");

assert_eq!(b"abc", pattern.test(input).unwrap().0);
source

fn repeats<I: Into<Interval<usize>>>(self, count: I) -> Repeat<Self>
where Self: Sized,

Expresses pattern repetition

Returns a new pattern that will match an input slice if self can match count times

Anything that can be safely converted to an Interval can be used as an argument .

use bparse::{Pattern};

let input = b"ababab";
let pattern = "a".and("b").repeats(1..=2);

assert_eq!(b"abab", pattern.test(input).unwrap().0);

Implementations on Foreign Types§

source§

impl Pattern for &str

source§

fn test<'i>(&self, input: &'i [u8]) -> Option<(&'i [u8], &'i [u8])>

Implementors§

source§

impl Pattern for ByteRange

source§

impl Pattern for NoneOf

source§

impl Pattern for OneOf

source§

impl<C1: Pattern, C2: Pattern> Pattern for And<C1, C2>

source§

impl<C1: Pattern, C2: Pattern> Pattern for Or<C1, C2>

source§

impl<F> Pattern for F
where F: Fn(&[u8]) -> Option<(&[u8], &[u8])>,

source§

impl<P> Pattern for Not<P>
where P: Pattern,

source§

impl<P> Pattern for Optional<P>
where P: Pattern,

source§

impl<P: Pattern> Pattern for Repeat<P>