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
/// Returns the substring after the last occurrence of a specified `substr` in the source string.
///
/// # Arguments
///
/// * `s` - The input string to perform after_last operation.
/// * `substr` - The substring to look for last occurrence in `s`.
///
/// # Returns
///
/// Returns the substring after the last occurrence of `substr` in `s`.
///
/// # Examples
///
/// ```
/// use rufl::string;
///
/// let foo = string::after_last("foo", "o");
/// assert_eq!("", foo);
///
/// let bar = string::after_last("bar", "a");
/// assert_eq!("r", bar);
///
/// let boo = string::after_last("boo", "c");
/// assert_eq!("boo", boo);
///
/// ```
pub fn after_last(s: impl AsRef<str>, substr: &str) -> String {
match s.as_ref().rfind(substr) {
Some(index) => s.as_ref()[index + substr.len()..].to_string(),
None => s.as_ref().to_string(),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_after_last() {
assert_eq!("", after_last("foo", ""));
assert_eq!("", after_last("foo", "o"));
assert_eq!("", after_last("foo", "foo"));
assert_eq!("foo", after_last("foo", "a"));
assert_eq!("boo", after_last("foo/bar/boo", "/"));
assert_eq!("rust", after_last("你好c++你好rust", "你好"));
assert_eq!(
" programátor",
after_last("Jsem počítačový programátor", "počítačový")
);
}
}