zeta_note/
util.rs

1/// Test the the text matches all characters in the query in order.
2///
3/// ```rust
4/// use zeta_note::util::text_matches_query;
5///
6/// let text = "Hello World!";
7/// assert_eq!(text_matches_query(&text, ""), true);
8/// assert_eq!(text_matches_query(&text, "h"), true);
9/// assert_eq!(text_matches_query(&text, "hel"), true);
10/// assert_eq!(text_matches_query(&text, "hw"), true);
11/// assert_eq!(text_matches_query(&text, "h!"), true);
12/// assert_eq!(text_matches_query(&text, "hz"), false);
13/// ```
14pub fn text_matches_query(text: &str, query: &str) -> bool {
15    if query.is_empty() {
16        return true;
17    }
18
19    let text = text.to_lowercase();
20    let query = query.to_lowercase();
21
22    let mut start = 0;
23    for c in query.chars() {
24        let char_pos = text[start..].find(c);
25        start = match char_pos {
26            Some(pos) => start + pos + c.len_utf8(),
27            _ => return false,
28        };
29    }
30
31    true
32}