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§
Provided Methods§
sourcefn or<P>(self, other: P) -> Or<Self, P>where
Self: Sized,
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);sourcefn then<P>(self, next: P) -> Then<Self, P>where
Self: Sized,
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);sourcefn repeats<R: Repetition>(self, count: R) -> Repeat<Self, R>where
Self: Sized,
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);sourcefn not(self) -> Not<Self>where
Self: Sized,
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);
sourcefn optional(self) -> Optional<Self>where
Self: Sized,
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);