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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
//! A configurable pattern matching (also known as globbing) library.
//!
//! The general use of this library is to compile `Pattern`s, then use them. For example:
//!
//! ```rust
//! use patmatch::{Pattern, MatchOptions};
//! let pat = Pattern::compile("*.png", MatchOptions::ALL);
//! assert!(pat.matches("file.png"));
//! assert!(!pat.matches("file.jpeg"));
//! ```

mod compiled;
pub mod options;
pub use options::*;

use dyn_clone::DynClone;
use std::{
    fmt,
    iter::FromIterator,
    iter::{FusedIterator, Peekable},
};

/// A pattern to match strings against.
#[derive(Debug, Clone)]
pub struct Pattern {
    pat: Box<dyn CompiledPat>,
}

impl fmt::Display for Pattern {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.pat.fmt(f)
    }
}

/// A trait for compiled patterns.
trait CompiledPat: fmt::Debug + DynClone {
    /// See [`Pattern::matches`], which calls this.
    fn matches(&self, string: &str) -> bool;
}

dyn_clone::clone_trait_object!(CompiledPat);

/// A part of a [`Pattern`].
/// Used to configure a compiled Pattern.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Chunk {
    /// A literal string. This matches the string exactly.
    Str(String),
    /// A wildcard (usually represented by an asterisk).
    /// Matches any amount of characters, including none.
    Wildcard,
    /// An unknown character (usually represented by a question mark).
    UnknownChar,
}

impl Chunk {
    /// Turns `chr` into any of the non-[`Chunk::Str`] variants (e.g. [`Chunk::Wildcard`], etc.)
    /// Only really used for ease of implementation of [`Pattern::compile`].
    fn from_char(chr: char) -> Option<Chunk> {
        use Chunk::*;
        match chr {
            '*' => Some(Wildcard),
            '?' => Some(UnknownChar),
            _ => None,
        }
    }
    pub fn str(s: &str) -> Chunk {
        Chunk::Str(s.to_owned())
    }
    fn take_str(self) -> Option<String> {
        match self {
            Chunk::Str(s) => Some(s),
            _ => None,
        }
    }
}

impl From<String> for Chunk {
    fn from(s: String) -> Self {
        Chunk::Str(s)
    }
}

impl FromIterator<Chunk> for Pattern {
    /// Used to compile a `Pattern` from an iterator of [`Chunk`]s.
    /// Note that this allocates memory to store *all* the `Chunk`s.
    ///
    /// Example usage:
    /// ```rust
    /// use patmatch::{Pattern, Chunk};
    /// let chunk_vec = vec![Chunk::str("IMG_"), Chunk::Wildcard, Chunk::str(".png")];
    /// let pat: Pattern = chunk_vec.into_iter().collect();
    /// assert!(pat.matches("IMG_20170301.png"));
    /// assert!(!pat.matches("stuff.png")); assert!(!pat.matches("IMG_20170302.jpeg"));
    /// ```
    fn from_iter<T: IntoIterator<Item = Chunk>>(iter: T) -> Self {
        use compiled::*;
        dbg!("HI!");
        let mut chunks = Vec::new();
        for chunk in iter {
            if !(chunk == Chunk::Wildcard && chunks.ends_with(&[Chunk::Wildcard])) {
                chunks.push(chunk);
            }
        }
        let chunks = chunks;
        dbg!(&chunks);
        if chunks.iter().all(|chunk| chunk == &Chunk::UnknownChar) {
            // (Also handles the empty string.)
            Pattern::from_compiled(OptionalCharLen { len: chunks.len() })
        } else if chunks.iter().all(|chunk| chunk == &Chunk::Wildcard) {
            Pattern::from_compiled(MatchAny {})
        } else if chunks.iter().all(|chunk| matches!(chunk, Chunk::Str(_))) {
            Pattern::from_compiled(LiteralStr(
                chunks.into_iter().map(|x| x.take_str().unwrap()).collect(),
            ))
        } else {
            let mut states = Vec::new();
            for chunk in chunks {
                match chunk {
                    Chunk::Wildcard => states.push(State::Wildcard),
                    Chunk::UnknownChar => states.push(State::UnknownChar),
                    Chunk::Str(string) => {
                        for chr in string.chars() {
                            states.push(State::Char(chr));
                        }
                    }
                }
            }
            Pattern::from_compiled(General { states })
        }
    }
}

/// An iterator for yielding `Chunk`s from an iterator.
struct CompileIter<T: Iterator<Item = char>> {
    iter: Peekable<T>,
    opts: MatchOptions,
}
impl<T: Iterator<Item = char>> Iterator for CompileIter<T> {
    type Item = Chunk;
    fn next(&mut self) -> Option<Self::Item> {
        use Chunk::*;
        match self.iter.next() {
            None => None,
            Some(chr) => Some(match chr {
                c if self.opts.contains(c.into()) => Chunk::from_char(c).unwrap(),
                _ => {
                    let mut string = String::new();
                    string.push(chr);
                    loop {
                        dbg!("ok");
                        match self.iter.peek() {
                            Some(peeked) if !self.opts.contains(MatchOptions::from(*peeked)) => {
                                string.push(*peeked);
                                self.iter.next();
                            }
                            _ => break,
                        }
                    }
                    Str(string)
                }
            }),
        }
    }
}

impl<T: Iterator<Item = char>> FusedIterator for CompileIter<T> {}

impl Pattern {
    /// Compiles a pattern from a string, using shell-like syntax.
    /// If you want to compile your own custom string format, see [`Pattern::from_iter`].
    ///
    /// Each of these can be toggled using [`MatchOptions`].
    /// * All characters prefixed with a backslash (`\`) are interpreted literally.
    /// * ([`MatchOptions::WILDCARDS`]) Asterisks (`*`s) are interpreted as wildcards: e.g. `a*b` is interpreted as `a`, a
    /// wildcard then `b`.
    /// * ([`MatchOptions::UNKNOWN_CHARS`]) Question marks (`?`s) are interpreted as optional characters.
    pub fn compile(pat: &str, opts: MatchOptions) -> Pattern {
        // Yield all chunks and collect them into a `Pattern`.
        CompileIter {
            iter: pat.chars().peekable(),
            opts,
        }
        .collect()
    }

    /// The same as [`Pattern::compile`], but with an iterator instead of a `&str`.
    pub fn compile_iter<T: IntoIterator<Item = char>>(pat: T, opts: MatchOptions) -> Pattern {
        CompileIter {
            iter: pat.into_iter().peekable(),
            opts,
        }
        .collect()
    }

    /// Checks if `string` matches the pattern.
    /// The pattern is checked for a match "perfectly",
    /// i.e. if it is possible to match by choosing all of the matches optimally,
    /// it will do so.
    /// This optimizes matching checks if not all features are used.
    pub fn matches(&self, string: &str) -> bool {
        self.pat.matches(string)
    }

    fn from_compiled<T: CompiledPat + 'static>(pat: T) -> Pattern {
        Pattern { pat: Box::new(pat) }
    }
}

#[cfg(test)]
mod tests {
    use super::{Chunk, MatchOptions, Pattern};
    use Chunk::*;

    /// Checks the match status of all patterns in `patterns` against all strings in `strings`.
    fn check_match(patterns: Vec<Pattern>, strings: Vec<&str>, expected: bool) {
        for pat in patterns {
            for string in strings.iter() {
                assert_eq!(
                    pat.matches(string),
                    expected,
                    "Pattern {} failed to match against {}",
                    pat,
                    string
                );
            }
        }
    }

    fn matches(patterns: Vec<Pattern>, strings: Vec<&str>) {
        check_match(patterns, strings, true)
    }

    fn matches_v(patterns: Vec<Vec<Chunk>>, strings: Vec<&str>) {
        matches(
            patterns
                .into_iter()
                .map(|v| v.into_iter().collect())
                .collect(),
            strings,
        )
    }

    fn strings_to_pats(patterns: Vec<&str>) -> Vec<Pattern> {
        patterns
            .into_iter()
            .map(|pat| Pattern::compile(pat, MatchOptions::ALL))
            .collect()
    }

    fn matches_s(patterns: Vec<&str>, strings: Vec<&str>) {
        matches(
            strings_to_pats(patterns),
            strings,
        )
    }

    fn no_match_s(patterns: Vec<&str>, strings: Vec<&str>) {
        check_match(
            strings_to_pats(patterns),
            strings,
            false
        )
    }

    macro_rules! chunk {
        ($i:ident) => {
            $i
        };
        ($s:tt) => {
            Str($s.to_string())
        };
    }
    macro_rules! chunks {
        ($($t:tt),*) => {
            vec![ $(chunk!($t)),* ]
        }
    }

    #[test]
    fn cat() {
        matches_v(vec![chunks!["c", UnknownChar, "t"]], vec!["cat"]);
        matches_s(
            vec![
                "*", "c*", "*t", "*a*", "c*t", "ca*", "*at", "???", "?a?", "c??", "??t", "ca?",
                "?at", "c?t", "cat", "ca*t", "c*at", "*cat", "cat*", "ca****t"
            ],
            vec!["cat"]
        );
        no_match_s(
            vec![
                "cat?", "?cat", "c?at", "ca?t", "?", "??", "????", "", " *"
            ],
            vec!["cat"]
        );
    }

    #[test]
    fn empty() {
        matches_s(
            vec![""],
            vec![""]
        );
        no_match_s(
            vec![""],
            vec!["a", "b", "c", " ", "\t", "\n"]
        );
    }
}