1 unstable release

0.1.0 Nov 29, 2024

#1407 in Rust patterns

Download history 98/week @ 2024-11-25 31/week @ 2024-12-02 8/week @ 2024-12-09

137 downloads per month

MIT license

9KB
160 lines

String Pattern Matching

Features

  • Fast
  • Simple

Example

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"

let x = match_str!("foo bar baz fuzz", {} "bar" {} "fuzz");
assert_eq!(Some(("foo ", " baz ")), x);

macro expansion:

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! ... 

Dependencies

~1.5MB
~38K SLoC