use alloc::string::String;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt::{self, Display};
use core::hash::{Hash, Hasher};
use core::ops::Add;
use core::str;
use super::repr::InnerString;
use super::CheetahString;
#[inline]
fn concatenate(left: &str, right: &str) -> CheetahString {
CheetahString::concat(&[left, right])
}
impl PartialEq for CheetahString {
#[inline]
fn eq(&self, other: &Self) -> bool {
match (&self.inner, &other.inner) {
(InnerString::Inline(left), InnerString::Inline(right)) => left == right,
(InnerString::Shared(left), InnerString::Shared(right)) if Arc::ptr_eq(left, right) => {
true
}
_ => crate::compare::bytes_eq(self.as_bytes(), other.as_bytes()),
}
}
}
impl PartialEq<str> for CheetahString {
#[inline]
fn eq(&self, other: &str) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other.as_bytes())
}
}
impl PartialEq<String> for CheetahString {
#[inline]
fn eq(&self, other: &String) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other.as_bytes())
}
}
impl PartialEq<Vec<u8>> for CheetahString {
#[inline]
fn eq(&self, other: &Vec<u8>) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other)
}
}
impl<'a> PartialEq<&'a str> for CheetahString {
#[inline]
fn eq(&self, other: &&'a str) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other.as_bytes())
}
}
impl PartialEq<CheetahString> for str {
#[inline]
fn eq(&self, other: &CheetahString) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other.as_bytes())
}
}
impl PartialEq<CheetahString> for String {
#[inline]
fn eq(&self, other: &CheetahString) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other.as_bytes())
}
}
impl PartialEq<CheetahString> for &str {
#[inline]
fn eq(&self, other: &CheetahString) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other.as_bytes())
}
}
impl Eq for CheetahString {}
impl PartialOrd for CheetahString {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CheetahString {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
match &self.inner {
InnerString::Inline(left) => match &other.inner {
InnerString::Inline(right) => left.cmp_bytes(right),
_ => left.as_str().cmp(other.as_str()),
},
_ => self.as_str().cmp(other.as_str()),
}
}
}
impl Hash for CheetahString {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
impl Display for CheetahString {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.as_str().fmt(f)
}
}
impl fmt::Debug for CheetahString {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}
}
impl Borrow<str> for CheetahString {
#[inline]
fn borrow(&self) -> &str {
self.as_str()
}
}
impl Add<&str> for CheetahString {
type Output = CheetahString;
#[inline]
fn add(self, rhs: &str) -> Self::Output {
if rhs.is_empty() {
return self;
}
concatenate(self.as_str(), rhs)
}
}
impl Add<&CheetahString> for CheetahString {
type Output = CheetahString;
#[inline]
fn add(self, rhs: &CheetahString) -> Self::Output {
self + rhs.as_str()
}
}
impl Add<String> for CheetahString {
type Output = CheetahString;
#[inline]
fn add(self, rhs: String) -> Self::Output {
if rhs.is_empty() {
return self;
}
if self.is_empty() {
return CheetahString::from_string(rhs);
}
concatenate(self.as_str(), &rhs)
}
}