use alloc::boxed::Box;
use alloc::string::String;
use core::borrow::Borrow;
use core::cmp::Ordering;
use core::fmt;
use core::hash::{Hash, Hasher};
use core::ops::{Bound, Deref, RangeBounds};
use core::str;
use bytes::Bytes;
use crate::{CheetahBytes, CheetahString, FromUtf8BytesError};
#[derive(Clone, Default)]
pub struct CheetahText {
bytes: Bytes,
}
impl CheetahText {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
bytes: Bytes::new(),
}
}
#[inline]
#[must_use]
pub const fn from_static(text: &'static str) -> Self {
Self {
bytes: Bytes::from_static(text.as_bytes()),
}
}
#[inline]
pub fn try_from_bytes(bytes: Bytes) -> Result<Self, FromUtf8BytesError> {
match str::from_utf8(&bytes) {
Ok(_) => Ok(Self { bytes }),
Err(error) => Err(FromUtf8BytesError::new(bytes, error)),
}
}
#[inline]
#[must_use]
pub unsafe fn from_bytes_unchecked(bytes: Bytes) -> Self {
Self { bytes }
}
#[inline]
#[must_use]
pub fn copy_from_str(text: &str) -> Self {
Self {
bytes: Bytes::copy_from_slice(text.as_bytes()),
}
}
#[inline]
#[must_use]
pub fn as_str(&self) -> &str {
unsafe { str::from_utf8_unchecked(&self.bytes) }
}
#[inline]
#[must_use]
pub fn as_bytes(&self) -> &[u8] {
&self.bytes
}
#[inline]
#[must_use]
pub fn bytes(&self) -> &Bytes {
&self.bytes
}
#[inline]
#[must_use]
pub fn len(&self) -> usize {
self.bytes.len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.bytes.is_empty()
}
#[inline]
#[must_use]
pub fn into_bytes(self) -> Bytes {
self.bytes
}
#[inline]
#[must_use]
pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
self.try_slice(range)
.expect("slice range must be in bounds and on UTF-8 character boundaries")
}
pub fn try_slice(&self, range: impl RangeBounds<usize>) -> crate::Result<Self> {
let len = self.len();
let start = match range.start_bound() {
Bound::Included(&start) => start,
Bound::Excluded(&start) => start
.checked_add(1)
.ok_or(crate::Error::IndexOutOfBounds { index: start, len })?,
Bound::Unbounded => 0,
};
let end = match range.end_bound() {
Bound::Included(&end) => end
.checked_add(1)
.ok_or(crate::Error::IndexOutOfBounds { index: end, len })?,
Bound::Excluded(&end) => end,
Bound::Unbounded => len,
};
if start > end {
return Err(crate::Error::InvalidRange { start, end });
}
if end > len {
return Err(crate::Error::IndexOutOfBounds { index: end, len });
}
let text = self.as_str();
if !text.is_char_boundary(start) {
return Err(crate::Error::InvalidCharBoundary { index: start });
}
if !text.is_char_boundary(end) {
return Err(crate::Error::InvalidCharBoundary { index: end });
}
Ok(Self {
bytes: self.bytes.slice(start..end),
})
}
#[inline]
#[must_use]
pub fn slice_ref(&self, subset: &str) -> Self {
Self {
bytes: self.bytes.slice_ref(subset.as_bytes()),
}
}
#[inline]
#[must_use]
pub fn to_cheetah_string(&self) -> CheetahString {
CheetahString::from_slice(self.as_str())
}
}
impl Deref for CheetahText {
type Target = str;
#[inline]
fn deref(&self) -> &str {
self.as_str()
}
}
impl AsRef<str> for CheetahText {
#[inline]
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl AsRef<[u8]> for CheetahText {
#[inline]
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
impl Borrow<str> for CheetahText {
#[inline]
fn borrow(&self) -> &str {
self.as_str()
}
}
impl PartialEq for CheetahText {
#[inline]
fn eq(&self, other: &Self) -> bool {
crate::compare::bytes_eq(self.as_bytes(), other.as_bytes())
}
}
impl Eq for CheetahText {}
impl PartialOrd for CheetahText {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for CheetahText {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
self.as_str().cmp(other.as_str())
}
}
impl Hash for CheetahText {
#[inline]
fn hash<H: Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}
macro_rules! impl_text_eq {
($($other:ty),*) => {$(
impl PartialEq<$other> for CheetahText {
#[inline]
fn eq(&self, other: &$other) -> bool {
crate::compare::bytes_eq(self.as_bytes(), AsRef::<str>::as_ref(other).as_bytes())
}
}
impl PartialEq<CheetahText> for $other {
#[inline]
fn eq(&self, other: &CheetahText) -> bool {
crate::compare::bytes_eq(AsRef::<str>::as_ref(self).as_bytes(), other.as_bytes())
}
}
)*};
}
impl_text_eq!(str, &str, String, CheetahString);
impl fmt::Display for CheetahText {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(self.as_str(), f)
}
}
impl fmt::Debug for CheetahText {
#[inline]
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(self.as_str(), f)
}
}
impl From<&'static str> for CheetahText {
#[inline]
fn from(text: &'static str) -> Self {
Self::from_static(text)
}
}
impl From<String> for CheetahText {
#[inline]
fn from(text: String) -> Self {
Self {
bytes: Bytes::from(text),
}
}
}
impl From<Box<str>> for CheetahText {
#[inline]
fn from(text: Box<str>) -> Self {
Self {
bytes: Bytes::from(text.into_boxed_bytes()),
}
}
}
impl TryFrom<Bytes> for CheetahText {
type Error = FromUtf8BytesError;
#[inline]
fn try_from(bytes: Bytes) -> Result<Self, Self::Error> {
Self::try_from_bytes(bytes)
}
}
impl TryFrom<CheetahBytes> for CheetahText {
type Error = FromUtf8BytesError;
#[inline]
fn try_from(bytes: CheetahBytes) -> Result<Self, Self::Error> {
Self::try_from_bytes(bytes.into_bytes())
}
}
impl From<CheetahText> for Bytes {
#[inline]
fn from(text: CheetahText) -> Self {
text.bytes
}
}
impl From<CheetahText> for CheetahBytes {
#[inline]
fn from(text: CheetahText) -> Self {
CheetahBytes::from_bytes(text.bytes)
}
}
impl From<&CheetahText> for CheetahString {
#[inline]
fn from(text: &CheetahText) -> Self {
text.to_cheetah_string()
}
}
impl From<CheetahText> for CheetahString {
#[inline]
fn from(text: CheetahText) -> Self {
text.to_cheetah_string()
}
}
#[cfg(feature = "serde")]
mod serde_impl {
use super::CheetahText;
use alloc::string::String;
use alloc::vec::Vec;
use core::fmt;
use serde::de::{Error, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
impl Serialize for CheetahText {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(self.as_str())
}
}
impl<'de> Deserialize<'de> for CheetahText {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct TextVisitor;
impl Visitor<'_> for TextVisitor {
type Value = CheetahText;
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter.write_str("a string")
}
fn visit_str<E: Error>(self, value: &str) -> Result<Self::Value, E> {
Ok(CheetahText::copy_from_str(value))
}
fn visit_string<E: Error>(self, value: String) -> Result<Self::Value, E> {
Ok(CheetahText::from(value))
}
fn visit_bytes<E: Error>(self, value: &[u8]) -> Result<Self::Value, E> {
core::str::from_utf8(value)
.map(CheetahText::copy_from_str)
.map_err(E::custom)
}
fn visit_byte_buf<E: Error>(self, value: Vec<u8>) -> Result<Self::Value, E> {
String::from_utf8(value)
.map(CheetahText::from)
.map_err(E::custom)
}
}
deserializer.deserialize_string(TextVisitor)
}
}
}