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
/// Searches a string and returns the index of the first occurrence of the specified searched substring.
///
/// # Arguments
///
/// * `s` - The input string to perform index.
/// * `search` - The substring to search for.
/// * `position` - The position of source string to start to search. Start position is 0.
///
/// # Returns
///
/// Returns index of the first occurrence of searched substring.
///
/// # Examples
///
/// ```
/// use rufl::string;
///
/// assert_eq!(2, string::index("hello", "l", 0));
///
/// assert_eq!(2, string::index("hello", "l", 2));
///
/// assert_eq!(3, string::index("hello", "l", 3));
///
/// assert_eq!(-1, string::index("hello", "l", 4));
///
/// ```
pub fn index(s: impl AsRef<str>, search: &str, position: usize) -> i32 {
match search.len() {
0 => 0,
_ => {
if s.as_ref().chars().count() <= position {
return -1;
}
let sub_string = &s.as_ref()[s.as_ref().char_indices().nth(position).unwrap().0..];
match crate::string::split_chars(sub_string)
.iter()
.enumerate()
.position(|(pos, _)| {
match &sub_string[sub_string.char_indices().nth(pos).unwrap().0..].find(search)
{
Some(n) => *n == 0,
None => false,
}
}) {
Some(n) => {
let result =
n + (s.as_ref().char_indices().count() - sub_string.char_indices().count());
result as i32
}
None => -1,
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_removen() {
assert_eq!(0, index("hello", "", 0));
assert_eq!(2, index("hello", "l", 0));
assert_eq!(2, index("hello", "l", 1));
assert_eq!(2, index("hello", "l", 2));
assert_eq!(3, index("hello", "l", 3));
assert_eq!(0, index("你好你好!", "你好", 0));
assert_eq!(2, index("你好你好", "你好", 1));
assert_eq!(2, index("你好你好", "你好", 2));
assert_eq!(7, index("你好hello你好", "你好", 2));
assert_eq!(-1, index("你好hello你好", "你好", 9));
assert_eq!(-1, index("你好hello你好", "你好", 10));
}
}