#![no_std]
pub trait SplitExact {
fn split_exact<const N: usize>(&self, predicate: impl FnMut(char) -> bool)
-> [Option<&str>; N];
fn split_exact_with_rest<const N: usize>(
&self,
predicate: impl FnMut(char) -> bool,
) -> ([Option<&str>; N], &str);
}
impl SplitExact for str {
fn split_exact<const N: usize>(
&self,
predicate: impl FnMut(char) -> bool,
) -> [Option<&str>; N] {
let mut elements = [None; N];
let mut splits = self.split(predicate);
for element in &mut elements {
*element = splits.next();
}
elements
}
fn split_exact_with_rest<const N: usize>(
&self,
mut predicate: impl FnMut(char) -> bool,
) -> ([Option<&str>; N], &str) {
let elements = self.split_exact(&mut predicate);
let offset = self
.match_indices(predicate)
.nth(N.saturating_sub(1))
.map(|(index, _)| index.saturating_add(1))
.unwrap_or(self.len());
(elements, &self[offset..])
}
}