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
/// Searches a string and returns all the indexs of the occurrence of the specified searched substring.
///
/// # Arguments
///
/// * `s` - The input string where to search.
/// * `search` - The substring to search for.
/// * `position` - The position to start searching.
///
/// # Returns
///
/// Returns index of the first occurrence of searched substring.
///
/// # Examples
///
/// ```
/// use rufl::string;
///
/// assert_eq!(vec![2, 3], string::index_all("hello", "l", 0));
///
/// assert_eq!(vec![0, 2], string::index_all("你好你好!", "你好", 0));
/// ```
pub fn index_all(s: impl AsRef<str>, search: &str, position: usize) -> Vec<usize> {
if s.as_ref().is_empty() || search.is_empty() || s.as_ref().chars().count() <= position {
return vec![];
}
let mut result = Vec::new();
let mut index = crate::string::index(&s, search, 0);
while index != -1 {
result.push(index as usize);
index = crate::string::index(&s, search, (index + 1) as usize);
}
result
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_index_all() {
assert_eq!(true, index_all("hello", "", 0).is_empty());
assert_eq!(true, index_all("hello", "", 10).is_empty());
assert_eq!(vec![2, 3], index_all("hello", "l", 0));
assert_eq!(vec![2], index_all("hello", "ll", 0));
assert_eq!(vec![2, 3], index_all("hello", "l", 3));
assert_eq!(vec![0, 2], index_all("你好你好!", "你好", 0));
}
}