use core::{iter::Map, str};
use non_empty_iter::NonEmptyIterator;
use crate::{internal::Byte, str::NonEmptyStr};
pub type NonEmptyStrFn<'s> = fn(&'s str) -> &'s NonEmptyStr;
#[derive(Debug)]
pub struct Bytes<'s> {
string: &'s NonEmptyStr,
}
impl<'s> Bytes<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for Bytes<'s> {
type Item = Byte;
type IntoIter = str::Bytes<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().bytes()
}
}
unsafe impl NonEmptyIterator for Bytes<'_> {}
#[derive(Debug)]
pub struct Chars<'s> {
string: &'s NonEmptyStr,
}
impl<'s> Chars<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for Chars<'s> {
type Item = char;
type IntoIter = str::Chars<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().chars()
}
}
unsafe impl NonEmptyIterator for Chars<'_> {}
#[derive(Debug)]
pub struct CharIndices<'s> {
string: &'s NonEmptyStr,
}
impl<'s> CharIndices<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for CharIndices<'s> {
type Item = (usize, char);
type IntoIter = str::CharIndices<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().char_indices()
}
}
unsafe impl NonEmptyIterator for CharIndices<'_> {}
#[derive(Debug)]
pub struct SplitWhitespace<'s> {
string: &'s NonEmptyStr,
}
impl<'s> SplitWhitespace<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for SplitWhitespace<'s> {
type Item = &'s NonEmptyStr;
type IntoIter = Map<str::SplitWhitespace<'s>, NonEmptyStrFn<'s>>;
fn into_iter(self) -> Self::IntoIter {
self.string
.as_str()
.split_whitespace()
.map(|string| unsafe { NonEmptyStr::from_str_unchecked(string) })
}
}
#[derive(Debug)]
pub struct SplitAsciiWhitespace<'s> {
string: &'s NonEmptyStr,
}
impl<'s> SplitAsciiWhitespace<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for SplitAsciiWhitespace<'s> {
type Item = &'s NonEmptyStr;
type IntoIter = Map<str::SplitAsciiWhitespace<'s>, NonEmptyStrFn<'s>>;
fn into_iter(self) -> Self::IntoIter {
self.string
.as_str()
.split_ascii_whitespace()
.map(|string| unsafe { NonEmptyStr::from_str_unchecked(string) })
}
}
#[derive(Debug)]
pub struct EncodeUtf16<'s> {
string: &'s NonEmptyStr,
}
impl<'s> EncodeUtf16<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for EncodeUtf16<'s> {
type Item = u16;
type IntoIter = str::EncodeUtf16<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().encode_utf16()
}
}
unsafe impl NonEmptyIterator for EncodeUtf16<'_> {}
#[derive(Debug)]
pub struct EscapeDebug<'s> {
string: &'s NonEmptyStr,
}
impl<'s> EscapeDebug<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for EscapeDebug<'s> {
type Item = char;
type IntoIter = str::EscapeDebug<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().escape_debug()
}
}
unsafe impl NonEmptyIterator for EscapeDebug<'_> {}
#[derive(Debug)]
pub struct EscapeDefault<'s> {
string: &'s NonEmptyStr,
}
impl<'s> EscapeDefault<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for EscapeDefault<'s> {
type Item = char;
type IntoIter = str::EscapeDefault<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().escape_default()
}
}
unsafe impl NonEmptyIterator for EscapeDefault<'_> {}
#[derive(Debug)]
pub struct EscapeUnicode<'s> {
string: &'s NonEmptyStr,
}
impl<'s> EscapeUnicode<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for EscapeUnicode<'s> {
type Item = char;
type IntoIter = str::EscapeUnicode<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().escape_unicode()
}
}
unsafe impl NonEmptyIterator for EscapeUnicode<'_> {}
#[derive(Debug)]
pub struct Lines<'s> {
string: &'s NonEmptyStr,
}
impl<'s> Lines<'s> {
#[must_use]
pub const fn new(string: &'s NonEmptyStr) -> Self {
Self { string }
}
}
impl<'s> IntoIterator for Lines<'s> {
type Item = &'s str;
type IntoIter = str::Lines<'s>;
fn into_iter(self) -> Self::IntoIter {
self.string.as_str().lines()
}
}
unsafe impl NonEmptyIterator for Lines<'_> {}