#string #regex #comparison #string-pattern #match #generate

stringmatch

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

6 releases (3 breaking)

0.4.0 Jul 27, 2022
0.3.3 Dec 6, 2021
0.3.2 Jul 10, 2021
0.3.0 Jan 10, 2021
0.1.0 Sep 28, 2020

#569 in Text processing

Download history 3584/week @ 2023-12-15 2226/week @ 2023-12-22 2731/week @ 2023-12-29 6215/week @ 2024-01-05 4415/week @ 2024-01-12 4744/week @ 2024-01-19 3763/week @ 2024-01-26 3989/week @ 2024-02-02 4952/week @ 2024-02-09 5383/week @ 2024-02-16 4892/week @ 2024-02-23 6335/week @ 2024-03-01 4634/week @ 2024-03-08 4220/week @ 2024-03-15 4159/week @ 2024-03-22 3678/week @ 2024-03-29

17,461 downloads per month
Used in 25 crates (3 directly)

MIT license

19KB
302 lines

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

Dependencies

~2.1–3.5MB
~57K SLoC