https://github.com/dalance/bitpattern
bitwise pattern matching and extracting
https://github.com/dalance/bitpattern
Last synced: 6 months ago
JSON representation
bitwise pattern matching and extracting
- Host: GitHub
- URL: https://github.com/dalance/bitpattern
- Owner: dalance
- License: apache-2.0
- Created: 2020-07-16T16:52:55.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2024-04-03T11:46:02.000Z (about 2 years ago)
- Last Synced: 2024-06-19T00:36:04.032Z (about 2 years ago)
- Language: Rust
- Size: 17.6 KB
- Stars: 5
- Watchers: 2
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# bitpattern
bitwise pattern matching and extracting
[](https://github.com/dalance/bitpattern/actions)
[](https://crates.io/crates/bitpattern)
[](https://docs.rs/bitpattern)

## Usage
```Cargo.toml
[dependencies]
bitpattern = "0.1.0"
```
## Example
```rust
let x = 0xacu8; // 10101100
// '0' means the bit must be 0.
// '1' means the bit must be 1.
// '_' can be uses as separator.
assert_eq!(bitpattern!("1010_1100", x), Some(()));
assert_eq!(bitpattern!("1010_0100", x), None);
// '?' means the bit can be 0 or 1.
assert_eq!(bitpattern!("1?10_1?00", x), Some(()));
// Other charactors can be used for extracting.
// 'a' extracts a single bit.
assert_eq!(bitpattern!("1a10_1100", x), Some(0));
assert_eq!(bitpattern!("10a0_1100", x), Some(1));
// Multi-bit extracting by continuous charactors.
assert_eq!(bitpattern!("1aaa_a100", x), Some(5));
// Multiple extracting.
assert_eq!(bitpattern!("1aa0_aa00", x), Some((1, 3)));
// If the extracting fields are adjacent, the different charactors can be used.
assert_eq!(bitpattern!("1aab_bccc", x), Some((1, 1, 4)));
```
## License
Licensed under either of
* Apache License, Version 2.0, ([LICENSE-APACHE](LICENSE-APACHE) or http://www.apache.org/licenses/LICENSE-2.0)
* MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT)
at your option.
### Contribution
Unless you explicitly state otherwise, any contribution intentionally
submitted for inclusion in the work by you, as defined in the Apache-2.0
license, shall be dual licensed as above, without any additional terms or
conditions.