Add ban command
This commit is contained in:
@@ -100,3 +100,32 @@ pub fn i64_to_u64(i: i64) -> u64 {
|
||||
pub fn u64_to_i64(u: u64) -> i64 {
|
||||
i64::from_ne_bytes(u.to_ne_bytes())
|
||||
}
|
||||
|
||||
/// Splits at the `n`th occurrence of `pattern` counting from the end of the string.
|
||||
///
|
||||
/// Arguments:
|
||||
/// * `s` - The string to split.
|
||||
/// * `pattern` - The pattern to split at.
|
||||
/// * `n` - The occurrence to split at (counting from the end).
|
||||
///
|
||||
/// Returns:
|
||||
/// [`Option<(&str, &str)>`] - The splitted parts of the string, or [`None`] if no occurrences of
|
||||
/// `pattern` were found in `s`.
|
||||
pub fn split_at_nth_from_end<'a, 'b>(s: &'a str, pattern: &'b str, n: usize) -> Option<(&'a str, &'a str)> {
|
||||
let mut end = s.len();
|
||||
let mut count = 0;
|
||||
|
||||
while let Some(pos) = s[..end].rfind(pattern) {
|
||||
count += 1;
|
||||
|
||||
if count == n {
|
||||
let before = &s[..pos];
|
||||
let after = &s[pos + pattern.len()..];
|
||||
return Some((before, after));
|
||||
}
|
||||
|
||||
end = pos; // move left and keep searching
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
Reference in New Issue
Block a user