pub fn ends_with(string: &str, target: &str) -> bool {
ends_with_position(string, target, None)
}
pub fn ends_with_position(string: &str, target: &str, position: Option<usize>) -> bool {
let end_position = position.unwrap_or(string.len());
if end_position == 0 {
return target.is_empty();
}
if end_position > target.len() {
let start_position = end_position - target.len();
let remaining = &string[start_position..end_position];
return remaining == target;
}
false
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_ends_with() {
assert_eq!(ends_with("abc", "b"), false);
assert_eq!(ends_with("abc", "c"), true);
}
#[test]
fn test_ends_with_position() {
assert_eq!(ends_with_position("abc", "b", Some(2)), true);
assert_eq!(ends_with_position("abc", "bc", Some(3)), true);
}
}