https://github.com/manighazaee/strpatmatch
Simple string pattern matching
https://github.com/manighazaee/strpatmatch
library pattern-matching rust string
Last synced: about 1 year ago
JSON representation
Simple string pattern matching
- Host: GitHub
- URL: https://github.com/manighazaee/strpatmatch
- Owner: ManiGhazaee
- License: mit
- Created: 2024-11-29T15:25:54.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-11-29T16:43:21.000Z (over 1 year ago)
- Last Synced: 2025-04-14T01:55:10.619Z (about 1 year ago)
- Topics: library, pattern-matching, rust, string
- Language: Rust
- Homepage:
- Size: 5.86 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# String Pattern Matching
## Features
- Fast
- Simple
## Example
```rust
let x = "2a343bb8 c9";
assert_eq!(Some(("2", "343", "8", "9")), match_str!(x, {} "a" {} "bb" {} " c" {}));
assert_eq!(None, match_str!(x, {} "a" {} "d" {})); // x doesn't contain "d"
assert_eq!(Some("a343bb8 c"), match_str!(x, "2" {} "9"));
assert_eq!(None, match_str!(x, "a" {} "c")); // x doesn't start with "a" and end with "c"
```
---
```rust
let x = match_str!("foo bar baz fuzz", {} "bar" {} "fuzz");
assert_eq!(Some(("foo ", " baz ")), x);
```
macro expansion:
```rust
let x = {
let s = strpatmatch::first_match_start("foo bar baz fuzz", "bar");
if let Some(s) = s {
if let Some(m) = {
if (&"foo bar baz fuzz"[s + "bar".len()..]).ends_with("fuzz") {
Some((
&(&"foo bar baz fuzz"[s
+ "bar"
.len() - "fuzz".len())],
))
} else {
None
}
} {
Some(strpatmatch::tuples::concat((&"foo bar baz fuzz"[0..s],), m))
} else {
None
}
} else {
None
}
};
// assert_eq! ...
```