#pattern #bit-manipulation #bits #macro

无std bitpatterns

整数位模式测试的简单实现

2个版本

0.1.1 2021年11月30日
0.1.0 2021年11月30日

#240 in 无标准库

MIT/Apache

25KB
234

bitpatterns


Crates.io docs.rs Crates.io Crates.io

整数位模式测试的简单实现。

提供功能,将整数与包含通配符的位模式进行比较。模式可以用方便的宏声明,生成的代码非常小且高效,no_std,并且该crate只依赖于其bitpatterns-proc-macro,而它本身没有任何依赖。

位模式存储在BitPattern类型中,该类型提供is_match方法来测试匹配。创建这些模式的最简单方法是使用bitpattern!宏,或者如果模式只使用一次,可以使用is_bit_match!宏。

示例

use bitpatterns::*;

// BitPatterns can be stored as normal variables
let pattern_1 = bitpattern!("0b1_0..1");
// Or even consts
const PATTERN_2: BitPattern<u8> = bitpattern!("00..1");

// Now, we can test these patterns against numbers
assert!(pattern_1.is_match(19)); // 19 == 0b10011
assert!(!PATTERN_2.is_match(19));

assert!(!pattern_1.is_match(5)); // 5 == 0b00101
assert!(PATTERN_2.is_match(5));

// Any digits which are not part of the pattern string are automatically
// treated as wildcards
assert!(PATTERN_2.is_match(35)); // 35 == 0b100011

// The is_bit_match macro can be used without declaring a pattern variable
assert!(is_bit_match!("101..", 20)); // 20 == 0b10100

// Using an `as` conversion to compare a number to a pattern of a different
// size/signedness always works, keeping in mind that bits not specified in
// the pattern are considered wildcards
let pattern_3 = bitpattern!("1..01", u8);
let x: i16 = 0b0100_0010_1001_1001;
assert!(pattern_3.is_match(x as u8));

许可证

许可协议为以下之一

任选其一。

贡献

除非你明确表示,否则,根据Apache-2.0许可证定义,你有意提交的任何贡献,均应按上述方式双重许可,不得附加任何额外条款或条件。

依赖项