3 releases

0.1.2 Feb 17, 2024
0.1.1 Sep 19, 2022
0.1.0 Sep 17, 2022

#264 in Procedural macros

Download history 135/week @ 2024-02-13 27/week @ 2024-02-20 25/week @ 2024-02-27 3/week @ 2024-03-19 72/week @ 2024-03-26 24/week @ 2024-04-02

99 downloads per month

MIT/Apache

14KB
339 lines

str-match

Match str with pattern like format!.

Usage

use str_match::str_match;

fn f(a: &str) -> &str{
    str_match! {
        match a {
            "abc{a}ghi" => a,
            "aaa{bb}" => bb,
            "{{{x}}}" => x,
            _ => "!",
        }
    }
}

assert_eq!(f("abcdefghi"), "def");
assert_eq!(f("aaabbbccc"), "bbbccc");
assert_eq!(f("{000}"), "000");
assert_eq!(f("xyz"), "!");

You can use "attribute" features in nightly.

// with `str-match.features = ["attribute"]` in Cargo.toml
#![feature(stmt_expr_attributes, proc_macro_hygiene)]
use str_match::str_match;

fn f(a: &str) -> &str{
    #[str_match]
    match a {
        "abc{x}ghi" => x,
        "aaa{x}" => x,
        "{{{x}}}" => x,
        _ => "!",
    }
}

Limitations

This macro converts &str to &[u8] and use match slice pattern. For example, "abc{x}ghi" pattern is converted to [b'a', b'b', b'c', x @ .., b'g', b'h', b'i' ]. Because two or more variadic patterns are not allowed in slice pattern, only zero or one placeholder in str pattern is also allowed.

This macro can use single &str matching, complex pattern (like (&str, &str)) is not supported.

Dependencies

~0.4–0.8MB
~20K SLoC