use crate::search::{find_short, ShortSearch, SHORT_NEEDLE_MAX};
use alloc::string::String;
use core::str;
pub enum StrPatternKind<'a> {
Char(char),
Str(&'a str),
}
mod private {
use alloc::string::String;
pub trait Sealed {
fn classify(&self) -> super::StrPatternKind<'_>;
}
impl Sealed for char {
#[inline]
fn classify(&self) -> super::StrPatternKind<'_> {
super::StrPatternKind::Char(*self)
}
}
impl Sealed for &str {
#[inline]
fn classify(&self) -> super::StrPatternKind<'_> {
super::StrPatternKind::Str(self)
}
}
impl Sealed for &String {
#[inline]
fn classify(&self) -> super::StrPatternKind<'_> {
super::StrPatternKind::Str(self.as_str())
}
}
impl Sealed for &super::super::CheetahString {
#[inline]
fn classify(&self) -> super::StrPatternKind<'_> {
super::StrPatternKind::Str(self.as_str())
}
}
pub trait SplitSealed {}
impl SplitSealed for char {}
impl SplitSealed for &str {}
}
pub trait StrPattern: private::Sealed {
#[doc(hidden)]
fn as_str_pattern(&self) -> StrPatternImpl<'_> {
match private::Sealed::classify(self) {
StrPatternKind::Char(value) => StrPatternImpl::Char(value),
StrPatternKind::Str(value) => StrPatternImpl::Str(value),
}
}
}
#[doc(hidden)]
pub enum StrPatternImpl<'a> {
Char(char),
Str(&'a str),
}
impl StrPattern for char {}
impl StrPattern for &str {}
impl StrPattern for &String {}
impl StrPattern for &super::CheetahString {}
#[inline]
pub(super) fn classify<P: StrPattern>(pattern: &P) -> StrPatternKind<'_> {
private::Sealed::classify(pattern)
}
pub trait SplitPattern<'a>: private::SplitSealed {
type Iter: Iterator<Item = &'a str>;
#[doc(hidden)]
fn split_pattern(self, value: &'a str) -> Self::Iter;
}
impl<'a> SplitPattern<'a> for char {
type Iter = str::Split<'a, char>;
#[inline]
fn split_pattern(self, value: &'a str) -> Self::Iter {
value.split(self)
}
}
impl<'a, 'p> SplitPattern<'a> for &'p str {
type Iter = SplitStr<'a, 'p>;
#[inline]
fn split_pattern(self, value: &'a str) -> Self::Iter {
SplitStr::new(value, self)
}
}
#[derive(Clone, Debug)]
pub struct SplitStr<'a, 'p>(SplitStrInner<'a, 'p>);
#[derive(Clone, Debug)]
enum SplitStrInner<'a, 'p> {
Str(str::Split<'a, &'p str>),
Char(str::Split<'a, char>),
Short(ShortSplit<'a, 'p>),
}
impl<'a, 'p> SplitStr<'a, 'p> {
#[inline]
pub(super) fn new(value: &'a str, pattern: &'p str) -> Self {
let inner = match single_char_pattern(pattern) {
Some(ch) => SplitStrInner::Char(value.split(ch)),
None if (2..=SHORT_NEEDLE_MAX).contains(&pattern.len()) => {
SplitStrInner::Short(ShortSplit {
rest: Some(value),
separator: pattern,
})
}
None => SplitStrInner::Str(value.split(pattern)),
};
Self(inner)
}
}
#[derive(Clone, Debug)]
struct ShortSplit<'a, 'p> {
rest: Option<&'a str>,
separator: &'p str,
}
#[inline]
fn single_char_pattern(pattern: &str) -> Option<char> {
let mut chars = pattern.chars();
let ch = chars.next()?;
if chars.next().is_none() {
Some(ch)
} else {
None
}
}
impl<'a, 'p> Iterator for SplitStr<'a, 'p> {
type Item = &'a str;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
match &mut self.0 {
SplitStrInner::Str(iter) => iter.next(),
SplitStrInner::Char(iter) => iter.next(),
SplitStrInner::Short(iter) => {
let rest = iter.rest?;
let separator = iter.separator;
match find_short(rest, separator) {
ShortSearch::Found(at) => {
iter.rest = Some(&rest[at + separator.len()..]);
Some(&rest[..at])
}
ShortSearch::Missing => {
iter.rest = None;
Some(rest)
}
ShortSearch::TooManyFalseCandidates(from) => {
self.hand_off(rest, separator, from)
}
}
}
}
}
}
impl<'a, 'p> SplitStr<'a, 'p> {
#[cold]
#[inline(never)]
fn hand_off(&mut self, rest: &'a str, separator: &'p str, from: usize) -> Option<&'a str> {
match rest[from..].find(separator) {
Some(offset) => {
let at = from + offset;
self.0 = SplitStrInner::Str(rest[at + separator.len()..].split(separator));
Some(&rest[..at])
}
None => {
self.0 = SplitStrInner::Short(ShortSplit {
rest: None,
separator,
});
Some(rest)
}
}
}
}
impl core::iter::FusedIterator for SplitStr<'_, '_> {}