Function bparse::oneof

source ·
pub const fn oneof(alternatives: &str) -> OneOf
Expand description

Returns a pattern that will match any one of the bytes in alternatives

This is a useful alternative to a long Pattern::or() chain when you have many single-byte alternatives.

§Example

use bparse::{Pattern, oneof};

let punctuation = oneof(".,\"'-?:!;");
assert_eq!(punctuation.test(b"!").unwrap().0, b"!");
assert_eq!(punctuation.test(b",").unwrap().0, b",");
assert_eq!(punctuation.test(b"a"), None);

This function is callable in const contexts. Additionally, the compile-time generated lookup table can be accessed from the OneOf struct

use bparse::oneof;
const lookup: [bool; 256] = oneof("abc").0;
assert!(lookup[b'a' as usize] == true);
assert!(lookup[b'b' as usize] == true);
assert!(lookup[b'c' as usize] == true);
assert!(lookup[b'd' as usize] == false);