This repository has been archived on 2025-08-14. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
conduwuit/src/core/utils/string/split.rs
Jason Volk 99ad404ea9 add str traits for split, between, unquote; consolidate tests
Signed-off-by: Jason Volk <jason@zemos.net>
2024-10-25 00:15:01 -04:00

22 lines
828 B
Rust

use super::EMPTY;
type Pair<'a> = (&'a str, &'a str);
/// Split a string with default behaviors on non-match.
pub trait SplitInfallible<'a> {
/// Split a string at the first occurrence of delim. If not found, the
/// entire string is returned in \[0\], while \[1\] is empty.
fn split_once_infallible(&self, delim: &str) -> Pair<'a>;
/// Split a string from the last occurrence of delim. If not found, the
/// entire string is returned in \[0\], while \[1\] is empty.
fn rsplit_once_infallible(&self, delim: &str) -> Pair<'a>;
}
impl<'a> SplitInfallible<'a> for &'a str {
#[inline]
fn rsplit_once_infallible(&self, delim: &str) -> Pair<'a> { self.rsplit_once(delim).unwrap_or((self, EMPTY)) }
#[inline]
fn split_once_infallible(&self, delim: &str) -> Pair<'a> { self.split_once(delim).unwrap_or((self, EMPTY)) }
}