Trait bparse::BytePattern

source ·
pub trait BytePattern {
    // 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 then<P>(self, next: P) -> Then<Self, P>
       where Self: Sized { ... }
    fn repeats<R: Repetition>(self, count: R) -> Repeat<Self, R>
       where Self: Sized { ... }
    fn not(self) -> Not<Self>
       where Self: Sized { ... }
    fn optional(self) -> Optional<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::BytePattern;

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

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

fn then<P>(self, next: P) -> Then<Self, P>
where Self: Sized,

Expreses a sequence of patterns

Returns a new pattern that will match an input slice if self matches it, and other matches the rest.

Example
use bparse::BytePattern;

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

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

fn repeats<R: Repetition>(self, count: R) -> Repeat<Self, R>
where Self: Sized,

Expresses pattern repetition

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

See Repetition for more examples of types that implement it.

use bparse::BytePattern;

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

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

fn not(self) -> Not<Self>
where Self: Sized,

Expresses pattern negation

Returns a new pattern that will match only if self does not match

Example

Say you want to match a string of multiple ’a’s and ’b’s, except the string must not end in a ‘b’:

use bparse::BytePattern;
use bparse::pattern::end;

let input1 = b"aabaaaa";
let input2 = b"aaaab";

// a pattern of either a's or b's that do not occur at the input
let pattern = "a".or("b".then(end.not())).repeats(0..);

assert_eq!(b"aabaaaa", pattern.test(input1).unwrap().0);
assert_eq!(b"aaaa", pattern.test(input2).unwrap().0);

source

fn optional(self) -> Optional<Self>
where Self: Sized,

Expresses an optional pattern

Returns a new pattern that will always match the input regardless of the outcome of matching self

Example
use bparse::BytePattern;

let input1 = b"aab";
let input2 = b"aa";

let pattern = "aa".then("b".optional());

assert_eq!(b"aab", pattern.test(input1).unwrap().0);
assert_eq!(b"aa", pattern.test(input2).unwrap().0);

Implementations on Foreign Types§

source§

impl BytePattern for &str

source§

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

source§

impl BytePattern for RangeFrom<char>

source§

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

source§

impl BytePattern for RangeFrom<u8>

source§

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

source§

impl BytePattern for RangeInclusive<char>

source§

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

source§

impl BytePattern for RangeInclusive<u8>

source§

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

source§

impl BytePattern for RangeToInclusive<char>

source§

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

source§

impl BytePattern for RangeToInclusive<u8>

source§

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

Implementors§

source§

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

source§

impl<C1: BytePattern, C2: BytePattern> BytePattern for Then<C1, C2>

source§

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

source§

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

source§

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

source§

impl<P: BytePattern, R: Repetition> BytePattern for Repeat<P, R>