sqruff_lib/core/
string_helpers.rs

1/// String Helpers for the parser module.
2/// Yields all the positions sbstr within in_str https://stackoverflow.com/questions/4664850/how-to-find-all-occurrences-of-a-substring
3pub fn find_all(substr: &str, in_str: &str) -> Vec<usize> {
4    // Return nothing if one of the inputs is trivial
5    if substr.is_empty() || in_str.is_empty() {
6        return Vec::new();
7    }
8    in_str.match_indices(substr).map(|(i, _)| i).collect()
9}
10
11#[cfg(test)]
12mod tests {
13    use super::*;
14
15    #[test]
16    fn test_find_all() {
17        vec![
18            ("", "", vec![]),
19            ("a", "a", vec![0]),
20            ("foobar", "o", vec![1, 2]),
21            ("bar bar bar bar", "bar", vec![0, 4, 8, 12]),
22        ]
23        .into_iter()
24        .for_each(|(in_str, substr, expected)| assert_eq!(expected, find_all(substr, in_str)));
25    }
26}