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
use std::iter::{Iterator, Peekable};


/// Returns the closest match from the given options to the given query.
///
/// This algorithm works by scanning through each option trying to match the beginning of the
/// query. Once a match has begun, any non-matching characters will cause the scan to skip to the
/// next word of the option. If the end of the option is reached before the entire query has been
/// matched somewhere, the option is considered not to match.
///
/// If multiple options match, it returns the shortest.
///
/// # Examples
///
/// ```
/// let options = &["one two", "three four", "five six"];
/// let query = "owo";
/// let result = close_enough::close_enough(options, query);
/// assert_eq!(result, Some(&"one two"));
/// ```
pub fn close_enough<I, O, Q>(options: I, query: Q) -> Option<O>
    where I: IntoIterator<Item=O>, O: AsRef<str>, Q: AsRef<str>
{
    let mut shortest_answer: Option<O> = None;

    for opt in options
    {
        let matches = {
            let mut optchars = opt.as_ref().chars().peekable();
            let mut querychars = query.as_ref().chars().peekable();

            while querychars.peek().is_some()
            {
                while optchars.peek().is_some() && !same_char(querychars.peek(), optchars.peek())
                {
                    optchars.next();
                }

                if optchars.peek().is_none() { break; }

                while querychars.peek().is_some() && same_char(querychars.peek(), optchars.peek())
                {
                    querychars.next();
                    optchars.next();
                }

                skip_word(&mut optchars);
            }

            querychars.peek().is_none()
        };

        if matches
        {
            shortest_answer = Some(select_shortest(opt, shortest_answer));
        }
    }

    shortest_answer
}


fn same_char(a: Option<&char>, b: Option<&char>) -> bool
{
    match (a, b)
    {
        (Some(achar), Some(bchar)) => achar.to_lowercase().next() == bchar.to_lowercase().next(),
        _ => false
    }
}


fn select_shortest<T>(proposed: T, previous: Option<T>) -> T
    where T: AsRef<str>
{
    match previous
    {
        None => proposed,
        Some(prev) => if proposed.as_ref().len() < prev.as_ref().len() { proposed } else { prev }
    }
}


fn skip_word<I>(chars: &mut Peekable<I>)
    where I: Iterator<Item=char>
{
    let mut has_more = chars.peek().is_some();
    while has_more
    {
        if let Some(c) = chars.peek()
        {
            if !c.is_alphanumeric() || c.is_uppercase()
            {
                break;
            }
        }
        has_more = chars.next().is_some();
    }
}


#[cfg(test)]
mod tests {

    use super::*;

    fn test(options: &[&str], query: &str, expected: Option<&str>)
    {
        assert_eq!(close_enough(options, query), expected.as_ref());
    }

    #[test]
    fn no_options_returns_none()
    {
        test(&[], "blah", None);
    }

    #[test]
    fn single_option_returned_on_match()
    {
        test(&["only"], "only", Some("only"));
    }

    #[test]
    fn single_option_returns_none_if_not_match()
    {
        test(&["only"], "different", None);
    }

    #[test]
    fn multiple_options_returns_matching()
    {
        test(&["one", "two"], "two", Some("two"));
    }

    #[test]
    fn partial_match_returns_answer()
    {
        test(&["only"], "on", Some("only"));
    }

    #[test]
    fn matching_is_case_insensitive()
    {
        test(&["OnLy"], "only", Some("OnLy"));
    }

    #[test]
    fn multiple_partial_matches_return_shortest_answer()
    {
        test(&["item_the_first", "item"], "it", Some("item"));
    }

    #[test]
    fn entire_query_must_be_present_for_match()
    {
        test(&["item_the_first"], "item_the_fist", None);
    }

    #[test]
    fn can_match_from_beyond_start()
    {
        test(&["theonlyitem"], "only", Some("theonlyitem"));
    }

    #[test]
    fn failed_match_looks_to_next_word()
    {
        test(&["A very_big-longMatch"], "avblm", Some("A very_big-longMatch"));
    }

    #[test]
    fn failed_match_does_not_look_within_same_word()
    {
        test(&["averybiglongmatch"], "avblm", None);
    }

    #[test]
    fn works_on_useful_collection_types()
    {
        assert_eq!(close_enough(["a", "thing"].iter(), "thing"), Some(&"thing"));
        assert_eq!(close_enough(&["a", "thing"], "thing"), Some(&"thing"));
        assert_eq!(close_enough(&vec!["a", "thing"], "thing"), Some(&"thing"));
        assert_eq!(close_enough(&vec!["a".to_owned(), "thing".to_owned()], "thing"), Some(&"thing".to_owned()));
    }
}