1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*!
This crate provides a library for compiling and matching simple text patterns.

# Example
Passed pattern `some*text` will be compiled into equivalent regexp `^some.*text$`.

# Syntax
* `*` - one or more any symbol.
* `any other text` - interpreted as simple text.

# Usage
```rust
use simple_text_pattern::Pattern;
let pattern = Pattern::new("some*text").expect("Unable to compile pattern");
assert_eq!(true, pattern.is_match("sometext"));
assert_eq!(true, pattern.is_match("some text"));
assert_eq!(false, pattern.is_match("not some text"));
```

*/
mod error;
mod parser;
mod token;
pub use self::error::Error;
use self::token::Token;
use self::parser::Parser;

#[derive(Debug, Clone)]
/// Structure represents pattern.
pub struct Pattern {
    tokens: Vec<Token>,
}

impl Pattern {
    /// Parses input pattern.
    pub fn new(input: &str) -> Result<Pattern, Error> {
        let parser = Parser::new();
        let result = parser.parse(input)?;
        return Ok(result);
    }

    /// Checks is input string matches current pattern.
    pub fn is_match(&self, input: &str) -> bool {
        let mut inputs = vec![input];
        let mut may_skip_chars = false;
        for token in self.tokens.iter() {
            match token {
                &Token::Text(ref text) => {
                    inputs = Self::handle_text(inputs, text, may_skip_chars);
                    may_skip_chars = false;
                },
                &Token::Any => {
                    may_skip_chars = true;
                },
            }
        }
        if may_skip_chars && !inputs.is_empty() {
            return true;
        }
        for input in inputs.iter() {
            if input.is_empty() {
                return true;
            }
        }
        return false;
    }

    fn handle_text<'a>(inputs: Vec<&'a str>, need: &str, may_skip_chars: bool) -> Vec<&'a str> {
        let mut result = Vec::with_capacity(inputs.len());
        for input in inputs {
            let end_index = input.len();
            for (found_index, _) in input.match_indices(need) {
                if !may_skip_chars && found_index != 0 {
                    continue;
                }
                let out_index = found_index + need.len();
                if out_index >= end_index {
                    result.push("");
                } else {
                    result.push(&input[out_index..end_index]);
                }
            }
        }
        result.sort();
        result.dedup_by(|a, b| {
            return a == b;
        });
        return result;
    }
}

#[cfg(test)]
mod tests {
    use crate::Pattern;

    #[test]
    fn test_empty_pattern() {
        assert_eq!(true, Pattern::new("").is_err());
    }

    #[test]
    fn test_text_pattern() {
        let pattern = Pattern::new("abcdef").expect("Unable to build text pattern");
        assert_eq!(false, pattern.is_match(""));
        assert_eq!(true, pattern.is_match("abcdef"));
        assert_eq!(false, pattern.is_match("1abcdef"));
        assert_eq!(false, pattern.is_match("abcdef1"));
        assert_eq!(false, pattern.is_match("abc"));
    }

    #[test]
    fn test_any_pattern() {
        let pattern = Pattern::new("*").expect("Unable to build any pattern");
        assert_eq!(true, pattern.is_match(""));
        assert_eq!(true, pattern.is_match("abcdef"));
        assert_eq!(true, pattern.is_match("1abcdef"));
        assert_eq!(true, pattern.is_match("abcdef1"));
        assert_eq!(true, pattern.is_match("abc"));
    }

    #[test]
    fn test_text_n_any_pattern() {
        let pattern = Pattern::new("abc*").expect("Unable to build any pattern with text pattern");
        assert_eq!(false, pattern.is_match(""));
        assert_eq!(true, pattern.is_match("abcdef"));
        assert_eq!(false, pattern.is_match("1abcdef"));
        assert_eq!(true, pattern.is_match("abcdef1"));
        assert_eq!(true, pattern.is_match("abc"));
        assert_eq!(false, pattern.is_match("q"));
        assert_eq!(false, pattern.is_match("qwe"));

        let pattern = Pattern::new("*abc").expect("Unable to build any pattern with text pattern");
        assert_eq!(false, pattern.is_match(""));
        assert_eq!(false, pattern.is_match("abcdef"));
        assert_eq!(false, pattern.is_match("1abcdef"));
        assert_eq!(false, pattern.is_match("abcdef1"));
        assert_eq!(true, pattern.is_match("abc"));
        assert_eq!(true, pattern.is_match("svsdfvsdfabc"));

        let pattern = Pattern::new("abc*def").expect("Unable to build any pattern with text pattern");
        assert_eq!(false, pattern.is_match(""));
        assert_eq!(true, pattern.is_match("abcdef"));
        assert_eq!(false, pattern.is_match("1abcdef"));
        assert_eq!(false, pattern.is_match("abcdef1"));
        assert_eq!(false, pattern.is_match("abc"));
        assert_eq!(true, pattern.is_match("abcabcdefdef"));
        assert_eq!(true, pattern.is_match("abc1def"));
        assert_eq!(true, pattern.is_match("abc1sdfvsdvdef"));
    }
}