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
use std::cmp::Ordering;
/// Checks whether a string starts with the specified prefix at `offset` position.
///
/// # Arguments
///
/// * `s` - The input string to perform remove.
/// * `prefix` - The prefix to find.
/// * `offset` - The position to start find.
///
/// # Returns
///
/// Returns true if source string starts with the prefix at offset position.
///
/// # Examples
///
/// ```
/// use rufl::string;
///
/// assert_eq!(true, string::starts_with_offset("abab", "a", 0));
///
/// assert_eq!(false, string::starts_with_offset("abab", "a", 1));
///
/// assert_eq!(true, string::starts_with_offset("abab", "a", 2));
///
/// assert_eq!(false, string::starts_with_offset("abab", "a", 6));
///
/// ```
pub fn starts_with_offset(s: impl AsRef<str>, prefix: &str, offset: usize) -> bool {
match offset.cmp(&s.as_ref().len()) {
Ordering::Greater => false,
_ => (&s.as_ref()[offset..]).starts_with(prefix),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_starts_with_offset() {
assert_eq!(true, starts_with_offset("abab", "a", 0));
assert_eq!(false, starts_with_offset("abab", "a", 1));
assert_eq!(true, starts_with_offset("abab", "a", 2));
assert_eq!(false, starts_with_offset("abab", "a", 6));
}
}