Skip to main content

tuwunel_core/utils/string/
split.rs

1use super::EMPTY;
2
3type Pair<'a> = (&'a str, &'a str);
4
5/// Split a string with default behaviors on non-match.
6pub trait SplitInfallible<'a> {
7	/// Split a string at the first occurrence of delim. If not found, the
8	/// entire string is returned in \[0\], while \[1\] is empty.
9	fn split_once_infallible(&self, delim: &str) -> Pair<'a>;
10
11	/// Split a string from the last occurrence of delim. If not found, the
12	/// entire string is returned in \[0\], while \[1\] is empty.
13	fn rsplit_once_infallible(&self, delim: &str) -> Pair<'a>;
14}
15
16impl<'a> SplitInfallible<'a> for &'a str {
17	#[inline]
18	fn rsplit_once_infallible(&self, delim: &str) -> Pair<'a> {
19		self.rsplit_once(delim).unwrap_or((self, EMPTY))
20	}
21
22	#[inline]
23	fn split_once_infallible(&self, delim: &str) -> Pair<'a> {
24		self.split_once(delim).unwrap_or((self, EMPTY))
25	}
26}