use std::borrow::{Borrow, Cow};
use std::fmt::{Display, Error, Formatter};
#[cfg(any(feature = "vic3", feature = "imperator"))]
use std::slice::SliceIndex;
#[cfg(any(feature = "vic3", feature = "imperator"))]
use std::str::RMatchIndices;
#[derive(Debug, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct Lowercase<'a>(Cow<'a, str>);
impl<'a> Lowercase<'a> {
pub fn new(s: &'a str) -> Self {
if s.chars().any(|c| c.is_ascii_uppercase()) {
Lowercase(Cow::Owned(s.to_ascii_lowercase()))
} else {
Lowercase(Cow::Borrowed(s))
}
}
pub fn new_unchecked(s: &'a str) -> Self {
Lowercase(Cow::Borrowed(s))
}
pub fn from_string_unchecked(s: String) -> Self {
Lowercase(Cow::Owned(s))
}
pub fn empty() -> &'static Self {
const EMPTY_LOWERCASE: Lowercase = Lowercase(Cow::Borrowed(""));
&EMPTY_LOWERCASE
}
pub fn as_str(&'a self) -> &'a str {
&self.0
}
pub fn into_cow(self) -> Cow<'a, str> {
self.0
}
pub fn to_uppercase(&self) -> String {
self.as_str().to_ascii_uppercase()
}
pub fn strip_prefix_unchecked<S: Borrow<str>>(&'a self, prefix: S) -> Option<Lowercase<'a>> {
self.0.strip_prefix(prefix.borrow()).map(|s| Self(Cow::Borrowed(s)))
}
pub fn strip_suffix_unchecked<S: Borrow<str>>(&'a self, suffix: S) -> Option<Lowercase<'a>> {
self.0.strip_suffix(suffix.borrow()).map(|s| Self(Cow::Borrowed(s)))
}
#[cfg(any(feature = "vic3", feature = "imperator"))]
pub fn rmatch_indices_unchecked(&self, separator: char) -> RMatchIndices<char> {
self.0.rmatch_indices(separator)
}
#[cfg(any(feature = "vic3", feature = "imperator"))]
pub fn slice<R: 'a + SliceIndex<str, Output = str>>(&'a self, range: R) -> Self {
Lowercase::new_unchecked(&self.0[range])
}
}
impl Display for Lowercase<'_> {
fn fmt(&self, f: &mut Formatter) -> Result<(), Error> {
write!(f, "{}", self.0)
}
}
impl<'a> Borrow<Cow<'a, str>> for Lowercase<'a> {
fn borrow(&self) -> &Cow<'a, str> {
&self.0
}
}
impl Borrow<str> for Lowercase<'_> {
fn borrow(&self) -> &str {
&self.0
}
}
impl Default for Lowercase<'static> {
fn default() -> Lowercase<'static> {
Lowercase::new_unchecked("")
}
}
impl PartialEq<str> for Lowercase<'_> {
fn eq(&self, s: &str) -> bool {
self.as_str() == s
}
}
impl PartialEq<&str> for Lowercase<'_> {
fn eq(&self, s: &&str) -> bool {
self.as_str() == *s
}
}
impl PartialEq<String> for &Lowercase<'_> {
fn eq(&self, s: &String) -> bool {
self.as_str() == s
}
}