use crate::string::{self, Pattern, PatternNorm, str_from, str_up_to};
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "iter")))]
pub const fn split_once<'a, 'p, P>(this: &'a str, delim: P) -> Option<(&'a str, &'a str)>
where
P: Pattern<'p>,
{
let delim = PatternNorm::new(delim);
let delim = delim.as_str();
if delim.is_empty() {
Some(string::split_at(this, 0))
} else {
crate::option::map! {
string::find(this, delim),
|pos| (str_up_to(this, pos), str_from(this, pos + delim.len()))
}
}
}
#[cfg_attr(feature = "docsrs", doc(cfg(feature = "iter")))]
pub const fn rsplit_once<'a, 'p, P>(this: &'a str, delim: P) -> Option<(&'a str, &'a str)>
where
P: Pattern<'p>,
{
let delim = PatternNorm::new(delim);
let delim = delim.as_str();
if delim.is_empty() {
Some(string::split_at(this, this.len()))
} else {
crate::option::map! {
string::rfind(this, delim),
|pos| (str_up_to(this, pos), str_from(this, pos + delim.len()))
}
}
}