sima-rs 0.1.0

A blazingly fast simple string matcher library.
Documentation
#[cfg(test)]
mod tests {
    use crate::{simple_match, simple_match_limit};

    struct TestCase<'a> {
        pattern: &'a str,
        text: &'a str,
        matched: bool,
    }

    #[test]
    fn test_wildcard_match() {
        let test_cases = [
            TestCase {
                pattern: "my-folder/oo*",
                text: "my-folder/oo",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/In*",
                text: "my-folder/India/Karnataka/",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/In*",
                text: "my-folder/Karnataka/India/",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/In*/Ka*/Ban",
                text: "my-folder/India/Karnataka/Ban",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/In*/Ka*/Ban",
                text: "my-folder/India/Karnataka/Ban/Ban/Ban/Ban/Ban",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/In*/Ka*/Ban",
                text: "my-folder/India/Karnataka/Area1/Area2/Area3/Ban",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/In*/Ka*/Ban",
                text: "my-folder/India/State1/State2/Karnataka/Area1/Area2/Area3/Ban",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/In*/Ka*/Ban",
                text: "my-folder/India/Karnataka/Bangalore",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/In*/Ka*/Ban*",
                text: "my-folder/India/Karnataka/Bangalore",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/*",
                text: "my-folder/India",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/oo*",
                text: "my-folder/odo",
                matched: false,
            },
            TestCase {
                pattern: "my-folder?/abc*",
                text: "myfolder/abc",
                matched: false,
            },
            TestCase {
                pattern: "my-folder?/abc*",
                text: "my-folder1/abc",
                matched: true,
            },
            TestCase {
                pattern: "my-?-folder/abc*",
                text: "my--folder/abc",
                matched: false,
            },
            TestCase {
                pattern: "my-?-folder/abc*",
                text: "my-1-folder/abc",
                matched: true,
            },
            TestCase {
                pattern: "my-?-folder/abc*",
                text: "my-k-folder/abc",
                matched: true,
            },
            TestCase {
                pattern: "my??folder/abc*",
                text: "myfolder/abc",
                matched: false,
            },
            TestCase {
                pattern: "my??folder/abc*",
                text: "my4afolder/abc",
                matched: true,
            },
            TestCase {
                pattern: "my-folder?abc*",
                text: "my-folder/abc",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/abc?efg",
                text: "my-folder/abcdefg",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/abc?efg",
                text: "my-folder/abc/efg",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/abc????",
                text: "my-folder/abc",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/abc????",
                text: "my-folder/abcde",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/abc????",
                text: "my-folder/abcdefg",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/abc?",
                text: "my-folder/abc",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/abc?",
                text: "my-folder/abcd",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/abc?",
                text: "my-folder/abcde",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?",
                text: "my-folder/mnop",
                matched: false,
            },
            // This is a special case => In my opinion this should never match
            TestCase {
                pattern: "my-folder/mnop*?",
                text: "my-folder/mnopqrst/mnopqr",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?",
                text: "my-folder/mnopqrst/mnopqrs",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?",
                text: "my-folder/mnop",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?",
                text: "my-folder/mnopq",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?",
                text: "my-folder/mnopqr",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?and",
                text: "my-folder/mnopqand",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?and",
                text: "my-folder/mnopand",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?and",
                text: "my-folder/mnopqand",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*qrst",
                text: "my-folder/mnopabcdegqrst",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/mnop*?and",
                text: "my-folder/mnopqand",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?and",
                text: "my-folder/mnopand",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*and?",
                text: "my-folder/mnopqanda",
                matched: true,
            },
            TestCase {
                pattern: "my-folder/mnop*?and?",
                text: "my-folder/mnopqanda",
                matched: false,
            },
            TestCase {
                pattern: "my-folder/mnop*?and",
                text: "my-folder/mnopqanda",
                matched: false,
            },
            TestCase {
                pattern: "my-?-folder/abc*",
                text: "my-folder/mnopqanda",
                matched: false,
            },
        ];

        for (i, test_case) in test_cases.iter().enumerate() {
            let actual_result = simple_match(test_case.text, test_case.pattern);
            assert_eq!(
                test_case.matched,
                actual_result,
                "Test {}: Expected the result to be `{}`, but instead found it to be `{}`\n\tFor pattern `{}` matching text `{}`",
                i + 1,
                test_case.matched,
                actual_result,
                test_case.text, test_case.pattern,
            );
        }
    }

    #[test]
    fn test_limit() {
        match simple_match_limit("this/is/my/super/nice/string", "this/*/m*/nice/*", 1) {
            Err(_) => {}
            _ => panic!("an error must be returned"),
        }
    }
}