Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE #21]Optimize CheetahString display #22

Merged
merged 1 commit into from
Nov 7, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[ISSUE #21]Optimize CheetahString display
mxsm committed Nov 7, 2024
commit 1fc393ce86ffbc38921430ec68de749b8c502c01
62 changes: 61 additions & 1 deletion src/cheetah_string.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::cmp::Ordering;
use std::fmt::Display;
use std::hash::Hash;
use std::ops::Deref;
use std::sync::Arc;

const EMPTY_STRING: &str = "";

#[derive(Clone)]
#[repr(transparent)]
pub struct CheetahString {
@@ -89,6 +93,13 @@ impl AsRef<[u8]> for CheetahString {
}

impl CheetahString {
#[inline]
pub fn empty() -> Self {
CheetahString {
inner: InnerString::Empty,
}
}

#[inline]
pub fn new() -> Self {
CheetahString::default()
@@ -136,7 +147,7 @@ impl CheetahString {
InnerString::StaticStr(s) => s,
#[cfg(feature = "bytes")]
InnerString::Bytes(b) => std::str::from_utf8(b.as_ref()).unwrap(),
InnerString::Empty => "",
InnerString::Empty => EMPTY_STRING,
}
}

@@ -162,6 +173,7 @@ impl CheetahString {
}
}

#[inline]
pub fn is_empty(&self) -> bool {
match &self.inner {
InnerString::ArcString(s) => s.is_empty(),
@@ -179,6 +191,42 @@ impl PartialEq for CheetahString {
}
}

impl PartialEq<str> for CheetahString {
fn eq(&self, other: &str) -> bool {
self.as_str() == other
}
}

impl PartialEq<String> for CheetahString {
fn eq(&self, other: &String) -> bool {
self.as_str() == other.as_str()
}
}

impl<'a> PartialEq<&'a str> for CheetahString {
fn eq(&self, other: &&'a str) -> bool {
self.as_str() == *other
}
}

impl PartialEq<CheetahString> for str {
fn eq(&self, other: &CheetahString) -> bool {
self == other.as_str()
}
}

impl PartialEq<CheetahString> for String {
fn eq(&self, other: &CheetahString) -> bool {
self.as_str() == other.as_str()
}
}

impl PartialEq<CheetahString> for &str {
fn eq(&self, other: &CheetahString) -> bool {
*self == other.as_str()
}
}

impl Eq for CheetahString {}

impl PartialOrd for CheetahString {
@@ -193,6 +241,18 @@ impl Ord for CheetahString {
}
}

impl Hash for CheetahString {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.as_str().hash(state);
}
}

impl Display for CheetahString {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}

/// The `InnerString` enum represents different types of string storage.
///
/// Variants: