stringmatch

Crates.iostringmatch
lib.rsstringmatch
version0.4.0
sourcesrc
created_at2020-09-28 14:06:48.098873
updated_at2022-07-27 12:43:35.624182
descriptionAllow the use of regular expressions or strings wherever you need string comparison
homepagehttps://github.com/stevepryde/stringmatch
repositoryhttps://github.com/stevepryde/stringmatch
max_upload_size
id293678
size22,422
Steve Pryde (stevepryde)

documentation

https://docs.rs/stringmatch

README

Crates.io docs.rs

Allow the use of regular expressions or strings wherever you need string comparison.

Examples

Using Generics / Monomorphization

This pattern is faster but generates more code, slightly larger binary.

If you don't have a preference, go with this option as the code is usually more convenient to write.

fn accept_needle<N>(needle: N) -> bool
where
    N: Needle,
{
    needle.is_match("Test")
}

And now all of the following will work:

accept_needle("Test");
accept_needle(String::from("Test"));
accept_needle(Regex::new("Test").unwrap());
accept_needle(Regex::new(r"^T.+t$").unwrap());

For string comparisons you can also use StringMatch which allows you to be more explicit about the comparison:

accept_needle(StringMatch::from("test").case_insensitive());
accept_needle(StringMatch::from("tes").partial());

By default StringMatch matches the whole string and is case sensitive (safety by default).

And finally there is the StringMatchable trait that is implemented for String and &str:

accept_needle("test".match_case_insensitive());
accept_needle("tes".match_partial());

Using Dynamic Dispatch

This pattern is slightly slower but generates less code, slightly smaller binary.

fn accept_needle(needle: &dyn Needle) -> bool
{
   needle.is_match("Test")
}

And now all of the following will work:

accept_needle(&"Test");
accept_needle(&String::from("Test"));
accept_needle(&Regex::new("Test").unwrap());
accept_needle(&Regex::new(r"^T.+t$").unwrap());
accept_needle(&StringMatch::from("test").case_insensitive());
accept_needle(&StringMatch::from("tes").partial());

LICENSE

This work is licensed under MIT.

SPDX-License-Identifier: MIT

Commit count: 13

cargo fmt