use crate::named::Named;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt::{Binary, Debug, LowerHex, Octal, UpperHex};
use hashbrown::HashSet;
use itertools::Itertools;
pub fn string_sort(s: &str) -> String {
let mut chars = s.chars().collect_vec();
chars.sort_unstable();
chars.iter().collect()
}
pub fn string_unique(s: &str) -> String {
let mut chars = HashSet::new();
let mut nub = String::new();
for c in s.chars() {
if chars.insert(c) {
nub.push(c);
}
}
nub
}
pub fn string_is_subset(s: &str, t: &str) -> bool {
let t_chars: HashSet<char> = t.chars().collect();
s.chars().all(|c| t_chars.contains(&c))
}
impl_named!(String);
pub trait ToDebugString: Debug {
fn to_debug_string(&self) -> String;
}
impl<T: Debug> ToDebugString for T {
#[inline]
fn to_debug_string(&self) -> String {
::alloc::format!("{self:?}")
}
}
pub trait ToBinaryString: Binary {
fn to_binary_string(&self) -> String;
}
impl<T: Binary> ToBinaryString for T {
#[inline]
fn to_binary_string(&self) -> String {
::alloc::format!("{self:b}")
}
}
pub trait ToOctalString: Octal {
fn to_octal_string(&self) -> String;
}
impl<T: Octal> ToOctalString for T {
#[inline]
fn to_octal_string(&self) -> String {
::alloc::format!("{self:o}")
}
}
pub trait ToLowerHexString: LowerHex {
fn to_lower_hex_string(&self) -> String;
}
impl<T: LowerHex> ToLowerHexString for T {
#[inline]
fn to_lower_hex_string(&self) -> String {
::alloc::format!("{self:x}")
}
}
pub trait ToUpperHexString: UpperHex {
fn to_upper_hex_string(&self) -> String;
}
impl<T: UpperHex> ToUpperHexString for T {
#[inline]
fn to_upper_hex_string(&self) -> String {
::alloc::format!("{self:X}")
}
}
#[derive(Clone, Debug)]
pub struct StringsFromCharVecs<I: Iterator<Item = Vec<char>>> {
css: I,
}
impl<I: Iterator<Item = Vec<char>>> Iterator for StringsFromCharVecs<I> {
type Item = String;
#[inline]
fn next(&mut self) -> Option<String> {
self.css.next().map(|cs| cs.into_iter().collect())
}
}
#[inline]
pub const fn strings_from_char_vecs<I: Iterator<Item = Vec<char>>>(
css: I,
) -> StringsFromCharVecs<I> {
StringsFromCharVecs { css }
}
pub mod exhaustive;
#[cfg(feature = "random")]
pub mod random;